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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | synchronized (ActivityManagerService.this) { |
| 916 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 917 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 918 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 922 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 923 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | d.show(); |
| 925 | proc.crashDialog = d; |
| 926 | } else { |
| 927 | // The device is asleep, so just pretend that the user |
| 928 | // saw a crash dialog and hit "force quit". |
| 929 | res.set(0); |
| 930 | } |
| 931 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 932 | |
| 933 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | } break; |
| 935 | case SHOW_NOT_RESPONDING_MSG: { |
| 936 | synchronized (ActivityManagerService.this) { |
| 937 | HashMap data = (HashMap) msg.obj; |
| 938 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 939 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 940 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | return; |
| 942 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 943 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 944 | Intent intent = new Intent("android.intent.action.ANR"); |
| 945 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 946 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 947 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 948 | } |
| 949 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 950 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 951 | 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] | 952 | |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 953 | if (mShowDialogs) { |
| 954 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Mike Lockwood | 69ccdbd | 2012-04-03 11:53:47 -0700 | [diff] [blame] | 955 | mContext, proc, (ActivityRecord)data.get("activity")); |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 956 | d.show(); |
| 957 | proc.anrDialog = d; |
| 958 | } else { |
| 959 | // Just kill the app if there is no dialog to be shown. |
| 960 | killAppAtUsersRequest(proc, null); |
| 961 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 963 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 964 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 966 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 967 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 968 | synchronized (ActivityManagerService.this) { |
| 969 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 970 | if (proc == null) { |
| 971 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 972 | break; |
| 973 | } |
| 974 | if (proc.crashDialog != null) { |
| 975 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 976 | return; |
| 977 | } |
| 978 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 979 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 980 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 981 | d.show(); |
| 982 | proc.crashDialog = d; |
| 983 | } else { |
| 984 | // The device is asleep, so just pretend that the user |
| 985 | // saw a crash dialog and hit "force quit". |
| 986 | res.set(0); |
| 987 | } |
| 988 | } |
| 989 | ensureBootCompleted(); |
| 990 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | case SHOW_FACTORY_ERROR_MSG: { |
| 992 | Dialog d = new FactoryErrorDialog( |
| 993 | mContext, msg.getData().getCharSequence("msg")); |
| 994 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 995 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | } break; |
| 997 | case UPDATE_CONFIGURATION_MSG: { |
| 998 | final ContentResolver resolver = mContext.getContentResolver(); |
| 999 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 1000 | } break; |
| 1001 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 1002 | synchronized (ActivityManagerService.this) { |
| 1003 | performAppGcsIfAppropriateLocked(); |
| 1004 | } |
| 1005 | } break; |
| 1006 | case WAIT_FOR_DEBUGGER_MSG: { |
| 1007 | synchronized (ActivityManagerService.this) { |
| 1008 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1009 | if (msg.arg1 != 0) { |
| 1010 | if (!app.waitedForDebugger) { |
| 1011 | Dialog d = new AppWaitingForDebuggerDialog( |
| 1012 | ActivityManagerService.this, |
| 1013 | mContext, app); |
| 1014 | app.waitDialog = d; |
| 1015 | app.waitedForDebugger = true; |
| 1016 | d.show(); |
| 1017 | } |
| 1018 | } else { |
| 1019 | if (app.waitDialog != null) { |
| 1020 | app.waitDialog.dismiss(); |
| 1021 | app.waitDialog = null; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1027 | if (mDidDexOpt) { |
| 1028 | mDidDexOpt = false; |
| 1029 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 1030 | nmsg.obj = msg.obj; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1031 | mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1032 | return; |
| 1033 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1034 | mServices.serviceTimeout((ProcessRecord)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | } break; |
| 1036 | case UPDATE_TIME_ZONE: { |
| 1037 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1038 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1039 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | if (r.thread != null) { |
| 1041 | try { |
| 1042 | r.thread.updateTimeZone(); |
| 1043 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1044 | 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] | 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1049 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1050 | case CLEAR_DNS_CACHE: { |
| 1051 | synchronized (ActivityManagerService.this) { |
| 1052 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1053 | ProcessRecord r = mLruProcesses.get(i); |
| 1054 | if (r.thread != null) { |
| 1055 | try { |
| 1056 | r.thread.clearDnsCache(); |
| 1057 | } catch (RemoteException ex) { |
| 1058 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1064 | case UPDATE_HTTP_PROXY: { |
| 1065 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1066 | String host = ""; |
| 1067 | String port = ""; |
| 1068 | String exclList = ""; |
| 1069 | if (proxy != null) { |
| 1070 | host = proxy.getHost(); |
| 1071 | port = Integer.toString(proxy.getPort()); |
| 1072 | exclList = proxy.getExclusionList(); |
| 1073 | } |
| 1074 | synchronized (ActivityManagerService.this) { |
| 1075 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1076 | ProcessRecord r = mLruProcesses.get(i); |
| 1077 | if (r.thread != null) { |
| 1078 | try { |
| 1079 | r.thread.setHttpProxy(host, port, exclList); |
| 1080 | } catch (RemoteException ex) { |
| 1081 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1082 | r.info.processName); |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1088 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1089 | String title = "System UIDs Inconsistent"; |
| 1090 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1091 | + " data partition or your device will be unstable."; |
| 1092 | Log.e(TAG, title + ": " + text); |
| 1093 | if (mShowDialogs) { |
| 1094 | // XXX This is a temporary dialog, no need to localize. |
| 1095 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1096 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1097 | d.setCancelable(false); |
| 1098 | d.setTitle(title); |
| 1099 | d.setMessage(text); |
| 1100 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1101 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1102 | mUidAlert = d; |
| 1103 | d.show(); |
| 1104 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | } break; |
| 1106 | case IM_FEELING_LUCKY_MSG: { |
| 1107 | if (mUidAlert != null) { |
| 1108 | mUidAlert.dismiss(); |
| 1109 | mUidAlert = null; |
| 1110 | } |
| 1111 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1113 | if (mDidDexOpt) { |
| 1114 | mDidDexOpt = false; |
| 1115 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1116 | nmsg.obj = msg.obj; |
| 1117 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1118 | return; |
| 1119 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1121 | synchronized (ActivityManagerService.this) { |
| 1122 | processStartTimedOutLocked(app); |
| 1123 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1124 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1125 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1126 | synchronized (ActivityManagerService.this) { |
| 1127 | doPendingActivityLaunchesLocked(true); |
| 1128 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1129 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1130 | case KILL_APPLICATION_MSG: { |
| 1131 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1132 | int appid = msg.arg1; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1133 | boolean restart = (msg.arg2 == 1); |
| 1134 | String pkg = (String) msg.obj; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1135 | forceStopPackageLocked(pkg, appid, restart, false, true, false, |
| 1136 | UserHandle.USER_ALL); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1139 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1140 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1141 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1142 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1143 | INotificationManager inm = NotificationManager.getService(); |
| 1144 | if (inm == null) { |
| 1145 | return; |
| 1146 | } |
| 1147 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1148 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1149 | ProcessRecord process = root.app; |
| 1150 | if (process == null) { |
| 1151 | return; |
| 1152 | } |
| 1153 | |
| 1154 | try { |
| 1155 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1156 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1157 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1158 | Notification notification = new Notification(); |
| 1159 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1160 | notification.when = 0; |
| 1161 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1162 | notification.tickerText = text; |
| 1163 | notification.defaults = 0; // please be quiet |
| 1164 | notification.sound = null; |
| 1165 | notification.vibrate = null; |
| 1166 | notification.setLatestEventInfo(context, text, |
| 1167 | mContext.getText(R.string.heavy_weight_notification_detail), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1168 | PendingIntent.getActivityAsUser(mContext, 0, root.intent, |
| 1169 | PendingIntent.FLAG_CANCEL_CURRENT, null, |
| 1170 | new UserHandle(root.userId))); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1171 | |
| 1172 | try { |
| 1173 | int[] outId = new int[1]; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1174 | inm.enqueueNotificationWithTag("android", null, |
| 1175 | R.string.heavy_weight_notification, |
| 1176 | notification, outId, root.userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1177 | } catch (RuntimeException e) { |
| 1178 | Slog.w(ActivityManagerService.TAG, |
| 1179 | "Error showing notification for heavy-weight app", e); |
| 1180 | } catch (RemoteException e) { |
| 1181 | } |
| 1182 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1183 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1184 | } |
| 1185 | } break; |
| 1186 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1187 | INotificationManager inm = NotificationManager.getService(); |
| 1188 | if (inm == null) { |
| 1189 | return; |
| 1190 | } |
| 1191 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1192 | inm.cancelNotificationWithTag("android", null, |
| 1193 | R.string.heavy_weight_notification, msg.arg1); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1194 | } catch (RuntimeException e) { |
| 1195 | Slog.w(ActivityManagerService.TAG, |
| 1196 | "Error canceling notification for service", e); |
| 1197 | } catch (RemoteException e) { |
| 1198 | } |
| 1199 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1200 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1201 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1202 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1203 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1204 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1205 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1206 | } |
| 1207 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1208 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1209 | synchronized (ActivityManagerService.this) { |
| 1210 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1211 | if (mCompatModeDialog != null) { |
| 1212 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1213 | ar.info.applicationInfo.packageName)) { |
| 1214 | return; |
| 1215 | } |
| 1216 | mCompatModeDialog.dismiss(); |
| 1217 | mCompatModeDialog = null; |
| 1218 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1219 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1220 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1221 | ar.packageName)) { |
| 1222 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1223 | ar.info.applicationInfo); |
| 1224 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1225 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1226 | mCompatModeDialog = new CompatModeDialog( |
| 1227 | ActivityManagerService.this, mContext, |
| 1228 | ar.info.applicationInfo); |
| 1229 | mCompatModeDialog.show(); |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1234 | break; |
| 1235 | } |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 1236 | case DISPATCH_PROCESSES_CHANGED: { |
| 1237 | dispatchProcessesChanged(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1238 | break; |
| 1239 | } |
| 1240 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1241 | final int pid = msg.arg1; |
| 1242 | final int uid = msg.arg2; |
| 1243 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1244 | break; |
| 1245 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1246 | case REPORT_MEM_USAGE: { |
| 1247 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1248 | if (!isDebuggable) { |
| 1249 | return; |
| 1250 | } |
| 1251 | synchronized (ActivityManagerService.this) { |
| 1252 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1253 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1254 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1255 | // avoid spamming. |
| 1256 | return; |
| 1257 | } |
| 1258 | mLastMemUsageReportTime = now; |
| 1259 | } |
| 1260 | Thread thread = new Thread() { |
| 1261 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1262 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1263 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1264 | StringWriter oomSw = new StringWriter(); |
| 1265 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1266 | StringWriter catSw = new StringWriter(); |
| 1267 | PrintWriter catPw = new PrintWriter(catSw); |
| 1268 | String[] emptyArgs = new String[] { }; |
| 1269 | StringBuilder tag = new StringBuilder(128); |
| 1270 | StringBuilder stack = new StringBuilder(128); |
| 1271 | tag.append("Low on memory -- "); |
| 1272 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1273 | tag, stack); |
| 1274 | dropBuilder.append(stack); |
| 1275 | dropBuilder.append('\n'); |
| 1276 | dropBuilder.append('\n'); |
| 1277 | String oomString = oomSw.toString(); |
| 1278 | dropBuilder.append(oomString); |
| 1279 | dropBuilder.append('\n'); |
| 1280 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1281 | try { |
| 1282 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1283 | "procrank", }); |
| 1284 | final InputStreamReader converter = new InputStreamReader( |
| 1285 | proc.getInputStream()); |
| 1286 | BufferedReader in = new BufferedReader(converter); |
| 1287 | String line; |
| 1288 | while (true) { |
| 1289 | line = in.readLine(); |
| 1290 | if (line == null) { |
| 1291 | break; |
| 1292 | } |
| 1293 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1294 | logBuilder.append(line); |
| 1295 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1296 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1297 | dropBuilder.append(line); |
| 1298 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1299 | } |
| 1300 | converter.close(); |
| 1301 | } catch (IOException e) { |
| 1302 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1303 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1304 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1305 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1306 | catPw.println(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1307 | mServices.dumpServicesLocked(null, catPw, emptyArgs, 0, |
| 1308 | false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1309 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1310 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1311 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1312 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1313 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1314 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1315 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1316 | synchronized (ActivityManagerService.this) { |
| 1317 | long now = SystemClock.uptimeMillis(); |
| 1318 | if (mLastMemUsageReportTime < now) { |
| 1319 | mLastMemUsageReportTime = now; |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | }; |
| 1324 | thread.start(); |
| 1325 | break; |
| 1326 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1327 | case REPORT_USER_SWITCH_MSG: { |
| 1328 | dispatchUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1329 | break; |
| 1330 | } |
| 1331 | case CONTINUE_USER_SWITCH_MSG: { |
| 1332 | continueUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1333 | break; |
| 1334 | } |
| 1335 | case USER_SWITCH_TIMEOUT_MSG: { |
| 1336 | timeoutUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1337 | break; |
| 1338 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | }; |
| 1342 | |
| 1343 | public static void setSystemProcess() { |
| 1344 | try { |
| 1345 | ActivityManagerService m = mSelf; |
| 1346 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1347 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1349 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1350 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | if (MONITOR_CPU_USAGE) { |
| 1352 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1353 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1354 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1355 | |
| 1356 | ApplicationInfo info = |
| 1357 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1358 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1359 | mSystemThread.installSystemApplicationInfo(info); |
| 1360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1361 | synchronized (mSelf) { |
| 1362 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1363 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1364 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1366 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1367 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1368 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | synchronized (mSelf.mPidsSelfLocked) { |
| 1370 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1371 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1372 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | } |
| 1374 | } catch (PackageManager.NameNotFoundException e) { |
| 1375 | throw new RuntimeException( |
| 1376 | "Unable to find android system package", e); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | public void setWindowManager(WindowManagerService wm) { |
| 1381 | mWindowManager = wm; |
| 1382 | } |
| 1383 | |
| 1384 | public static final Context main(int factoryTest) { |
| 1385 | AThread thr = new AThread(); |
| 1386 | thr.start(); |
| 1387 | |
| 1388 | synchronized (thr) { |
| 1389 | while (thr.mService == null) { |
| 1390 | try { |
| 1391 | thr.wait(); |
| 1392 | } catch (InterruptedException e) { |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | ActivityManagerService m = thr.mService; |
| 1398 | mSelf = m; |
| 1399 | ActivityThread at = ActivityThread.systemMain(); |
| 1400 | mSystemThread = at; |
| 1401 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1402 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | m.mContext = context; |
| 1404 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1405 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | |
| 1407 | m.mBatteryStatsService.publish(context); |
| 1408 | m.mUsageStatsService.publish(context); |
| 1409 | |
| 1410 | synchronized (thr) { |
| 1411 | thr.mReady = true; |
| 1412 | thr.notifyAll(); |
| 1413 | } |
| 1414 | |
| 1415 | m.startRunning(null, null, null, null); |
| 1416 | |
| 1417 | return context; |
| 1418 | } |
| 1419 | |
| 1420 | public static ActivityManagerService self() { |
| 1421 | return mSelf; |
| 1422 | } |
| 1423 | |
| 1424 | static class AThread extends Thread { |
| 1425 | ActivityManagerService mService; |
| 1426 | boolean mReady = false; |
| 1427 | |
| 1428 | public AThread() { |
| 1429 | super("ActivityManager"); |
| 1430 | } |
| 1431 | |
| 1432 | public void run() { |
| 1433 | Looper.prepare(); |
| 1434 | |
| 1435 | android.os.Process.setThreadPriority( |
| 1436 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1437 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | |
| 1439 | ActivityManagerService m = new ActivityManagerService(); |
| 1440 | |
| 1441 | synchronized (this) { |
| 1442 | mService = m; |
| 1443 | notifyAll(); |
| 1444 | } |
| 1445 | |
| 1446 | synchronized (this) { |
| 1447 | while (!mReady) { |
| 1448 | try { |
| 1449 | wait(); |
| 1450 | } catch (InterruptedException e) { |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1455 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1456 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1457 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1458 | } |
| 1459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | Looper.loop(); |
| 1461 | } |
| 1462 | } |
| 1463 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | static class MemBinder extends Binder { |
| 1465 | ActivityManagerService mActivityManagerService; |
| 1466 | MemBinder(ActivityManagerService activityManagerService) { |
| 1467 | mActivityManagerService = activityManagerService; |
| 1468 | } |
| 1469 | |
| 1470 | @Override |
| 1471 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1472 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1473 | != PackageManager.PERMISSION_GRANTED) { |
| 1474 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1475 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1476 | + " without permission " + android.Manifest.permission.DUMP); |
| 1477 | return; |
| 1478 | } |
| 1479 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1480 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1481 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1485 | static class GraphicsBinder extends Binder { |
| 1486 | ActivityManagerService mActivityManagerService; |
| 1487 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1488 | mActivityManagerService = activityManagerService; |
| 1489 | } |
| 1490 | |
| 1491 | @Override |
| 1492 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1493 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1494 | != PackageManager.PERMISSION_GRANTED) { |
| 1495 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1496 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1497 | + " without permission " + android.Manifest.permission.DUMP); |
| 1498 | return; |
| 1499 | } |
| 1500 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1501 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1505 | static class DbBinder extends Binder { |
| 1506 | ActivityManagerService mActivityManagerService; |
| 1507 | DbBinder(ActivityManagerService activityManagerService) { |
| 1508 | mActivityManagerService = activityManagerService; |
| 1509 | } |
| 1510 | |
| 1511 | @Override |
| 1512 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1513 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1514 | != PackageManager.PERMISSION_GRANTED) { |
| 1515 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1516 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1517 | + " without permission " + android.Manifest.permission.DUMP); |
| 1518 | return; |
| 1519 | } |
| 1520 | |
| 1521 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1522 | } |
| 1523 | } |
| 1524 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | static class CpuBinder extends Binder { |
| 1526 | ActivityManagerService mActivityManagerService; |
| 1527 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1528 | mActivityManagerService = activityManagerService; |
| 1529 | } |
| 1530 | |
| 1531 | @Override |
| 1532 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1533 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1534 | != PackageManager.PERMISSION_GRANTED) { |
| 1535 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1536 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1537 | + " without permission " + android.Manifest.permission.DUMP); |
| 1538 | return; |
| 1539 | } |
| 1540 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1542 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1543 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1544 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1550 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1551 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1552 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1553 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1554 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1555 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 1556 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1557 | mServices = new ActiveServices(this); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1558 | mProviderMap = new ProviderMap(this); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1559 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1560 | File dataDir = Environment.getDataDirectory(); |
| 1561 | File systemDir = new File(dataDir, "system"); |
| 1562 | systemDir.mkdirs(); |
| 1563 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1564 | systemDir, "batterystats.bin").toString()); |
| 1565 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1566 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1567 | mOnBattery = DEBUG_POWER ? true |
| 1568 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1569 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1570 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1571 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1572 | systemDir, "usagestats").toString()); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1573 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1575 | // User 0 is the first and only user that runs at boot. |
| 1576 | mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true)); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1577 | mUserLru.add(Integer.valueOf(0)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1578 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1579 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1580 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1581 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1582 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1583 | mConfiguration.setToDefaults(); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1584 | mConfiguration.setLocale(Locale.getDefault()); |
| 1585 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1586 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | mProcessStats.init(); |
| 1588 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1589 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1590 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | // Add ourself to the Watchdog monitors. |
| 1592 | Watchdog.getInstance().addMonitor(this); |
| 1593 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1595 | public void run() { |
| 1596 | while (true) { |
| 1597 | try { |
| 1598 | try { |
| 1599 | synchronized(this) { |
| 1600 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1601 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1602 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1603 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | // + ", write delay=" + nextWriteDelay); |
| 1605 | if (nextWriteDelay < nextCpuDelay) { |
| 1606 | nextCpuDelay = nextWriteDelay; |
| 1607 | } |
| 1608 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1609 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | this.wait(nextCpuDelay); |
| 1611 | } |
| 1612 | } |
| 1613 | } catch (InterruptedException e) { |
| 1614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | updateCpuStatsNow(); |
| 1616 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1617 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | }; |
| 1622 | mProcessStatsThread.start(); |
| 1623 | } |
| 1624 | |
| 1625 | @Override |
| 1626 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1627 | throws RemoteException { |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 1628 | if (code == SYSPROPS_TRANSACTION) { |
| 1629 | // We need to tell all apps about the system property change. |
| 1630 | ArrayList<IBinder> procs = new ArrayList<IBinder>(); |
| 1631 | synchronized(this) { |
| 1632 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 1633 | final int NA = apps.size(); |
| 1634 | for (int ia=0; ia<NA; ia++) { |
| 1635 | ProcessRecord app = apps.valueAt(ia); |
| 1636 | if (app.thread != null) { |
| 1637 | procs.add(app.thread.asBinder()); |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | int N = procs.size(); |
| 1644 | for (int i=0; i<N; i++) { |
| 1645 | Parcel data2 = Parcel.obtain(); |
| 1646 | try { |
| 1647 | procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0); |
| 1648 | } catch (RemoteException e) { |
| 1649 | } |
| 1650 | data2.recycle(); |
| 1651 | } |
| 1652 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | try { |
| 1654 | return super.onTransact(code, data, reply, flags); |
| 1655 | } catch (RuntimeException e) { |
| 1656 | // The activity manager only throws security exceptions, so let's |
| 1657 | // log all others. |
| 1658 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1659 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1660 | } |
| 1661 | throw e; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1666 | final long now = SystemClock.uptimeMillis(); |
| 1667 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1668 | return; |
| 1669 | } |
| 1670 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1671 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | mProcessStatsThread.notify(); |
| 1673 | } |
| 1674 | } |
| 1675 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | void updateCpuStatsNow() { |
| 1678 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1679 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1680 | final long now = SystemClock.uptimeMillis(); |
| 1681 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1683 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1684 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1685 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | haveNewCpuStats = true; |
| 1687 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1688 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1689 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1691 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1692 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1694 | int user = mProcessStats.getLastUserTime(); |
| 1695 | int system = mProcessStats.getLastSystemTime(); |
| 1696 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1697 | int irq = mProcessStats.getLastIrqTime(); |
| 1698 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1699 | int idle = mProcessStats.getLastIdleTime(); |
| 1700 | |
| 1701 | int total = user + system + iowait + irq + softIrq + idle; |
| 1702 | if (total == 0) total = 1; |
| 1703 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1704 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1705 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1706 | (user * 100) / total, |
| 1707 | (system * 100) / total, |
| 1708 | (iowait * 100) / total, |
| 1709 | (irq * 100) / total, |
| 1710 | (softIrq * 100) / total); |
| 1711 | } |
| 1712 | } |
| 1713 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1714 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1715 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1716 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1717 | synchronized(mPidsSelfLocked) { |
| 1718 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1719 | if (mOnBattery) { |
| 1720 | int perc = bstats.startAddingCpuLocked(); |
| 1721 | int totalUTime = 0; |
| 1722 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1723 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1725 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1726 | if (!st.working) { |
| 1727 | continue; |
| 1728 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1729 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1730 | int otherUTime = (st.rel_utime*perc)/100; |
| 1731 | int otherSTime = (st.rel_stime*perc)/100; |
| 1732 | totalUTime += otherUTime; |
| 1733 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | if (pr != null) { |
| 1735 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1736 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1737 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1738 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1739 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1740 | } else { |
| 1741 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1742 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1743 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1744 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1745 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1746 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1747 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1748 | } |
| 1749 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1750 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1751 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1755 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1756 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1757 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1758 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | } |
| 1762 | } |
| 1763 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1764 | @Override |
| 1765 | public void batteryNeedsCpuUpdate() { |
| 1766 | updateCpuStatsNow(); |
| 1767 | } |
| 1768 | |
| 1769 | @Override |
| 1770 | public void batteryPowerChanged(boolean onBattery) { |
| 1771 | // When plugging in, update the CPU stats first before changing |
| 1772 | // the plug state. |
| 1773 | updateCpuStatsNow(); |
| 1774 | synchronized (this) { |
| 1775 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1776 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | /** |
| 1782 | * Initialize the application bind args. These are passed to each |
| 1783 | * process when the bindApplication() IPC is sent to the process. They're |
| 1784 | * lazily setup to make sure the services are running when they're asked for. |
| 1785 | */ |
| 1786 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1787 | if (mAppBindArgs == null) { |
| 1788 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1789 | |
| 1790 | // Setup the application init args |
| 1791 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1792 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1793 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1794 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1795 | } |
| 1796 | return mAppBindArgs; |
| 1797 | } |
| 1798 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1799 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | if (mFocusedActivity != r) { |
| 1801 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1802 | if (r != null) { |
| 1803 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1804 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1808 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1809 | boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1810 | // 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] | 1811 | int lrui = mLruProcesses.indexOf(app); |
| 1812 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1813 | |
| 1814 | int i = mLruProcesses.size()-1; |
| 1815 | int skipTop = 0; |
| 1816 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1817 | app.lruSeq = mLruSeq; |
| 1818 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1819 | // compute the new weight for this process. |
| 1820 | if (updateActivityTime) { |
| 1821 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1822 | } |
| 1823 | if (app.activities.size() > 0) { |
| 1824 | // If this process has activities, we more strongly want to keep |
| 1825 | // it around. |
| 1826 | app.lruWeight = app.lastActivityTime; |
| 1827 | } else if (app.pubProviders.size() > 0) { |
| 1828 | // If this process contains content providers, we want to keep |
| 1829 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1830 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1831 | // 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] | 1832 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1833 | } else { |
| 1834 | // If this process doesn't have activities, we less strongly |
| 1835 | // want to keep it around, and generally want to avoid getting |
| 1836 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1837 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1838 | // 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] | 1839 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1840 | } |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1841 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1842 | while (i >= 0) { |
| 1843 | ProcessRecord p = mLruProcesses.get(i); |
| 1844 | // If this app shouldn't be in front of the first N background |
| 1845 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1846 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1847 | skipTop--; |
| 1848 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1849 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1850 | mLruProcesses.add(i+1, app); |
| 1851 | break; |
| 1852 | } |
| 1853 | i--; |
| 1854 | } |
| 1855 | if (i < 0) { |
| 1856 | mLruProcesses.add(0, app); |
| 1857 | } |
| 1858 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1859 | // If the app is currently using a content provider or service, |
| 1860 | // bump those processes as well. |
| 1861 | if (app.connections.size() > 0) { |
| 1862 | for (ConnectionRecord cr : app.connections) { |
| 1863 | if (cr.binding != null && cr.binding.service != null |
| 1864 | && cr.binding.service.app != null |
| 1865 | && cr.binding.service.app.lruSeq != mLruSeq) { |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1866 | updateLruProcessInternalLocked(cr.binding.service.app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1867 | updateActivityTime, i+1); |
| 1868 | } |
| 1869 | } |
| 1870 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1871 | for (int j=app.conProviders.size()-1; j>=0; j--) { |
| 1872 | ContentProviderRecord cpr = app.conProviders.get(j).provider; |
| 1873 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1874 | updateLruProcessInternalLocked(cpr.proc, |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1875 | updateActivityTime, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1876 | } |
| 1877 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1880 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1881 | boolean oomAdj, boolean updateActivityTime) { |
| 1882 | mLruSeq++; |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1883 | updateLruProcessInternalLocked(app, updateActivityTime, 0); |
| 1884 | |
| 1885 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
| 1886 | if (oomAdj) { |
| 1887 | updateOomAdjLocked(); |
| 1888 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1889 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1890 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1891 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | String processName, int uid) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1893 | if (uid == Process.SYSTEM_UID) { |
Amith Yamasani | 0184ce9 | 2012-03-28 22:41:41 -0700 | [diff] [blame] | 1894 | // The system gets to run in any process. If there are multiple |
| 1895 | // processes with the same uid, just pick the first (this |
| 1896 | // should never happen). |
| 1897 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1898 | processName); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1899 | if (procs == null) return null; |
| 1900 | final int N = procs.size(); |
| 1901 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1902 | if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1903 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1904 | } |
| 1905 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1906 | return proc; |
| 1907 | } |
| 1908 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1909 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1910 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1911 | try { |
| 1912 | if (pm.performDexOpt(packageName)) { |
| 1913 | mDidDexOpt = true; |
| 1914 | } |
| 1915 | } catch (RemoteException e) { |
| 1916 | } |
| 1917 | } |
| 1918 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1919 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1920 | int transit = mWindowManager.getPendingAppTransition(); |
| 1921 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1922 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1923 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1924 | } |
| 1925 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1926 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1927 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1928 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 1929 | boolean isolated) { |
| 1930 | ProcessRecord app; |
| 1931 | if (!isolated) { |
| 1932 | app = getProcessRecordLocked(processName, info.uid); |
| 1933 | } else { |
| 1934 | // If this is an isolated process, it can't re-use an existing process. |
| 1935 | app = null; |
| 1936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1937 | // We don't have to do anything more if: |
| 1938 | // (1) There is an existing application record; and |
| 1939 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1940 | // object attached to it so we know it couldn't have crashed; and |
| 1941 | // (3) There is a pid assigned to it, so it is either starting or |
| 1942 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1943 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1944 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1945 | + " thread=" + (app != null ? app.thread : null) |
| 1946 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1947 | if (app != null && app.pid > 0) { |
| 1948 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1949 | // We already have the app running, or are waiting for it to |
| 1950 | // 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] | 1951 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1952 | // If this is a new package in the process, add the package to the list |
| 1953 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1954 | return app; |
| 1955 | } else { |
| 1956 | // An application record is attached to a previous process, |
| 1957 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1958 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1959 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1960 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1961 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1962 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1963 | String hostingNameStr = hostingName != null |
| 1964 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1965 | |
| 1966 | if (!isolated) { |
| 1967 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1968 | // If we are in the background, then check to see if this process |
| 1969 | // is bad. If so, we will just silently fail. |
| 1970 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1971 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1972 | + "/" + info.processName); |
| 1973 | return null; |
| 1974 | } |
| 1975 | } else { |
| 1976 | // When the user is explicitly starting a process, then clear its |
| 1977 | // crash count so that we won't make it bad until they see at |
| 1978 | // least one crash dialog again, and make the process good again |
| 1979 | // if it had been bad. |
| 1980 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1981 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1982 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1983 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1984 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
| 1985 | info.processName); |
| 1986 | mBadProcesses.remove(info.processName, info.uid); |
| 1987 | if (app != null) { |
| 1988 | app.bad = false; |
| 1989 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | } |
| 1991 | } |
| 1992 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1993 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1994 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1995 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 1996 | if (app == null) { |
| 1997 | Slog.w(TAG, "Failed making new process record for " |
| 1998 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 1999 | return null; |
| 2000 | } |
| 2001 | mProcessNames.put(processName, app.uid, app); |
| 2002 | if (isolated) { |
| 2003 | mIsolatedProcesses.put(app.uid, app); |
| 2004 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2005 | } else { |
| 2006 | // If this is a new package in the process, add the package to the list |
| 2007 | app.addPackage(info.packageName); |
| 2008 | } |
| 2009 | |
| 2010 | // If the system is not ready yet, then hold off on starting this |
| 2011 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2012 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2013 | && !isAllowedWhileBooting(info) |
| 2014 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2015 | if (!mProcessesOnHold.contains(app)) { |
| 2016 | mProcessesOnHold.add(app); |
| 2017 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2018 | 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] | 2019 | return app; |
| 2020 | } |
| 2021 | |
| 2022 | startProcessLocked(app, hostingType, hostingNameStr); |
| 2023 | return (app.pid != 0) ? app : null; |
| 2024 | } |
| 2025 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2026 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 2027 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 2028 | } |
| 2029 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2030 | private final void startProcessLocked(ProcessRecord app, |
| 2031 | String hostingType, String hostingNameStr) { |
| 2032 | if (app.pid > 0 && app.pid != MY_PID) { |
| 2033 | synchronized (mPidsSelfLocked) { |
| 2034 | mPidsSelfLocked.remove(app.pid); |
| 2035 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 2036 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2037 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2040 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 2041 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2042 | mProcessesOnHold.remove(app); |
| 2043 | |
| 2044 | updateCpuStats(); |
| 2045 | |
| 2046 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 2047 | mProcDeaths[0] = 0; |
| 2048 | |
| 2049 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2050 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2051 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2052 | int[] gids = null; |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2053 | int mountExternal = Zygote.MOUNT_EXTERNAL_NONE; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2054 | if (!app.isolated) { |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2055 | int[] permGids = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2056 | try { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2057 | final PackageManager pm = mContext.getPackageManager(); |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2058 | permGids = pm.getPackageGids(app.info.packageName); |
Jeff Sharkey | e217ee4 | 2012-08-28 16:23:01 -0700 | [diff] [blame] | 2059 | |
| 2060 | if (Environment.isExternalStorageEmulated()) { |
| 2061 | if (pm.checkPermission( |
| 2062 | android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE, |
| 2063 | app.info.packageName) == PERMISSION_GRANTED) { |
| 2064 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL; |
| 2065 | } else { |
| 2066 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER; |
| 2067 | } |
| 2068 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2069 | } catch (PackageManager.NameNotFoundException e) { |
| 2070 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 2071 | } |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2072 | |
| 2073 | /* |
| 2074 | * Add shared application GID so applications can share some |
| 2075 | * resources like shared libraries |
| 2076 | */ |
| 2077 | if (permGids == null) { |
| 2078 | gids = new int[1]; |
| 2079 | } else { |
| 2080 | gids = new int[permGids.length + 1]; |
| 2081 | System.arraycopy(permGids, 0, gids, 1, permGids.length); |
| 2082 | } |
| 2083 | gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2084 | } |
| 2085 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 2086 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2087 | && mTopComponent != null |
| 2088 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 2089 | uid = 0; |
| 2090 | } |
| 2091 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 2092 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 2093 | uid = 0; |
| 2094 | } |
| 2095 | } |
| 2096 | int debugFlags = 0; |
| 2097 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 2098 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 2099 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 2100 | // awkward to turn on otherwise. |
| 2101 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2102 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 2103 | // Run the app in safe mode if its manifest requests so or the |
| 2104 | // system is booted in safe mode. |
| 2105 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 2106 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 2107 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 2108 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2109 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 2110 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 2111 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 2112 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 2113 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 2114 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2115 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 2116 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 2117 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2118 | |
| 2119 | // Start the process. It will either succeed and return a result containing |
| 2120 | // the PID of the new process, or else throw a RuntimeException. |
| 2121 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2122 | app.processName, uid, uid, gids, debugFlags, mountExternal, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 2123 | app.info.targetSdkVersion, null, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2125 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2126 | synchronized (bs) { |
| 2127 | if (bs.isOnBattery()) { |
| 2128 | app.batteryStats.incStartsLocked(); |
| 2129 | } |
| 2130 | } |
| 2131 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2132 | EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2133 | app.processName, hostingType, |
| 2134 | hostingNameStr != null ? hostingNameStr : ""); |
| 2135 | |
| 2136 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2137 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2138 | } |
| 2139 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2140 | StringBuilder buf = mStringBuilder; |
| 2141 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2142 | buf.append("Start proc "); |
| 2143 | buf.append(app.processName); |
| 2144 | buf.append(" for "); |
| 2145 | buf.append(hostingType); |
| 2146 | if (hostingNameStr != null) { |
| 2147 | buf.append(" "); |
| 2148 | buf.append(hostingNameStr); |
| 2149 | } |
| 2150 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2151 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2152 | buf.append(" uid="); |
| 2153 | buf.append(uid); |
| 2154 | buf.append(" gids={"); |
| 2155 | if (gids != null) { |
| 2156 | for (int gi=0; gi<gids.length; gi++) { |
| 2157 | if (gi != 0) buf.append(", "); |
| 2158 | buf.append(gids[gi]); |
| 2159 | |
| 2160 | } |
| 2161 | } |
| 2162 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2163 | Slog.i(TAG, buf.toString()); |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2164 | app.setPid(startResult.pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2165 | app.usingWrapper = startResult.usingWrapper; |
| 2166 | app.removed = false; |
| 2167 | synchronized (mPidsSelfLocked) { |
| 2168 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2169 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2170 | msg.obj = app; |
| 2171 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2172 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2173 | } |
| 2174 | } catch (RuntimeException e) { |
| 2175 | // XXX do better error recovery. |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2176 | app.setPid(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2177 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2178 | } |
| 2179 | } |
| 2180 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2181 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2182 | if (resumed) { |
| 2183 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2184 | } else { |
| 2185 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2186 | } |
| 2187 | } |
| 2188 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2189 | boolean startHomeActivityLocked(int userId) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2190 | if (mHeadless) { |
| 2191 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2192 | // when running headless. |
| 2193 | ensureBootCompleted(); |
| 2194 | return false; |
| 2195 | } |
| 2196 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2197 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2198 | && mTopAction == null) { |
| 2199 | // We are running in factory test mode, but unable to find |
| 2200 | // the factory test app, so just sit around displaying the |
| 2201 | // error message and don't try to start anything. |
| 2202 | return false; |
| 2203 | } |
| 2204 | Intent intent = new Intent( |
| 2205 | mTopAction, |
| 2206 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2207 | intent.setComponent(mTopComponent); |
| 2208 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2209 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2210 | } |
| 2211 | ActivityInfo aInfo = |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2212 | resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2213 | if (aInfo != null) { |
| 2214 | intent.setComponent(new ComponentName( |
| 2215 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2216 | // Don't do this if the home app is currently being |
| 2217 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2218 | aInfo = new ActivityInfo(aInfo); |
| 2219 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2220 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2221 | aInfo.applicationInfo.uid); |
| 2222 | if (app == null || app.instrumentationClass == null) { |
| 2223 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2224 | mMainStack.startActivityLocked(null, intent, null, aInfo, |
| 2225 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2226 | } |
| 2227 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2228 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2229 | return true; |
| 2230 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2231 | |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2232 | private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) { |
| 2233 | ActivityInfo ai = null; |
| 2234 | ComponentName comp = intent.getComponent(); |
| 2235 | try { |
| 2236 | if (comp != null) { |
| 2237 | ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId); |
| 2238 | } else { |
| 2239 | ResolveInfo info = AppGlobals.getPackageManager().resolveIntent( |
| 2240 | intent, |
| 2241 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
| 2242 | flags, userId); |
| 2243 | |
| 2244 | if (info != null) { |
| 2245 | ai = info.activityInfo; |
| 2246 | } |
| 2247 | } |
| 2248 | } catch (RemoteException e) { |
| 2249 | // ignore |
| 2250 | } |
| 2251 | |
| 2252 | return ai; |
| 2253 | } |
| 2254 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2255 | /** |
| 2256 | * Starts the "new version setup screen" if appropriate. |
| 2257 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2258 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2259 | // Only do this once per boot. |
| 2260 | if (mCheckedForSetup) { |
| 2261 | return; |
| 2262 | } |
| 2263 | |
| 2264 | // We will show this screen if the current one is a different |
| 2265 | // version than the last one shown, and we are not running in |
| 2266 | // low-level factory test mode. |
| 2267 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2268 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame^] | 2269 | Settings.Global.getInt(resolver, |
| 2270 | Settings.Global.DEVICE_PROVISIONED, 0) != 0) { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2271 | mCheckedForSetup = true; |
| 2272 | |
| 2273 | // See if we should be showing the platform update setup UI. |
| 2274 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2275 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2276 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2277 | |
| 2278 | // We don't allow third party apps to replace this. |
| 2279 | ResolveInfo ri = null; |
| 2280 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2281 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2282 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2283 | ri = ris.get(i); |
| 2284 | break; |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | if (ri != null) { |
| 2289 | String vers = ri.activityInfo.metaData != null |
| 2290 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2291 | : null; |
| 2292 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2293 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2294 | Intent.METADATA_SETUP_VERSION); |
| 2295 | } |
| 2296 | String lastVers = Settings.Secure.getString( |
| 2297 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2298 | if (vers != null && !vers.equals(lastVers)) { |
| 2299 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2300 | intent.setComponent(new ComponentName( |
| 2301 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2302 | mMainStack.startActivityLocked(null, intent, null, ri.activityInfo, |
| 2303 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | } |
| 2308 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2309 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2310 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2311 | } |
| 2312 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2313 | void enforceNotIsolatedCaller(String caller) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2314 | if (UserHandle.isIsolated(Binder.getCallingUid())) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2315 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2316 | } |
| 2317 | } |
| 2318 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2319 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2320 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2321 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2322 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2323 | } |
| 2324 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2325 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2326 | public void setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2327 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2328 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2329 | synchronized (this) { |
| 2330 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2331 | } |
| 2332 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2333 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2334 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2335 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2336 | synchronized (this) { |
| 2337 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 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 setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2342 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2343 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2344 | synchronized (this) { |
| 2345 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2346 | } |
| 2347 | } |
| 2348 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2349 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2350 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2351 | synchronized (this) { |
| 2352 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2357 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2358 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2359 | synchronized (this) { |
| 2360 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2361 | } |
| 2362 | } |
| 2363 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2364 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2365 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2366 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2367 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2368 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2369 | private void dispatchProcessesChanged() { |
| 2370 | int N; |
| 2371 | synchronized (this) { |
| 2372 | N = mPendingProcessChanges.size(); |
| 2373 | if (mActiveProcessChanges.length < N) { |
| 2374 | mActiveProcessChanges = new ProcessChangeItem[N]; |
| 2375 | } |
| 2376 | mPendingProcessChanges.toArray(mActiveProcessChanges); |
| 2377 | mAvailProcessChanges.addAll(mPendingProcessChanges); |
| 2378 | mPendingProcessChanges.clear(); |
| 2379 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes"); |
| 2380 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2381 | int i = mProcessObservers.beginBroadcast(); |
| 2382 | while (i > 0) { |
| 2383 | i--; |
| 2384 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2385 | if (observer != null) { |
| 2386 | try { |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2387 | for (int j=0; j<N; j++) { |
| 2388 | ProcessChangeItem item = mActiveProcessChanges[j]; |
| 2389 | if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) { |
| 2390 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid=" |
| 2391 | + item.pid + " uid=" + item.uid + ": " |
| 2392 | + item.foregroundActivities); |
| 2393 | observer.onForegroundActivitiesChanged(item.pid, item.uid, |
| 2394 | item.foregroundActivities); |
| 2395 | } |
| 2396 | if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) { |
| 2397 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid=" |
| 2398 | + item.pid + " uid=" + item.uid + ": " + item.importance); |
| 2399 | observer.onImportanceChanged(item.pid, item.uid, |
| 2400 | item.importance); |
| 2401 | } |
| 2402 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2403 | } catch (RemoteException e) { |
| 2404 | } |
| 2405 | } |
| 2406 | } |
| 2407 | mProcessObservers.finishBroadcast(); |
| 2408 | } |
| 2409 | |
| 2410 | private void dispatchProcessDied(int pid, int uid) { |
| 2411 | int i = mProcessObservers.beginBroadcast(); |
| 2412 | while (i > 0) { |
| 2413 | i--; |
| 2414 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2415 | if (observer != null) { |
| 2416 | try { |
| 2417 | observer.onProcessDied(pid, uid); |
| 2418 | } catch (RemoteException e) { |
| 2419 | } |
| 2420 | } |
| 2421 | } |
| 2422 | mProcessObservers.finishBroadcast(); |
| 2423 | } |
| 2424 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2425 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2426 | final int N = mPendingActivityLaunches.size(); |
| 2427 | if (N <= 0) { |
| 2428 | return; |
| 2429 | } |
| 2430 | for (int i=0; i<N; i++) { |
| 2431 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2432 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2433 | pal.startFlags, doResume && i == (N-1), null); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2434 | } |
| 2435 | mPendingActivityLaunches.clear(); |
| 2436 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2437 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2438 | public final int startActivity(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2439 | Intent intent, String resolvedType, IBinder resultTo, |
| 2440 | String resultWho, int requestCode, int startFlags, |
| 2441 | String profileFile, ParcelFileDescriptor profileFd, Bundle options) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2442 | return startActivityAsUser(caller, intent, resolvedType, resultTo, resultWho, requestCode, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2443 | startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId()); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | public final int startActivityAsUser(IApplicationThread caller, |
| 2447 | Intent intent, String resolvedType, IBinder resultTo, |
| 2448 | String resultWho, int requestCode, int startFlags, |
| 2449 | String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2450 | enforceNotIsolatedCaller("startActivity"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2451 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | d888399 | 2012-09-07 15:58:52 -0700 | [diff] [blame] | 2452 | false, true, "startActivity", null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2453 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2454 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2455 | null, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2459 | Intent intent, String resolvedType, IBinder resultTo, |
| 2460 | String resultWho, int requestCode, int startFlags, String profileFile, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2461 | ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2462 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2463 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2464 | false, true, "startActivityAndWait", null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2465 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2466 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2467 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2468 | res, null, options, UserHandle.getCallingUserId()); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2469 | return res; |
| 2470 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 2471 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2472 | public final int startActivityWithConfig(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2473 | Intent intent, String resolvedType, IBinder resultTo, |
| 2474 | String resultWho, int requestCode, int startFlags, Configuration config, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2475 | Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2476 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2477 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2478 | false, true, "startActivityWithConfig", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2479 | int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2480 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2481 | null, null, null, config, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2482 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2483 | } |
| 2484 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2485 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2486 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2487 | IBinder resultTo, String resultWho, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2488 | int flagsMask, int flagsValues, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2489 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2490 | // Refuse possible leaked file descriptors |
| 2491 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2492 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2493 | } |
| 2494 | |
| 2495 | IIntentSender sender = intent.getTarget(); |
| 2496 | if (!(sender instanceof PendingIntentRecord)) { |
| 2497 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2498 | } |
| 2499 | |
| 2500 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2501 | |
| 2502 | synchronized (this) { |
| 2503 | // If this is coming from the currently resumed activity, it is |
| 2504 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2505 | if (mMainStack.mResumedActivity != null |
| 2506 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2507 | Binder.getCallingUid()) { |
| 2508 | mAppSwitchesAllowedTime = 0; |
| 2509 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2510 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2511 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null, |
| 2512 | resultTo, resultWho, requestCode, flagsMask, flagsValues, options); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2513 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2514 | } |
| 2515 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2516 | public boolean startNextMatchingActivity(IBinder callingActivity, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2517 | Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2518 | // Refuse possible leaked file descriptors |
| 2519 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2520 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2521 | } |
| 2522 | |
| 2523 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2524 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2525 | if (r == null) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2526 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2527 | return false; |
| 2528 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2529 | if (r.app == null || r.app.thread == null) { |
| 2530 | // The caller is not running... d'oh! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2531 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2532 | return false; |
| 2533 | } |
| 2534 | intent = new Intent(intent); |
| 2535 | // The caller is not allowed to change the data. |
| 2536 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2537 | // And we are resetting to find the next component... |
| 2538 | intent.setComponent(null); |
| 2539 | |
| 2540 | ActivityInfo aInfo = null; |
| 2541 | try { |
| 2542 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2543 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2544 | intent, r.resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2545 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2546 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2547 | |
| 2548 | // Look for the original activity in the list... |
| 2549 | final int N = resolves != null ? resolves.size() : 0; |
| 2550 | for (int i=0; i<N; i++) { |
| 2551 | ResolveInfo rInfo = resolves.get(i); |
| 2552 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2553 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2554 | // We found the current one... the next matching is |
| 2555 | // after it. |
| 2556 | i++; |
| 2557 | if (i<N) { |
| 2558 | aInfo = resolves.get(i).activityInfo; |
| 2559 | } |
| 2560 | break; |
| 2561 | } |
| 2562 | } |
| 2563 | } catch (RemoteException e) { |
| 2564 | } |
| 2565 | |
| 2566 | if (aInfo == null) { |
| 2567 | // Nobody who is next! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2568 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2569 | return false; |
| 2570 | } |
| 2571 | |
| 2572 | intent.setComponent(new ComponentName( |
| 2573 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2574 | intent.setFlags(intent.getFlags()&~( |
| 2575 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2576 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2577 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2578 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2579 | |
| 2580 | // Okay now we need to start the new activity, replacing the |
| 2581 | // currently running activity. This is a little tricky because |
| 2582 | // we want to start the new one as if the current one is finished, |
| 2583 | // but not finish the current one first so that there is no flicker. |
| 2584 | // And thus... |
| 2585 | final boolean wasFinishing = r.finishing; |
| 2586 | r.finishing = true; |
| 2587 | |
| 2588 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2589 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2590 | final String resultWho = r.resultWho; |
| 2591 | final int requestCode = r.requestCode; |
| 2592 | r.resultTo = null; |
| 2593 | if (resultTo != null) { |
| 2594 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2595 | } |
| 2596 | |
| 2597 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2598 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2599 | r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null, |
| 2600 | resultWho, requestCode, -1, r.launchedFromUid, 0, |
| 2601 | options, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | Binder.restoreCallingIdentity(origId); |
| 2603 | |
| 2604 | r.finishing = wasFinishing; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2605 | if (res != ActivityManager.START_SUCCESS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2606 | return false; |
| 2607 | } |
| 2608 | return true; |
| 2609 | } |
| 2610 | } |
| 2611 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2612 | final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2613 | Intent intent, String resolvedType, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2614 | String resultWho, int requestCode, int startFlags, Bundle options, int userId) { |
| 2615 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2616 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2617 | false, true, "startActivityInPackage", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2618 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2619 | int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2620 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2621 | null, null, null, null, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2622 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | public final int startActivities(IApplicationThread caller, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2626 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options, |
| 2627 | int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2628 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 9529386 | 2012-09-24 18:14:47 -0700 | [diff] [blame] | 2629 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2630 | false, true, "startActivity", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2631 | int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2632 | options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2633 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2634 | } |
| 2635 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2636 | final int startActivitiesInPackage(int uid, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2637 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2638 | Bundle options, int userId) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2639 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2640 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2641 | false, true, "startActivityInPackage", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2642 | int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2643 | options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2644 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2645 | } |
| 2646 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2647 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2648 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2649 | // Quick case: check if the top-most recent task is the same. |
| 2650 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2651 | return; |
| 2652 | } |
| 2653 | // 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] | 2654 | for (int i=0; i<N; i++) { |
| 2655 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2656 | if (task.userId == tr.userId |
| 2657 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2658 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2659 | mRecentTasks.remove(i); |
| 2660 | i--; |
| 2661 | N--; |
| 2662 | if (task.intent == null) { |
| 2663 | // If the new recent task we are adding is not fully |
| 2664 | // specified, then replace it with the existing recent task. |
| 2665 | task = tr; |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | if (N >= MAX_RECENT_TASKS) { |
| 2670 | mRecentTasks.remove(N-1); |
| 2671 | } |
| 2672 | mRecentTasks.add(0, task); |
| 2673 | } |
| 2674 | |
| 2675 | public void setRequestedOrientation(IBinder token, |
| 2676 | int requestedOrientation) { |
| 2677 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2678 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2679 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2680 | return; |
| 2681 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2682 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2683 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2684 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2685 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2686 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2687 | if (config != null) { |
| 2688 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2689 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2690 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2691 | } |
| 2692 | } |
| 2693 | Binder.restoreCallingIdentity(origId); |
| 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | public int getRequestedOrientation(IBinder token) { |
| 2698 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2699 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2700 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2701 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2702 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2703 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2707 | /** |
| 2708 | * This is the internal entry point for handling Activity.finish(). |
| 2709 | * |
| 2710 | * @param token The Binder token referencing the Activity we want to finish. |
| 2711 | * @param resultCode Result code, if any, from this Activity. |
| 2712 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2713 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2714 | * @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] | 2715 | */ |
| 2716 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2717 | // Refuse possible leaked file descriptors |
| 2718 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2719 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2720 | } |
| 2721 | |
| 2722 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2723 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2724 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2725 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2726 | if (next != null) { |
| 2727 | // ask watcher if this is allowed |
| 2728 | boolean resumeOK = true; |
| 2729 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2730 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2732 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | if (!resumeOK) { |
| 2736 | return false; |
| 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2741 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2742 | resultData, "app-request", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2743 | Binder.restoreCallingIdentity(origId); |
| 2744 | return res; |
| 2745 | } |
| 2746 | } |
| 2747 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2748 | public final void finishHeavyWeightApp() { |
| 2749 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2750 | != PackageManager.PERMISSION_GRANTED) { |
| 2751 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2752 | + Binder.getCallingPid() |
| 2753 | + ", uid=" + Binder.getCallingUid() |
| 2754 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2755 | Slog.w(TAG, msg); |
| 2756 | throw new SecurityException(msg); |
| 2757 | } |
| 2758 | |
| 2759 | synchronized(this) { |
| 2760 | if (mHeavyWeightProcess == null) { |
| 2761 | return; |
| 2762 | } |
| 2763 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2764 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2765 | mHeavyWeightProcess.activities); |
| 2766 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2767 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2768 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2769 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2770 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2771 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2772 | null, "finish-heavy", true); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2773 | } |
| 2774 | } |
| 2775 | } |
| 2776 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2777 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 2778 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2779 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2783 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2784 | String message) { |
| 2785 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2786 | != PackageManager.PERMISSION_GRANTED) { |
| 2787 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2788 | + Binder.getCallingPid() |
| 2789 | + ", uid=" + Binder.getCallingUid() |
| 2790 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2791 | Slog.w(TAG, msg); |
| 2792 | throw new SecurityException(msg); |
| 2793 | } |
| 2794 | |
| 2795 | synchronized(this) { |
| 2796 | ProcessRecord proc = null; |
| 2797 | |
| 2798 | // Figure out which process to kill. We don't trust that initialPid |
| 2799 | // still has any relation to current pids, so must scan through the |
| 2800 | // list. |
| 2801 | synchronized (mPidsSelfLocked) { |
| 2802 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2803 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2804 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2805 | continue; |
| 2806 | } |
| 2807 | if (p.pid == initialPid) { |
| 2808 | proc = p; |
| 2809 | break; |
| 2810 | } |
| 2811 | for (String str : p.pkgList) { |
| 2812 | if (str.equals(packageName)) { |
| 2813 | proc = p; |
| 2814 | } |
| 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | |
| 2819 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2820 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2821 | + " initialPid=" + initialPid |
| 2822 | + " packageName=" + packageName); |
| 2823 | return; |
| 2824 | } |
| 2825 | |
| 2826 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2827 | if (proc.pid == Process.myPid()) { |
| 2828 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2829 | return; |
| 2830 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2831 | long ident = Binder.clearCallingIdentity(); |
| 2832 | try { |
| 2833 | proc.thread.scheduleCrash(message); |
| 2834 | } catch (RemoteException e) { |
| 2835 | } |
| 2836 | Binder.restoreCallingIdentity(ident); |
| 2837 | } |
| 2838 | } |
| 2839 | } |
| 2840 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2841 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2842 | int requestCode) { |
| 2843 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2844 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2845 | mMainStack.finishSubActivityLocked(token, resultWho, requestCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2846 | Binder.restoreCallingIdentity(origId); |
| 2847 | } |
| 2848 | } |
| 2849 | |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2850 | public boolean finishActivityAffinity(IBinder token) { |
| 2851 | synchronized(this) { |
| 2852 | final long origId = Binder.clearCallingIdentity(); |
| 2853 | boolean res = mMainStack.finishActivityAffinityLocked(token); |
| 2854 | Binder.restoreCallingIdentity(origId); |
| 2855 | return res; |
| 2856 | } |
| 2857 | } |
| 2858 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2859 | public boolean willActivityBeVisible(IBinder token) { |
| 2860 | synchronized(this) { |
| 2861 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2862 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2863 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2864 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2865 | return true; |
| 2866 | } |
| 2867 | if (r.fullscreen && !r.finishing) { |
| 2868 | return false; |
| 2869 | } |
| 2870 | } |
| 2871 | return true; |
| 2872 | } |
| 2873 | } |
| 2874 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2875 | public void overridePendingTransition(IBinder token, String packageName, |
| 2876 | int enterAnim, int exitAnim) { |
| 2877 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2878 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2879 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2880 | return; |
| 2881 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2882 | |
| 2883 | final long origId = Binder.clearCallingIdentity(); |
| 2884 | |
| 2885 | if (self.state == ActivityState.RESUMED |
| 2886 | || self.state == ActivityState.PAUSING) { |
| 2887 | mWindowManager.overridePendingAppTransition(packageName, |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2888 | enterAnim, exitAnim, null); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | Binder.restoreCallingIdentity(origId); |
| 2892 | } |
| 2893 | } |
| 2894 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2895 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2896 | * Main function for removing an existing process from the activity manager |
| 2897 | * as a result of that process going away. Clears out all connections |
| 2898 | * to the process. |
| 2899 | */ |
| 2900 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2901 | boolean restarting, boolean allowRestart) { |
| 2902 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2903 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2904 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2905 | } |
| 2906 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2907 | if (mProfileProc == app) { |
| 2908 | clearProfilerLocked(); |
| 2909 | } |
| 2910 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2911 | // Just in case... |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2912 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2913 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2914 | mMainStack.mPausingActivity = null; |
| 2915 | } |
| 2916 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2917 | mMainStack.mLastPausedActivity = null; |
| 2918 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2919 | |
| 2920 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2921 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2922 | |
| 2923 | boolean atTop = true; |
| 2924 | boolean hasVisibleActivities = false; |
| 2925 | |
| 2926 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2927 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2928 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2929 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2930 | while (i > 0) { |
| 2931 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2932 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2933 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2934 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2935 | if (r.app == app) { |
| 2936 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2937 | if (ActivityStack.DEBUG_ADD_REMOVE) { |
| 2938 | RuntimeException here = new RuntimeException("here"); |
| 2939 | here.fillInStackTrace(); |
| 2940 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 2941 | + ": haveState=" + r.haveState |
| 2942 | + " stateNotNeeded=" + r.stateNotNeeded |
| 2943 | + " finishing=" + r.finishing |
| 2944 | + " state=" + r.state, here); |
| 2945 | } |
| 2946 | if (!r.finishing) { |
| 2947 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 2948 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
| 2949 | System.identityHashCode(r), |
| 2950 | r.task.taskId, r.shortComponentName, |
| 2951 | "proc died without state saved"); |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2952 | } |
Dianne Hackborn | 5c60743 | 2012-02-28 14:44:19 -0800 | [diff] [blame] | 2953 | mMainStack.removeActivityFromHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2954 | |
| 2955 | } else { |
| 2956 | // We have the current state for this activity, so |
| 2957 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2958 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2959 | TAG, "Keeping entry, setting app to null"); |
| 2960 | if (r.visible) { |
| 2961 | hasVisibleActivities = true; |
| 2962 | } |
| 2963 | r.app = null; |
| 2964 | r.nowVisible = false; |
| 2965 | if (!r.haveState) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2966 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 2967 | "App died, clearing saved state of " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2968 | r.icicle = null; |
| 2969 | } |
| 2970 | } |
| 2971 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2972 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2973 | } |
| 2974 | atTop = false; |
| 2975 | } |
| 2976 | |
| 2977 | app.activities.clear(); |
| 2978 | |
| 2979 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2980 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2981 | + " running instrumentation " + app.instrumentationClass); |
| 2982 | Bundle info = new Bundle(); |
| 2983 | info.putString("shortMsg", "Process crashed."); |
| 2984 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2985 | } |
| 2986 | |
| 2987 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2988 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2989 | // If there was nothing to resume, and we are not already |
| 2990 | // restarting this process, but there is a visible activity that |
| 2991 | // is hosted by the process... then make sure all visible |
| 2992 | // activities are running, taking care of restarting this |
| 2993 | // process. |
| 2994 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2995 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2996 | } |
| 2997 | } |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 3002 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3003 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3004 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3005 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3006 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 3007 | return i; |
| 3008 | } |
| 3009 | } |
| 3010 | return -1; |
| 3011 | } |
| 3012 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3013 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3014 | IApplicationThread thread) { |
| 3015 | if (thread == null) { |
| 3016 | return null; |
| 3017 | } |
| 3018 | |
| 3019 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3020 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3021 | } |
| 3022 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3023 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3024 | IApplicationThread thread) { |
| 3025 | |
| 3026 | mProcDeaths[0]++; |
| 3027 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3028 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 3029 | synchronized (stats) { |
| 3030 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 3031 | } |
| 3032 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3033 | // Clean up already done if the process has been re-started. |
| 3034 | if (app.pid == pid && app.thread != null && |
| 3035 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 3036 | if (!app.killedBackground) { |
| 3037 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 3038 | + ") has died."); |
| 3039 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3040 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3041 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3042 | TAG, "Dying app: " + app + ", pid: " + pid |
| 3043 | + ", thread: " + thread.asBinder()); |
| 3044 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3045 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3046 | |
| 3047 | if (doLowMem) { |
| 3048 | // If there are no longer any background processes running, |
| 3049 | // and the app that died was not running instrumentation, |
| 3050 | // then tell everyone we are now low on memory. |
| 3051 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3052 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3053 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3054 | 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] | 3055 | haveBg = true; |
| 3056 | break; |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3061 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3062 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3063 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3064 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 3065 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3066 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 3067 | // The low memory report is overriding any current |
| 3068 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 3069 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3070 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3071 | rec.lastRequestedGc = 0; |
| 3072 | } else { |
| 3073 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3074 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3075 | rec.reportLowMemory = true; |
| 3076 | rec.lastLowMemory = now; |
| 3077 | mProcessesToGc.remove(rec); |
| 3078 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3079 | } |
| 3080 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 3081 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3082 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3083 | } |
| 3084 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3085 | } else if (app.pid != pid) { |
| 3086 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3087 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3088 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 3089 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3090 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3091 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3092 | + thread.asBinder()); |
| 3093 | } |
| 3094 | } |
| 3095 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3096 | /** |
| 3097 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3098 | * @param clearTraces causes the dump file to be erased prior to the new |
| 3099 | * traces being written, if true; when false, the new traces will be |
| 3100 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3101 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 3102 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3103 | * @param nativeProcs optional list of native process names to dump stack crawls |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3104 | * @return file containing stack traces, or null if no dump file is configured |
| 3105 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3106 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3107 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3108 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3109 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3110 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3111 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3112 | |
| 3113 | File tracesFile = new File(tracesPath); |
| 3114 | try { |
| 3115 | File tracesDir = tracesFile.getParentFile(); |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3116 | if (!tracesDir.exists()) { |
| 3117 | tracesFile.mkdirs(); |
| 3118 | if (!SELinux.restorecon(tracesDir)) { |
| 3119 | return null; |
| 3120 | } |
| 3121 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3122 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3123 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3124 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3125 | tracesFile.createNewFile(); |
| 3126 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3127 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3128 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3129 | return null; |
| 3130 | } |
| 3131 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3132 | dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3133 | return tracesFile; |
| 3134 | } |
| 3135 | |
| 3136 | private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3137 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3138 | // Use a FileObserver to detect when traces finish writing. |
| 3139 | // The order of traces is considered important to maintain for legibility. |
| 3140 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 3141 | public synchronized void onEvent(int event, String path) { notify(); } |
| 3142 | }; |
| 3143 | |
| 3144 | try { |
| 3145 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3146 | |
| 3147 | // First collect all of the stacks of the most important pids. |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3148 | if (firstPids != null) { |
| 3149 | try { |
| 3150 | int num = firstPids.size(); |
| 3151 | for (int i = 0; i < num; i++) { |
| 3152 | synchronized (observer) { |
| 3153 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3154 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3155 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3156 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3157 | } catch (InterruptedException e) { |
| 3158 | Log.wtf(TAG, e); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3159 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | // Next measure CPU usage. |
| 3163 | if (processStats != null) { |
| 3164 | processStats.init(); |
| 3165 | System.gc(); |
| 3166 | processStats.update(); |
| 3167 | try { |
| 3168 | synchronized (processStats) { |
| 3169 | processStats.wait(500); // measure over 1/2 second. |
| 3170 | } |
| 3171 | } catch (InterruptedException e) { |
| 3172 | } |
| 3173 | processStats.update(); |
| 3174 | |
| 3175 | // We'll take the stack crawls of just the top apps using CPU. |
| 3176 | final int N = processStats.countWorkingStats(); |
| 3177 | int numProcs = 0; |
| 3178 | for (int i=0; i<N && numProcs<5; i++) { |
| 3179 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3180 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3181 | numProcs++; |
| 3182 | try { |
| 3183 | synchronized (observer) { |
| 3184 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3185 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3186 | } |
| 3187 | } catch (InterruptedException e) { |
| 3188 | Log.wtf(TAG, e); |
| 3189 | } |
| 3190 | |
| 3191 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3192 | } |
| 3193 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3194 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3195 | } finally { |
| 3196 | observer.stopWatching(); |
| 3197 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3198 | |
| 3199 | if (nativeProcs != null) { |
| 3200 | int[] pids = Process.getPidsForCommands(nativeProcs); |
| 3201 | if (pids != null) { |
| 3202 | for (int pid : pids) { |
| 3203 | Debug.dumpNativeBacktraceToFile(pid, tracesPath); |
| 3204 | } |
| 3205 | } |
| 3206 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3207 | } |
| 3208 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3209 | final void logAppTooSlow(ProcessRecord app, long startTime, String msg) { |
Dianne Hackborn | 69dc66e | 2012-03-26 10:50:54 -0700 | [diff] [blame] | 3210 | if (true || IS_USER_BUILD) { |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3211 | return; |
| 3212 | } |
| 3213 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3214 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3215 | return; |
| 3216 | } |
| 3217 | |
| 3218 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 3219 | StrictMode.allowThreadDiskWrites(); |
| 3220 | try { |
| 3221 | final File tracesFile = new File(tracesPath); |
| 3222 | final File tracesDir = tracesFile.getParentFile(); |
| 3223 | final File tracesTmp = new File(tracesDir, "__tmp__"); |
| 3224 | try { |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3225 | if (!tracesDir.exists()) { |
| 3226 | tracesFile.mkdirs(); |
| 3227 | if (!SELinux.restorecon(tracesDir.getPath())) { |
| 3228 | return; |
| 3229 | } |
| 3230 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3231 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3232 | |
| 3233 | if (tracesFile.exists()) { |
| 3234 | tracesTmp.delete(); |
| 3235 | tracesFile.renameTo(tracesTmp); |
| 3236 | } |
| 3237 | StringBuilder sb = new StringBuilder(); |
| 3238 | Time tobj = new Time(); |
| 3239 | tobj.set(System.currentTimeMillis()); |
| 3240 | sb.append(tobj.format("%Y-%m-%d %H:%M:%S")); |
| 3241 | sb.append(": "); |
| 3242 | TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb); |
| 3243 | sb.append(" since "); |
| 3244 | sb.append(msg); |
| 3245 | FileOutputStream fos = new FileOutputStream(tracesFile); |
| 3246 | fos.write(sb.toString().getBytes()); |
| 3247 | if (app == null) { |
| 3248 | fos.write("\n*** No application process!".getBytes()); |
| 3249 | } |
| 3250 | fos.close(); |
| 3251 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3252 | } catch (IOException e) { |
| 3253 | Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e); |
| 3254 | return; |
| 3255 | } |
| 3256 | |
| 3257 | if (app != null) { |
| 3258 | ArrayList<Integer> firstPids = new ArrayList<Integer>(); |
| 3259 | firstPids.add(app.pid); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3260 | dumpStackTraces(tracesPath, firstPids, null, null, null); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | File lastTracesFile = null; |
| 3264 | File curTracesFile = null; |
| 3265 | for (int i=9; i>=0; i--) { |
| 3266 | String name = String.format("slow%02d.txt", i); |
| 3267 | curTracesFile = new File(tracesDir, name); |
| 3268 | if (curTracesFile.exists()) { |
| 3269 | if (lastTracesFile != null) { |
| 3270 | curTracesFile.renameTo(lastTracesFile); |
| 3271 | } else { |
| 3272 | curTracesFile.delete(); |
| 3273 | } |
| 3274 | } |
| 3275 | lastTracesFile = curTracesFile; |
| 3276 | } |
| 3277 | tracesFile.renameTo(curTracesFile); |
| 3278 | if (tracesTmp.exists()) { |
| 3279 | tracesTmp.renameTo(tracesFile); |
| 3280 | } |
| 3281 | } finally { |
| 3282 | StrictMode.setThreadPolicy(oldPolicy); |
| 3283 | } |
| 3284 | } |
| 3285 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3286 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 3287 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3288 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3289 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3290 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3291 | if (mController != null) { |
| 3292 | try { |
| 3293 | // 0 == continue, -1 = kill process immediately |
| 3294 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3295 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3296 | } catch (RemoteException e) { |
| 3297 | mController = null; |
| 3298 | } |
| 3299 | } |
| 3300 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3301 | long anrTime = SystemClock.uptimeMillis(); |
| 3302 | if (MONITOR_CPU_USAGE) { |
| 3303 | updateCpuStatsNow(); |
| 3304 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3305 | |
| 3306 | synchronized (this) { |
| 3307 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3308 | if (mShuttingDown) { |
| 3309 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3310 | return; |
| 3311 | } else if (app.notResponding) { |
| 3312 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3313 | return; |
| 3314 | } else if (app.crashing) { |
| 3315 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3316 | return; |
| 3317 | } |
| 3318 | |
| 3319 | // In case we come through here for the same app before completing |
| 3320 | // this one, mark as anring now so we will bail out. |
| 3321 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3322 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3323 | // Log the ANR to the event log. |
| 3324 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 3325 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3326 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3327 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3328 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3329 | |
| 3330 | int parentPid = app.pid; |
| 3331 | 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] | 3332 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3333 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3334 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3335 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3336 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3337 | ProcessRecord r = mLruProcesses.get(i); |
| 3338 | if (r != null && r.thread != null) { |
| 3339 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3340 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3341 | if (r.persistent) { |
| 3342 | firstPids.add(pid); |
| 3343 | } else { |
| 3344 | lastPids.put(pid, Boolean.TRUE); |
| 3345 | } |
| 3346 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3347 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3348 | } |
| 3349 | } |
| 3350 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3351 | // Log the ANR to the main log. |
Jeff Brown | deb6ed8 | 2012-04-10 14:26:26 -0700 | [diff] [blame] | 3352 | StringBuilder info = new StringBuilder(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3353 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3354 | info.append("ANR in ").append(app.processName); |
| 3355 | if (activity != null && activity.shortComponentName != null) { |
| 3356 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3357 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3358 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3359 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3360 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3361 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3362 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3363 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3364 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3365 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3366 | final ProcessStats processStats = new ProcessStats(true); |
| 3367 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3368 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3369 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3370 | String cpuInfo = null; |
| 3371 | if (MONITOR_CPU_USAGE) { |
| 3372 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3373 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3374 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3375 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3376 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3377 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3378 | } |
| 3379 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3380 | info.append(processStats.printCurrentState(anrTime)); |
| 3381 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3382 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3383 | if (tracesFile == null) { |
| 3384 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3385 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3386 | } |
| 3387 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3388 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3389 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3390 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3391 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3392 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3393 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3394 | 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] | 3395 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3396 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3397 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3398 | } |
| 3399 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3400 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3401 | } |
| 3402 | } |
| 3403 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3404 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3405 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3406 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3407 | |
| 3408 | synchronized (this) { |
| 3409 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3410 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 3411 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 3412 | app.processName, app.setAdj, "background ANR"); |
| 3413 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3414 | return; |
| 3415 | } |
| 3416 | |
| 3417 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3418 | makeAppNotRespondingLocked(app, |
| 3419 | activity != null ? activity.shortComponentName : null, |
| 3420 | annotation != null ? "ANR " + annotation : "ANR", |
| 3421 | info.toString()); |
| 3422 | |
| 3423 | // Bring up the infamous App Not Responding dialog |
| 3424 | Message msg = Message.obtain(); |
| 3425 | HashMap map = new HashMap(); |
| 3426 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3427 | msg.obj = map; |
| 3428 | map.put("app", app); |
| 3429 | if (activity != null) { |
| 3430 | map.put("activity", activity); |
| 3431 | } |
| 3432 | |
| 3433 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3434 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3435 | } |
| 3436 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3437 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3438 | if (!mLaunchWarningShown) { |
| 3439 | mLaunchWarningShown = true; |
| 3440 | mHandler.post(new Runnable() { |
| 3441 | @Override |
| 3442 | public void run() { |
| 3443 | synchronized (ActivityManagerService.this) { |
| 3444 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3445 | d.show(); |
| 3446 | mHandler.postDelayed(new Runnable() { |
| 3447 | @Override |
| 3448 | public void run() { |
| 3449 | synchronized (ActivityManagerService.this) { |
| 3450 | d.dismiss(); |
| 3451 | mLaunchWarningShown = false; |
| 3452 | } |
| 3453 | } |
| 3454 | }, 4000); |
| 3455 | } |
| 3456 | } |
| 3457 | }); |
| 3458 | } |
| 3459 | } |
| 3460 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3461 | public boolean clearApplicationUserData(final String packageName, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3462 | final IPackageDataObserver observer, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3463 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3464 | int uid = Binder.getCallingUid(); |
| 3465 | int pid = Binder.getCallingPid(); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3466 | userId = handleIncomingUser(pid, uid, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3467 | userId, false, true, "clearApplicationUserData", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3468 | long callingId = Binder.clearCallingIdentity(); |
| 3469 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3470 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3471 | int pkgUid = -1; |
| 3472 | synchronized(this) { |
| 3473 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3474 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3475 | } catch (RemoteException e) { |
| 3476 | } |
| 3477 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3478 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3479 | return false; |
| 3480 | } |
| 3481 | if (uid == pkgUid || checkComponentPermission( |
| 3482 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3483 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3484 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3485 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3486 | } else { |
| 3487 | throw new SecurityException(pid+" does not have permission:"+ |
| 3488 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3489 | "for process:"+packageName); |
| 3490 | } |
| 3491 | } |
| 3492 | |
| 3493 | try { |
| 3494 | //clear application user data |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3495 | pm.clearApplicationUserData(packageName, observer, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3496 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3497 | Uri.fromParts("package", packageName, null)); |
| 3498 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3499 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3500 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3501 | } catch (RemoteException e) { |
| 3502 | } |
| 3503 | } finally { |
| 3504 | Binder.restoreCallingIdentity(callingId); |
| 3505 | } |
| 3506 | return true; |
| 3507 | } |
| 3508 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3509 | public void killBackgroundProcesses(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3510 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3511 | != PackageManager.PERMISSION_GRANTED && |
| 3512 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3513 | != PackageManager.PERMISSION_GRANTED) { |
| 3514 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3515 | + Binder.getCallingPid() |
| 3516 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3517 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3518 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3519 | throw new SecurityException(msg); |
| 3520 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3521 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3522 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3523 | userId, true, true, "killBackgroundProcesses", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3524 | long callingId = Binder.clearCallingIdentity(); |
| 3525 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3526 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3527 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3528 | int appId = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3529 | try { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3530 | appId = UserHandle.getAppId(pm.getPackageUid(packageName, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3531 | } catch (RemoteException e) { |
| 3532 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3533 | if (appId == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3534 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3535 | return; |
| 3536 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3537 | killPackageProcessesLocked(packageName, appId, userId, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3538 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3539 | } |
| 3540 | } finally { |
| 3541 | Binder.restoreCallingIdentity(callingId); |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | public void killAllBackgroundProcesses() { |
| 3546 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3547 | != PackageManager.PERMISSION_GRANTED) { |
| 3548 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3549 | + Binder.getCallingPid() |
| 3550 | + ", uid=" + Binder.getCallingUid() |
| 3551 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3552 | Slog.w(TAG, msg); |
| 3553 | throw new SecurityException(msg); |
| 3554 | } |
| 3555 | |
| 3556 | long callingId = Binder.clearCallingIdentity(); |
| 3557 | try { |
| 3558 | synchronized(this) { |
| 3559 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3560 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3561 | final int NA = apps.size(); |
| 3562 | for (int ia=0; ia<NA; ia++) { |
| 3563 | ProcessRecord app = apps.valueAt(ia); |
| 3564 | if (app.persistent) { |
| 3565 | // we don't kill persistent processes |
| 3566 | continue; |
| 3567 | } |
| 3568 | if (app.removed) { |
| 3569 | procs.add(app); |
| 3570 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3571 | app.removed = true; |
| 3572 | procs.add(app); |
| 3573 | } |
| 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | int N = procs.size(); |
| 3578 | for (int i=0; i<N; i++) { |
| 3579 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3580 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3581 | } |
| 3582 | } finally { |
| 3583 | Binder.restoreCallingIdentity(callingId); |
| 3584 | } |
| 3585 | } |
| 3586 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3587 | public void forceStopPackage(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3588 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3589 | != PackageManager.PERMISSION_GRANTED) { |
| 3590 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3591 | + Binder.getCallingPid() |
| 3592 | + ", uid=" + Binder.getCallingUid() |
| 3593 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3594 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3595 | throw new SecurityException(msg); |
| 3596 | } |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3597 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3598 | userId, true, true, "forceStopPackage", null); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3599 | long callingId = Binder.clearCallingIdentity(); |
| 3600 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3601 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3602 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3603 | int[] users = userId == UserHandle.USER_ALL |
| 3604 | ? getUsersLocked() : new int[] { userId }; |
| 3605 | for (int user : users) { |
| 3606 | int pkgUid = -1; |
| 3607 | try { |
| 3608 | pkgUid = pm.getPackageUid(packageName, user); |
| 3609 | } catch (RemoteException e) { |
| 3610 | } |
| 3611 | if (pkgUid == -1) { |
| 3612 | Slog.w(TAG, "Invalid packageName: " + packageName); |
| 3613 | continue; |
| 3614 | } |
| 3615 | try { |
| 3616 | pm.setPackageStoppedState(packageName, true, user); |
| 3617 | } catch (RemoteException e) { |
| 3618 | } catch (IllegalArgumentException e) { |
| 3619 | Slog.w(TAG, "Failed trying to unstop package " |
| 3620 | + packageName + ": " + e); |
| 3621 | } |
| 3622 | if (isUserRunningLocked(user)) { |
| 3623 | forceStopPackageLocked(packageName, pkgUid); |
| 3624 | } |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3625 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3626 | } |
| 3627 | } finally { |
| 3628 | Binder.restoreCallingIdentity(callingId); |
| 3629 | } |
| 3630 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3631 | |
| 3632 | /* |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3633 | * The pkg name and app id have to be specified. |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3634 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3635 | public void killApplicationWithAppId(String pkg, int appid) { |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3636 | if (pkg == null) { |
| 3637 | return; |
| 3638 | } |
| 3639 | // Make sure the uid is valid. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3640 | if (appid < 0) { |
| 3641 | Slog.w(TAG, "Invalid appid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3642 | return; |
| 3643 | } |
| 3644 | int callerUid = Binder.getCallingUid(); |
| 3645 | // Only the system server can kill an application |
| 3646 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3647 | // Post an aysnc message to kill the application |
| 3648 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3649 | msg.arg1 = appid; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3650 | msg.arg2 = 0; |
| 3651 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3652 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3653 | } else { |
| 3654 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3655 | pkg); |
| 3656 | } |
| 3657 | } |
| 3658 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3659 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3660 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3661 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3662 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3663 | final int uid = Binder.getCallingUid(); |
| 3664 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3665 | try { |
| 3666 | synchronized (this) { |
| 3667 | // Only allow this from foreground processes, so that background |
| 3668 | // applications can't abuse it to prevent system UI from being shown. |
| 3669 | if (uid >= Process.FIRST_APPLICATION_UID) { |
| 3670 | ProcessRecord proc; |
| 3671 | synchronized (mPidsSelfLocked) { |
| 3672 | proc = mPidsSelfLocked.get(pid); |
| 3673 | } |
| 3674 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 3675 | Slog.w(TAG, "Ignoring closeSystemDialogs " + reason |
| 3676 | + " from background process " + proc); |
| 3677 | return; |
| 3678 | } |
| 3679 | } |
| 3680 | closeSystemDialogsLocked(reason); |
| 3681 | } |
| 3682 | } finally { |
| 3683 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3684 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3685 | } |
| 3686 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3687 | void closeSystemDialogsLocked(String reason) { |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3688 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3689 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3690 | if (reason != null) { |
| 3691 | intent.putExtra("reason", reason); |
| 3692 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3693 | mWindowManager.closeSystemDialogs(reason); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3694 | |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3695 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3696 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 3697 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3698 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3699 | Activity.RESULT_CANCELED, null, "close-sys", true); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3700 | } |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3701 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3702 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3703 | broadcastIntentLocked(null, null, intent, null, |
| 3704 | null, 0, null, null, null, false, false, -1, |
| 3705 | Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3706 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3707 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3708 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3709 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3710 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3711 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3712 | for (int i=pids.length-1; i>=0; i--) { |
| 3713 | infos[i] = new Debug.MemoryInfo(); |
| 3714 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3715 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3716 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3717 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3718 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3719 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3720 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3721 | long[] pss = new long[pids.length]; |
| 3722 | for (int i=pids.length-1; i>=0; i--) { |
| 3723 | pss[i] = Debug.getPss(pids[i]); |
| 3724 | } |
| 3725 | return pss; |
| 3726 | } |
| 3727 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3728 | public void killApplicationProcess(String processName, int uid) { |
| 3729 | if (processName == null) { |
| 3730 | return; |
| 3731 | } |
| 3732 | |
| 3733 | int callerUid = Binder.getCallingUid(); |
| 3734 | // Only the system server can kill an application |
| 3735 | if (callerUid == Process.SYSTEM_UID) { |
| 3736 | synchronized (this) { |
| 3737 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3738 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3739 | try { |
| 3740 | app.thread.scheduleSuicide(); |
| 3741 | } catch (RemoteException e) { |
| 3742 | // If the other end already died, then our work here is done. |
| 3743 | } |
| 3744 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3745 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3746 | + processName + " / " + uid); |
| 3747 | } |
| 3748 | } |
| 3749 | } else { |
| 3750 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3751 | processName); |
| 3752 | } |
| 3753 | } |
| 3754 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3755 | private void forceStopPackageLocked(final String packageName, int uid) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3756 | forceStopPackageLocked(packageName, UserHandle.getAppId(uid), false, |
| 3757 | false, true, false, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3758 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3759 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3760 | if (!mProcessesReady) { |
| 3761 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3762 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3763 | intent.putExtra(Intent.EXTRA_UID, uid); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 3764 | intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3765 | broadcastIntentLocked(null, null, intent, |
| 3766 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3767 | false, false, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3768 | MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3769 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3770 | |
| 3771 | private void forceStopUserLocked(int userId) { |
| 3772 | forceStopPackageLocked(null, -1, false, false, true, false, userId); |
| 3773 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
| 3774 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3775 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 3776 | broadcastIntentLocked(null, null, intent, |
| 3777 | null, null, 0, null, null, null, |
| 3778 | false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 3779 | MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3782 | private final boolean killPackageProcessesLocked(String packageName, int appId, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3783 | int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart, |
| 3784 | boolean doit, boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3785 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3786 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3787 | // Remove all processes this package may have touched: all with the |
| 3788 | // same UID (except for the system or root user), and all whose name |
| 3789 | // matches the package name. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3790 | final String procNamePrefix = packageName != null ? (packageName + ":") : null; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3791 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3792 | final int NA = apps.size(); |
| 3793 | for (int ia=0; ia<NA; ia++) { |
| 3794 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3795 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3796 | // we don't kill persistent processes |
| 3797 | continue; |
| 3798 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3799 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3800 | if (doit) { |
| 3801 | procs.add(app); |
| 3802 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3803 | continue; |
| 3804 | } |
| 3805 | |
| 3806 | // Skip process if it doesn't meet our oom adj requirement. |
| 3807 | if (app.setAdj < minOomAdj) { |
| 3808 | continue; |
| 3809 | } |
| 3810 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3811 | // If no package is specified, we call all processes under the |
| 3812 | // give user id. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3813 | if (packageName == null) { |
| 3814 | if (app.userId != userId) { |
| 3815 | continue; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3816 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3817 | // Package has been specified, we want to hit all processes |
| 3818 | // that match it. We need to qualify this by the processes |
| 3819 | // that are running under the specified app and user ID. |
| 3820 | } else { |
| 3821 | if (UserHandle.getAppId(app.uid) != appId) { |
| 3822 | continue; |
| 3823 | } |
| 3824 | if (userId != UserHandle.USER_ALL && app.userId != userId) { |
| 3825 | continue; |
| 3826 | } |
| 3827 | if (!app.pkgList.contains(packageName)) { |
| 3828 | continue; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3829 | } |
| 3830 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3831 | |
| 3832 | // Process has passed all conditions, kill it! |
| 3833 | if (!doit) { |
| 3834 | return true; |
| 3835 | } |
| 3836 | app.removed = true; |
| 3837 | procs.add(app); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | int N = procs.size(); |
| 3842 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3843 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3844 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3845 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3846 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3847 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3848 | private final boolean forceStopPackageLocked(String name, int appId, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3849 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3850 | boolean evenPersistent, int userId) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3851 | int i; |
| 3852 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3853 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3854 | if (userId == UserHandle.USER_ALL && name == null) { |
| 3855 | Slog.w(TAG, "Can't force stop all processes of all users, that is insane!"); |
| 3856 | } |
| 3857 | |
| 3858 | if (appId < 0 && name != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3859 | try { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3860 | appId = UserHandle.getAppId( |
| 3861 | AppGlobals.getPackageManager().getPackageUid(name, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3862 | } catch (RemoteException e) { |
| 3863 | } |
| 3864 | } |
| 3865 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3866 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3867 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3868 | Slog.i(TAG, "Force stopping package " + name + " appid=" + appId |
| 3869 | + " user=" + userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3870 | } else { |
| 3871 | Slog.i(TAG, "Force stopping user " + userId); |
| 3872 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3873 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3874 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3875 | while (badApps.hasNext()) { |
| 3876 | SparseArray<Long> ba = badApps.next(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3877 | for (i=ba.size()-1; i>=0; i--) { |
| 3878 | boolean remove = false; |
| 3879 | final int entUid = ba.keyAt(i); |
| 3880 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3881 | if (userId == UserHandle.USER_ALL) { |
| 3882 | if (UserHandle.getAppId(entUid) == appId) { |
| 3883 | remove = true; |
| 3884 | } |
| 3885 | } else { |
| 3886 | if (entUid == UserHandle.getUid(userId, appId)) { |
| 3887 | remove = true; |
| 3888 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3889 | } |
| 3890 | } else if (UserHandle.getUserId(entUid) == userId) { |
| 3891 | remove = true; |
| 3892 | } |
| 3893 | if (remove) { |
| 3894 | ba.removeAt(i); |
| 3895 | } |
| 3896 | } |
| 3897 | if (ba.size() == 0) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3898 | badApps.remove(); |
| 3899 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3900 | } |
| 3901 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3902 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3903 | boolean didSomething = killPackageProcessesLocked(name, appId, userId, |
| 3904 | -100, callerWillRestart, false, doit, evenPersistent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3905 | name == null ? ("force stop user " + userId) : ("force stop " + name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3906 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3907 | TaskRecord lastTask = null; |
| 3908 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3909 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3910 | final boolean samePackage = r.packageName.equals(name) |
| 3911 | || (name == null && r.userId == userId); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3912 | if ((userId == UserHandle.USER_ALL || r.userId == userId) |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3913 | && (samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3914 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3915 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3916 | if (r.finishing) { |
| 3917 | // If this activity is just finishing, then it is not |
| 3918 | // interesting as far as something to stop. |
| 3919 | continue; |
| 3920 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3921 | return true; |
| 3922 | } |
| 3923 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3924 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3925 | if (samePackage) { |
| 3926 | if (r.app != null) { |
| 3927 | r.app.removed = true; |
| 3928 | } |
| 3929 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3930 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3931 | lastTask = r.task; |
| 3932 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3933 | null, "force-stop", true)) { |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3934 | i--; |
| 3935 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3936 | } |
| 3937 | } |
| 3938 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3939 | if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) { |
| 3940 | if (!doit) { |
| 3941 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3942 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3943 | didSomething = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3944 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3945 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3946 | if (name == null) { |
| 3947 | // Remove all sticky broadcasts from this user. |
| 3948 | mStickyBroadcasts.remove(userId); |
| 3949 | } |
| 3950 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3951 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3952 | if (mProviderMap.collectForceStopProviders(name, appId, doit, evenPersistent, |
| 3953 | userId, providers)) { |
| 3954 | if (!doit) { |
| 3955 | return true; |
| 3956 | } |
| 3957 | didSomething = true; |
| 3958 | } |
| 3959 | N = providers.size(); |
| 3960 | for (i=0; i<N; i++) { |
| 3961 | removeDyingProviderLocked(null, providers.get(i), true); |
| 3962 | } |
| 3963 | |
| 3964 | if (mIntentSenderRecords.size() > 0) { |
| 3965 | Iterator<WeakReference<PendingIntentRecord>> it |
| 3966 | = mIntentSenderRecords.values().iterator(); |
| 3967 | while (it.hasNext()) { |
| 3968 | WeakReference<PendingIntentRecord> wpir = it.next(); |
| 3969 | if (wpir == null) { |
| 3970 | it.remove(); |
| 3971 | continue; |
| 3972 | } |
| 3973 | PendingIntentRecord pir = wpir.get(); |
| 3974 | if (pir == null) { |
| 3975 | it.remove(); |
| 3976 | continue; |
| 3977 | } |
| 3978 | if (name == null) { |
| 3979 | // Stopping user, remove all objects for the user. |
| 3980 | if (pir.key.userId != userId) { |
| 3981 | // Not the same user, skip it. |
| 3982 | continue; |
| 3983 | } |
| 3984 | } else { |
| 3985 | if (UserHandle.getAppId(pir.uid) != appId) { |
| 3986 | // Different app id, skip it. |
| 3987 | continue; |
| 3988 | } |
| 3989 | if (userId != UserHandle.USER_ALL && pir.key.userId != userId) { |
| 3990 | // Different user, skip it. |
| 3991 | continue; |
| 3992 | } |
| 3993 | if (!pir.key.packageName.equals(name)) { |
| 3994 | // Different package, skip it. |
| 3995 | continue; |
| 3996 | } |
| 3997 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3998 | if (!doit) { |
| 3999 | return true; |
| 4000 | } |
| 4001 | didSomething = true; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4002 | it.remove(); |
| 4003 | pir.canceled = true; |
| 4004 | if (pir.key.activity != null) { |
| 4005 | pir.key.activity.pendingResults.remove(pir.ref); |
| 4006 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 4007 | } |
| 4008 | } |
| 4009 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4010 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4011 | if (purgeCache && name != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4012 | AttributeCache ac = AttributeCache.instance(); |
| 4013 | if (ac != null) { |
| 4014 | ac.removePackage(name); |
| 4015 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4016 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 4017 | if (mBooted) { |
| 4018 | mMainStack.resumeTopActivityLocked(null); |
| 4019 | mMainStack.scheduleIdleLocked(); |
| 4020 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4021 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4022 | |
| 4023 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4024 | } |
| 4025 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4026 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4027 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4028 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4029 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4030 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4031 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4032 | + "/" + uid + ")"); |
| 4033 | |
| 4034 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4035 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4036 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4037 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4038 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4039 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4041 | boolean needRestart = false; |
| 4042 | if (app.pid > 0 && app.pid != MY_PID) { |
| 4043 | int pid = app.pid; |
| 4044 | synchronized (mPidsSelfLocked) { |
| 4045 | mPidsSelfLocked.remove(pid); |
| 4046 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4047 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4048 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4049 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 4050 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4051 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4052 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4053 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4054 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4055 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4056 | } else { |
| 4057 | needRestart = true; |
| 4058 | } |
| 4059 | } |
| 4060 | } else { |
| 4061 | mRemovedProcesses.add(app); |
| 4062 | } |
| 4063 | |
| 4064 | return needRestart; |
| 4065 | } |
| 4066 | |
| 4067 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 4068 | final int pid = app.pid; |
| 4069 | boolean gone = false; |
| 4070 | synchronized (mPidsSelfLocked) { |
| 4071 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 4072 | if (knownApp != null && knownApp.thread == null) { |
| 4073 | mPidsSelfLocked.remove(pid); |
| 4074 | gone = true; |
| 4075 | } |
| 4076 | } |
| 4077 | |
| 4078 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4079 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4080 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4081 | app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4082 | mProcessNames.remove(app.processName, app.uid); |
| 4083 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4084 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4085 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4086 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4087 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4088 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4089 | // Take care of any launching providers waiting for this process. |
| 4090 | checkAppInLaunchingProvidersLocked(app, true); |
| 4091 | // Take care of any services that are waiting for the process. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4092 | mServices.processStartTimedOutLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4093 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 4094 | app.processName, app.setAdj, "start timeout"); |
| 4095 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4096 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4097 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4098 | try { |
| 4099 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 4100 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 4101 | bm.agentDisconnected(app.info.packageName); |
| 4102 | } catch (RemoteException e) { |
| 4103 | // Can't happen; the backup manager is local |
| 4104 | } |
| 4105 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4106 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4107 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4108 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4110 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4111 | 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] | 4112 | } |
| 4113 | } |
| 4114 | |
| 4115 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 4116 | int pid) { |
| 4117 | |
| 4118 | // Find the application record that is being attached... either via |
| 4119 | // the pid if we are running in multiple processes, or just pull the |
| 4120 | // next app record if we are emulating process with anonymous threads. |
| 4121 | ProcessRecord app; |
| 4122 | if (pid != MY_PID && pid >= 0) { |
| 4123 | synchronized (mPidsSelfLocked) { |
| 4124 | app = mPidsSelfLocked.get(pid); |
| 4125 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4126 | } else { |
| 4127 | app = null; |
| 4128 | } |
| 4129 | |
| 4130 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4131 | 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] | 4132 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4133 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4134 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4135 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4136 | } else { |
| 4137 | try { |
| 4138 | thread.scheduleExit(); |
| 4139 | } catch (Exception e) { |
| 4140 | // Ignore exceptions. |
| 4141 | } |
| 4142 | } |
| 4143 | return false; |
| 4144 | } |
| 4145 | |
| 4146 | // If this application record is still attached to a previous |
| 4147 | // process, clean it up now. |
| 4148 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4149 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | // Tell the process all about itself. |
| 4153 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4154 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4155 | TAG, "Binding process pid " + pid + " to record " + app); |
| 4156 | |
| 4157 | String processName = app.processName; |
| 4158 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4159 | AppDeathRecipient adr = new AppDeathRecipient( |
| 4160 | app, pid, thread); |
| 4161 | thread.asBinder().linkToDeath(adr, 0); |
| 4162 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4163 | } catch (RemoteException e) { |
| 4164 | app.resetPackageList(); |
| 4165 | startProcessLocked(app, "link fail", processName); |
| 4166 | return false; |
| 4167 | } |
| 4168 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4169 | EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4170 | |
| 4171 | app.thread = thread; |
| 4172 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 4173 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 4174 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4175 | app.forcingToForeground = null; |
| 4176 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 4177 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4178 | app.debugging = false; |
| 4179 | |
| 4180 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4181 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4182 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4183 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4184 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4185 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4186 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4187 | } |
| 4188 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4189 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4190 | TAG, "New app record " + app |
| 4191 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 4192 | try { |
| 4193 | int testMode = IApplicationThread.DEBUG_OFF; |
| 4194 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 4195 | testMode = mWaitForDebugger |
| 4196 | ? IApplicationThread.DEBUG_WAIT |
| 4197 | : IApplicationThread.DEBUG_ON; |
| 4198 | app.debugging = true; |
| 4199 | if (mDebugTransient) { |
| 4200 | mDebugApp = mOrigDebugApp; |
| 4201 | mWaitForDebugger = mOrigWaitForDebugger; |
| 4202 | } |
| 4203 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4204 | String profileFile = app.instrumentationProfileFile; |
| 4205 | ParcelFileDescriptor profileFd = null; |
| 4206 | boolean profileAutoStop = false; |
| 4207 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 4208 | mProfileProc = app; |
| 4209 | profileFile = mProfileFile; |
| 4210 | profileFd = mProfileFd; |
| 4211 | profileAutoStop = mAutoStopProfiler; |
| 4212 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4213 | boolean enableOpenGlTrace = false; |
| 4214 | if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) { |
| 4215 | enableOpenGlTrace = true; |
| 4216 | mOpenGlTraceApp = null; |
| 4217 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4218 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4219 | // If the app is being launched for restore or full backup, set it up specially |
| 4220 | boolean isRestrictedBackupMode = false; |
| 4221 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 4222 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4223 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4224 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 4225 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4226 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 4227 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 4228 | ? app.instrumentationInfo.packageName |
| 4229 | : app.info.packageName); |
| 4230 | if (app.instrumentationClass != null) { |
| 4231 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 4232 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4233 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 4234 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4235 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 4236 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 4237 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4238 | if (profileFd != null) { |
| 4239 | profileFd = profileFd.dup(); |
| 4240 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4241 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4242 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4243 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
| 4244 | enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 4245 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 4246 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 4247 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 4248 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4249 | } catch (Exception e) { |
| 4250 | // todo: Yikes! What should we do? For now we will try to |
| 4251 | // start another process, but that could easily get us in |
| 4252 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4253 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4254 | |
| 4255 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4256 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4257 | startProcessLocked(app, "bind fail", processName); |
| 4258 | return false; |
| 4259 | } |
| 4260 | |
| 4261 | // Remove this record from the list of starting applications. |
| 4262 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4263 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 4264 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4265 | mProcessesOnHold.remove(app); |
| 4266 | |
| 4267 | boolean badApp = false; |
| 4268 | boolean didSomething = false; |
| 4269 | |
| 4270 | // 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] | 4271 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 4272 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4273 | 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] | 4274 | && processName.equals(hr.processName)) { |
| 4275 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 4276 | if (mHeadless) { |
| 4277 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 4278 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4279 | didSomething = true; |
| 4280 | } |
| 4281 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4282 | 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] | 4283 | + hr.intent.getComponent().flattenToShortString(), e); |
| 4284 | badApp = true; |
| 4285 | } |
| 4286 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4287 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | // Find any services that should be running in this process... |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4292 | if (!badApp) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4293 | try { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4294 | didSomething |= mServices.attachApplicationLocked(app, processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4295 | } catch (Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4296 | badApp = true; |
| 4297 | } |
| 4298 | } |
| 4299 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4300 | // Check if a next-broadcast receiver is in this process... |
| 4301 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4302 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4303 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4304 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4305 | // 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] | 4306 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4307 | } |
| 4308 | } |
| 4309 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4310 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4311 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4312 | 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] | 4313 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4314 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4315 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 4316 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 4317 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4318 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4319 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4320 | e.printStackTrace(); |
| 4321 | } |
| 4322 | } |
| 4323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4324 | if (badApp) { |
| 4325 | // todo: Also need to kill application to deal with all |
| 4326 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4327 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4328 | return false; |
| 4329 | } |
| 4330 | |
| 4331 | if (!didSomething) { |
| 4332 | updateOomAdjLocked(); |
| 4333 | } |
| 4334 | |
| 4335 | return true; |
| 4336 | } |
| 4337 | |
| 4338 | public final void attachApplication(IApplicationThread thread) { |
| 4339 | synchronized (this) { |
| 4340 | int callingPid = Binder.getCallingPid(); |
| 4341 | final long origId = Binder.clearCallingIdentity(); |
| 4342 | attachApplicationLocked(thread, callingPid); |
| 4343 | Binder.restoreCallingIdentity(origId); |
| 4344 | } |
| 4345 | } |
| 4346 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4347 | 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] | 4348 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4349 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4350 | if (stopProfiling) { |
| 4351 | synchronized (this) { |
| 4352 | if (mProfileProc == r.app) { |
| 4353 | if (mProfileFd != null) { |
| 4354 | try { |
| 4355 | mProfileFd.close(); |
| 4356 | } catch (IOException e) { |
| 4357 | } |
| 4358 | clearProfilerLocked(); |
| 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4363 | Binder.restoreCallingIdentity(origId); |
| 4364 | } |
| 4365 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4366 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4367 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4368 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4369 | mWindowManager.enableScreenAfterBoot(); |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 4370 | |
| 4371 | synchronized (this) { |
| 4372 | updateEventDispatchingLocked(); |
| 4373 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4374 | } |
| 4375 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4376 | public void showBootMessage(final CharSequence msg, final boolean always) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4377 | enforceNotIsolatedCaller("showBootMessage"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4378 | mWindowManager.showBootMessage(msg, always); |
| 4379 | } |
| 4380 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4381 | public void dismissKeyguardOnNextActivity() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4382 | enforceNotIsolatedCaller("dismissKeyguardOnNextActivity"); |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4383 | final long token = Binder.clearCallingIdentity(); |
| 4384 | try { |
| 4385 | synchronized (this) { |
| 4386 | if (mLockScreenShown) { |
| 4387 | mLockScreenShown = false; |
| 4388 | comeOutOfSleepIfNeededLocked(); |
| 4389 | } |
| 4390 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
Dianne Hackborn | 1e88e98 | 2012-04-24 18:35:55 -0700 | [diff] [blame] | 4391 | } |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4392 | } finally { |
| 4393 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4394 | } |
| 4395 | } |
| 4396 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4397 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4398 | IntentFilter pkgFilter = new IntentFilter(); |
| 4399 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4400 | pkgFilter.addDataScheme("package"); |
| 4401 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4402 | @Override |
| 4403 | public void onReceive(Context context, Intent intent) { |
| 4404 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4405 | if (pkgs != null) { |
| 4406 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4407 | synchronized (ActivityManagerService.this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4408 | if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) { |
| 4409 | setResultCode(Activity.RESULT_OK); |
| 4410 | return; |
| 4411 | } |
| 4412 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4413 | } |
| 4414 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4415 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4416 | }, pkgFilter); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4417 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4418 | synchronized (this) { |
| 4419 | // Ensure that any processes we had put on hold are now started |
| 4420 | // up. |
| 4421 | final int NP = mProcessesOnHold.size(); |
| 4422 | if (NP > 0) { |
| 4423 | ArrayList<ProcessRecord> procs = |
| 4424 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4425 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4426 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4427 | + procs.get(ip)); |
| 4428 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4429 | } |
| 4430 | } |
| 4431 | |
| 4432 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4433 | // Start looking for apps that are abusing wake locks. |
| 4434 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4435 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4436 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4437 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4438 | SystemProperties.set("dev.bootcomplete", "1"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4439 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 4440 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 4441 | if (uss.mState == UserStartedState.STATE_BOOTING) { |
| 4442 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4443 | final int userId = mStartedUsers.keyAt(i); |
| 4444 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 4445 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 4446 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4447 | null, null, 0, null, null, |
| 4448 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4449 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4450 | } |
Amith Yamasani | 4860cfc | 2012-08-08 19:14:18 -0700 | [diff] [blame] | 4451 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4452 | } |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | final void ensureBootCompleted() { |
| 4457 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4458 | boolean enableScreen; |
| 4459 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4460 | booting = mBooting; |
| 4461 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4462 | enableScreen = !mBooted; |
| 4463 | mBooted = true; |
| 4464 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4465 | |
| 4466 | if (booting) { |
| 4467 | finishBooting(); |
| 4468 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4469 | |
| 4470 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4471 | enableScreenAfterBoot(); |
| 4472 | } |
| 4473 | } |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 4474 | |
| 4475 | public final void activityResumed(IBinder token) { |
| 4476 | final long origId = Binder.clearCallingIdentity(); |
| 4477 | mMainStack.activityResumed(token); |
| 4478 | Binder.restoreCallingIdentity(origId); |
| 4479 | } |
| 4480 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4481 | public final void activityPaused(IBinder token) { |
| 4482 | final long origId = Binder.clearCallingIdentity(); |
| 4483 | mMainStack.activityPaused(token, false); |
| 4484 | Binder.restoreCallingIdentity(origId); |
| 4485 | } |
| 4486 | |
| 4487 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4488 | CharSequence description) { |
| 4489 | if (localLOGV) Slog.v( |
| 4490 | TAG, "Activity stopped: token=" + token); |
| 4491 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4492 | // Refuse possible leaked file descriptors |
| 4493 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4494 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4495 | } |
| 4496 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4497 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4498 | |
| 4499 | final long origId = Binder.clearCallingIdentity(); |
| 4500 | |
| 4501 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4502 | r = mMainStack.isInStackLocked(token); |
| 4503 | if (r != null) { |
| 4504 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | if (r != null) { |
| 4509 | sendPendingThumbnail(r, null, null, null, false); |
| 4510 | } |
| 4511 | |
| 4512 | trimApplications(); |
| 4513 | |
| 4514 | Binder.restoreCallingIdentity(origId); |
| 4515 | } |
| 4516 | |
| 4517 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4518 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4519 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | public String getCallingPackage(IBinder token) { |
| 4523 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4524 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4525 | 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] | 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | public ComponentName getCallingActivity(IBinder token) { |
| 4530 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4531 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4532 | return r != null ? r.intent.getComponent() : null; |
| 4533 | } |
| 4534 | } |
| 4535 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4536 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4537 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4538 | if (r == null) { |
| 4539 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4540 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4541 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4545 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4546 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4547 | if (r == null) { |
| 4548 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4549 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4550 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | |
| 4554 | public String getPackageForToken(IBinder token) { |
| 4555 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4556 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4557 | if (r == null) { |
| 4558 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4559 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4560 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4561 | } |
| 4562 | } |
| 4563 | |
| 4564 | public IIntentSender getIntentSender(int type, |
| 4565 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4566 | int requestCode, Intent[] intents, String[] resolvedTypes, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4567 | int flags, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4568 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4569 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4570 | if (intents != null) { |
| 4571 | if (intents.length < 1) { |
| 4572 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4573 | } |
| 4574 | for (int i=0; i<intents.length; i++) { |
| 4575 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4576 | if (intent != null) { |
| 4577 | if (intent.hasFileDescriptors()) { |
| 4578 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4579 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4580 | if (type == ActivityManager.INTENT_SENDER_BROADCAST && |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4581 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4582 | throw new IllegalArgumentException( |
| 4583 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4584 | } |
| 4585 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4586 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4587 | } |
| 4588 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4589 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4590 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4591 | } |
| 4592 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4593 | if (options != null) { |
| 4594 | if (options.hasFileDescriptors()) { |
| 4595 | throw new IllegalArgumentException("File descriptors passed in options"); |
| 4596 | } |
| 4597 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4599 | synchronized(this) { |
| 4600 | int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4601 | int origUserId = userId; |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 4602 | userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4603 | type == ActivityManager.INTENT_SENDER_BROADCAST, true, |
| 4604 | "getIntentSender", null); |
| 4605 | if (origUserId == UserHandle.USER_CURRENT) { |
| 4606 | // We don't want to evaluate this until the pending intent is |
| 4607 | // actually executed. However, we do want to always do the |
| 4608 | // security checking for it above. |
| 4609 | userId = UserHandle.USER_CURRENT; |
| 4610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4611 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4612 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4613 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4614 | .getPackageUid(packageName, UserHandle.getUserId(callingUid)); |
| 4615 | if (!UserHandle.isSameApp(callingUid, uid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4616 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4617 | + Binder.getCallingPid() |
| 4618 | + ", uid=" + Binder.getCallingUid() |
| 4619 | + ", (need uid=" + uid + ")" |
| 4620 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4621 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4622 | throw new SecurityException(msg); |
| 4623 | } |
| 4624 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4625 | |
| 4626 | return getIntentSenderLocked(type, packageName, callingUid, userId, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4627 | token, resultWho, requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4629 | } catch (RemoteException e) { |
| 4630 | throw new SecurityException(e); |
| 4631 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4632 | } |
| 4633 | } |
| 4634 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4635 | IIntentSender getIntentSenderLocked(int type, String packageName, |
| 4636 | int callingUid, int userId, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4637 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags, |
| 4638 | Bundle options) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4639 | if (DEBUG_MU) |
| 4640 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4641 | ActivityRecord activity = null; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4642 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4643 | activity = mMainStack.isInStackLocked(token); |
| 4644 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4645 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4646 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4647 | if (activity.finishing) { |
| 4648 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4649 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4653 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4654 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4655 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4656 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4657 | |
| 4658 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4659 | type, packageName, activity, resultWho, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4660 | requestCode, intents, resolvedTypes, flags, options, userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4661 | WeakReference<PendingIntentRecord> ref; |
| 4662 | ref = mIntentSenderRecords.get(key); |
| 4663 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4664 | if (rec != null) { |
| 4665 | if (!cancelCurrent) { |
| 4666 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4667 | if (rec.key.requestIntent != null) { |
Adam Powell | 501d4a5 | 2012-04-30 15:03:57 -0700 | [diff] [blame] | 4668 | rec.key.requestIntent.replaceExtras(intents != null ? |
| 4669 | intents[intents.length - 1] : null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4670 | } |
| 4671 | if (intents != null) { |
| 4672 | intents[intents.length-1] = rec.key.requestIntent; |
| 4673 | rec.key.allIntents = intents; |
| 4674 | rec.key.allResolvedTypes = resolvedTypes; |
| 4675 | } else { |
| 4676 | rec.key.allIntents = null; |
| 4677 | rec.key.allResolvedTypes = null; |
| 4678 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4679 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4680 | return rec; |
| 4681 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4682 | rec.canceled = true; |
| 4683 | mIntentSenderRecords.remove(key); |
| 4684 | } |
| 4685 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4686 | return rec; |
| 4687 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4688 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4689 | mIntentSenderRecords.put(key, rec.ref); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4690 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4691 | if (activity.pendingResults == null) { |
| 4692 | activity.pendingResults |
| 4693 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4694 | } |
| 4695 | activity.pendingResults.add(rec.ref); |
| 4696 | } |
| 4697 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | public void cancelIntentSender(IIntentSender sender) { |
| 4701 | if (!(sender instanceof PendingIntentRecord)) { |
| 4702 | return; |
| 4703 | } |
| 4704 | synchronized(this) { |
| 4705 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4706 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4707 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4708 | .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId()); |
| 4709 | if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4710 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4711 | + Binder.getCallingPid() |
| 4712 | + ", uid=" + Binder.getCallingUid() |
| 4713 | + " is not allowed to cancel packges " |
| 4714 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4715 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4716 | throw new SecurityException(msg); |
| 4717 | } |
| 4718 | } catch (RemoteException e) { |
| 4719 | throw new SecurityException(e); |
| 4720 | } |
| 4721 | cancelIntentSenderLocked(rec, true); |
| 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4726 | rec.canceled = true; |
| 4727 | mIntentSenderRecords.remove(rec.key); |
| 4728 | if (cleanActivity && rec.key.activity != null) { |
| 4729 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4730 | } |
| 4731 | } |
| 4732 | |
| 4733 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4734 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4735 | return null; |
| 4736 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4737 | try { |
| 4738 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4739 | return res.key.packageName; |
| 4740 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4741 | } |
| 4742 | return null; |
| 4743 | } |
| 4744 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4745 | public int getUidForIntentSender(IIntentSender sender) { |
| 4746 | if (sender instanceof PendingIntentRecord) { |
| 4747 | try { |
| 4748 | PendingIntentRecord res = (PendingIntentRecord)sender; |
| 4749 | return res.uid; |
| 4750 | } catch (ClassCastException e) { |
| 4751 | } |
| 4752 | } |
| 4753 | return -1; |
| 4754 | } |
| 4755 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4756 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4757 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4758 | return false; |
| 4759 | } |
| 4760 | try { |
| 4761 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4762 | if (res.key.allIntents == null) { |
| 4763 | return false; |
| 4764 | } |
| 4765 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4766 | Intent intent = res.key.allIntents[i]; |
| 4767 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4768 | return false; |
| 4769 | } |
| 4770 | } |
| 4771 | return true; |
| 4772 | } catch (ClassCastException e) { |
| 4773 | } |
| 4774 | return false; |
| 4775 | } |
| 4776 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 4777 | public boolean isIntentSenderAnActivity(IIntentSender pendingResult) { |
| 4778 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4779 | return false; |
| 4780 | } |
| 4781 | try { |
| 4782 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4783 | if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) { |
| 4784 | return true; |
| 4785 | } |
| 4786 | return false; |
| 4787 | } catch (ClassCastException e) { |
| 4788 | } |
| 4789 | return false; |
| 4790 | } |
| 4791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4792 | public void setProcessLimit(int max) { |
| 4793 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4794 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4795 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4796 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4797 | mProcessLimitOverride = max; |
| 4798 | } |
| 4799 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4803 | synchronized (this) { |
| 4804 | return mProcessLimitOverride; |
| 4805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | void foregroundTokenDied(ForegroundToken token) { |
| 4809 | synchronized (ActivityManagerService.this) { |
| 4810 | synchronized (mPidsSelfLocked) { |
| 4811 | ForegroundToken cur |
| 4812 | = mForegroundProcesses.get(token.pid); |
| 4813 | if (cur != token) { |
| 4814 | return; |
| 4815 | } |
| 4816 | mForegroundProcesses.remove(token.pid); |
| 4817 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4818 | if (pr == null) { |
| 4819 | return; |
| 4820 | } |
| 4821 | pr.forcingToForeground = null; |
| 4822 | pr.foregroundServices = false; |
| 4823 | } |
| 4824 | updateOomAdjLocked(); |
| 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4829 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4830 | "setProcessForeground()"); |
| 4831 | synchronized(this) { |
| 4832 | boolean changed = false; |
| 4833 | |
| 4834 | synchronized (mPidsSelfLocked) { |
| 4835 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4836 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4837 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4838 | return; |
| 4839 | } |
| 4840 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4841 | if (oldToken != null) { |
| 4842 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4843 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4844 | if (pr != null) { |
| 4845 | pr.forcingToForeground = null; |
| 4846 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4847 | changed = true; |
| 4848 | } |
| 4849 | if (isForeground && token != null) { |
| 4850 | ForegroundToken newToken = new ForegroundToken() { |
| 4851 | public void binderDied() { |
| 4852 | foregroundTokenDied(this); |
| 4853 | } |
| 4854 | }; |
| 4855 | newToken.pid = pid; |
| 4856 | newToken.token = token; |
| 4857 | try { |
| 4858 | token.linkToDeath(newToken, 0); |
| 4859 | mForegroundProcesses.put(pid, newToken); |
| 4860 | pr.forcingToForeground = token; |
| 4861 | changed = true; |
| 4862 | } catch (RemoteException e) { |
| 4863 | // If the process died while doing this, we will later |
| 4864 | // do the cleanup with the process death link. |
| 4865 | } |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | if (changed) { |
| 4870 | updateOomAdjLocked(); |
| 4871 | } |
| 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | // ========================================================= |
| 4876 | // PERMISSIONS |
| 4877 | // ========================================================= |
| 4878 | |
| 4879 | static class PermissionController extends IPermissionController.Stub { |
| 4880 | ActivityManagerService mActivityManagerService; |
| 4881 | PermissionController(ActivityManagerService activityManagerService) { |
| 4882 | mActivityManagerService = activityManagerService; |
| 4883 | } |
| 4884 | |
| 4885 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4886 | return mActivityManagerService.checkPermission(permission, pid, |
| 4887 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | /** |
| 4892 | * This can be called with or without the global lock held. |
| 4893 | */ |
| 4894 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4895 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4896 | // We might be performing an operation on behalf of an indirect binder |
| 4897 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4898 | // client identity accordingly before proceeding. |
| 4899 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4900 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4901 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4902 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4903 | uid = tlsIdentity.uid; |
| 4904 | pid = tlsIdentity.pid; |
| 4905 | } |
| 4906 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4907 | if (pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4908 | return PackageManager.PERMISSION_GRANTED; |
| 4909 | } |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4910 | |
| 4911 | return ActivityManager.checkComponentPermission(permission, uid, |
| 4912 | owningUid, exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | /** |
| 4916 | * As the only public entry point for permissions checking, this method |
| 4917 | * can enforce the semantic that requesting a check on a null global |
| 4918 | * permission is automatically denied. (Internally a null permission |
| 4919 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4920 | * when only uid-based security is needed.) |
| 4921 | * |
| 4922 | * This can be called with or without the global lock held. |
| 4923 | */ |
| 4924 | public int checkPermission(String permission, int pid, int uid) { |
| 4925 | if (permission == null) { |
| 4926 | return PackageManager.PERMISSION_DENIED; |
| 4927 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4928 | return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | /** |
| 4932 | * Binder IPC calls go through the public entry point. |
| 4933 | * This can be called with or without the global lock held. |
| 4934 | */ |
| 4935 | int checkCallingPermission(String permission) { |
| 4936 | return checkPermission(permission, |
| 4937 | Binder.getCallingPid(), |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4938 | UserHandle.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | /** |
| 4942 | * This can be called with or without the global lock held. |
| 4943 | */ |
| 4944 | void enforceCallingPermission(String permission, String func) { |
| 4945 | if (checkCallingPermission(permission) |
| 4946 | == PackageManager.PERMISSION_GRANTED) { |
| 4947 | return; |
| 4948 | } |
| 4949 | |
| 4950 | String msg = "Permission Denial: " + func + " from pid=" |
| 4951 | + Binder.getCallingPid() |
| 4952 | + ", uid=" + Binder.getCallingUid() |
| 4953 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4954 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4955 | throw new SecurityException(msg); |
| 4956 | } |
| 4957 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4958 | /** |
| 4959 | * Determine if UID is holding permissions required to access {@link Uri} in |
| 4960 | * the given {@link ProviderInfo}. Final permission checking is always done |
| 4961 | * in {@link ContentProvider}. |
| 4962 | */ |
| 4963 | private final boolean checkHoldingPermissionsLocked( |
| 4964 | IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4965 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4966 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4967 | |
| 4968 | if (pi.applicationInfo.uid == uid) { |
| 4969 | return true; |
| 4970 | } else if (!pi.exported) { |
| 4971 | return false; |
| 4972 | } |
| 4973 | |
| 4974 | boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4975 | 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] | 4976 | try { |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4977 | // check if target holds top-level <provider> permissions |
| 4978 | if (!readMet && pi.readPermission != null |
| 4979 | && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) { |
| 4980 | readMet = true; |
| 4981 | } |
| 4982 | if (!writeMet && pi.writePermission != null |
| 4983 | && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) { |
| 4984 | writeMet = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4985 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4986 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4987 | // track if unprotected read/write is allowed; any denied |
| 4988 | // <path-permission> below removes this ability |
| 4989 | boolean allowDefaultRead = pi.readPermission == null; |
| 4990 | boolean allowDefaultWrite = pi.writePermission == null; |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4991 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4992 | // check if target holds any <path-permission> that match uri |
| 4993 | final PathPermission[] pps = pi.pathPermissions; |
| 4994 | if (pps != null) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4995 | final String path = uri.getPath(); |
| 4996 | int i = pps.length; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4997 | while (i > 0 && (!readMet || !writeMet)) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4998 | i--; |
| 4999 | PathPermission pp = pps[i]; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5000 | if (pp.match(path)) { |
| 5001 | if (!readMet) { |
| 5002 | final String pprperm = pp.getReadPermission(); |
| 5003 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 5004 | + pprperm + " for " + pp.getPath() |
| 5005 | + ": match=" + pp.match(path) |
| 5006 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 5007 | if (pprperm != null) { |
| 5008 | if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) { |
| 5009 | readMet = true; |
| 5010 | } else { |
| 5011 | allowDefaultRead = false; |
| 5012 | } |
| 5013 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5014 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5015 | if (!writeMet) { |
| 5016 | final String ppwperm = pp.getWritePermission(); |
| 5017 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 5018 | + ppwperm + " for " + pp.getPath() |
| 5019 | + ": match=" + pp.match(path) |
| 5020 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 5021 | if (ppwperm != null) { |
| 5022 | if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) { |
| 5023 | writeMet = true; |
| 5024 | } else { |
| 5025 | allowDefaultWrite = false; |
| 5026 | } |
| 5027 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5028 | } |
| 5029 | } |
| 5030 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5031 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5032 | |
| 5033 | // grant unprotected <provider> read/write, if not blocked by |
| 5034 | // <path-permission> above |
| 5035 | if (allowDefaultRead) readMet = true; |
| 5036 | if (allowDefaultWrite) writeMet = true; |
| 5037 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5038 | } catch (RemoteException e) { |
| 5039 | return false; |
| 5040 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5041 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5042 | return readMet && writeMet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 5046 | int modeFlags) { |
| 5047 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5048 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5049 | return true; |
| 5050 | } |
| 5051 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 5052 | if (perms == null) return false; |
| 5053 | UriPermission perm = perms.get(uri); |
| 5054 | if (perm == null) return false; |
| 5055 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 5056 | } |
| 5057 | |
| 5058 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5059 | enforceNotIsolatedCaller("checkUriPermission"); |
| 5060 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5061 | // Another redirected-binder-call permissions check as in |
| 5062 | // {@link checkComponentPermission}. |
| 5063 | Identity tlsIdentity = sCallerIdentity.get(); |
| 5064 | if (tlsIdentity != null) { |
| 5065 | uid = tlsIdentity.uid; |
| 5066 | pid = tlsIdentity.pid; |
| 5067 | } |
| 5068 | |
| 5069 | // Our own process gets to do everything. |
| 5070 | if (pid == MY_PID) { |
| 5071 | return PackageManager.PERMISSION_GRANTED; |
| 5072 | } |
| 5073 | synchronized(this) { |
| 5074 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 5075 | ? PackageManager.PERMISSION_GRANTED |
| 5076 | : PackageManager.PERMISSION_DENIED; |
| 5077 | } |
| 5078 | } |
| 5079 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5080 | /** |
| 5081 | * Check if the targetPkg can be granted permission to access uri by |
| 5082 | * the callingUid using the given modeFlags. Throws a security exception |
| 5083 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 5084 | * if the URI permission grant should be performed; returns -1 if it is not |
| 5085 | * needed (for example targetPkg already has permission to access the URI). |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5086 | * If you already know the uid of the target, you can supply it in |
| 5087 | * lastTargetUid else set that to -1. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5088 | */ |
| 5089 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5090 | Uri uri, int modeFlags, int lastTargetUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5091 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5092 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5093 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5094 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5095 | } |
| 5096 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5097 | if (targetPkg != null) { |
| 5098 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5099 | "Checking grant " + targetPkg + " permission to " + uri); |
| 5100 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5101 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5102 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5103 | |
| 5104 | // If this is not a content: uri, we can't do anything with it. |
| 5105 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5106 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5107 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5108 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | String name = uri.getAuthority(); |
| 5112 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5113 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5114 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5115 | if (cpr != null) { |
| 5116 | pi = cpr.info; |
| 5117 | } else { |
| 5118 | try { |
| 5119 | pi = pm.resolveContentProvider(name, |
Dianne Hackborn | 11f0cb7 | 2012-09-16 17:12:34 -0700 | [diff] [blame] | 5120 | PackageManager.GET_URI_PERMISSION_PATTERNS, |
| 5121 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5122 | } catch (RemoteException ex) { |
| 5123 | } |
| 5124 | } |
| 5125 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5126 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5127 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5128 | } |
| 5129 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5130 | int targetUid = lastTargetUid; |
| 5131 | if (targetUid < 0 && targetPkg != null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5132 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5133 | targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5134 | if (targetUid < 0) { |
| 5135 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5136 | "Can't grant URI permission no uid for: " + targetPkg); |
| 5137 | return -1; |
| 5138 | } |
| 5139 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5140 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5141 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5142 | } |
| 5143 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5144 | if (targetUid >= 0) { |
| 5145 | // First... does the target actually need this permission? |
| 5146 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 5147 | // No need to grant the target this permission. |
| 5148 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5149 | "Target " + targetPkg + " already has full permission to " + uri); |
| 5150 | return -1; |
| 5151 | } |
| 5152 | } else { |
| 5153 | // First... there is no target package, so can anyone access it? |
| 5154 | boolean allowed = pi.exported; |
| 5155 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5156 | if (pi.readPermission != null) { |
| 5157 | allowed = false; |
| 5158 | } |
| 5159 | } |
| 5160 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5161 | if (pi.writePermission != null) { |
| 5162 | allowed = false; |
| 5163 | } |
| 5164 | } |
| 5165 | if (allowed) { |
| 5166 | return -1; |
| 5167 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5168 | } |
| 5169 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5170 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5171 | if (!pi.grantUriPermissions) { |
| 5172 | throw new SecurityException("Provider " + pi.packageName |
| 5173 | + "/" + pi.name |
| 5174 | + " does not allow granting of Uri permissions (uri " |
| 5175 | + uri + ")"); |
| 5176 | } |
| 5177 | if (pi.uriPermissionPatterns != null) { |
| 5178 | final int N = pi.uriPermissionPatterns.length; |
| 5179 | boolean allowed = false; |
| 5180 | for (int i=0; i<N; i++) { |
| 5181 | if (pi.uriPermissionPatterns[i] != null |
| 5182 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 5183 | allowed = true; |
| 5184 | break; |
| 5185 | } |
| 5186 | } |
| 5187 | if (!allowed) { |
| 5188 | throw new SecurityException("Provider " + pi.packageName |
| 5189 | + "/" + pi.name |
| 5190 | + " does not allow granting of permission to path of Uri " |
| 5191 | + uri); |
| 5192 | } |
| 5193 | } |
| 5194 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5195 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5196 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5197 | if (callingUid != Process.myUid()) { |
| 5198 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 5199 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5200 | throw new SecurityException("Uid " + callingUid |
| 5201 | + " does not have permission to uri " + uri); |
| 5202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5203 | } |
| 5204 | } |
| 5205 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5206 | return targetUid; |
| 5207 | } |
| 5208 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5209 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 5210 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5211 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5212 | synchronized(this) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5213 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5214 | } |
| 5215 | } |
| 5216 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5217 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 5218 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 5219 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5220 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5221 | if (modeFlags == 0) { |
| 5222 | return; |
| 5223 | } |
| 5224 | |
| 5225 | // 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] | 5226 | // to the uri, and the target doesn't. Let's now give this to |
| 5227 | // the target. |
| 5228 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5229 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5230 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5232 | HashMap<Uri, UriPermission> targetUris |
| 5233 | = mGrantedUriPermissions.get(targetUid); |
| 5234 | if (targetUris == null) { |
| 5235 | targetUris = new HashMap<Uri, UriPermission>(); |
| 5236 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 5237 | } |
| 5238 | |
| 5239 | UriPermission perm = targetUris.get(uri); |
| 5240 | if (perm == null) { |
| 5241 | perm = new UriPermission(targetUid, uri); |
| 5242 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5243 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5245 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5246 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5247 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 5248 | } else { |
| 5249 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5250 | perm.readOwners.add(owner); |
| 5251 | owner.addReadPermission(perm); |
| 5252 | } |
| 5253 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5254 | perm.writeOwners.add(owner); |
| 5255 | owner.addWritePermission(perm); |
| 5256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5257 | } |
| 5258 | } |
| 5259 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5260 | void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri, |
| 5261 | int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5262 | if (targetPkg == null) { |
| 5263 | throw new NullPointerException("targetPkg"); |
| 5264 | } |
| 5265 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5266 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5267 | if (targetUid < 0) { |
| 5268 | return; |
| 5269 | } |
| 5270 | |
| 5271 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 5272 | } |
| 5273 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5274 | static class NeededUriGrants extends ArrayList<Uri> { |
| 5275 | final String targetPkg; |
| 5276 | final int targetUid; |
| 5277 | final int flags; |
| 5278 | |
| 5279 | NeededUriGrants(String _targetPkg, int _targetUid, int _flags) { |
| 5280 | targetPkg = _targetPkg; |
| 5281 | targetUid = _targetUid; |
| 5282 | flags = _flags; |
| 5283 | } |
| 5284 | } |
| 5285 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5286 | /** |
| 5287 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 5288 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5289 | NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 5290 | String targetPkg, Intent intent, int mode, NeededUriGrants needed) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5291 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5292 | "Checking URI perm to data=" + (intent != null ? intent.getData() : null) |
| 5293 | + " clip=" + (intent != null ? intent.getClipData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5294 | + " from " + intent + "; flags=0x" |
| 5295 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 5296 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5297 | if (targetPkg == null) { |
| 5298 | throw new NullPointerException("targetPkg"); |
| 5299 | } |
| 5300 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5301 | if (intent == null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5302 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5303 | } |
| 5304 | Uri data = intent.getData(); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5305 | ClipData clip = intent.getClipData(); |
| 5306 | if (data == null && clip == null) { |
| 5307 | return null; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5308 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5309 | if (data != null) { |
| 5310 | int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 5311 | mode, needed != null ? needed.targetUid : -1); |
| 5312 | if (target > 0) { |
| 5313 | if (needed == null) { |
| 5314 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5315 | } |
| 5316 | needed.add(data); |
| 5317 | } |
| 5318 | } |
| 5319 | if (clip != null) { |
| 5320 | for (int i=0; i<clip.getItemCount(); i++) { |
| 5321 | Uri uri = clip.getItemAt(i).getUri(); |
| 5322 | if (uri != null) { |
| 5323 | int target = -1; |
| 5324 | target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, |
| 5325 | mode, needed != null ? needed.targetUid : -1); |
| 5326 | if (target > 0) { |
| 5327 | if (needed == null) { |
| 5328 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5329 | } |
| 5330 | needed.add(uri); |
| 5331 | } |
| 5332 | } else { |
| 5333 | Intent clipIntent = clip.getItemAt(i).getIntent(); |
| 5334 | if (clipIntent != null) { |
| 5335 | NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked( |
| 5336 | callingUid, targetPkg, clipIntent, mode, needed); |
| 5337 | if (newNeeded != null) { |
| 5338 | needed = newNeeded; |
| 5339 | } |
| 5340 | } |
| 5341 | } |
| 5342 | } |
| 5343 | } |
| 5344 | |
| 5345 | return needed; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | /** |
| 5349 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 5350 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5351 | void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed, |
| 5352 | UriPermissionOwner owner) { |
| 5353 | if (needed != null) { |
| 5354 | for (int i=0; i<needed.size(); i++) { |
| 5355 | grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg, |
| 5356 | needed.get(i), needed.flags, owner); |
| 5357 | } |
| 5358 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 5362 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5363 | NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, |
Dianne Hackborn | d9781fe | 2012-03-08 18:04:18 -0800 | [diff] [blame] | 5364 | intent, intent != null ? intent.getFlags() : 0, null); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5365 | if (needed == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5366 | return; |
| 5367 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5368 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5369 | grantUriPermissionUncheckedFromIntentLocked(needed, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 5373 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5374 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5375 | synchronized(this) { |
| 5376 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5377 | if (r == null) { |
| 5378 | throw new SecurityException("Unable to find app for caller " |
| 5379 | + caller |
| 5380 | + " when granting permission to uri " + uri); |
| 5381 | } |
| 5382 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5383 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5384 | } |
| 5385 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5386 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5389 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5390 | null); |
| 5391 | } |
| 5392 | } |
| 5393 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5394 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5395 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5396 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 5397 | HashMap<Uri, UriPermission> perms |
| 5398 | = mGrantedUriPermissions.get(perm.uid); |
| 5399 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5400 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5401 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5402 | perms.remove(perm.uri); |
| 5403 | if (perms.size() == 0) { |
| 5404 | mGrantedUriPermissions.remove(perm.uid); |
| 5405 | } |
| 5406 | } |
| 5407 | } |
| 5408 | } |
| 5409 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5410 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 5411 | int modeFlags) { |
| 5412 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5413 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5414 | if (modeFlags == 0) { |
| 5415 | return; |
| 5416 | } |
| 5417 | |
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 | "Revoking all granted permissions to " + uri); |
| 5420 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5421 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5422 | |
| 5423 | final String authority = uri.getAuthority(); |
| 5424 | ProviderInfo pi = null; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5425 | int userId = UserHandle.getUserId(callingUid); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5426 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5427 | if (cpr != null) { |
| 5428 | pi = cpr.info; |
| 5429 | } else { |
| 5430 | try { |
| 5431 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5432 | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5433 | } catch (RemoteException ex) { |
| 5434 | } |
| 5435 | } |
| 5436 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5437 | 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] | 5438 | return; |
| 5439 | } |
| 5440 | |
| 5441 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5442 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5443 | // Right now, if you are not the original owner of the permission, |
| 5444 | // you are not allowed to revoke it. |
| 5445 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5446 | throw new SecurityException("Uid " + callingUid |
| 5447 | + " does not have permission to uri " + uri); |
| 5448 | //} |
| 5449 | } |
| 5450 | |
| 5451 | // Go through all of the permissions and remove any that match. |
| 5452 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 5453 | if (SEGMENTS != null) { |
| 5454 | final int NS = SEGMENTS.size(); |
| 5455 | int N = mGrantedUriPermissions.size(); |
| 5456 | for (int i=0; i<N; i++) { |
| 5457 | HashMap<Uri, UriPermission> perms |
| 5458 | = mGrantedUriPermissions.valueAt(i); |
| 5459 | Iterator<UriPermission> it = perms.values().iterator(); |
| 5460 | toploop: |
| 5461 | while (it.hasNext()) { |
| 5462 | UriPermission perm = it.next(); |
| 5463 | Uri targetUri = perm.uri; |
| 5464 | if (!authority.equals(targetUri.getAuthority())) { |
| 5465 | continue; |
| 5466 | } |
| 5467 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5468 | if (targetSegments == null) { |
| 5469 | continue; |
| 5470 | } |
| 5471 | if (targetSegments.size() < NS) { |
| 5472 | continue; |
| 5473 | } |
| 5474 | for (int j=0; j<NS; j++) { |
| 5475 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5476 | continue toploop; |
| 5477 | } |
| 5478 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5479 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5480 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5481 | perm.clearModes(modeFlags); |
| 5482 | if (perm.modeFlags == 0) { |
| 5483 | it.remove(); |
| 5484 | } |
| 5485 | } |
| 5486 | if (perms.size() == 0) { |
| 5487 | mGrantedUriPermissions.remove( |
| 5488 | mGrantedUriPermissions.keyAt(i)); |
| 5489 | N--; |
| 5490 | i--; |
| 5491 | } |
| 5492 | } |
| 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5497 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5498 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5499 | synchronized(this) { |
| 5500 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5501 | if (r == null) { |
| 5502 | throw new SecurityException("Unable to find app for caller " |
| 5503 | + caller |
| 5504 | + " when revoking permission to uri " + uri); |
| 5505 | } |
| 5506 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5507 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5508 | return; |
| 5509 | } |
| 5510 | |
| 5511 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5512 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5513 | if (modeFlags == 0) { |
| 5514 | return; |
| 5515 | } |
| 5516 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5517 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5518 | |
| 5519 | final String authority = uri.getAuthority(); |
| 5520 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5521 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5522 | if (cpr != null) { |
| 5523 | pi = cpr.info; |
| 5524 | } else { |
| 5525 | try { |
| 5526 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5527 | PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5528 | } catch (RemoteException ex) { |
| 5529 | } |
| 5530 | } |
| 5531 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5532 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5533 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5534 | return; |
| 5535 | } |
| 5536 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5537 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5538 | } |
| 5539 | } |
| 5540 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5541 | @Override |
| 5542 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5543 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5544 | synchronized(this) { |
| 5545 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5546 | return owner.getExternalTokenLocked(); |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | @Override |
| 5551 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5552 | Uri uri, int modeFlags) { |
| 5553 | synchronized(this) { |
| 5554 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5555 | if (owner == null) { |
| 5556 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5557 | } |
| 5558 | if (fromUid != Binder.getCallingUid()) { |
| 5559 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5560 | // Only system code can grant URI permissions on behalf |
| 5561 | // of other users. |
| 5562 | throw new SecurityException("nice try"); |
| 5563 | } |
| 5564 | } |
| 5565 | if (targetPkg == null) { |
| 5566 | throw new IllegalArgumentException("null target"); |
| 5567 | } |
| 5568 | if (uri == null) { |
| 5569 | throw new IllegalArgumentException("null uri"); |
| 5570 | } |
| 5571 | |
| 5572 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5573 | } |
| 5574 | } |
| 5575 | |
| 5576 | @Override |
| 5577 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5578 | synchronized(this) { |
| 5579 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5580 | if (owner == null) { |
| 5581 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5582 | } |
| 5583 | |
| 5584 | if (uri == null) { |
| 5585 | owner.removeUriPermissionsLocked(mode); |
| 5586 | } else { |
| 5587 | owner.removeUriPermissionLocked(uri, mode); |
| 5588 | } |
| 5589 | } |
| 5590 | } |
| 5591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5592 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5593 | synchronized (this) { |
| 5594 | ProcessRecord app = |
| 5595 | who != null ? getRecordForAppLocked(who) : null; |
| 5596 | if (app == null) return; |
| 5597 | |
| 5598 | Message msg = Message.obtain(); |
| 5599 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5600 | msg.obj = app; |
| 5601 | msg.arg1 = waiting ? 1 : 0; |
| 5602 | mHandler.sendMessage(msg); |
| 5603 | } |
| 5604 | } |
| 5605 | |
| 5606 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5607 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5608 | 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] | 5609 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 5610 | outInfo.totalMem = Process.getTotalMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5611 | outInfo.threshold = homeAppMem; |
| 5612 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5613 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5614 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5615 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5616 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5617 | ProcessList.VISIBLE_APP_ADJ); |
| 5618 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5619 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | // ========================================================= |
| 5623 | // TASK MANAGEMENT |
| 5624 | // ========================================================= |
| 5625 | |
| 5626 | public List getTasks(int maxNum, int flags, |
| 5627 | IThumbnailReceiver receiver) { |
| 5628 | ArrayList list = new ArrayList(); |
| 5629 | |
| 5630 | PendingThumbnailsRecord pending = null; |
| 5631 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5632 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5633 | |
| 5634 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5635 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5636 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5637 | + ", receiver=" + receiver); |
| 5638 | |
| 5639 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5640 | != PackageManager.PERMISSION_GRANTED) { |
| 5641 | if (receiver != null) { |
| 5642 | // If the caller wants to wait for pending thumbnails, |
| 5643 | // it ain't gonna get them. |
| 5644 | try { |
| 5645 | receiver.finished(); |
| 5646 | } catch (RemoteException ex) { |
| 5647 | } |
| 5648 | } |
| 5649 | String msg = "Permission Denial: getTasks() from pid=" |
| 5650 | + Binder.getCallingPid() |
| 5651 | + ", uid=" + Binder.getCallingUid() |
| 5652 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5653 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5654 | throw new SecurityException(msg); |
| 5655 | } |
| 5656 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5657 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5658 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5659 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5660 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5661 | TaskRecord curTask = null; |
| 5662 | int numActivities = 0; |
| 5663 | int numRunning = 0; |
| 5664 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5665 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5666 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5667 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5668 | |
| 5669 | // Initialize state for next task if needed. |
| 5670 | if (top == null || |
| 5671 | (top.state == ActivityState.INITIALIZING |
| 5672 | && top.task == r.task)) { |
| 5673 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5674 | curTask = r.task; |
| 5675 | numActivities = numRunning = 0; |
| 5676 | } |
| 5677 | |
| 5678 | // Add 'r' into the current task. |
| 5679 | numActivities++; |
| 5680 | if (r.app != null && r.app.thread != null) { |
| 5681 | numRunning++; |
| 5682 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5683 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5684 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5685 | TAG, r.intent.getComponent().flattenToShortString() |
| 5686 | + ": task=" + r.task); |
| 5687 | |
| 5688 | // If the next one is a different task, generate a new |
| 5689 | // TaskInfo entry for what we have. |
| 5690 | if (next == null || next.task != curTask) { |
| 5691 | ActivityManager.RunningTaskInfo ci |
| 5692 | = new ActivityManager.RunningTaskInfo(); |
| 5693 | ci.id = curTask.taskId; |
| 5694 | ci.baseActivity = r.intent.getComponent(); |
| 5695 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5696 | if (top.thumbHolder != null) { |
| 5697 | ci.description = top.thumbHolder.lastDescription; |
| 5698 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5699 | ci.numActivities = numActivities; |
| 5700 | ci.numRunning = numRunning; |
| 5701 | //System.out.println( |
| 5702 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5703 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5704 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5705 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5706 | + " app=" + top.app |
| 5707 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5708 | if (top.state == ActivityState.RESUMED |
| 5709 | || top.state == ActivityState.PAUSING) { |
| 5710 | if (top.idle && top.app != null |
| 5711 | && top.app.thread != null) { |
| 5712 | topRecord = top; |
| 5713 | topThumbnail = top.app.thread; |
| 5714 | } else { |
| 5715 | top.thumbnailNeeded = true; |
| 5716 | } |
| 5717 | } |
| 5718 | if (pending == null) { |
| 5719 | pending = new PendingThumbnailsRecord(receiver); |
| 5720 | } |
| 5721 | pending.pendingRecords.add(top); |
| 5722 | } |
| 5723 | list.add(ci); |
| 5724 | maxNum--; |
| 5725 | top = null; |
| 5726 | } |
| 5727 | } |
| 5728 | |
| 5729 | if (pending != null) { |
| 5730 | mPendingThumbnails.add(pending); |
| 5731 | } |
| 5732 | } |
| 5733 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5734 | 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] | 5735 | |
| 5736 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5737 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5738 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5739 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5740 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5741 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5742 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5743 | } |
| 5744 | } |
| 5745 | |
| 5746 | if (pending == null && receiver != null) { |
| 5747 | // In this case all thumbnails were available and the client |
| 5748 | // is being asked to be told when the remaining ones come in... |
| 5749 | // which is unusually, since the top-most currently running |
| 5750 | // activity should never have a canned thumbnail! Oh well. |
| 5751 | try { |
| 5752 | receiver.finished(); |
| 5753 | } catch (RemoteException ex) { |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | return list; |
| 5758 | } |
| 5759 | |
| 5760 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5761 | int flags, int userId) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 5762 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 5763 | false, true, "getRecentTasks", null); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5765 | synchronized (this) { |
| 5766 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5767 | "getRecentTasks()"); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5768 | final boolean detailed = checkCallingPermission( |
| 5769 | android.Manifest.permission.GET_DETAILED_TASKS) |
| 5770 | == PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5771 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5772 | IPackageManager pm = AppGlobals.getPackageManager(); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5773 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5774 | final int N = mRecentTasks.size(); |
| 5775 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5776 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5777 | maxNum < N ? maxNum : N); |
| 5778 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5779 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5780 | // Only add calling user's recent tasks |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5781 | if (tr.userId != userId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5782 | // Return the entry if desired by the caller. We always return |
| 5783 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5784 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5785 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5786 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5787 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5788 | if (i == 0 |
| 5789 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5790 | || (tr.intent == null) |
| 5791 | || ((tr.intent.getFlags() |
| 5792 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5793 | ActivityManager.RecentTaskInfo rti |
| 5794 | = new ActivityManager.RecentTaskInfo(); |
| 5795 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5796 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5797 | rti.baseIntent = new Intent( |
| 5798 | tr.intent != null ? tr.intent : tr.affinityIntent); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5799 | if (!detailed) { |
| 5800 | rti.baseIntent.replaceExtras((Bundle)null); |
| 5801 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5802 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5803 | rti.description = tr.lastDescription; |
| 5804 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5805 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5806 | // Check whether this activity is currently available. |
| 5807 | try { |
| 5808 | if (rti.origActivity != null) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5809 | if (pm.getActivityInfo(rti.origActivity, 0, userId) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5810 | == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5811 | continue; |
| 5812 | } |
| 5813 | } else if (rti.baseIntent != null) { |
| 5814 | if (pm.queryIntentActivities(rti.baseIntent, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5815 | null, 0, userId) == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5816 | continue; |
| 5817 | } |
| 5818 | } |
| 5819 | } catch (RemoteException e) { |
| 5820 | // Will never happen. |
| 5821 | } |
| 5822 | } |
| 5823 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5824 | res.add(rti); |
| 5825 | maxNum--; |
| 5826 | } |
| 5827 | } |
| 5828 | return res; |
| 5829 | } |
| 5830 | } |
| 5831 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5832 | private TaskRecord taskForIdLocked(int id) { |
| 5833 | final int N = mRecentTasks.size(); |
| 5834 | for (int i=0; i<N; i++) { |
| 5835 | TaskRecord tr = mRecentTasks.get(i); |
| 5836 | if (tr.taskId == id) { |
| 5837 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5838 | } |
| 5839 | } |
| 5840 | return null; |
| 5841 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5842 | |
| 5843 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5844 | synchronized (this) { |
| 5845 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5846 | "getTaskThumbnails()"); |
| 5847 | TaskRecord tr = taskForIdLocked(id); |
| 5848 | if (tr != null) { |
| 5849 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5850 | } |
| 5851 | } |
| 5852 | return null; |
| 5853 | } |
| 5854 | |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 5855 | public Bitmap getTaskTopThumbnail(int id) { |
| 5856 | synchronized (this) { |
| 5857 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5858 | "getTaskTopThumbnail()"); |
| 5859 | TaskRecord tr = taskForIdLocked(id); |
| 5860 | if (tr != null) { |
| 5861 | return mMainStack.getTaskTopThumbnailLocked(tr); |
| 5862 | } |
| 5863 | } |
| 5864 | return null; |
| 5865 | } |
| 5866 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5867 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5868 | synchronized (this) { |
| 5869 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5870 | "removeSubTask()"); |
| 5871 | long ident = Binder.clearCallingIdentity(); |
| 5872 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5873 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex, |
| 5874 | true) != null; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5875 | } finally { |
| 5876 | Binder.restoreCallingIdentity(ident); |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5881 | private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) { |
| 5882 | final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5883 | Intent baseIntent = new Intent( |
| 5884 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5885 | ComponentName component = baseIntent.getComponent(); |
| 5886 | if (component == null) { |
| 5887 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5888 | return; |
| 5889 | } |
| 5890 | |
| 5891 | // Find any running services associated with this app. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 5892 | mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5893 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5894 | if (killProcesses) { |
| 5895 | // Find any running processes associated with this app. |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5896 | final String pkg = component.getPackageName(); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5897 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5898 | HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap(); |
| 5899 | for (SparseArray<ProcessRecord> uids : pmap.values()) { |
| 5900 | for (int i=0; i<uids.size(); i++) { |
| 5901 | ProcessRecord proc = uids.valueAt(i); |
| 5902 | if (proc.userId != tr.userId) { |
| 5903 | continue; |
| 5904 | } |
| 5905 | if (!proc.pkgList.contains(pkg)) { |
| 5906 | continue; |
| 5907 | } |
| 5908 | procs.add(proc); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5909 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5910 | } |
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 | // Kill the running processes. |
| 5913 | for (int i=0; i<procs.size(); i++) { |
| 5914 | ProcessRecord pr = procs.get(i); |
| 5915 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5916 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
| 5917 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5918 | pr.processName, pr.setAdj, "remove task"); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5919 | pr.killedBackground = true; |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5920 | Process.killProcessQuiet(pr.pid); |
| 5921 | } else { |
| 5922 | pr.waitingToKill = "remove task"; |
| 5923 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5924 | } |
| 5925 | } |
| 5926 | } |
| 5927 | |
| 5928 | public boolean removeTask(int taskId, int flags) { |
| 5929 | synchronized (this) { |
| 5930 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5931 | "removeTask()"); |
| 5932 | long ident = Binder.clearCallingIdentity(); |
| 5933 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5934 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1, |
| 5935 | false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5936 | if (r != null) { |
| 5937 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5938 | cleanUpRemovedTaskLocked(r.task, flags); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5939 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5940 | } else { |
| 5941 | TaskRecord tr = null; |
| 5942 | int i=0; |
| 5943 | while (i < mRecentTasks.size()) { |
| 5944 | TaskRecord t = mRecentTasks.get(i); |
| 5945 | if (t.taskId == taskId) { |
| 5946 | tr = t; |
| 5947 | break; |
| 5948 | } |
| 5949 | i++; |
| 5950 | } |
| 5951 | if (tr != null) { |
| 5952 | if (tr.numActivities <= 0) { |
| 5953 | // Caller is just removing a recent task that is |
| 5954 | // not actively running. That is easy! |
| 5955 | mRecentTasks.remove(i); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5956 | cleanUpRemovedTaskLocked(tr, flags); |
| 5957 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5958 | } else { |
| 5959 | Slog.w(TAG, "removeTask: task " + taskId |
| 5960 | + " does not have activities to remove, " |
| 5961 | + " but numActivities=" + tr.numActivities |
| 5962 | + ": " + tr); |
| 5963 | } |
| 5964 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5965 | } |
| 5966 | } finally { |
| 5967 | Binder.restoreCallingIdentity(ident); |
| 5968 | } |
| 5969 | } |
| 5970 | return false; |
| 5971 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5972 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5973 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5974 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5975 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5976 | TaskRecord jt = startTask; |
| 5977 | |
| 5978 | // First look backwards |
| 5979 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5980 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5981 | if (r.task != jt) { |
| 5982 | jt = r.task; |
| 5983 | if (affinity.equals(jt.affinity)) { |
| 5984 | return j; |
| 5985 | } |
| 5986 | } |
| 5987 | } |
| 5988 | |
| 5989 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5990 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5991 | jt = startTask; |
| 5992 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5993 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5994 | if (r.task != jt) { |
| 5995 | if (affinity.equals(jt.affinity)) { |
| 5996 | return j; |
| 5997 | } |
| 5998 | jt = r.task; |
| 5999 | } |
| 6000 | } |
| 6001 | |
| 6002 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6003 | 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] | 6004 | return N-1; |
| 6005 | } |
| 6006 | |
| 6007 | return -1; |
| 6008 | } |
| 6009 | |
| 6010 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6011 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6012 | */ |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6013 | public void moveTaskToFront(int task, int flags, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6014 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6015 | "moveTaskToFront()"); |
| 6016 | |
| 6017 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6018 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6019 | Binder.getCallingUid(), "Task to front")) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6020 | ActivityOptions.abort(options); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6021 | return; |
| 6022 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6023 | final long origId = Binder.clearCallingIdentity(); |
| 6024 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6025 | TaskRecord tr = taskForIdLocked(task); |
| 6026 | if (tr != null) { |
| 6027 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 6028 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6029 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6030 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 6031 | // Caller wants the home activity moved with it. To accomplish this, |
| 6032 | // we'll just move the home task to the top first. |
| 6033 | mMainStack.moveHomeToFrontLocked(); |
| 6034 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6035 | mMainStack.moveTaskToFrontLocked(tr, null, options); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6036 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6037 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6038 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 6039 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6040 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 6041 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 6042 | mMainStack.mUserLeaving = true; |
| 6043 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 6044 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 6045 | // Caller wants the home activity moved with it. To accomplish this, |
| 6046 | // we'll just move the home task to the top first. |
| 6047 | mMainStack.moveHomeToFrontLocked(); |
| 6048 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6049 | mMainStack.moveTaskToFrontLocked(hr.task, null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6050 | return; |
| 6051 | } |
| 6052 | } |
| 6053 | } finally { |
| 6054 | Binder.restoreCallingIdentity(origId); |
| 6055 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6056 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6057 | } |
| 6058 | } |
| 6059 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6060 | public void moveTaskToBack(int task) { |
| 6061 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6062 | "moveTaskToBack()"); |
| 6063 | |
| 6064 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6065 | if (mMainStack.mResumedActivity != null |
| 6066 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6067 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6068 | Binder.getCallingUid(), "Task to back")) { |
| 6069 | return; |
| 6070 | } |
| 6071 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6072 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6073 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6074 | Binder.restoreCallingIdentity(origId); |
| 6075 | } |
| 6076 | } |
| 6077 | |
| 6078 | /** |
| 6079 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 6080 | * of the history stack. The activity's order within the task is unchanged. |
| 6081 | * |
| 6082 | * @param token A reference to the activity we wish to move |
| 6083 | * @param nonRoot If false then this only works if the activity is the root |
| 6084 | * of a task; if true it will work for any activity in a task. |
| 6085 | * @return Returns true if the move completed, false if not. |
| 6086 | */ |
| 6087 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6088 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6089 | synchronized(this) { |
| 6090 | final long origId = Binder.clearCallingIdentity(); |
| 6091 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 6092 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6093 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6094 | } |
| 6095 | Binder.restoreCallingIdentity(origId); |
| 6096 | } |
| 6097 | return false; |
| 6098 | } |
| 6099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6100 | public void moveTaskBackwards(int task) { |
| 6101 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6102 | "moveTaskBackwards()"); |
| 6103 | |
| 6104 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6105 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6106 | Binder.getCallingUid(), "Task backwards")) { |
| 6107 | return; |
| 6108 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6109 | final long origId = Binder.clearCallingIdentity(); |
| 6110 | moveTaskBackwardsLocked(task); |
| 6111 | Binder.restoreCallingIdentity(origId); |
| 6112 | } |
| 6113 | } |
| 6114 | |
| 6115 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6116 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 6120 | synchronized(this) { |
| 6121 | return getTaskForActivityLocked(token, onlyRoot); |
| 6122 | } |
| 6123 | } |
| 6124 | |
| 6125 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6126 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6127 | TaskRecord lastTask = null; |
| 6128 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6129 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 6130 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6131 | if (!onlyRoot || lastTask != r.task) { |
| 6132 | return r.task.taskId; |
| 6133 | } |
| 6134 | return -1; |
| 6135 | } |
| 6136 | lastTask = r.task; |
| 6137 | } |
| 6138 | |
| 6139 | return -1; |
| 6140 | } |
| 6141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6142 | // ========================================================= |
| 6143 | // THUMBNAILS |
| 6144 | // ========================================================= |
| 6145 | |
| 6146 | public void reportThumbnail(IBinder token, |
| 6147 | Bitmap thumbnail, CharSequence description) { |
| 6148 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 6149 | final long origId = Binder.clearCallingIdentity(); |
| 6150 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 6151 | Binder.restoreCallingIdentity(origId); |
| 6152 | } |
| 6153 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6154 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6155 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 6156 | TaskRecord task = null; |
| 6157 | ArrayList receivers = null; |
| 6158 | |
| 6159 | //System.out.println("Send pending thumbnail: " + r); |
| 6160 | |
| 6161 | synchronized(this) { |
| 6162 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6163 | r = mMainStack.isInStackLocked(token); |
| 6164 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6165 | return; |
| 6166 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6167 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 6168 | if (thumbnail == null && r.thumbHolder != null) { |
| 6169 | thumbnail = r.thumbHolder.lastThumbnail; |
| 6170 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6171 | } |
| 6172 | if (thumbnail == null && !always) { |
| 6173 | // If there is no thumbnail, and this entry is not actually |
| 6174 | // going away, then abort for now and pick up the next |
| 6175 | // thumbnail we get. |
| 6176 | return; |
| 6177 | } |
| 6178 | task = r.task; |
| 6179 | |
| 6180 | int N = mPendingThumbnails.size(); |
| 6181 | int i=0; |
| 6182 | while (i<N) { |
| 6183 | PendingThumbnailsRecord pr = |
| 6184 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 6185 | //System.out.println("Looking in " + pr.pendingRecords); |
| 6186 | if (pr.pendingRecords.remove(r)) { |
| 6187 | if (receivers == null) { |
| 6188 | receivers = new ArrayList(); |
| 6189 | } |
| 6190 | receivers.add(pr); |
| 6191 | if (pr.pendingRecords.size() == 0) { |
| 6192 | pr.finished = true; |
| 6193 | mPendingThumbnails.remove(i); |
| 6194 | N--; |
| 6195 | continue; |
| 6196 | } |
| 6197 | } |
| 6198 | i++; |
| 6199 | } |
| 6200 | } |
| 6201 | |
| 6202 | if (receivers != null) { |
| 6203 | final int N = receivers.size(); |
| 6204 | for (int i=0; i<N; i++) { |
| 6205 | try { |
| 6206 | PendingThumbnailsRecord pr = |
| 6207 | (PendingThumbnailsRecord)receivers.get(i); |
| 6208 | pr.receiver.newThumbnail( |
| 6209 | task != null ? task.taskId : -1, thumbnail, description); |
| 6210 | if (pr.finished) { |
| 6211 | pr.receiver.finished(); |
| 6212 | } |
| 6213 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6214 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6215 | } |
| 6216 | } |
| 6217 | } |
| 6218 | } |
| 6219 | |
| 6220 | // ========================================================= |
| 6221 | // CONTENT PROVIDERS |
| 6222 | // ========================================================= |
| 6223 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6224 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 6225 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6226 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6227 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6228 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6229 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6230 | } catch (RemoteException ex) { |
| 6231 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6232 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6233 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 6234 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6235 | if (providers != null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6236 | int N = providers.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6237 | for (int i=0; i<N; i++) { |
| 6238 | ProviderInfo cpi = |
| 6239 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6240 | boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6241 | cpi.name, cpi.flags); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6242 | if (singleton && UserHandle.getUserId(app.uid) != 0) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6243 | // This is a singleton provider, but a user besides the |
| 6244 | // default user is asking to initialize a process it runs |
| 6245 | // in... well, no, it doesn't actually run in this process, |
| 6246 | // it runs in the process of the default user. Get rid of it. |
| 6247 | providers.remove(i); |
| 6248 | N--; |
| 6249 | continue; |
| 6250 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6251 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6252 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6253 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6254 | if (cpr == null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6255 | cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6256 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6257 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6258 | if (DEBUG_MU) |
| 6259 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6260 | app.pubProviders.put(cpi.name, cpr); |
| 6261 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 6262 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6263 | } |
| 6264 | } |
| 6265 | return providers; |
| 6266 | } |
| 6267 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 6268 | /** |
| 6269 | * Check if {@link ProcessRecord} has a possible chance at accessing the |
| 6270 | * given {@link ProviderInfo}. Final permission checking is always done |
| 6271 | * in {@link ContentProvider}. |
| 6272 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6273 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6274 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6275 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6276 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6277 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6278 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6279 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6280 | return null; |
| 6281 | } |
| 6282 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6283 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6284 | == PackageManager.PERMISSION_GRANTED) { |
| 6285 | return null; |
| 6286 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6287 | |
| 6288 | PathPermission[] pps = cpi.pathPermissions; |
| 6289 | if (pps != null) { |
| 6290 | int i = pps.length; |
| 6291 | while (i > 0) { |
| 6292 | i--; |
| 6293 | PathPermission pp = pps[i]; |
| 6294 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6295 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6296 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6297 | return null; |
| 6298 | } |
| 6299 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6300 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6301 | == PackageManager.PERMISSION_GRANTED) { |
| 6302 | return null; |
| 6303 | } |
| 6304 | } |
| 6305 | } |
| 6306 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6307 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 6308 | if (perms != null) { |
| 6309 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 6310 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 6311 | return null; |
| 6312 | } |
| 6313 | } |
| 6314 | } |
| 6315 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6316 | String msg; |
| 6317 | if (!cpi.exported) { |
| 6318 | msg = "Permission Denial: opening provider " + cpi.name |
| 6319 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6320 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 6321 | + cpi.applicationInfo.uid; |
| 6322 | } else { |
| 6323 | msg = "Permission Denial: opening provider " + cpi.name |
| 6324 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6325 | + ", uid=" + callingUid + ") requires " |
| 6326 | + cpi.readPermission + " or " + cpi.writePermission; |
| 6327 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6328 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6329 | return msg; |
| 6330 | } |
| 6331 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6332 | ContentProviderConnection incProviderCountLocked(ProcessRecord r, |
| 6333 | final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6334 | if (r != null) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6335 | for (int i=0; i<r.conProviders.size(); i++) { |
| 6336 | ContentProviderConnection conn = r.conProviders.get(i); |
| 6337 | if (conn.provider == cpr) { |
| 6338 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6339 | "Adding provider requested by " |
| 6340 | + r.processName + " from process " |
| 6341 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6342 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6343 | if (stable) { |
| 6344 | conn.stableCount++; |
| 6345 | conn.numStableIncs++; |
| 6346 | } else { |
| 6347 | conn.unstableCount++; |
| 6348 | conn.numUnstableIncs++; |
| 6349 | } |
| 6350 | return conn; |
| 6351 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6352 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6353 | ContentProviderConnection conn = new ContentProviderConnection(cpr, r); |
| 6354 | if (stable) { |
| 6355 | conn.stableCount = 1; |
| 6356 | conn.numStableIncs = 1; |
| 6357 | } else { |
| 6358 | conn.unstableCount = 1; |
| 6359 | conn.numUnstableIncs = 1; |
| 6360 | } |
| 6361 | cpr.connections.add(conn); |
| 6362 | r.conProviders.add(conn); |
| 6363 | return conn; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6364 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6365 | cpr.addExternalProcessHandleLocked(externalProcessToken); |
| 6366 | return null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6367 | } |
| 6368 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6369 | boolean decProviderCountLocked(ContentProviderConnection conn, |
| 6370 | ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
| 6371 | if (conn != null) { |
| 6372 | cpr = conn.provider; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6373 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6374 | "Removing provider requested by " |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6375 | + conn.client.processName + " from process " |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6376 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6377 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6378 | if (stable) { |
| 6379 | conn.stableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6380 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6381 | conn.unstableCount--; |
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 | if (conn.stableCount == 0 && conn.unstableCount == 0) { |
| 6384 | cpr.connections.remove(conn); |
| 6385 | conn.client.conProviders.remove(conn); |
| 6386 | return true; |
| 6387 | } |
| 6388 | return false; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6389 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6390 | cpr.removeExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6391 | return false; |
| 6392 | } |
| 6393 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6394 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6395 | String name, IBinder token, boolean stable, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6396 | ContentProviderRecord cpr; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6397 | ContentProviderConnection conn = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6398 | ProviderInfo cpi = null; |
| 6399 | |
| 6400 | synchronized(this) { |
| 6401 | ProcessRecord r = null; |
| 6402 | if (caller != null) { |
| 6403 | r = getRecordForAppLocked(caller); |
| 6404 | if (r == null) { |
| 6405 | throw new SecurityException( |
| 6406 | "Unable to find app for caller " + caller |
| 6407 | + " (pid=" + Binder.getCallingPid() |
| 6408 | + ") when getting content provider " + name); |
| 6409 | } |
| 6410 | } |
| 6411 | |
| 6412 | // First check if this content provider has been published... |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6413 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6414 | boolean providerRunning = cpr != null; |
| 6415 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6416 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6417 | String msg; |
| 6418 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6419 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6420 | } |
| 6421 | |
| 6422 | if (r != null && cpr.canRunHere(r)) { |
| 6423 | // This provider has been published or is in the process |
| 6424 | // of being published... but it is also allowed to run |
| 6425 | // in the caller's process, so don't make a connection |
| 6426 | // and just let the caller instantiate its own instance. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6427 | ContentProviderHolder holder = cpr.newHolder(null); |
| 6428 | // don't give caller the provider object, it needs |
| 6429 | // to make its own. |
| 6430 | holder.provider = null; |
| 6431 | return holder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | final long origId = Binder.clearCallingIdentity(); |
| 6435 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6436 | // 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] | 6437 | // return it right away. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6438 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6439 | if (conn != null && (conn.stableCount+conn.unstableCount) == 1) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6440 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 6441 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6442 | // make sure to count it as being accessed and thus |
| 6443 | // back up on the LRU list. This is good because |
| 6444 | // content providers are often expensive to start. |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6445 | updateLruProcessLocked(cpr.proc, false, true); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6446 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 6447 | } |
| 6448 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6449 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6450 | if (false) { |
| 6451 | if (cpr.name.flattenToShortString().equals( |
| 6452 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 6453 | Slog.v(TAG, "****************** KILLING " |
| 6454 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6455 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6456 | } |
| 6457 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6458 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6459 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 6460 | // NOTE: there is still a race here where a signal could be |
| 6461 | // pending on the process even though we managed to update its |
| 6462 | // adj level. Not sure what to do about this, but at least |
| 6463 | // the race is now smaller. |
| 6464 | if (!success) { |
| 6465 | // Uh oh... it looks like the provider's process |
| 6466 | // has been killed on us. We need to wait for a new |
| 6467 | // process to be started, and make sure its death |
| 6468 | // doesn't kill our process. |
| 6469 | Slog.i(TAG, |
| 6470 | "Existing provider " + cpr.name.flattenToShortString() |
| 6471 | + " is crashing; detaching " + r); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6472 | boolean lastRef = decProviderCountLocked(conn, cpr, token, stable); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6473 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6474 | if (!lastRef) { |
| 6475 | // This wasn't the last ref our process had on |
| 6476 | // the provider... we have now been killed, bail. |
| 6477 | return null; |
| 6478 | } |
| 6479 | providerRunning = false; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6480 | conn = null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6481 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6482 | } |
| 6483 | |
| 6484 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6485 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6486 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6487 | boolean singleton; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6488 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6489 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6490 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6491 | resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6492 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6493 | } catch (RemoteException ex) { |
| 6494 | } |
| 6495 | if (cpi == null) { |
| 6496 | return null; |
| 6497 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6498 | singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6499 | cpi.name, cpi.flags); |
| 6500 | if (singleton) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 6501 | userId = 0; |
| 6502 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6503 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6504 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6505 | String msg; |
| 6506 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6507 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6508 | } |
| 6509 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6510 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6511 | && !cpi.processName.equals("system")) { |
| 6512 | // If this content provider does not run in the system |
| 6513 | // process, and the system is not yet ready to run other |
| 6514 | // processes, then fail fast instead of hanging. |
| 6515 | throw new IllegalArgumentException( |
| 6516 | "Attempt to launch content provider before system ready"); |
| 6517 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6518 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 6519 | // Make sure that the user who owns this provider is started. If not, |
| 6520 | // we don't want to allow it to run. |
| 6521 | if (mStartedUsers.get(userId) == null) { |
| 6522 | Slog.w(TAG, "Unable to launch app " |
| 6523 | + cpi.applicationInfo.packageName + "/" |
| 6524 | + cpi.applicationInfo.uid + " for provider " |
| 6525 | + name + ": user " + userId + " is stopped"); |
| 6526 | return null; |
| 6527 | } |
| 6528 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6529 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6530 | cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6531 | final boolean firstClass = cpr == null; |
| 6532 | if (firstClass) { |
| 6533 | try { |
| 6534 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6535 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6536 | getApplicationInfo( |
| 6537 | cpi.applicationInfo.packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6538 | STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6539 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6540 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6541 | + cpi.name); |
| 6542 | return null; |
| 6543 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6544 | ai = getAppInfoForUser(ai, userId); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6545 | cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6546 | } catch (RemoteException ex) { |
| 6547 | // pm is in same process, this will never happen. |
| 6548 | } |
| 6549 | } |
| 6550 | |
| 6551 | if (r != null && cpr.canRunHere(r)) { |
| 6552 | // If this is a multiprocess provider, then just return its |
| 6553 | // info and allow the caller to instantiate it. Only do |
| 6554 | // this if the provider is the same user as the caller's |
| 6555 | // process, or can run as root (so can be in any process). |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6556 | return cpr.newHolder(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6557 | } |
| 6558 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6559 | if (DEBUG_PROVIDER) { |
| 6560 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6561 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6562 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6563 | } |
| 6564 | |
| 6565 | // This is single process, and our app is now connecting to it. |
| 6566 | // See if we are already in the process of launching this |
| 6567 | // provider. |
| 6568 | final int N = mLaunchingProviders.size(); |
| 6569 | int i; |
| 6570 | for (i=0; i<N; i++) { |
| 6571 | if (mLaunchingProviders.get(i) == cpr) { |
| 6572 | break; |
| 6573 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6574 | } |
| 6575 | |
| 6576 | // If the provider is not already being launched, then get it |
| 6577 | // started. |
| 6578 | if (i >= N) { |
| 6579 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6580 | |
| 6581 | try { |
| 6582 | // Content provider is now in use, its package can't be stopped. |
| 6583 | try { |
| 6584 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6585 | cpr.appInfo.packageName, false, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6586 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6587 | } catch (IllegalArgumentException e) { |
| 6588 | Slog.w(TAG, "Failed trying to unstop package " |
| 6589 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6590 | } |
| 6591 | |
| 6592 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6593 | cpr.appInfo, false, 0, "content provider", |
| 6594 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6595 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6596 | if (proc == null) { |
| 6597 | Slog.w(TAG, "Unable to launch app " |
| 6598 | + cpi.applicationInfo.packageName + "/" |
| 6599 | + cpi.applicationInfo.uid + " for provider " |
| 6600 | + name + ": process is bad"); |
| 6601 | return null; |
| 6602 | } |
| 6603 | cpr.launchingApp = proc; |
| 6604 | mLaunchingProviders.add(cpr); |
| 6605 | } finally { |
| 6606 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6607 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6608 | } |
| 6609 | |
| 6610 | // Make sure the provider is published (the same provider class |
| 6611 | // may be published under multiple names). |
| 6612 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6613 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6614 | } |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6615 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6616 | mProviderMap.putProviderByName(name, cpr); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6617 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6618 | if (conn != null) { |
| 6619 | conn.waiting = true; |
| 6620 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6621 | } |
| 6622 | } |
| 6623 | |
| 6624 | // Wait for the provider to be published... |
| 6625 | synchronized (cpr) { |
| 6626 | while (cpr.provider == null) { |
| 6627 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6628 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6629 | + cpi.applicationInfo.packageName + "/" |
| 6630 | + cpi.applicationInfo.uid + " for provider " |
| 6631 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6632 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6633 | cpi.applicationInfo.packageName, |
| 6634 | cpi.applicationInfo.uid, name); |
| 6635 | return null; |
| 6636 | } |
| 6637 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6638 | if (DEBUG_MU) { |
| 6639 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6640 | + cpr.launchingApp); |
| 6641 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6642 | if (conn != null) { |
| 6643 | conn.waiting = true; |
| 6644 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6645 | cpr.wait(); |
| 6646 | } catch (InterruptedException ex) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6647 | } finally { |
| 6648 | if (conn != null) { |
| 6649 | conn.waiting = false; |
| 6650 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6651 | } |
| 6652 | } |
| 6653 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6654 | return cpr != null ? cpr.newHolder(conn) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | public final ContentProviderHolder getContentProvider( |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6658 | IApplicationThread caller, String name, int userId, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6659 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6660 | if (caller == null) { |
| 6661 | String msg = "null IApplicationThread when getting content provider " |
| 6662 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6663 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6664 | throw new SecurityException(msg); |
| 6665 | } |
| 6666 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6667 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6668 | false, true, "getContentProvider", null); |
| 6669 | return getContentProviderImpl(caller, name, null, stable, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6670 | } |
| 6671 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6672 | public ContentProviderHolder getContentProviderExternal( |
| 6673 | String name, int userId, IBinder token) { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6674 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6675 | "Do not have permission in call getContentProviderExternal()"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6676 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6677 | false, true, "getContentProvider", null); |
| 6678 | return getContentProviderExternalUnchecked(name, token, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6679 | } |
| 6680 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6681 | private ContentProviderHolder getContentProviderExternalUnchecked(String name, |
| 6682 | IBinder token, int userId) { |
| 6683 | return getContentProviderImpl(null, name, token, true, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | /** |
| 6687 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6688 | * @param cpr |
| 6689 | */ |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6690 | public void removeContentProvider(IBinder connection, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6691 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6692 | synchronized (this) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6693 | ContentProviderConnection conn; |
| 6694 | try { |
| 6695 | conn = (ContentProviderConnection)connection; |
| 6696 | } catch (ClassCastException e) { |
| 6697 | String msg ="removeContentProvider: " + connection |
| 6698 | + " not a ContentProviderConnection"; |
| 6699 | Slog.w(TAG, msg); |
| 6700 | throw new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6701 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6702 | if (conn == null) { |
| 6703 | throw new NullPointerException("connection is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6704 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6705 | if (decProviderCountLocked(conn, null, null, stable)) { |
| 6706 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6707 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6708 | } |
| 6709 | } |
| 6710 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6711 | public void removeContentProviderExternal(String name, IBinder token) { |
| 6712 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6713 | "Do not have permission in call removeContentProviderExternal()"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6714 | removeContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6715 | } |
| 6716 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6717 | private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6718 | synchronized (this) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6719 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6720 | if(cpr == null) { |
| 6721 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6722 | 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] | 6723 | return; |
| 6724 | } |
| 6725 | |
| 6726 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6727 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6728 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6729 | if (localCpr.hasExternalProcessHandles()) { |
| 6730 | if (localCpr.removeExternalProcessHandleLocked(token)) { |
| 6731 | updateOomAdjLocked(); |
| 6732 | } else { |
| 6733 | Slog.e(TAG, "Attmpt to remove content provider " + localCpr |
| 6734 | + " with no external reference for token: " |
| 6735 | + token + "."); |
| 6736 | } |
| 6737 | } else { |
| 6738 | Slog.e(TAG, "Attmpt to remove content provider: " + localCpr |
| 6739 | + " with no external references."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6740 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6741 | } |
| 6742 | } |
| 6743 | |
| 6744 | public final void publishContentProviders(IApplicationThread caller, |
| 6745 | List<ContentProviderHolder> providers) { |
| 6746 | if (providers == null) { |
| 6747 | return; |
| 6748 | } |
| 6749 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6750 | enforceNotIsolatedCaller("publishContentProviders"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6751 | synchronized (this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6752 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6753 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6754 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6755 | if (r == null) { |
| 6756 | throw new SecurityException( |
| 6757 | "Unable to find app for caller " + caller |
| 6758 | + " (pid=" + Binder.getCallingPid() |
| 6759 | + ") when publishing content providers"); |
| 6760 | } |
| 6761 | |
| 6762 | final long origId = Binder.clearCallingIdentity(); |
| 6763 | |
| 6764 | final int N = providers.size(); |
| 6765 | for (int i=0; i<N; i++) { |
| 6766 | ContentProviderHolder src = providers.get(i); |
| 6767 | if (src == null || src.info == null || src.provider == null) { |
| 6768 | continue; |
| 6769 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6770 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6771 | if (DEBUG_MU) |
| 6772 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6773 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6774 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6775 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6776 | String names[] = dst.info.authority.split(";"); |
| 6777 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6778 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6779 | } |
| 6780 | |
| 6781 | int NL = mLaunchingProviders.size(); |
| 6782 | int j; |
| 6783 | for (j=0; j<NL; j++) { |
| 6784 | if (mLaunchingProviders.get(j) == dst) { |
| 6785 | mLaunchingProviders.remove(j); |
| 6786 | j--; |
| 6787 | NL--; |
| 6788 | } |
| 6789 | } |
| 6790 | synchronized (dst) { |
| 6791 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6792 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6793 | dst.notifyAll(); |
| 6794 | } |
| 6795 | updateOomAdjLocked(r); |
| 6796 | } |
| 6797 | } |
| 6798 | |
| 6799 | Binder.restoreCallingIdentity(origId); |
| 6800 | } |
| 6801 | } |
| 6802 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6803 | public boolean refContentProvider(IBinder connection, int stable, int unstable) { |
| 6804 | ContentProviderConnection conn; |
| 6805 | try { |
| 6806 | conn = (ContentProviderConnection)connection; |
| 6807 | } catch (ClassCastException e) { |
| 6808 | String msg ="refContentProvider: " + connection |
| 6809 | + " not a ContentProviderConnection"; |
| 6810 | Slog.w(TAG, msg); |
| 6811 | throw new IllegalArgumentException(msg); |
| 6812 | } |
| 6813 | if (conn == null) { |
| 6814 | throw new NullPointerException("connection is null"); |
| 6815 | } |
| 6816 | |
| 6817 | synchronized (this) { |
| 6818 | if (stable > 0) { |
| 6819 | conn.numStableIncs += stable; |
| 6820 | } |
| 6821 | stable = conn.stableCount + stable; |
| 6822 | if (stable < 0) { |
| 6823 | throw new IllegalStateException("stableCount < 0: " + stable); |
| 6824 | } |
| 6825 | |
| 6826 | if (unstable > 0) { |
| 6827 | conn.numUnstableIncs += unstable; |
| 6828 | } |
| 6829 | unstable = conn.unstableCount + unstable; |
| 6830 | if (unstable < 0) { |
| 6831 | throw new IllegalStateException("unstableCount < 0: " + unstable); |
| 6832 | } |
| 6833 | |
| 6834 | if ((stable+unstable) <= 0) { |
| 6835 | throw new IllegalStateException("ref counts can't go to zero here: stable=" |
| 6836 | + stable + " unstable=" + unstable); |
| 6837 | } |
| 6838 | conn.stableCount = stable; |
| 6839 | conn.unstableCount = unstable; |
| 6840 | return !conn.dead; |
| 6841 | } |
| 6842 | } |
| 6843 | |
| 6844 | public void unstableProviderDied(IBinder connection) { |
| 6845 | ContentProviderConnection conn; |
| 6846 | try { |
| 6847 | conn = (ContentProviderConnection)connection; |
| 6848 | } catch (ClassCastException e) { |
| 6849 | String msg ="refContentProvider: " + connection |
| 6850 | + " not a ContentProviderConnection"; |
| 6851 | Slog.w(TAG, msg); |
| 6852 | throw new IllegalArgumentException(msg); |
| 6853 | } |
| 6854 | if (conn == null) { |
| 6855 | throw new NullPointerException("connection is null"); |
| 6856 | } |
| 6857 | |
| 6858 | // Safely retrieve the content provider associated with the connection. |
| 6859 | IContentProvider provider; |
| 6860 | synchronized (this) { |
| 6861 | provider = conn.provider.provider; |
| 6862 | } |
| 6863 | |
| 6864 | if (provider == null) { |
| 6865 | // Um, yeah, we're way ahead of you. |
| 6866 | return; |
| 6867 | } |
| 6868 | |
| 6869 | // Make sure the caller is being honest with us. |
| 6870 | if (provider.asBinder().pingBinder()) { |
| 6871 | // Er, no, still looks good to us. |
| 6872 | synchronized (this) { |
| 6873 | Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid() |
| 6874 | + " says " + conn + " died, but we don't agree"); |
| 6875 | return; |
| 6876 | } |
| 6877 | } |
| 6878 | |
| 6879 | // Well look at that! It's dead! |
| 6880 | synchronized (this) { |
| 6881 | if (conn.provider.provider != provider) { |
| 6882 | // But something changed... good enough. |
| 6883 | return; |
| 6884 | } |
| 6885 | |
| 6886 | ProcessRecord proc = conn.provider.proc; |
| 6887 | if (proc == null || proc.thread == null) { |
| 6888 | // Seems like the process is already cleaned up. |
| 6889 | return; |
| 6890 | } |
| 6891 | |
| 6892 | // As far as we're concerned, this is just like receiving a |
| 6893 | // death notification... just a bit prematurely. |
| 6894 | Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid |
| 6895 | + ") early provider death"); |
Dianne Hackborn | bd145db | 2012-06-05 16:20:46 -0700 | [diff] [blame] | 6896 | final long ident = Binder.clearCallingIdentity(); |
| 6897 | try { |
| 6898 | appDiedLocked(proc, proc.pid, proc.thread); |
| 6899 | } finally { |
| 6900 | Binder.restoreCallingIdentity(ident); |
| 6901 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6902 | } |
| 6903 | } |
| 6904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6905 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6906 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6907 | synchronized (mSelf) { |
| 6908 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6909 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6910 | if (providers != null) { |
| 6911 | for (int i=providers.size()-1; i>=0; i--) { |
| 6912 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6913 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6914 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6915 | + ": not system .apk"); |
| 6916 | providers.remove(i); |
| 6917 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6918 | } |
| 6919 | } |
| 6920 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6921 | if (providers != null) { |
| 6922 | mSystemThread.installSystemProviders(providers); |
| 6923 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6924 | |
| 6925 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6926 | |
| 6927 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6928 | } |
| 6929 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6930 | /** |
| 6931 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6932 | * to access its content provider. |
| 6933 | * |
| 6934 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6935 | * |
| 6936 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6937 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6938 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6939 | public String getProviderMimeType(Uri uri, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6940 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6941 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6942 | userId, false, true, "getProviderMimeType", null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6943 | final String name = uri.getAuthority(); |
| 6944 | final long ident = Binder.clearCallingIdentity(); |
| 6945 | ContentProviderHolder holder = null; |
| 6946 | |
| 6947 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6948 | holder = getContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6949 | if (holder != null) { |
| 6950 | return holder.provider.getType(uri); |
| 6951 | } |
| 6952 | } catch (RemoteException e) { |
| 6953 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6954 | return null; |
| 6955 | } finally { |
| 6956 | if (holder != null) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6957 | removeContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6958 | } |
| 6959 | Binder.restoreCallingIdentity(ident); |
| 6960 | } |
| 6961 | |
| 6962 | return null; |
| 6963 | } |
| 6964 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6965 | // ========================================================= |
| 6966 | // GLOBAL MANAGEMENT |
| 6967 | // ========================================================= |
| 6968 | |
| 6969 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6970 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6971 | String proc = customProcess != null ? customProcess : info.processName; |
| 6972 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6973 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6974 | int uid = info.uid; |
| 6975 | if (isolated) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6976 | int userId = UserHandle.getUserId(uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6977 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 6978 | uid = 0; |
| 6979 | while (true) { |
| 6980 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 6981 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 6982 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 6983 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6984 | uid = UserHandle.getUid(userId, mNextIsolatedProcessUid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6985 | mNextIsolatedProcessUid++; |
| 6986 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 6987 | // No process for this uid, use it. |
| 6988 | break; |
| 6989 | } |
| 6990 | stepsLeft--; |
| 6991 | if (stepsLeft <= 0) { |
| 6992 | return null; |
| 6993 | } |
| 6994 | } |
| 6995 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6996 | synchronized (stats) { |
| 6997 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6998 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6999 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7000 | } |
| 7001 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7002 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 7003 | ProcessRecord app; |
| 7004 | if (!isolated) { |
| 7005 | app = getProcessRecordLocked(info.processName, info.uid); |
| 7006 | } else { |
| 7007 | app = null; |
| 7008 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7009 | |
| 7010 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7011 | app = newProcessRecordLocked(null, info, null, isolated); |
| 7012 | mProcessNames.put(info.processName, app.uid, app); |
| 7013 | if (isolated) { |
| 7014 | mIsolatedProcesses.put(app.uid, app); |
| 7015 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7016 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7017 | } |
| 7018 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7019 | // This package really, really can not be stopped. |
| 7020 | try { |
| 7021 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7022 | info.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7023 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 7024 | } catch (IllegalArgumentException e) { |
| 7025 | Slog.w(TAG, "Failed trying to unstop package " |
| 7026 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7027 | } |
| 7028 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7029 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 7030 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 7031 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7032 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7033 | } |
| 7034 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 7035 | mPersistentStartingProcesses.add(app); |
| 7036 | startProcessLocked(app, "added application", app.processName); |
| 7037 | } |
| 7038 | |
| 7039 | return app; |
| 7040 | } |
| 7041 | |
| 7042 | public void unhandledBack() { |
| 7043 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 7044 | "unhandledBack()"); |
| 7045 | |
| 7046 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7047 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7048 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7049 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 7050 | if (count > 1) { |
| 7051 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7052 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 7053 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7054 | Binder.restoreCallingIdentity(origId); |
| 7055 | } |
| 7056 | } |
| 7057 | } |
| 7058 | |
| 7059 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7060 | enforceNotIsolatedCaller("openContentUri"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7061 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7062 | String name = uri.getAuthority(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7063 | ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7064 | ParcelFileDescriptor pfd = null; |
| 7065 | if (cph != null) { |
| 7066 | // We record the binder invoker's uid in thread-local storage before |
| 7067 | // going to the content provider to open the file. Later, in the code |
| 7068 | // that handles all permissions checks, we look for this uid and use |
| 7069 | // that rather than the Activity Manager's own uid. The effect is that |
| 7070 | // we do the check against the caller's permissions even though it looks |
| 7071 | // to the content provider like the Activity Manager itself is making |
| 7072 | // the request. |
| 7073 | sCallerIdentity.set(new Identity( |
| 7074 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 7075 | try { |
| 7076 | pfd = cph.provider.openFile(uri, "r"); |
| 7077 | } catch (FileNotFoundException e) { |
| 7078 | // do nothing; pfd will be returned null |
| 7079 | } finally { |
| 7080 | // Ensure that whatever happens, we clean up the identity state |
| 7081 | sCallerIdentity.remove(); |
| 7082 | } |
| 7083 | |
| 7084 | // 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] | 7085 | removeContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7086 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7087 | 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] | 7088 | } |
| 7089 | return pfd; |
| 7090 | } |
| 7091 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7092 | // Actually is sleeping or shutting down or whatever else in the future |
| 7093 | // is an inactive state. |
| 7094 | public boolean isSleeping() { |
| 7095 | return mSleeping || mShuttingDown; |
| 7096 | } |
| 7097 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7098 | public void goingToSleep() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7099 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7100 | != PackageManager.PERMISSION_GRANTED) { |
| 7101 | throw new SecurityException("Requires permission " |
| 7102 | + android.Manifest.permission.DEVICE_POWER); |
| 7103 | } |
| 7104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7105 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7106 | mWentToSleep = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7107 | updateEventDispatchingLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7108 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7109 | if (!mSleeping) { |
| 7110 | mSleeping = true; |
| 7111 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 7112 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7113 | // Initialize the wake times of all processes. |
| 7114 | checkExcessivePowerUsageLocked(false); |
| 7115 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 7116 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 7117 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
| 7118 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7119 | } |
| 7120 | } |
| 7121 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7122 | public boolean shutdown(int timeout) { |
| 7123 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 7124 | != PackageManager.PERMISSION_GRANTED) { |
| 7125 | throw new SecurityException("Requires permission " |
| 7126 | + android.Manifest.permission.SHUTDOWN); |
| 7127 | } |
| 7128 | |
| 7129 | boolean timedout = false; |
| 7130 | |
| 7131 | synchronized(this) { |
| 7132 | mShuttingDown = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7133 | updateEventDispatchingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7134 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7135 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7136 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7137 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7138 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 7139 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7140 | long delay = endTime - System.currentTimeMillis(); |
| 7141 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7142 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7143 | timedout = true; |
| 7144 | break; |
| 7145 | } |
| 7146 | try { |
| 7147 | this.wait(); |
| 7148 | } catch (InterruptedException e) { |
| 7149 | } |
| 7150 | } |
| 7151 | } |
| 7152 | } |
| 7153 | |
| 7154 | mUsageStatsService.shutdown(); |
| 7155 | mBatteryStatsService.shutdown(); |
| 7156 | |
| 7157 | return timedout; |
| 7158 | } |
| 7159 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7160 | public final void activitySlept(IBinder token) { |
| 7161 | if (localLOGV) Slog.v( |
| 7162 | TAG, "Activity slept: token=" + token); |
| 7163 | |
| 7164 | ActivityRecord r = null; |
| 7165 | |
| 7166 | final long origId = Binder.clearCallingIdentity(); |
| 7167 | |
| 7168 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7169 | r = mMainStack.isInStackLocked(token); |
| 7170 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7171 | mMainStack.activitySleptLocked(r); |
| 7172 | } |
| 7173 | } |
| 7174 | |
| 7175 | Binder.restoreCallingIdentity(origId); |
| 7176 | } |
| 7177 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7178 | private void comeOutOfSleepIfNeededLocked() { |
| 7179 | if (!mWentToSleep && !mLockScreenShown) { |
| 7180 | if (mSleeping) { |
| 7181 | mSleeping = false; |
| 7182 | mMainStack.awakeFromSleepingLocked(); |
| 7183 | mMainStack.resumeTopActivityLocked(null); |
| 7184 | } |
| 7185 | } |
| 7186 | } |
| 7187 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7188 | public void wakingUp() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7189 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7190 | != PackageManager.PERMISSION_GRANTED) { |
| 7191 | throw new SecurityException("Requires permission " |
| 7192 | + android.Manifest.permission.DEVICE_POWER); |
| 7193 | } |
| 7194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7195 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7196 | mWentToSleep = false; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7197 | updateEventDispatchingLocked(); |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7198 | comeOutOfSleepIfNeededLocked(); |
| 7199 | } |
| 7200 | } |
| 7201 | |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7202 | private void updateEventDispatchingLocked() { |
| 7203 | mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown); |
| 7204 | } |
| 7205 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7206 | public void setLockScreenShown(boolean shown) { |
| 7207 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7208 | != PackageManager.PERMISSION_GRANTED) { |
| 7209 | throw new SecurityException("Requires permission " |
| 7210 | + android.Manifest.permission.DEVICE_POWER); |
| 7211 | } |
| 7212 | |
| 7213 | synchronized(this) { |
| 7214 | mLockScreenShown = shown; |
| 7215 | comeOutOfSleepIfNeededLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7216 | } |
| 7217 | } |
| 7218 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7219 | public void stopAppSwitches() { |
| 7220 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7221 | != PackageManager.PERMISSION_GRANTED) { |
| 7222 | throw new SecurityException("Requires permission " |
| 7223 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7224 | } |
| 7225 | |
| 7226 | synchronized(this) { |
| 7227 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 7228 | + APP_SWITCH_DELAY_TIME; |
| 7229 | mDidAppSwitch = false; |
| 7230 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7231 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7232 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 7233 | } |
| 7234 | } |
| 7235 | |
| 7236 | public void resumeAppSwitches() { |
| 7237 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7238 | != PackageManager.PERMISSION_GRANTED) { |
| 7239 | throw new SecurityException("Requires permission " |
| 7240 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7241 | } |
| 7242 | |
| 7243 | synchronized(this) { |
| 7244 | // Note that we don't execute any pending app switches... we will |
| 7245 | // let those wait until either the timeout, or the next start |
| 7246 | // activity request. |
| 7247 | mAppSwitchesAllowedTime = 0; |
| 7248 | } |
| 7249 | } |
| 7250 | |
| 7251 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 7252 | String name) { |
| 7253 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 7254 | return true; |
| 7255 | } |
| 7256 | |
| 7257 | final int perm = checkComponentPermission( |
| 7258 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 7259 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7260 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 7261 | return true; |
| 7262 | } |
| 7263 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7264 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7265 | return false; |
| 7266 | } |
| 7267 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7268 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 7269 | boolean persistent) { |
| 7270 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 7271 | "setDebugApp()"); |
| 7272 | |
| 7273 | // Note that this is not really thread safe if there are multiple |
| 7274 | // callers into it at the same time, but that's not a situation we |
| 7275 | // care about. |
| 7276 | if (persistent) { |
| 7277 | final ContentResolver resolver = mContext.getContentResolver(); |
| 7278 | Settings.System.putString( |
| 7279 | resolver, Settings.System.DEBUG_APP, |
| 7280 | packageName); |
| 7281 | Settings.System.putInt( |
| 7282 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 7283 | waitForDebugger ? 1 : 0); |
| 7284 | } |
| 7285 | |
| 7286 | synchronized (this) { |
| 7287 | if (!persistent) { |
| 7288 | mOrigDebugApp = mDebugApp; |
| 7289 | mOrigWaitForDebugger = mWaitForDebugger; |
| 7290 | } |
| 7291 | mDebugApp = packageName; |
| 7292 | mWaitForDebugger = waitForDebugger; |
| 7293 | mDebugTransient = !persistent; |
| 7294 | if (packageName != null) { |
| 7295 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 7296 | forceStopPackageLocked(packageName, -1, false, false, true, true, |
| 7297 | UserHandle.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7298 | Binder.restoreCallingIdentity(origId); |
| 7299 | } |
| 7300 | } |
| 7301 | } |
| 7302 | |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 7303 | void setOpenGlTraceApp(ApplicationInfo app, String processName) { |
| 7304 | synchronized (this) { |
| 7305 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7306 | if (!isDebuggable) { |
| 7307 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7308 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7309 | } |
| 7310 | } |
| 7311 | |
| 7312 | mOpenGlTraceApp = processName; |
| 7313 | } |
| 7314 | } |
| 7315 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 7316 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 7317 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 7318 | synchronized (this) { |
| 7319 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7320 | if (!isDebuggable) { |
| 7321 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7322 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7323 | } |
| 7324 | } |
| 7325 | mProfileApp = processName; |
| 7326 | mProfileFile = profileFile; |
| 7327 | if (mProfileFd != null) { |
| 7328 | try { |
| 7329 | mProfileFd.close(); |
| 7330 | } catch (IOException e) { |
| 7331 | } |
| 7332 | mProfileFd = null; |
| 7333 | } |
| 7334 | mProfileFd = profileFd; |
| 7335 | mProfileType = 0; |
| 7336 | mAutoStopProfiler = autoStopProfiler; |
| 7337 | } |
| 7338 | } |
| 7339 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7340 | public void setAlwaysFinish(boolean enabled) { |
| 7341 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 7342 | "setAlwaysFinish()"); |
| 7343 | |
| 7344 | Settings.System.putInt( |
| 7345 | mContext.getContentResolver(), |
| 7346 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 7347 | |
| 7348 | synchronized (this) { |
| 7349 | mAlwaysFinishActivities = enabled; |
| 7350 | } |
| 7351 | } |
| 7352 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7353 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7354 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7355 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7356 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7357 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7358 | } |
| 7359 | } |
| 7360 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 7361 | public boolean isUserAMonkey() { |
| 7362 | // For now the fact that there is a controller implies |
| 7363 | // we have a monkey. |
| 7364 | synchronized (this) { |
| 7365 | return mController != null; |
| 7366 | } |
| 7367 | } |
| 7368 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7369 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7370 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 7371 | "registerProcessObserver()"); |
| 7372 | synchronized (this) { |
| 7373 | mProcessObservers.register(observer); |
| 7374 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7375 | } |
| 7376 | |
| 7377 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7378 | synchronized (this) { |
| 7379 | mProcessObservers.unregister(observer); |
| 7380 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7381 | } |
| 7382 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7383 | public void setImmersive(IBinder token, boolean immersive) { |
| 7384 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7385 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7386 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7387 | throw new IllegalArgumentException(); |
| 7388 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7389 | r.immersive = immersive; |
| 7390 | } |
| 7391 | } |
| 7392 | |
| 7393 | public boolean isImmersive(IBinder token) { |
| 7394 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7395 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7396 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7397 | throw new IllegalArgumentException(); |
| 7398 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7399 | return r.immersive; |
| 7400 | } |
| 7401 | } |
| 7402 | |
| 7403 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7404 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7405 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7406 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7407 | return (r != null) ? r.immersive : false; |
| 7408 | } |
| 7409 | } |
| 7410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7411 | public final void enterSafeMode() { |
| 7412 | synchronized(this) { |
| 7413 | // It only makes sense to do this before the system is ready |
| 7414 | // and started launching other packages. |
| 7415 | if (!mSystemReady) { |
| 7416 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7417 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7418 | } catch (RemoteException e) { |
| 7419 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7420 | } |
| 7421 | } |
| 7422 | } |
| 7423 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7424 | public final void showSafeModeOverlay() { |
| 7425 | View v = LayoutInflater.from(mContext).inflate( |
| 7426 | com.android.internal.R.layout.safe_mode, null); |
| 7427 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 7428 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 7429 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 7430 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 7431 | lp.gravity = Gravity.BOTTOM | Gravity.START; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7432 | lp.format = v.getBackground().getOpacity(); |
| 7433 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 7434 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 7435 | ((WindowManager)mContext.getSystemService( |
| 7436 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 7437 | } |
| 7438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7439 | public void noteWakeupAlarm(IIntentSender sender) { |
| 7440 | if (!(sender instanceof PendingIntentRecord)) { |
| 7441 | return; |
| 7442 | } |
| 7443 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 7444 | synchronized (stats) { |
| 7445 | if (mBatteryStatsService.isOnBattery()) { |
| 7446 | mBatteryStatsService.enforceCallingPermission(); |
| 7447 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 7448 | int MY_UID = Binder.getCallingUid(); |
| 7449 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 7450 | BatteryStatsImpl.Uid.Pkg pkg = |
| 7451 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 7452 | pkg.incWakeupsLocked(); |
| 7453 | } |
| 7454 | } |
| 7455 | } |
| 7456 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7457 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7458 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7459 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7460 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7461 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7462 | // XXX Note: don't acquire main activity lock here, because the window |
| 7463 | // manager calls in with its locks held. |
| 7464 | |
| 7465 | boolean killed = false; |
| 7466 | synchronized (mPidsSelfLocked) { |
| 7467 | int[] types = new int[pids.length]; |
| 7468 | int worstType = 0; |
| 7469 | for (int i=0; i<pids.length; i++) { |
| 7470 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7471 | if (proc != null) { |
| 7472 | int type = proc.setAdj; |
| 7473 | types[i] = type; |
| 7474 | if (type > worstType) { |
| 7475 | worstType = type; |
| 7476 | } |
| 7477 | } |
| 7478 | } |
| 7479 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7480 | // 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] | 7481 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7482 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 7483 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7484 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7485 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7486 | |
| 7487 | // If this is not a secure call, don't let it kill processes that |
| 7488 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7489 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 7490 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7491 | } |
| 7492 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7493 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7494 | for (int i=0; i<pids.length; i++) { |
| 7495 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7496 | if (proc == null) { |
| 7497 | continue; |
| 7498 | } |
| 7499 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7500 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7501 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7502 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 7503 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7504 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7505 | proc.killedBackground = true; |
| 7506 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7507 | } |
| 7508 | } |
| 7509 | } |
| 7510 | return killed; |
| 7511 | } |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7512 | |
| 7513 | @Override |
| 7514 | public boolean killProcessesBelowForeground(String reason) { |
| 7515 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7516 | throw new SecurityException("killProcessesBelowForeground() only available to system"); |
| 7517 | } |
| 7518 | |
| 7519 | return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason); |
| 7520 | } |
| 7521 | |
| 7522 | private boolean killProcessesBelowAdj(int belowAdj, String reason) { |
| 7523 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7524 | throw new SecurityException("killProcessesBelowAdj() only available to system"); |
| 7525 | } |
| 7526 | |
| 7527 | boolean killed = false; |
| 7528 | synchronized (mPidsSelfLocked) { |
| 7529 | final int size = mPidsSelfLocked.size(); |
| 7530 | for (int i = 0; i < size; i++) { |
| 7531 | final int pid = mPidsSelfLocked.keyAt(i); |
| 7532 | final ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7533 | if (proc == null) continue; |
| 7534 | |
| 7535 | final int adj = proc.setAdj; |
| 7536 | if (adj > belowAdj && !proc.killedBackground) { |
| 7537 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
| 7538 | EventLog.writeEvent( |
| 7539 | EventLogTags.AM_KILL, proc.pid, proc.processName, adj, reason); |
| 7540 | killed = true; |
| 7541 | proc.killedBackground = true; |
| 7542 | Process.killProcessQuiet(pid); |
| 7543 | } |
| 7544 | } |
| 7545 | } |
| 7546 | return killed; |
| 7547 | } |
| 7548 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7549 | public final void startRunning(String pkg, String cls, String action, |
| 7550 | String data) { |
| 7551 | synchronized(this) { |
| 7552 | if (mStartRunning) { |
| 7553 | return; |
| 7554 | } |
| 7555 | mStartRunning = true; |
| 7556 | mTopComponent = pkg != null && cls != null |
| 7557 | ? new ComponentName(pkg, cls) : null; |
| 7558 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 7559 | mTopData = data; |
| 7560 | if (!mSystemReady) { |
| 7561 | return; |
| 7562 | } |
| 7563 | } |
| 7564 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7565 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7566 | } |
| 7567 | |
| 7568 | private void retrieveSettings() { |
| 7569 | final ContentResolver resolver = mContext.getContentResolver(); |
| 7570 | String debugApp = Settings.System.getString( |
| 7571 | resolver, Settings.System.DEBUG_APP); |
| 7572 | boolean waitForDebugger = Settings.System.getInt( |
| 7573 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 7574 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 7575 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 7576 | |
| 7577 | Configuration configuration = new Configuration(); |
| 7578 | Settings.System.getConfiguration(resolver, configuration); |
| 7579 | |
| 7580 | synchronized (this) { |
| 7581 | mDebugApp = mOrigDebugApp = debugApp; |
| 7582 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 7583 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 7584 | // This happens before any activities are started, so we can |
| 7585 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 7586 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7587 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7588 | } |
| 7589 | } |
| 7590 | |
| 7591 | public boolean testIsSystemReady() { |
| 7592 | // no need to synchronize(this) just to read & return the value |
| 7593 | return mSystemReady; |
| 7594 | } |
| 7595 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7596 | private static File getCalledPreBootReceiversFile() { |
| 7597 | File dataDir = Environment.getDataDirectory(); |
| 7598 | File systemDir = new File(dataDir, "system"); |
| 7599 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 7600 | return fname; |
| 7601 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7602 | |
| 7603 | static final int LAST_DONE_VERSION = 10000; |
| 7604 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7605 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 7606 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 7607 | File file = getCalledPreBootReceiversFile(); |
| 7608 | FileInputStream fis = null; |
| 7609 | try { |
| 7610 | fis = new FileInputStream(file); |
| 7611 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7612 | int fvers = dis.readInt(); |
| 7613 | if (fvers == LAST_DONE_VERSION) { |
| 7614 | String vers = dis.readUTF(); |
| 7615 | String codename = dis.readUTF(); |
| 7616 | String build = dis.readUTF(); |
| 7617 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 7618 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 7619 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 7620 | int num = dis.readInt(); |
| 7621 | while (num > 0) { |
| 7622 | num--; |
| 7623 | String pkg = dis.readUTF(); |
| 7624 | String cls = dis.readUTF(); |
| 7625 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 7626 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7627 | } |
| 7628 | } |
| 7629 | } catch (FileNotFoundException e) { |
| 7630 | } catch (IOException e) { |
| 7631 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 7632 | } finally { |
| 7633 | if (fis != null) { |
| 7634 | try { |
| 7635 | fis.close(); |
| 7636 | } catch (IOException e) { |
| 7637 | } |
| 7638 | } |
| 7639 | } |
| 7640 | return lastDoneReceivers; |
| 7641 | } |
| 7642 | |
| 7643 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 7644 | File file = getCalledPreBootReceiversFile(); |
| 7645 | FileOutputStream fos = null; |
| 7646 | DataOutputStream dos = null; |
| 7647 | try { |
| 7648 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 7649 | fos = new FileOutputStream(file); |
| 7650 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7651 | dos.writeInt(LAST_DONE_VERSION); |
| 7652 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7653 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7654 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7655 | dos.writeInt(list.size()); |
| 7656 | for (int i=0; i<list.size(); i++) { |
| 7657 | dos.writeUTF(list.get(i).getPackageName()); |
| 7658 | dos.writeUTF(list.get(i).getClassName()); |
| 7659 | } |
| 7660 | } catch (IOException e) { |
| 7661 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 7662 | file.delete(); |
| 7663 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 7664 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7665 | if (dos != null) { |
| 7666 | try { |
| 7667 | dos.close(); |
| 7668 | } catch (IOException e) { |
| 7669 | // TODO Auto-generated catch block |
| 7670 | e.printStackTrace(); |
| 7671 | } |
| 7672 | } |
| 7673 | } |
| 7674 | } |
| 7675 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7676 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7677 | synchronized(this) { |
| 7678 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7679 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7680 | return; |
| 7681 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7682 | |
| 7683 | // Check to see if there are any update receivers to run. |
| 7684 | if (!mDidUpdate) { |
| 7685 | if (mWaitingUpdate) { |
| 7686 | return; |
| 7687 | } |
| 7688 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 7689 | List<ResolveInfo> ris = null; |
| 7690 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7691 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7692 | intent, null, 0, 0); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7693 | } catch (RemoteException e) { |
| 7694 | } |
| 7695 | if (ris != null) { |
| 7696 | for (int i=ris.size()-1; i>=0; i--) { |
| 7697 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 7698 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7699 | ris.remove(i); |
| 7700 | } |
| 7701 | } |
| 7702 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7703 | |
| 7704 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 7705 | |
| 7706 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7707 | for (int i=0; i<ris.size(); i++) { |
| 7708 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7709 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7710 | if (lastDoneReceivers.contains(comp)) { |
| 7711 | ris.remove(i); |
| 7712 | i--; |
| 7713 | } |
| 7714 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 7715 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7716 | final int[] users = getUsersLocked(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7717 | for (int i=0; i<ris.size(); i++) { |
| 7718 | ActivityInfo ai = ris.get(i).activityInfo; |
| 7719 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7720 | doneReceivers.add(comp); |
| 7721 | intent.setComponent(comp); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7722 | for (int j=0; j<users.length; j++) { |
| 7723 | IIntentReceiver finisher = null; |
| 7724 | if (i == ris.size()-1 && j == users.length-1) { |
| 7725 | finisher = new IIntentReceiver.Stub() { |
| 7726 | public void performReceive(Intent intent, int resultCode, |
| 7727 | String data, Bundle extras, boolean ordered, |
| 7728 | boolean sticky, int sendingUser) { |
| 7729 | // The raw IIntentReceiver interface is called |
| 7730 | // with the AM lock held, so redispatch to |
| 7731 | // execute our code without the lock. |
| 7732 | mHandler.post(new Runnable() { |
| 7733 | public void run() { |
| 7734 | synchronized (ActivityManagerService.this) { |
| 7735 | mDidUpdate = true; |
| 7736 | } |
| 7737 | writeLastDonePreBootReceivers(doneReceivers); |
| 7738 | showBootMessage(mContext.getText( |
| 7739 | R.string.android_upgrading_complete), |
| 7740 | false); |
| 7741 | systemReady(goingCallback); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7742 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7743 | }); |
| 7744 | } |
| 7745 | }; |
| 7746 | } |
| 7747 | Slog.i(TAG, "Sending system update to " + intent.getComponent() |
| 7748 | + " for user " + users[j]); |
| 7749 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 7750 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 7751 | users[j]); |
| 7752 | if (finisher != null) { |
| 7753 | mWaitingUpdate = true; |
| 7754 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7755 | } |
| 7756 | } |
| 7757 | } |
| 7758 | if (mWaitingUpdate) { |
| 7759 | return; |
| 7760 | } |
| 7761 | mDidUpdate = true; |
| 7762 | } |
| 7763 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7764 | mSystemReady = true; |
| 7765 | if (!mStartRunning) { |
| 7766 | return; |
| 7767 | } |
| 7768 | } |
| 7769 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7770 | ArrayList<ProcessRecord> procsToKill = null; |
| 7771 | synchronized(mPidsSelfLocked) { |
| 7772 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7773 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7774 | if (!isAllowedWhileBooting(proc.info)){ |
| 7775 | if (procsToKill == null) { |
| 7776 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7777 | } |
| 7778 | procsToKill.add(proc); |
| 7779 | } |
| 7780 | } |
| 7781 | } |
| 7782 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7783 | synchronized(this) { |
| 7784 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7785 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7786 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7787 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7788 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7789 | } |
| 7790 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7791 | |
| 7792 | // Now that we have cleaned up any update processes, we |
| 7793 | // are ready to start launching real processes and know that |
| 7794 | // we won't trample on them any more. |
| 7795 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7796 | } |
| 7797 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7798 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7799 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7800 | SystemClock.uptimeMillis()); |
| 7801 | |
| 7802 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7803 | // Make sure we have no pre-ready processes sitting around. |
| 7804 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7805 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7806 | ResolveInfo ri = mContext.getPackageManager() |
| 7807 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7808 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7809 | CharSequence errorMsg = null; |
| 7810 | if (ri != null) { |
| 7811 | ActivityInfo ai = ri.activityInfo; |
| 7812 | ApplicationInfo app = ai.applicationInfo; |
| 7813 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7814 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7815 | mTopData = null; |
| 7816 | mTopComponent = new ComponentName(app.packageName, |
| 7817 | ai.name); |
| 7818 | } else { |
| 7819 | errorMsg = mContext.getResources().getText( |
| 7820 | com.android.internal.R.string.factorytest_not_system); |
| 7821 | } |
| 7822 | } else { |
| 7823 | errorMsg = mContext.getResources().getText( |
| 7824 | com.android.internal.R.string.factorytest_no_action); |
| 7825 | } |
| 7826 | if (errorMsg != null) { |
| 7827 | mTopAction = null; |
| 7828 | mTopData = null; |
| 7829 | mTopComponent = null; |
| 7830 | Message msg = Message.obtain(); |
| 7831 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7832 | msg.getData().putCharSequence("msg", errorMsg); |
| 7833 | mHandler.sendMessage(msg); |
| 7834 | } |
| 7835 | } |
| 7836 | } |
| 7837 | |
| 7838 | retrieveSettings(); |
| 7839 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7840 | if (goingCallback != null) goingCallback.run(); |
| 7841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7842 | synchronized (this) { |
| 7843 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7844 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7845 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7846 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7847 | if (apps != null) { |
| 7848 | int N = apps.size(); |
| 7849 | int i; |
| 7850 | for (i=0; i<N; i++) { |
| 7851 | ApplicationInfo info |
| 7852 | = (ApplicationInfo)apps.get(i); |
| 7853 | if (info != null && |
| 7854 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7855 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7856 | } |
| 7857 | } |
| 7858 | } |
| 7859 | } catch (RemoteException ex) { |
| 7860 | // pm is in same process, this will never happen. |
| 7861 | } |
| 7862 | } |
| 7863 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7864 | // Start up initial activity. |
| 7865 | mBooting = true; |
| 7866 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7867 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7868 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7869 | Message msg = Message.obtain(); |
| 7870 | msg.what = SHOW_UID_ERROR_MSG; |
| 7871 | mHandler.sendMessage(msg); |
| 7872 | } |
| 7873 | } catch (RemoteException e) { |
| 7874 | } |
| 7875 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7876 | long ident = Binder.clearCallingIdentity(); |
| 7877 | try { |
| 7878 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
| 7879 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 7880 | intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId); |
| 7881 | broadcastIntentLocked(null, null, intent, |
| 7882 | null, null, 0, null, null, null, |
| 7883 | false, false, MY_PID, Process.SYSTEM_UID, mCurrentUserId); |
| 7884 | } finally { |
| 7885 | Binder.restoreCallingIdentity(ident); |
| 7886 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7887 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7888 | sendUserSwitchBroadcastsLocked(-1, mCurrentUserId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7889 | } |
| 7890 | } |
| 7891 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7892 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7893 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7894 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7895 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7896 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7897 | startAppProblemLocked(app); |
| 7898 | app.stopFreezingAllLocked(); |
| 7899 | return handleAppCrashLocked(app); |
| 7900 | } |
| 7901 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7902 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7903 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7904 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7905 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7906 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7907 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7908 | startAppProblemLocked(app); |
| 7909 | app.stopFreezingAllLocked(); |
| 7910 | } |
| 7911 | |
| 7912 | /** |
| 7913 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7914 | * |
| 7915 | * @param app The ProcessRecord in which the error occurred. |
| 7916 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7917 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7918 | * @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] | 7919 | * @param shortMsg Short message describing the crash. |
| 7920 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7921 | * @param stackTrace Full crash stack trace, may be null. |
| 7922 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7923 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7924 | */ |
| 7925 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7926 | 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] | 7927 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7928 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7929 | report.condition = condition; |
| 7930 | report.processName = app.processName; |
| 7931 | report.pid = app.pid; |
| 7932 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7933 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7934 | report.shortMsg = shortMsg; |
| 7935 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7936 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7937 | |
| 7938 | return report; |
| 7939 | } |
| 7940 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7941 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7942 | synchronized (this) { |
| 7943 | app.crashing = false; |
| 7944 | app.crashingReport = null; |
| 7945 | app.notResponding = false; |
| 7946 | app.notRespondingReport = null; |
| 7947 | if (app.anrDialog == fromDialog) { |
| 7948 | app.anrDialog = null; |
| 7949 | } |
| 7950 | if (app.waitDialog == fromDialog) { |
| 7951 | app.waitDialog = null; |
| 7952 | } |
| 7953 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7954 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7955 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7956 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 7957 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7958 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7959 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7960 | } |
| 7961 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7962 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7963 | private boolean handleAppCrashLocked(ProcessRecord app) { |
Mike Lockwood | 86548c4 | 2011-09-13 17:21:46 -0400 | [diff] [blame] | 7964 | if (mHeadless) { |
| 7965 | Log.e(TAG, "handleAppCrashLocked: " + app.processName); |
| 7966 | return false; |
| 7967 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7968 | long now = SystemClock.uptimeMillis(); |
| 7969 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7970 | Long crashTime; |
| 7971 | if (!app.isolated) { |
| 7972 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 7973 | } else { |
| 7974 | crashTime = null; |
| 7975 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7976 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7977 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7978 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7979 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7980 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7981 | app.info.processName, app.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7982 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 7983 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7984 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7985 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7986 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 7987 | r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
| 7988 | null, "crashed", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7989 | } |
| 7990 | } |
| 7991 | if (!app.persistent) { |
| 7992 | // We don't want to start this process again until the user |
| 7993 | // explicitly does so... but for persistent process, we really |
| 7994 | // need to keep it running. If a persistent process is actually |
| 7995 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7996 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7997 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7998 | if (!app.isolated) { |
| 7999 | // XXX We don't have a way to mark isolated processes |
| 8000 | // as bad, since they don't have a peristent identity. |
| 8001 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 8002 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 8003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8004 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8005 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 8006 | // Don't let services in this process be restarted and potentially |
| 8007 | // annoy the user repeatedly. Unless it is persistent, since those |
| 8008 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 8009 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8010 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8011 | return false; |
| 8012 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8013 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8014 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8015 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Kevin Hester-Chow | d87a9be | 2012-03-05 08:01:00 -0800 | [diff] [blame] | 8016 | if (r != null && r.app == app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8017 | // If the top running activity is from this crashing |
| 8018 | // process, then terminate it to avoid getting in a loop. |
| 8019 | Slog.w(TAG, " Force finishing activity " |
| 8020 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 8021 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8022 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8023 | Activity.RESULT_CANCELED, null, "crashed", false); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 8024 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8025 | // stopped, to avoid a situation where one will get |
| 8026 | // re-start our crashing activity once it gets resumed again. |
| 8027 | index--; |
| 8028 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8029 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8030 | if (r.state == ActivityState.RESUMED |
| 8031 | || r.state == ActivityState.PAUSING |
| 8032 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 8033 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8034 | Slog.w(TAG, " Force finishing activity " |
| 8035 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8036 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8037 | Activity.RESULT_CANCELED, null, "crashed", false); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8038 | } |
| 8039 | } |
| 8040 | } |
| 8041 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8042 | } |
| 8043 | |
| 8044 | // Bump up the crash count of any services currently running in the proc. |
| 8045 | if (app.services.size() != 0) { |
| 8046 | // Any services running in the application need to be placed |
| 8047 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8048 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8049 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8050 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8051 | sr.crashCount++; |
| 8052 | } |
| 8053 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8054 | |
| 8055 | // If the crashing process is what we consider to be the "home process" and it has been |
| 8056 | // replaced by a third-party app, clear the package preferred activities from packages |
| 8057 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 8058 | // from blocking the user to manually clear the list. |
| 8059 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 8060 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 8061 | Iterator it = mHomeProcess.activities.iterator(); |
| 8062 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 8063 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8064 | if (r.isHomeActivity) { |
| 8065 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 8066 | try { |
| 8067 | ActivityThread.getPackageManager() |
| 8068 | .clearPackagePreferredActivities(r.packageName); |
| 8069 | } catch (RemoteException c) { |
| 8070 | // pm is in same process, this will never happen. |
| 8071 | } |
| 8072 | } |
| 8073 | } |
| 8074 | } |
| 8075 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8076 | if (!app.isolated) { |
| 8077 | // XXX Can't keep track of crash times for isolated processes, |
| 8078 | // because they don't have a perisistent identity. |
| 8079 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 8080 | } |
| 8081 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8082 | return true; |
| 8083 | } |
| 8084 | |
| 8085 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 8086 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 8087 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8088 | skipCurrentReceiverLocked(app); |
| 8089 | } |
| 8090 | |
| 8091 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 8092 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 8093 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8094 | } |
| 8095 | } |
| 8096 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8097 | /** |
| 8098 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 8099 | * The application process will exit immediately after this call returns. |
| 8100 | * @param app object of the crashing app, null for the system server |
| 8101 | * @param crashInfo describing the exception |
| 8102 | */ |
| 8103 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8104 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8105 | final String processName = app == null ? "system_server" |
| 8106 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8107 | |
| 8108 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8109 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8110 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8111 | crashInfo.exceptionClassName, |
| 8112 | crashInfo.exceptionMessage, |
| 8113 | crashInfo.throwFileName, |
| 8114 | crashInfo.throwLineNumber); |
| 8115 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8116 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8117 | |
| 8118 | crashApplication(r, crashInfo); |
| 8119 | } |
| 8120 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8121 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8122 | IBinder app, |
| 8123 | int violationMask, |
| 8124 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8125 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 8126 | if (r == null) { |
| 8127 | return; |
| 8128 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8129 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8130 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 8131 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8132 | boolean logIt = true; |
| 8133 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 8134 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 8135 | logIt = false; |
| 8136 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8137 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8138 | // the relative pain numbers, without logging all |
| 8139 | // the stack traces repeatedly. We'd want to do |
| 8140 | // likewise in the client code, which also does |
| 8141 | // dup suppression, before the Binder call. |
| 8142 | } else { |
| 8143 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 8144 | mAlreadyLoggedViolatedStacks.clear(); |
| 8145 | } |
| 8146 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 8147 | } |
| 8148 | } |
| 8149 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8150 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8151 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8152 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8153 | |
| 8154 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 8155 | AppErrorResult result = new AppErrorResult(); |
| 8156 | synchronized (this) { |
| 8157 | final long origId = Binder.clearCallingIdentity(); |
| 8158 | |
| 8159 | Message msg = Message.obtain(); |
| 8160 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 8161 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 8162 | data.put("result", result); |
| 8163 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8164 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8165 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8166 | msg.obj = data; |
| 8167 | mHandler.sendMessage(msg); |
| 8168 | |
| 8169 | Binder.restoreCallingIdentity(origId); |
| 8170 | } |
| 8171 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 8172 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8173 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8174 | } |
| 8175 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8176 | // Depending on the policy in effect, there could be a bunch of |
| 8177 | // these in quick succession so we try to batch these together to |
| 8178 | // minimize disk writes, number of dropbox entries, and maximize |
| 8179 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8180 | private void logStrictModeViolationToDropBox( |
| 8181 | ProcessRecord process, |
| 8182 | StrictMode.ViolationInfo info) { |
| 8183 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8184 | return; |
| 8185 | } |
| 8186 | final boolean isSystemApp = process == null || |
| 8187 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 8188 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8189 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8190 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 8191 | final DropBoxManager dbox = (DropBoxManager) |
| 8192 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8193 | |
| 8194 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8195 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8196 | |
| 8197 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8198 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8199 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 8200 | synchronized (sb) { |
| 8201 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8202 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8203 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 8204 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8205 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 8206 | if (info.violationNumThisLoop != 0) { |
| 8207 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 8208 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 8209 | if (info.numAnimationsRunning != 0) { |
| 8210 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 8211 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 8212 | if (info.broadcastIntentAction != null) { |
| 8213 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 8214 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8215 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8216 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8217 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 8218 | if (info.numInstances != -1) { |
| 8219 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 8220 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8221 | if (info.tags != null) { |
| 8222 | for (String tag : info.tags) { |
| 8223 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 8224 | } |
| 8225 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8226 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8227 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 8228 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8229 | } |
| 8230 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8231 | |
| 8232 | // Only buffer up to ~64k. Various logging bits truncate |
| 8233 | // things at 128k. |
| 8234 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8235 | } |
| 8236 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8237 | // Flush immediately if the buffer's grown too large, or this |
| 8238 | // is a non-system app. Non-system apps are isolated with a |
| 8239 | // different tag & policy and not batched. |
| 8240 | // |
| 8241 | // Batching is useful during internal testing with |
| 8242 | // StrictMode settings turned up high. Without batching, |
| 8243 | // thousands of separate files could be created on boot. |
| 8244 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8245 | new Thread("Error dump: " + dropboxTag) { |
| 8246 | @Override |
| 8247 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8248 | String report; |
| 8249 | synchronized (sb) { |
| 8250 | report = sb.toString(); |
| 8251 | sb.delete(0, sb.length()); |
| 8252 | sb.trimToSize(); |
| 8253 | } |
| 8254 | if (report.length() != 0) { |
| 8255 | dbox.addText(dropboxTag, report); |
| 8256 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8257 | } |
| 8258 | }.start(); |
| 8259 | return; |
| 8260 | } |
| 8261 | |
| 8262 | // System app batching: |
| 8263 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8264 | // An existing dropbox-writing thread is outstanding, so |
| 8265 | // we don't need to start it up. The existing thread will |
| 8266 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8267 | return; |
| 8268 | } |
| 8269 | |
| 8270 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 8271 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8272 | new Thread("Error dump: " + dropboxTag) { |
| 8273 | @Override |
| 8274 | public void run() { |
| 8275 | // 5 second sleep to let stacks arrive and be batched together |
| 8276 | try { |
| 8277 | Thread.sleep(5000); // 5 seconds |
| 8278 | } catch (InterruptedException e) {} |
| 8279 | |
| 8280 | String errorReport; |
| 8281 | synchronized (mStrictModeBuffer) { |
| 8282 | errorReport = mStrictModeBuffer.toString(); |
| 8283 | if (errorReport.length() == 0) { |
| 8284 | return; |
| 8285 | } |
| 8286 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 8287 | mStrictModeBuffer.trimToSize(); |
| 8288 | } |
| 8289 | dbox.addText(dropboxTag, errorReport); |
| 8290 | } |
| 8291 | }.start(); |
| 8292 | } |
| 8293 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8294 | /** |
| 8295 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 8296 | * @param app object of the crashing app, null for the system server |
| 8297 | * @param tag reported by the caller |
| 8298 | * @param crashInfo describing the context of the error |
| 8299 | * @return true if the process should exit immediately (WTF is fatal) |
| 8300 | */ |
| 8301 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8302 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8303 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8304 | final String processName = app == null ? "system_server" |
| 8305 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8306 | |
| 8307 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8308 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8309 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8310 | tag, crashInfo.exceptionMessage); |
| 8311 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8312 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8313 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 8314 | if (r != null && r.pid != Process.myPid() && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame^] | 8315 | Settings.Global.getInt(mContext.getContentResolver(), |
| 8316 | Settings.Global.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8317 | crashApplication(r, crashInfo); |
| 8318 | return true; |
| 8319 | } else { |
| 8320 | return false; |
| 8321 | } |
| 8322 | } |
| 8323 | |
| 8324 | /** |
| 8325 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 8326 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 8327 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8328 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8329 | if (app == null) { |
| 8330 | return null; |
| 8331 | } |
| 8332 | |
| 8333 | synchronized (this) { |
| 8334 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 8335 | final int NA = apps.size(); |
| 8336 | for (int ia=0; ia<NA; ia++) { |
| 8337 | ProcessRecord p = apps.valueAt(ia); |
| 8338 | if (p.thread != null && p.thread.asBinder() == app) { |
| 8339 | return p; |
| 8340 | } |
| 8341 | } |
| 8342 | } |
| 8343 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8344 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 8345 | + " from pid=" + Binder.getCallingPid() |
| 8346 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8347 | return null; |
| 8348 | } |
| 8349 | } |
| 8350 | |
| 8351 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8352 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 8353 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8354 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8355 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 8356 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8357 | // Watchdog thread ends up invoking this function (with |
| 8358 | // a null ProcessRecord) to add the stack file to dropbox. |
| 8359 | // Do not acquire a lock on this (am) in such cases, as it |
| 8360 | // could cause a potential deadlock, if and when watchdog |
| 8361 | // is invoked due to unavailability of lock on am and it |
| 8362 | // would prevent watchdog from killing system_server. |
| 8363 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8364 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8365 | return; |
| 8366 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 8367 | // Note: ProcessRecord 'process' is guarded by the service |
| 8368 | // instance. (notably process.pkgList, which could otherwise change |
| 8369 | // concurrently during execution of this method) |
| 8370 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8371 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8372 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8373 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8374 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 8375 | for (String pkg : process.pkgList) { |
| 8376 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8377 | try { |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 8378 | PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId()); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8379 | if (pi != null) { |
| 8380 | sb.append(" v").append(pi.versionCode); |
| 8381 | if (pi.versionName != null) { |
| 8382 | sb.append(" (").append(pi.versionName).append(")"); |
| 8383 | } |
| 8384 | } |
| 8385 | } catch (RemoteException e) { |
| 8386 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8387 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8388 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8389 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8390 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8391 | } |
| 8392 | |
| 8393 | private static String processClass(ProcessRecord process) { |
| 8394 | if (process == null || process.pid == MY_PID) { |
| 8395 | return "system_server"; |
| 8396 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 8397 | return "system_app"; |
| 8398 | } else { |
| 8399 | return "data_app"; |
| 8400 | } |
| 8401 | } |
| 8402 | |
| 8403 | /** |
| 8404 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 8405 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 8406 | * @param process which caused the error, null means the system server |
| 8407 | * @param activity which triggered the error, null if unknown |
| 8408 | * @param parent activity related to the error, null if unknown |
| 8409 | * @param subject line related to the error, null if absent |
| 8410 | * @param report in long form describing the error, null if absent |
| 8411 | * @param logFile to include in the report, null if none |
| 8412 | * @param crashInfo giving an application stack trace, null if absent |
| 8413 | */ |
| 8414 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8415 | ProcessRecord process, String processName, ActivityRecord activity, |
| 8416 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8417 | final String report, final File logFile, |
| 8418 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 8419 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 8420 | // otherwise the watchdog may be prevented from resetting the system. |
| 8421 | |
| 8422 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 8423 | final DropBoxManager dbox = (DropBoxManager) |
| 8424 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8425 | |
| 8426 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8427 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8428 | |
| 8429 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8430 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8431 | if (activity != null) { |
| 8432 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 8433 | } |
| 8434 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 8435 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 8436 | } |
| 8437 | if (parent != null && parent != activity) { |
| 8438 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 8439 | } |
| 8440 | if (subject != null) { |
| 8441 | sb.append("Subject: ").append(subject).append("\n"); |
| 8442 | } |
| 8443 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 8444 | if (Debug.isDebuggerConnected()) { |
| 8445 | sb.append("Debugger: Connected\n"); |
| 8446 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8447 | sb.append("\n"); |
| 8448 | |
| 8449 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 8450 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8451 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 8452 | @Override |
| 8453 | public void run() { |
| 8454 | if (report != null) { |
| 8455 | sb.append(report); |
| 8456 | } |
| 8457 | if (logFile != null) { |
| 8458 | try { |
| 8459 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 8460 | } catch (IOException e) { |
| 8461 | Slog.e(TAG, "Error reading " + logFile, e); |
| 8462 | } |
| 8463 | } |
| 8464 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 8465 | sb.append(crashInfo.stackTrace); |
| 8466 | } |
| 8467 | |
| 8468 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 8469 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 8470 | if (lines > 0) { |
| 8471 | sb.append("\n"); |
| 8472 | |
| 8473 | // Merge several logcat streams, and take the last N lines |
| 8474 | InputStreamReader input = null; |
| 8475 | try { |
| 8476 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 8477 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 8478 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 8479 | |
| 8480 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 8481 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 8482 | input = new InputStreamReader(logcat.getInputStream()); |
| 8483 | |
| 8484 | int num; |
| 8485 | char[] buf = new char[8192]; |
| 8486 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 8487 | } catch (IOException e) { |
| 8488 | Slog.e(TAG, "Error running logcat", e); |
| 8489 | } finally { |
| 8490 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 8491 | } |
| 8492 | } |
| 8493 | |
| 8494 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8495 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8496 | }; |
| 8497 | |
Dianne Hackborn | 56385cc | 2012-04-30 15:07:47 -0700 | [diff] [blame] | 8498 | if (process == null) { |
| 8499 | // If process is null, we are being called from some internal code |
| 8500 | // and may be about to die -- run this synchronously. |
| 8501 | worker.run(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8502 | } else { |
| 8503 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8504 | } |
| 8505 | } |
| 8506 | |
| 8507 | /** |
| 8508 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 8509 | * Deal with edge cases (intercepts from instrumented applications, |
| 8510 | * ActivityController, error intent receivers, that sort of thing). |
| 8511 | * @param r the application crashing |
| 8512 | * @param crashInfo describing the failure |
| 8513 | */ |
| 8514 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8515 | long timeMillis = System.currentTimeMillis(); |
| 8516 | String shortMsg = crashInfo.exceptionClassName; |
| 8517 | String longMsg = crashInfo.exceptionMessage; |
| 8518 | String stackTrace = crashInfo.stackTrace; |
| 8519 | if (shortMsg != null && longMsg != null) { |
| 8520 | longMsg = shortMsg + ": " + longMsg; |
| 8521 | } else if (shortMsg != null) { |
| 8522 | longMsg = shortMsg; |
| 8523 | } |
| 8524 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8525 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8526 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8527 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8528 | try { |
| 8529 | String name = r != null ? r.processName : null; |
| 8530 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8531 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8532 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8533 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8534 | + " at watcher's request"); |
| 8535 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8536 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8537 | } |
| 8538 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8539 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8540 | } |
| 8541 | } |
| 8542 | |
| 8543 | final long origId = Binder.clearCallingIdentity(); |
| 8544 | |
| 8545 | // If this process is running instrumentation, finish it. |
| 8546 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8547 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8548 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8549 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 8550 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8551 | Bundle info = new Bundle(); |
| 8552 | info.putString("shortMsg", shortMsg); |
| 8553 | info.putString("longMsg", longMsg); |
| 8554 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 8555 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8556 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8557 | } |
| 8558 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8559 | // If we can't identify the process or it's already exceeded its crash quota, |
| 8560 | // quit right away without showing a crash dialog. |
| 8561 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8562 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8563 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8564 | } |
| 8565 | |
| 8566 | Message msg = Message.obtain(); |
| 8567 | msg.what = SHOW_ERROR_MSG; |
| 8568 | HashMap data = new HashMap(); |
| 8569 | data.put("result", result); |
| 8570 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8571 | msg.obj = data; |
| 8572 | mHandler.sendMessage(msg); |
| 8573 | |
| 8574 | Binder.restoreCallingIdentity(origId); |
| 8575 | } |
| 8576 | |
| 8577 | int res = result.get(); |
| 8578 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8579 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8580 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8581 | if (r != null && !r.isolated) { |
| 8582 | // XXX Can't keep track of crash time for isolated processes, |
| 8583 | // since they don't have a persistent identity. |
| 8584 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8585 | SystemClock.uptimeMillis()); |
| 8586 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8587 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8588 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8589 | } |
| 8590 | } |
| 8591 | |
| 8592 | if (appErrorIntent != null) { |
| 8593 | try { |
| 8594 | mContext.startActivity(appErrorIntent); |
| 8595 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8596 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8597 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8598 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8599 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8600 | |
| 8601 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 8602 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 8603 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8604 | if (report == null) { |
| 8605 | return null; |
| 8606 | } |
| 8607 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 8608 | result.setComponent(r.errorReportReceiver); |
| 8609 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 8610 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 8611 | return result; |
| 8612 | } |
| 8613 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8614 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 8615 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8616 | if (r.errorReportReceiver == null) { |
| 8617 | return null; |
| 8618 | } |
| 8619 | |
| 8620 | if (!r.crashing && !r.notResponding) { |
| 8621 | return null; |
| 8622 | } |
| 8623 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8624 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 8625 | report.packageName = r.info.packageName; |
| 8626 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 8627 | report.processName = r.processName; |
| 8628 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 8629 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8630 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8631 | if (r.crashing) { |
| 8632 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 8633 | report.crashInfo = crashInfo; |
| 8634 | } else if (r.notResponding) { |
| 8635 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 8636 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8637 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8638 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 8639 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 8640 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8641 | } |
| 8642 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8643 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8644 | } |
| 8645 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8646 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8647 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8648 | // assume our apps are happy - lazy create the list |
| 8649 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 8650 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8651 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8652 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8653 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8654 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 8655 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8656 | synchronized (this) { |
| 8657 | |
| 8658 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8659 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8660 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8661 | if (!allUsers && app.userId != userId) { |
| 8662 | continue; |
| 8663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8664 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 8665 | // This one's in trouble, so we'll generate a report for it |
| 8666 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 8667 | ActivityManager.ProcessErrorStateInfo report = null; |
| 8668 | if (app.crashing) { |
| 8669 | report = app.crashingReport; |
| 8670 | } else if (app.notResponding) { |
| 8671 | report = app.notRespondingReport; |
| 8672 | } |
| 8673 | |
| 8674 | if (report != null) { |
| 8675 | if (errList == null) { |
| 8676 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 8677 | } |
| 8678 | errList.add(report); |
| 8679 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8680 | 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] | 8681 | " crashing = " + app.crashing + |
| 8682 | " notResponding = " + app.notResponding); |
| 8683 | } |
| 8684 | } |
| 8685 | } |
| 8686 | } |
| 8687 | |
| 8688 | return errList; |
| 8689 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8690 | |
| 8691 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8692 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8693 | if (currApp != null) { |
| 8694 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 8695 | } |
| 8696 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8697 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 8698 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8699 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 8700 | if (currApp != null) { |
| 8701 | currApp.lru = 0; |
| 8702 | } |
| 8703 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8704 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8705 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 8706 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8707 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 8708 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8709 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 8710 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8711 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 8712 | } else { |
| 8713 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 8714 | } |
| 8715 | } |
| 8716 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8717 | private void fillInProcMemInfo(ProcessRecord app, |
| 8718 | ActivityManager.RunningAppProcessInfo outInfo) { |
| 8719 | outInfo.pid = app.pid; |
| 8720 | outInfo.uid = app.info.uid; |
| 8721 | if (mHeavyWeightProcess == app) { |
| 8722 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
| 8723 | } |
| 8724 | if (app.persistent) { |
| 8725 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 8726 | } |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8727 | if (app.hasActivities) { |
| 8728 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES; |
| 8729 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8730 | outInfo.lastTrimLevel = app.trimMemoryLevel; |
| 8731 | int adj = app.curAdj; |
| 8732 | outInfo.importance = oomAdjToImportance(adj, outInfo); |
| 8733 | outInfo.importanceReasonCode = app.adjTypeCode; |
| 8734 | } |
| 8735 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8736 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8737 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8738 | // Lazy instantiation of list |
| 8739 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8740 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8741 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8742 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8743 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8744 | synchronized (this) { |
| 8745 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8746 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8747 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8748 | if (!allUsers && app.userId != userId) { |
| 8749 | continue; |
| 8750 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8751 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 8752 | // Generate process state info for running application |
| 8753 | ActivityManager.RunningAppProcessInfo currApp = |
| 8754 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 8755 | app.pid, app.getPackageList()); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8756 | fillInProcMemInfo(app, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8757 | if (app.adjSource instanceof ProcessRecord) { |
| 8758 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8759 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 8760 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8761 | } else if (app.adjSource instanceof ActivityRecord) { |
| 8762 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8763 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 8764 | } |
| 8765 | if (app.adjTarget instanceof ComponentName) { |
| 8766 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 8767 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8768 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8769 | // + " lru=" + currApp.lru); |
| 8770 | if (runList == null) { |
| 8771 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 8772 | } |
| 8773 | runList.add(currApp); |
| 8774 | } |
| 8775 | } |
| 8776 | } |
| 8777 | return runList; |
| 8778 | } |
| 8779 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8780 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8781 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8782 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8783 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8784 | if (runningApps != null && runningApps.size() > 0) { |
| 8785 | Set<String> extList = new HashSet<String>(); |
| 8786 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8787 | if (app.pkgList != null) { |
| 8788 | for (String pkg : app.pkgList) { |
| 8789 | extList.add(pkg); |
| 8790 | } |
| 8791 | } |
| 8792 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8793 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8794 | for (String pkg : extList) { |
| 8795 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 8796 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId()); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8797 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8798 | retList.add(info); |
| 8799 | } |
| 8800 | } catch (RemoteException e) { |
| 8801 | } |
| 8802 | } |
| 8803 | } |
| 8804 | return retList; |
| 8805 | } |
| 8806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8807 | @Override |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8808 | public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) { |
| 8809 | enforceNotIsolatedCaller("getMyMemoryState"); |
| 8810 | synchronized (this) { |
| 8811 | ProcessRecord proc; |
| 8812 | synchronized (mPidsSelfLocked) { |
| 8813 | proc = mPidsSelfLocked.get(Binder.getCallingPid()); |
| 8814 | } |
| 8815 | fillInProcMemInfo(proc, outInfo); |
| 8816 | } |
| 8817 | } |
| 8818 | |
| 8819 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8820 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8821 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 8822 | != PackageManager.PERMISSION_GRANTED) { |
| 8823 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8824 | + Binder.getCallingPid() |
| 8825 | + ", uid=" + Binder.getCallingUid() |
| 8826 | + " without permission " |
| 8827 | + android.Manifest.permission.DUMP); |
| 8828 | return; |
| 8829 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8830 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8831 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8832 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8833 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8834 | |
| 8835 | int opti = 0; |
| 8836 | while (opti < args.length) { |
| 8837 | String opt = args[opti]; |
| 8838 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8839 | break; |
| 8840 | } |
| 8841 | opti++; |
| 8842 | if ("-a".equals(opt)) { |
| 8843 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8844 | } else if ("-c".equals(opt)) { |
| 8845 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8846 | } else if ("-h".equals(opt)) { |
| 8847 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8848 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8849 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8850 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8851 | pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state"); |
| 8852 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 8853 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8854 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8855 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8856 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8857 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8858 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8859 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8860 | pw.println(" all: dump all activities"); |
| 8861 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8862 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8863 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 8864 | pw.println(" a partial substring in a component name, a"); |
| 8865 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8866 | pw.println(" -a: include all available server state."); |
| 8867 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8868 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8869 | } else { |
| 8870 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8871 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8872 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8873 | |
| 8874 | long origId = Binder.clearCallingIdentity(); |
| 8875 | boolean more = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8876 | // Is the caller requesting to dump a particular piece of data? |
| 8877 | if (opti < args.length) { |
| 8878 | String cmd = args[opti]; |
| 8879 | opti++; |
| 8880 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 8881 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8882 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8883 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8884 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8885 | String[] newArgs; |
| 8886 | String name; |
| 8887 | if (opti >= args.length) { |
| 8888 | name = null; |
| 8889 | newArgs = EMPTY_STRING_ARRAY; |
| 8890 | } else { |
| 8891 | name = args[opti]; |
| 8892 | opti++; |
| 8893 | newArgs = new String[args.length - opti]; |
| 8894 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8895 | args.length - opti); |
| 8896 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8897 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8898 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8899 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8900 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8901 | String[] newArgs; |
| 8902 | String name; |
| 8903 | if (opti >= args.length) { |
| 8904 | name = null; |
| 8905 | newArgs = EMPTY_STRING_ARRAY; |
| 8906 | } else { |
| 8907 | name = args[opti]; |
| 8908 | opti++; |
| 8909 | newArgs = new String[args.length - opti]; |
| 8910 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8911 | args.length - opti); |
| 8912 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8913 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8914 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8915 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8916 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8917 | String[] newArgs; |
| 8918 | String name; |
| 8919 | if (opti >= args.length) { |
| 8920 | name = null; |
| 8921 | newArgs = EMPTY_STRING_ARRAY; |
| 8922 | } else { |
| 8923 | name = args[opti]; |
| 8924 | opti++; |
| 8925 | newArgs = new String[args.length - opti]; |
| 8926 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8927 | args.length - opti); |
| 8928 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8929 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8930 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8931 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8932 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8933 | synchronized (this) { |
| 8934 | dumpOomLocked(fd, pw, args, opti, true); |
| 8935 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8936 | } else if ("provider".equals(cmd)) { |
| 8937 | String[] newArgs; |
| 8938 | String name; |
| 8939 | if (opti >= args.length) { |
| 8940 | name = null; |
| 8941 | newArgs = EMPTY_STRING_ARRAY; |
| 8942 | } else { |
| 8943 | name = args[opti]; |
| 8944 | opti++; |
| 8945 | newArgs = new String[args.length - opti]; |
| 8946 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8947 | } |
| 8948 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8949 | pw.println("No providers match: " + name); |
| 8950 | pw.println("Use -h for help."); |
| 8951 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8952 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8953 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8954 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8955 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8956 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8957 | String[] newArgs; |
| 8958 | String name; |
| 8959 | if (opti >= args.length) { |
| 8960 | name = null; |
| 8961 | newArgs = EMPTY_STRING_ARRAY; |
| 8962 | } else { |
| 8963 | name = args[opti]; |
| 8964 | opti++; |
| 8965 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8966 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8967 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8968 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8969 | if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8970 | pw.println("No services match: " + name); |
| 8971 | pw.println("Use -h for help."); |
| 8972 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8973 | } else if ("package".equals(cmd)) { |
| 8974 | String[] newArgs; |
| 8975 | if (opti >= args.length) { |
| 8976 | pw.println("package: no package name specified"); |
| 8977 | pw.println("Use -h for help."); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8978 | } else { |
| 8979 | dumpPackage = args[opti]; |
| 8980 | opti++; |
| 8981 | newArgs = new String[args.length - opti]; |
| 8982 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8983 | args.length - opti); |
| 8984 | args = newArgs; |
| 8985 | opti = 0; |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8986 | more = true; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8987 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8988 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 8989 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8990 | mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8991 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8992 | } else { |
| 8993 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8994 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 8995 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 8996 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8997 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8998 | } |
| 8999 | if (!more) { |
| 9000 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9001 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9002 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9003 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9004 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9005 | // No piece of data specified, dump everything. |
| 9006 | synchronized (this) { |
| 9007 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9008 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9009 | if (needSep) { |
| 9010 | pw.println(" "); |
| 9011 | } |
| 9012 | if (dumpAll) { |
| 9013 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9014 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9015 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9016 | if (needSep) { |
| 9017 | pw.println(" "); |
| 9018 | } |
| 9019 | if (dumpAll) { |
| 9020 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9021 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9022 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9023 | if (needSep) { |
| 9024 | pw.println(" "); |
| 9025 | } |
| 9026 | if (dumpAll) { |
| 9027 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9028 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9029 | needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9030 | if (needSep) { |
| 9031 | pw.println(" "); |
| 9032 | } |
| 9033 | if (dumpAll) { |
| 9034 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9035 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9036 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9037 | if (needSep) { |
| 9038 | pw.println(" "); |
| 9039 | } |
| 9040 | if (dumpAll) { |
| 9041 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9042 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9043 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9044 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9045 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9046 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9047 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9048 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9049 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9050 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 9051 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9052 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 9053 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9054 | pw.println(" "); |
| 9055 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9056 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 9057 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9058 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9059 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9060 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9061 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9062 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9063 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9064 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9065 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9066 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9067 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9068 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9069 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9070 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 9071 | pw.println(" "); |
| 9072 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9073 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9074 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9075 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9076 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9077 | pw.println(" "); |
| 9078 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9079 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9080 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9081 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9082 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9083 | pw.println(" "); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 9084 | if (mMainStack.mPausingActivity != null) { |
| 9085 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9086 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9087 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9088 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9089 | if (dumpAll) { |
| 9090 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 9091 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9092 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 9093 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9094 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9095 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9096 | if (mRecentTasks.size() > 0) { |
| 9097 | pw.println(); |
| 9098 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9099 | |
| 9100 | final int N = mRecentTasks.size(); |
| 9101 | for (int i=0; i<N; i++) { |
| 9102 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9103 | if (dumpPackage != null) { |
| 9104 | if (tr.realActivity == null || |
| 9105 | !dumpPackage.equals(tr.realActivity)) { |
| 9106 | continue; |
| 9107 | } |
| 9108 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9109 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 9110 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9111 | if (dumpAll) { |
| 9112 | mRecentTasks.get(i).dump(pw, " "); |
| 9113 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9114 | } |
| 9115 | } |
| 9116 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9117 | if (dumpAll) { |
| 9118 | pw.println(" "); |
| 9119 | pw.println(" mCurTask: " + mCurTask); |
| 9120 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9121 | |
| 9122 | return true; |
| 9123 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9124 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9125 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9126 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9127 | boolean needSep = false; |
| 9128 | int numPers = 0; |
| 9129 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9130 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 9131 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9132 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9133 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 9134 | final int NA = procs.size(); |
| 9135 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9136 | ProcessRecord r = procs.valueAt(ia); |
| 9137 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9138 | continue; |
| 9139 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9140 | if (!needSep) { |
| 9141 | pw.println(" All known processes:"); |
| 9142 | needSep = true; |
| 9143 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9144 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 9145 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 9146 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9147 | r.dump(pw, " "); |
| 9148 | if (r.persistent) { |
| 9149 | numPers++; |
| 9150 | } |
| 9151 | } |
| 9152 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9153 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 9154 | |
| 9155 | if (mIsolatedProcesses.size() > 0) { |
| 9156 | if (needSep) pw.println(" "); |
| 9157 | needSep = true; |
| 9158 | pw.println(" Isolated process list (sorted by uid):"); |
| 9159 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 9160 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 9161 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9162 | continue; |
| 9163 | } |
| 9164 | pw.println(String.format("%sIsolated #%2d: %s", |
| 9165 | " ", i, r.toString())); |
| 9166 | } |
| 9167 | } |
| 9168 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9169 | if (mLruProcesses.size() > 0) { |
| 9170 | if (needSep) pw.println(" "); |
| 9171 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9172 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9173 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9174 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9175 | needSep = true; |
| 9176 | } |
| 9177 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9178 | if (dumpAll) { |
| 9179 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9180 | boolean printed = false; |
| 9181 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 9182 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 9183 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9184 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9185 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9186 | if (!printed) { |
| 9187 | if (needSep) pw.println(" "); |
| 9188 | needSep = true; |
| 9189 | pw.println(" PID mappings:"); |
| 9190 | printed = true; |
| 9191 | } |
| 9192 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 9193 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9194 | } |
| 9195 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9196 | } |
| 9197 | |
| 9198 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9199 | synchronized (mPidsSelfLocked) { |
| 9200 | boolean printed = false; |
| 9201 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 9202 | ProcessRecord r = mPidsSelfLocked.get( |
| 9203 | mForegroundProcesses.valueAt(i).pid); |
| 9204 | if (dumpPackage != null && (r == null |
| 9205 | || !dumpPackage.equals(r.info.packageName))) { |
| 9206 | continue; |
| 9207 | } |
| 9208 | if (!printed) { |
| 9209 | if (needSep) pw.println(" "); |
| 9210 | needSep = true; |
| 9211 | pw.println(" Foreground Processes:"); |
| 9212 | printed = true; |
| 9213 | } |
| 9214 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 9215 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 9216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9217 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9218 | } |
| 9219 | |
| 9220 | if (mPersistentStartingProcesses.size() > 0) { |
| 9221 | if (needSep) pw.println(" "); |
| 9222 | needSep = true; |
| 9223 | pw.println(" Persisent processes that are starting:"); |
| 9224 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9225 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9226 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9227 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9228 | if (mRemovedProcesses.size() > 0) { |
| 9229 | if (needSep) pw.println(" "); |
| 9230 | needSep = true; |
| 9231 | pw.println(" Processes that are being removed:"); |
| 9232 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9233 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9234 | } |
| 9235 | |
| 9236 | if (mProcessesOnHold.size() > 0) { |
| 9237 | if (needSep) pw.println(" "); |
| 9238 | needSep = true; |
| 9239 | pw.println(" Processes that are on old until the system is ready:"); |
| 9240 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9241 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9243 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9244 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9245 | |
| 9246 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9247 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9248 | long now = SystemClock.uptimeMillis(); |
| 9249 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9250 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9251 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9252 | SparseArray<Long> uids = procs.getValue(); |
| 9253 | final int N = uids.size(); |
| 9254 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9255 | int puid = uids.keyAt(i); |
| 9256 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9257 | if (dumpPackage != null && (r == null |
| 9258 | || !dumpPackage.equals(r.info.packageName))) { |
| 9259 | continue; |
| 9260 | } |
| 9261 | if (!printed) { |
| 9262 | if (needSep) pw.println(" "); |
| 9263 | needSep = true; |
| 9264 | pw.println(" Time since processes crashed:"); |
| 9265 | printed = true; |
| 9266 | } |
| 9267 | pw.print(" Process "); pw.print(pname); |
| 9268 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9269 | pw.print(": last crashed "); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 9270 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 9271 | pw.println(" ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9272 | } |
| 9273 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9274 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9275 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9276 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9277 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9278 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9279 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9280 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9281 | SparseArray<Long> uids = procs.getValue(); |
| 9282 | final int N = uids.size(); |
| 9283 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9284 | int puid = uids.keyAt(i); |
| 9285 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9286 | if (dumpPackage != null && (r == null |
| 9287 | || !dumpPackage.equals(r.info.packageName))) { |
| 9288 | continue; |
| 9289 | } |
| 9290 | if (!printed) { |
| 9291 | if (needSep) pw.println(" "); |
| 9292 | needSep = true; |
| 9293 | pw.println(" Bad processes:"); |
| 9294 | } |
| 9295 | pw.print(" Bad process "); pw.print(pname); |
| 9296 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9297 | pw.print(": crashed at time "); |
| 9298 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9299 | } |
| 9300 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9302 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9303 | pw.println(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9304 | pw.println(" mStartedUsers:"); |
| 9305 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 9306 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 9307 | pw.print(" User #"); pw.print(uss.mHandle.getIdentifier()); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9308 | pw.print(": "); uss.dump("", pw); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9309 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9310 | pw.print(" mUserLru: ["); |
| 9311 | for (int i=0; i<mUserLru.size(); i++) { |
| 9312 | if (i > 0) pw.print(", "); |
| 9313 | pw.print(mUserLru.get(i)); |
| 9314 | } |
| 9315 | pw.println("]"); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 9316 | if (dumpAll) { |
| 9317 | pw.print(" mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray)); |
| 9318 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9319 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9320 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 9321 | if (dumpAll) { |
| 9322 | StringBuilder sb = new StringBuilder(128); |
| 9323 | sb.append(" mPreviousProcessVisibleTime: "); |
| 9324 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 9325 | pw.println(sb); |
| 9326 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9327 | if (mHeavyWeightProcess != null) { |
| 9328 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9329 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9330 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9331 | if (dumpAll) { |
| 9332 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 9333 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9334 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9335 | for (Map.Entry<String, Integer> entry |
| 9336 | : mCompatModePackages.getPackages().entrySet()) { |
| 9337 | String pkg = entry.getKey(); |
| 9338 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9339 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 9340 | continue; |
| 9341 | } |
| 9342 | if (!printed) { |
| 9343 | pw.println(" mScreenCompatPackages:"); |
| 9344 | printed = true; |
| 9345 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9346 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 9347 | pw.print(mode); pw.println(); |
| 9348 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 9349 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9350 | } |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 9351 | if (mSleeping || mWentToSleep || mLockScreenShown) { |
| 9352 | pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep |
| 9353 | + " mLockScreenShown " + mLockScreenShown); |
| 9354 | } |
| 9355 | if (mShuttingDown) { |
| 9356 | pw.println(" mShuttingDown=" + mShuttingDown); |
| 9357 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9358 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 9359 | || mOrigWaitForDebugger) { |
| 9360 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 9361 | + " mDebugTransient=" + mDebugTransient |
| 9362 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 9363 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 9364 | if (mOpenGlTraceApp != null) { |
| 9365 | pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp); |
| 9366 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 9367 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 9368 | || mProfileFd != null) { |
| 9369 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 9370 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 9371 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 9372 | + mAutoStopProfiler); |
| 9373 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9374 | if (mAlwaysFinishActivities || mController != null) { |
| 9375 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 9376 | + " mController=" + mController); |
| 9377 | } |
| 9378 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9379 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9380 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9381 | + " mProcessesReady=" + mProcessesReady |
| 9382 | + " mSystemReady=" + mSystemReady); |
| 9383 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9384 | + " mBooted=" + mBooted |
| 9385 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9386 | pw.print(" mLastPowerCheckRealtime="); |
| 9387 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 9388 | pw.println(""); |
| 9389 | pw.print(" mLastPowerCheckUptime="); |
| 9390 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 9391 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9392 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 9393 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 9394 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 9395 | pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs |
| 9396 | + " mNumHiddenProcs=" + mNumHiddenProcs |
| 9397 | + " mNumServiceProcs=" + mNumServiceProcs |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9398 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9399 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9400 | |
| 9401 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9402 | } |
| 9403 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9404 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9405 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9406 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9407 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9408 | long now = SystemClock.uptimeMillis(); |
| 9409 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 9410 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9411 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 9412 | continue; |
| 9413 | } |
| 9414 | if (!printed) { |
| 9415 | if (needSep) pw.println(" "); |
| 9416 | needSep = true; |
| 9417 | pw.println(" Processes that are waiting to GC:"); |
| 9418 | printed = true; |
| 9419 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9420 | pw.print(" Process "); pw.println(proc); |
| 9421 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 9422 | pw.print(", last gced="); |
| 9423 | pw.print(now-proc.lastRequestedGc); |
| 9424 | pw.print(" ms ago, last lowMem="); |
| 9425 | pw.print(now-proc.lastLowMemory); |
| 9426 | pw.println(" ms ago"); |
| 9427 | |
| 9428 | } |
| 9429 | } |
| 9430 | return needSep; |
| 9431 | } |
| 9432 | |
| 9433 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 9434 | int opti, boolean dumpAll) { |
| 9435 | boolean needSep = false; |
| 9436 | |
| 9437 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9438 | if (needSep) pw.println(" "); |
| 9439 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9440 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9441 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9442 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9443 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 9444 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 9445 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9446 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9447 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9448 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9449 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9450 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9451 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9452 | 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] | 9453 | 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] | 9454 | |
| 9455 | if (needSep) pw.println(" "); |
| 9456 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9457 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9458 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9459 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9460 | needSep = true; |
| 9461 | } |
| 9462 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9463 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9464 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9465 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9466 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9467 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9468 | if (mHeavyWeightProcess != null) { |
| 9469 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9470 | } |
| 9471 | |
| 9472 | return true; |
| 9473 | } |
| 9474 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9475 | /** |
| 9476 | * There are three ways to call this: |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9477 | * - no provider specified: dump all the providers |
| 9478 | * - a flattened component name that matched an existing provider was specified as the |
| 9479 | * first arg: dump that one provider |
| 9480 | * - the first arg isn't the flattened component name of an existing provider: |
| 9481 | * dump all providers whose component contains the first arg as a substring |
| 9482 | */ |
| 9483 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9484 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 9485 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9486 | } |
| 9487 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9488 | static class ItemMatcher { |
| 9489 | ArrayList<ComponentName> components; |
| 9490 | ArrayList<String> strings; |
| 9491 | ArrayList<Integer> objects; |
| 9492 | boolean all; |
| 9493 | |
| 9494 | ItemMatcher() { |
| 9495 | all = true; |
| 9496 | } |
| 9497 | |
| 9498 | void build(String name) { |
| 9499 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 9500 | if (componentName != null) { |
| 9501 | if (components == null) { |
| 9502 | components = new ArrayList<ComponentName>(); |
| 9503 | } |
| 9504 | components.add(componentName); |
| 9505 | all = false; |
| 9506 | } else { |
| 9507 | int objectId = 0; |
| 9508 | // Not a '/' separated full component name; maybe an object ID? |
| 9509 | try { |
| 9510 | objectId = Integer.parseInt(name, 16); |
| 9511 | if (objects == null) { |
| 9512 | objects = new ArrayList<Integer>(); |
| 9513 | } |
| 9514 | objects.add(objectId); |
| 9515 | all = false; |
| 9516 | } catch (RuntimeException e) { |
| 9517 | // Not an integer; just do string match. |
| 9518 | if (strings == null) { |
| 9519 | strings = new ArrayList<String>(); |
| 9520 | } |
| 9521 | strings.add(name); |
| 9522 | all = false; |
| 9523 | } |
| 9524 | } |
| 9525 | } |
| 9526 | |
| 9527 | int build(String[] args, int opti) { |
| 9528 | for (; opti<args.length; opti++) { |
| 9529 | String name = args[opti]; |
| 9530 | if ("--".equals(name)) { |
| 9531 | return opti+1; |
| 9532 | } |
| 9533 | build(name); |
| 9534 | } |
| 9535 | return opti; |
| 9536 | } |
| 9537 | |
| 9538 | boolean match(Object object, ComponentName comp) { |
| 9539 | if (all) { |
| 9540 | return true; |
| 9541 | } |
| 9542 | if (components != null) { |
| 9543 | for (int i=0; i<components.size(); i++) { |
| 9544 | if (components.get(i).equals(comp)) { |
| 9545 | return true; |
| 9546 | } |
| 9547 | } |
| 9548 | } |
| 9549 | if (objects != null) { |
| 9550 | for (int i=0; i<objects.size(); i++) { |
| 9551 | if (System.identityHashCode(object) == objects.get(i)) { |
| 9552 | return true; |
| 9553 | } |
| 9554 | } |
| 9555 | } |
| 9556 | if (strings != null) { |
| 9557 | String flat = comp.flattenToString(); |
| 9558 | for (int i=0; i<strings.size(); i++) { |
| 9559 | if (flat.contains(strings.get(i))) { |
| 9560 | return true; |
| 9561 | } |
| 9562 | } |
| 9563 | } |
| 9564 | return false; |
| 9565 | } |
| 9566 | } |
| 9567 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9568 | /** |
| 9569 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9570 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9571 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 9572 | * dump all activity whose component contains the cmd as a substring |
| 9573 | * - A hex number of the ActivityRecord object instance. |
| 9574 | */ |
| 9575 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9576 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9577 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9578 | |
| 9579 | if ("all".equals(name)) { |
| 9580 | synchronized (this) { |
| 9581 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9582 | activities.add(r1); |
| 9583 | } |
| 9584 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 9585 | } else if ("top".equals(name)) { |
| 9586 | synchronized (this) { |
| 9587 | final int N = mMainStack.mHistory.size(); |
| 9588 | if (N > 0) { |
| 9589 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 9590 | } |
| 9591 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9592 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9593 | ItemMatcher matcher = new ItemMatcher(); |
| 9594 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9595 | |
| 9596 | synchronized (this) { |
| 9597 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9598 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9599 | activities.add(r1); |
| 9600 | } |
| 9601 | } |
| 9602 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9603 | } |
| 9604 | |
| 9605 | if (activities.size() <= 0) { |
| 9606 | return false; |
| 9607 | } |
| 9608 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9609 | String[] newArgs = new String[args.length - opti]; |
| 9610 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9611 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9612 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9613 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9614 | for (int i=activities.size()-1; i>=0; i--) { |
| 9615 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9616 | if (needSep) { |
| 9617 | pw.println(); |
| 9618 | } |
| 9619 | needSep = true; |
| 9620 | synchronized (this) { |
| 9621 | if (lastTask != r.task) { |
| 9622 | lastTask = r.task; |
| 9623 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 9624 | pw.print(" id="); pw.println(lastTask.taskId); |
| 9625 | if (dumpAll) { |
| 9626 | lastTask.dump(pw, " "); |
| 9627 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9628 | } |
| 9629 | } |
| 9630 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9631 | } |
| 9632 | return true; |
| 9633 | } |
| 9634 | |
| 9635 | /** |
| 9636 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 9637 | * there is a thread associated with the activity. |
| 9638 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9639 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9640 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 9641 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9642 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9643 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 9644 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9645 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9646 | if (r.app != null) pw.println(r.app.pid); |
| 9647 | else pw.println("(not running)"); |
| 9648 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9649 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9650 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9651 | } |
| 9652 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9653 | // flush anything that is already in the PrintWriter since the thread is going |
| 9654 | // to write to the file descriptor directly |
| 9655 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9656 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9657 | TransferPipe tp = new TransferPipe(); |
| 9658 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9659 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9660 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9661 | tp.go(fd); |
| 9662 | } finally { |
| 9663 | tp.kill(); |
| 9664 | } |
| 9665 | } catch (IOException e) { |
| 9666 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9667 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9668 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9669 | } |
| 9670 | } |
| 9671 | } |
| 9672 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9673 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9674 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9675 | boolean needSep = false; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9676 | boolean onlyHistory = false; |
| 9677 | |
| 9678 | if ("history".equals(dumpPackage)) { |
| 9679 | onlyHistory = true; |
| 9680 | dumpPackage = null; |
| 9681 | } |
| 9682 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9683 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9684 | if (!onlyHistory && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9685 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9686 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9687 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 9688 | while (it.hasNext()) { |
| 9689 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9690 | if (dumpPackage != null && (r.app == null || |
| 9691 | !dumpPackage.equals(r.app.info.packageName))) { |
| 9692 | continue; |
| 9693 | } |
| 9694 | if (!printed) { |
| 9695 | pw.println(" Registered Receivers:"); |
| 9696 | needSep = true; |
| 9697 | printed = true; |
| 9698 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9699 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9700 | r.dump(pw, " "); |
| 9701 | } |
| 9702 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9703 | |
| 9704 | if (mReceiverResolver.dump(pw, needSep ? |
| 9705 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9706 | " ", dumpPackage, false)) { |
| 9707 | needSep = true; |
| 9708 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9709 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9710 | |
| 9711 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9712 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9713 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9714 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9715 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9716 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9717 | if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9718 | for (int user=0; user<mStickyBroadcasts.size(); user++) { |
| 9719 | if (needSep) { |
| 9720 | pw.println(); |
| 9721 | } |
| 9722 | needSep = true; |
| 9723 | pw.print(" Sticky broadcasts for user "); |
| 9724 | pw.print(mStickyBroadcasts.keyAt(user)); pw.println(":"); |
| 9725 | StringBuilder sb = new StringBuilder(128); |
| 9726 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9727 | : mStickyBroadcasts.valueAt(user).entrySet()) { |
| 9728 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
| 9729 | if (dumpAll) { |
| 9730 | pw.println(":"); |
| 9731 | ArrayList<Intent> intents = ent.getValue(); |
| 9732 | final int N = intents.size(); |
| 9733 | for (int i=0; i<N; i++) { |
| 9734 | sb.setLength(0); |
| 9735 | sb.append(" Intent: "); |
| 9736 | intents.get(i).toShortString(sb, false, true, false, false); |
| 9737 | pw.println(sb.toString()); |
| 9738 | Bundle bundle = intents.get(i).getExtras(); |
| 9739 | if (bundle != null) { |
| 9740 | pw.print(" "); |
| 9741 | pw.println(bundle.toString()); |
| 9742 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9743 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9744 | } else { |
| 9745 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9746 | } |
| 9747 | } |
| 9748 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9749 | } |
| 9750 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9751 | if (!onlyHistory && dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9752 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9753 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9754 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9755 | + queue.mBroadcastsScheduled); |
| 9756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9757 | pw.println(" mHandler:"); |
| 9758 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9759 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9760 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9761 | |
| 9762 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9763 | } |
| 9764 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9765 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9766 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 9767 | boolean needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9768 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9769 | ItemMatcher matcher = new ItemMatcher(); |
| 9770 | matcher.build(args, opti); |
| 9771 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9772 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9773 | |
| 9774 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9775 | |
| 9776 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9777 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9778 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9779 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9780 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9781 | continue; |
| 9782 | } |
| 9783 | if (!printed) { |
| 9784 | if (needSep) pw.println(" "); |
| 9785 | needSep = true; |
| 9786 | pw.println(" Launching content providers:"); |
| 9787 | printed = true; |
| 9788 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9789 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9790 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9791 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9792 | } |
| 9793 | |
| 9794 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9795 | if (needSep) pw.println(); |
| 9796 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9797 | pw.println("Granted Uri Permissions:"); |
| 9798 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9799 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9800 | HashMap<Uri, UriPermission> perms |
| 9801 | = mGrantedUriPermissions.valueAt(i); |
| 9802 | pw.print(" * UID "); pw.print(uid); |
| 9803 | pw.println(" holds:"); |
| 9804 | for (UriPermission perm : perms.values()) { |
| 9805 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9806 | if (dumpAll) { |
| 9807 | perm.dump(pw, " "); |
| 9808 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9809 | } |
| 9810 | } |
| 9811 | needSep = true; |
| 9812 | } |
| 9813 | |
| 9814 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9815 | } |
| 9816 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9817 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9818 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9819 | boolean needSep = false; |
| 9820 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9821 | if (mIntentSenderRecords.size() > 0) { |
| 9822 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9823 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9824 | = mIntentSenderRecords.values().iterator(); |
| 9825 | while (it.hasNext()) { |
| 9826 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9827 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9828 | if (dumpPackage != null && (rec == null |
| 9829 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9830 | continue; |
| 9831 | } |
| 9832 | if (!printed) { |
| 9833 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9834 | printed = true; |
| 9835 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9836 | needSep = true; |
| 9837 | if (rec != null) { |
| 9838 | pw.print(" * "); pw.println(rec); |
| 9839 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9840 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9841 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9842 | } else { |
| 9843 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9844 | } |
| 9845 | } |
| 9846 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9847 | |
| 9848 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9849 | } |
| 9850 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9851 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9852 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9853 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9854 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9855 | boolean needNL = false; |
| 9856 | final String innerPrefix = prefix + " "; |
| 9857 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9858 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9859 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9860 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9861 | continue; |
| 9862 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9863 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9864 | if (needNL) { |
| 9865 | pw.println(" "); |
| 9866 | needNL = false; |
| 9867 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9868 | if (lastTask != r.task) { |
| 9869 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9870 | pw.print(prefix); |
| 9871 | pw.print(full ? "* " : " "); |
| 9872 | pw.println(lastTask); |
| 9873 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9874 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9875 | } else if (complete) { |
| 9876 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9877 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9878 | pw.print(prefix); pw.print(" "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9879 | pw.println(lastTask.intent.toInsecureStringWithClip()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9880 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9881 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9882 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9883 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9884 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9885 | pw.println(r); |
| 9886 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9887 | r.dump(pw, innerPrefix); |
| 9888 | } else if (complete) { |
| 9889 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9890 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9891 | if (r.app != null) { |
| 9892 | pw.print(innerPrefix); pw.println(r.app); |
| 9893 | } |
| 9894 | } |
| 9895 | if (client && r.app != null && r.app.thread != null) { |
| 9896 | // flush anything that is already in the PrintWriter since the thread is going |
| 9897 | // to write to the file descriptor directly |
| 9898 | pw.flush(); |
| 9899 | try { |
| 9900 | TransferPipe tp = new TransferPipe(); |
| 9901 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9902 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9903 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9904 | // Short timeout, since blocking here can |
| 9905 | // deadlock with the application. |
| 9906 | tp.go(fd, 2000); |
| 9907 | } finally { |
| 9908 | tp.kill(); |
| 9909 | } |
| 9910 | } catch (IOException e) { |
| 9911 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9912 | } catch (RemoteException e) { |
| 9913 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9914 | } |
| 9915 | needNL = true; |
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 | } |
| 9918 | } |
| 9919 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9920 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9921 | if (val == base) { |
| 9922 | if (space == null) return prefix; |
| 9923 | return prefix + " "; |
| 9924 | } |
| 9925 | return prefix + "+" + Integer.toString(val-base); |
| 9926 | } |
| 9927 | |
| 9928 | private static final int dumpProcessList(PrintWriter pw, |
| 9929 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9930 | String prefix, String normalLabel, String persistentLabel, |
| 9931 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9932 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9933 | final int N = list.size()-1; |
| 9934 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9935 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9936 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9937 | continue; |
| 9938 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9939 | pw.println(String.format("%s%s #%2d: %s", |
| 9940 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9941 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9942 | if (r.persistent) { |
| 9943 | numPers++; |
| 9944 | } |
| 9945 | } |
| 9946 | return numPers; |
| 9947 | } |
| 9948 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9949 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9950 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9951 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9952 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9953 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9954 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9955 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9956 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9957 | ProcessRecord r = origList.get(i); |
| 9958 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9959 | continue; |
| 9960 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9961 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9962 | } |
| 9963 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9964 | if (list.size() <= 0) { |
| 9965 | return false; |
| 9966 | } |
| 9967 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9968 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 9969 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 9970 | @Override |
| 9971 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 9972 | Pair<ProcessRecord, Integer> object2) { |
| 9973 | if (object1.first.setAdj != object2.first.setAdj) { |
| 9974 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 9975 | } |
| 9976 | if (object1.second.intValue() != object2.second.intValue()) { |
| 9977 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 9978 | } |
| 9979 | return 0; |
| 9980 | } |
| 9981 | }; |
| 9982 | |
| 9983 | Collections.sort(list, comparator); |
| 9984 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9985 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 9986 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 9987 | final long curUptime = SystemClock.uptimeMillis(); |
| 9988 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 9989 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9990 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9991 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9992 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9993 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9994 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9995 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 9996 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9997 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 9998 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9999 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 10000 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10001 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 10002 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10003 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10004 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10005 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 10006 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 10007 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 10008 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 10009 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 10010 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 10011 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10012 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10013 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 10014 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10015 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 10016 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10017 | } else { |
| 10018 | oomAdj = Integer.toString(r.setAdj); |
| 10019 | } |
| 10020 | String schedGroup; |
| 10021 | switch (r.setSchedGroup) { |
| 10022 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 10023 | schedGroup = "B"; |
| 10024 | break; |
| 10025 | case Process.THREAD_GROUP_DEFAULT: |
| 10026 | schedGroup = "F"; |
| 10027 | break; |
| 10028 | default: |
| 10029 | schedGroup = Integer.toString(r.setSchedGroup); |
| 10030 | break; |
| 10031 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10032 | String foreground; |
| 10033 | if (r.foregroundActivities) { |
| 10034 | foreground = "A"; |
| 10035 | } else if (r.foregroundServices) { |
| 10036 | foreground = "S"; |
| 10037 | } else { |
| 10038 | foreground = " "; |
| 10039 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 10040 | 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] | 10041 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10042 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 10043 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10044 | if (r.adjSource != null || r.adjTarget != null) { |
| 10045 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10046 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10047 | if (r.adjTarget instanceof ComponentName) { |
| 10048 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 10049 | } else if (r.adjTarget != null) { |
| 10050 | pw.print(r.adjTarget.toString()); |
| 10051 | } else { |
| 10052 | pw.print("{null}"); |
| 10053 | } |
| 10054 | pw.print("<="); |
| 10055 | if (r.adjSource instanceof ProcessRecord) { |
| 10056 | pw.print("Proc{"); |
| 10057 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 10058 | pw.println("}"); |
| 10059 | } else if (r.adjSource != null) { |
| 10060 | pw.println(r.adjSource.toString()); |
| 10061 | } else { |
| 10062 | pw.println("{null}"); |
| 10063 | } |
| 10064 | } |
| 10065 | if (inclDetails) { |
| 10066 | pw.print(prefix); |
| 10067 | pw.print(" "); |
| 10068 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 10069 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 10070 | pw.print(" empty="); pw.print(r.emptyAdj); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10071 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 10072 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 10073 | pw.print(" cur="); pw.print(r.curAdj); |
| 10074 | pw.print(" set="); pw.println(r.setAdj); |
| 10075 | pw.print(prefix); |
| 10076 | pw.print(" "); |
| 10077 | pw.print("keeping="); pw.print(r.keeping); |
| 10078 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10079 | pw.print(" empty="); pw.print(r.empty); |
| 10080 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10081 | |
| 10082 | if (!r.keeping) { |
| 10083 | if (r.lastWakeTime != 0) { |
| 10084 | long wtime; |
| 10085 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 10086 | synchronized (stats) { |
| 10087 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 10088 | r.pid, curRealtime); |
| 10089 | } |
| 10090 | long timeUsed = wtime - r.lastWakeTime; |
| 10091 | pw.print(prefix); |
| 10092 | pw.print(" "); |
| 10093 | pw.print("keep awake over "); |
| 10094 | TimeUtils.formatDuration(realtimeSince, pw); |
| 10095 | pw.print(" used "); |
| 10096 | TimeUtils.formatDuration(timeUsed, pw); |
| 10097 | pw.print(" ("); |
| 10098 | pw.print((timeUsed*100)/realtimeSince); |
| 10099 | pw.println("%)"); |
| 10100 | } |
| 10101 | if (r.lastCpuTime != 0) { |
| 10102 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 10103 | pw.print(prefix); |
| 10104 | pw.print(" "); |
| 10105 | pw.print("run cpu over "); |
| 10106 | TimeUtils.formatDuration(uptimeSince, pw); |
| 10107 | pw.print(" used "); |
| 10108 | TimeUtils.formatDuration(timeUsed, pw); |
| 10109 | pw.print(" ("); |
| 10110 | pw.print((timeUsed*100)/uptimeSince); |
| 10111 | pw.println("%)"); |
| 10112 | } |
| 10113 | } |
| 10114 | } |
| 10115 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10116 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10117 | } |
| 10118 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10119 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10120 | ArrayList<ProcessRecord> procs; |
| 10121 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10122 | if (args != null && args.length > start |
| 10123 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10124 | procs = new ArrayList<ProcessRecord>(); |
| 10125 | int pid = -1; |
| 10126 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10127 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10128 | } catch (NumberFormatException e) { |
| 10129 | |
| 10130 | } |
| 10131 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 10132 | ProcessRecord proc = mLruProcesses.get(i); |
| 10133 | if (proc.pid == pid) { |
| 10134 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10135 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10136 | procs.add(proc); |
| 10137 | } |
| 10138 | } |
| 10139 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10140 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10141 | return null; |
| 10142 | } |
| 10143 | } else { |
| 10144 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 10145 | } |
| 10146 | } |
| 10147 | return procs; |
| 10148 | } |
| 10149 | |
| 10150 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 10151 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10152 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10153 | if (procs == null) { |
| 10154 | return; |
| 10155 | } |
| 10156 | |
| 10157 | long uptime = SystemClock.uptimeMillis(); |
| 10158 | long realtime = SystemClock.elapsedRealtime(); |
| 10159 | pw.println("Applications Graphics Acceleration Info:"); |
| 10160 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10161 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10162 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10163 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10164 | if (r.thread != null) { |
| 10165 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10166 | pw.flush(); |
| 10167 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10168 | TransferPipe tp = new TransferPipe(); |
| 10169 | try { |
| 10170 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10171 | tp.go(fd); |
| 10172 | } finally { |
| 10173 | tp.kill(); |
| 10174 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10175 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10176 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10177 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10178 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10179 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10180 | pw.flush(); |
| 10181 | } |
| 10182 | } |
| 10183 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10184 | } |
| 10185 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 10186 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 10187 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 10188 | if (procs == null) { |
| 10189 | return; |
| 10190 | } |
| 10191 | |
| 10192 | pw.println("Applications Database Info:"); |
| 10193 | |
| 10194 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10195 | ProcessRecord r = procs.get(i); |
| 10196 | if (r.thread != null) { |
| 10197 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10198 | pw.flush(); |
| 10199 | try { |
| 10200 | TransferPipe tp = new TransferPipe(); |
| 10201 | try { |
| 10202 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10203 | tp.go(fd); |
| 10204 | } finally { |
| 10205 | tp.kill(); |
| 10206 | } |
| 10207 | } catch (IOException e) { |
| 10208 | pw.println("Failure while dumping the app: " + r); |
| 10209 | pw.flush(); |
| 10210 | } catch (RemoteException e) { |
| 10211 | pw.println("Got a RemoteException while dumping the app " + r); |
| 10212 | pw.flush(); |
| 10213 | } |
| 10214 | } |
| 10215 | } |
| 10216 | } |
| 10217 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10218 | final static class MemItem { |
| 10219 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10220 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10221 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10222 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10223 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10224 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10225 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10226 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10227 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10228 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10229 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10230 | } |
| 10231 | } |
| 10232 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10233 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10234 | boolean sort) { |
| 10235 | if (sort) { |
| 10236 | Collections.sort(items, new Comparator<MemItem>() { |
| 10237 | @Override |
| 10238 | public int compare(MemItem lhs, MemItem rhs) { |
| 10239 | if (lhs.pss < rhs.pss) { |
| 10240 | return 1; |
| 10241 | } else if (lhs.pss > rhs.pss) { |
| 10242 | return -1; |
| 10243 | } |
| 10244 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10245 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10246 | }); |
| 10247 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10248 | |
| 10249 | for (int i=0; i<items.size(); i++) { |
| 10250 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10251 | 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] | 10252 | if (mi.subitems != null) { |
| 10253 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 10254 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10255 | } |
| 10256 | } |
| 10257 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10258 | // These are in KB. |
| 10259 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 10260 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 10261 | 120*1024, 160*1024, 200*1024, |
| 10262 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 10263 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 10264 | }; |
| 10265 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10266 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 10267 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10268 | int start = label.lastIndexOf('.'); |
| 10269 | if (start >= 0) start++; |
| 10270 | else start = 0; |
| 10271 | int end = label.length(); |
| 10272 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 10273 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 10274 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 10275 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10276 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10277 | out.append(label, start, end); |
| 10278 | return; |
| 10279 | } |
| 10280 | } |
| 10281 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10282 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10283 | out.append(label, start, end); |
| 10284 | } |
| 10285 | |
| 10286 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 10287 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 10288 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 10289 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 10290 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 10291 | }; |
| 10292 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 10293 | "System", "Persistent", "Foreground", |
| 10294 | "Visible", "Perceptible", "Heavy Weight", |
| 10295 | "Backup", "A Services", "Home", "Previous", |
| 10296 | "B Services", "Background" |
| 10297 | }; |
| 10298 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10299 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10300 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10301 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10302 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10303 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10304 | |
| 10305 | int opti = 0; |
| 10306 | while (opti < args.length) { |
| 10307 | String opt = args[opti]; |
| 10308 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 10309 | break; |
| 10310 | } |
| 10311 | opti++; |
| 10312 | if ("-a".equals(opt)) { |
| 10313 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10314 | } else if ("--oom".equals(opt)) { |
| 10315 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10316 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10317 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10318 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10319 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10320 | pw.println("If [process] is specified it can be the name or "); |
| 10321 | pw.println("pid of a specific process to dump."); |
| 10322 | return; |
| 10323 | } else { |
| 10324 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 10325 | } |
| 10326 | } |
| 10327 | |
| 10328 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10329 | if (procs == null) { |
| 10330 | return; |
| 10331 | } |
| 10332 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 10333 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10334 | long uptime = SystemClock.uptimeMillis(); |
| 10335 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10336 | |
| 10337 | if (procs.size() == 1 || isCheckinRequest) { |
| 10338 | dumpAll = true; |
| 10339 | } |
| 10340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10341 | if (isCheckinRequest) { |
| 10342 | // short checkin version |
| 10343 | pw.println(uptime + "," + realtime); |
| 10344 | pw.flush(); |
| 10345 | } else { |
| 10346 | pw.println("Applications Memory Usage (kB):"); |
| 10347 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10348 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10349 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10350 | String[] innerArgs = new String[args.length-opti]; |
| 10351 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 10352 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10353 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 10354 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 10355 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 10356 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10357 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 10358 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 10359 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10360 | |
| 10361 | long totalPss = 0; |
| 10362 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10363 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10364 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10365 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10366 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10367 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 10368 | pw.flush(); |
| 10369 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10370 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10371 | if (dumpAll) { |
| 10372 | try { |
| 10373 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 10374 | } catch (RemoteException e) { |
| 10375 | if (!isCheckinRequest) { |
| 10376 | pw.println("Got RemoteException!"); |
| 10377 | pw.flush(); |
| 10378 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10379 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10380 | } else { |
| 10381 | mi = new Debug.MemoryInfo(); |
| 10382 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10383 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10384 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10385 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10386 | long myTotalPss = mi.getTotalPss(); |
| 10387 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10388 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10389 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10390 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10391 | |
| 10392 | nativePss += mi.nativePss; |
| 10393 | dalvikPss += mi.dalvikPss; |
| 10394 | otherPss += mi.otherPss; |
| 10395 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 10396 | long mem = mi.getOtherPss(j); |
| 10397 | miscPss[j] += mem; |
| 10398 | otherPss -= mem; |
| 10399 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10400 | |
| 10401 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10402 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 10403 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10404 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10405 | if (oomProcs[oomIndex] == null) { |
| 10406 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 10407 | } |
| 10408 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10409 | break; |
| 10410 | } |
| 10411 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10412 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10413 | } |
| 10414 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10415 | |
| 10416 | if (!isCheckinRequest && procs.size() > 1) { |
| 10417 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 10418 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10419 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10420 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10421 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10422 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10423 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10424 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10425 | } |
| 10426 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10427 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10428 | for (int j=0; j<oomPss.length; j++) { |
| 10429 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10430 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10431 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10432 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10433 | item.subitems = oomProcs[j]; |
| 10434 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10435 | } |
| 10436 | } |
| 10437 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10438 | if (outTag != null || outStack != null) { |
| 10439 | if (outTag != null) { |
| 10440 | appendMemBucket(outTag, totalPss, "total", false); |
| 10441 | } |
| 10442 | if (outStack != null) { |
| 10443 | appendMemBucket(outStack, totalPss, "total", true); |
| 10444 | } |
| 10445 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10446 | for (int i=0; i<oomMems.size(); i++) { |
| 10447 | MemItem miCat = oomMems.get(i); |
| 10448 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10449 | continue; |
| 10450 | } |
| 10451 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10452 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10453 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10454 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10455 | outTag.append(" / "); |
| 10456 | } |
| 10457 | if (outStack != null) { |
| 10458 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10459 | if (firstLine) { |
| 10460 | outStack.append(":"); |
| 10461 | firstLine = false; |
| 10462 | } |
| 10463 | outStack.append("\n\t at "); |
| 10464 | } else { |
| 10465 | outStack.append("$"); |
| 10466 | } |
| 10467 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10468 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10469 | MemItem mi = miCat.subitems.get(j); |
| 10470 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10471 | if (outTag != null) { |
| 10472 | outTag.append(" "); |
| 10473 | } |
| 10474 | if (outStack != null) { |
| 10475 | outStack.append("$"); |
| 10476 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10477 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10478 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10479 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10480 | } |
| 10481 | if (outStack != null) { |
| 10482 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10483 | } |
| 10484 | } |
| 10485 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10486 | outStack.append("("); |
| 10487 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10488 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10489 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10490 | outStack.append(":"); |
| 10491 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10492 | } |
| 10493 | } |
| 10494 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10495 | } |
| 10496 | } |
| 10497 | } |
| 10498 | } |
| 10499 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10500 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10501 | pw.println(); |
| 10502 | pw.println("Total PSS by process:"); |
| 10503 | dumpMemItems(pw, " ", procMems, true); |
| 10504 | pw.println(); |
| 10505 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10506 | pw.println("Total PSS by OOM adjustment:"); |
| 10507 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10508 | if (!oomOnly) { |
| 10509 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10510 | out.println(); |
| 10511 | out.println("Total PSS by category:"); |
| 10512 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10513 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10514 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10515 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | d3e677b | 2012-04-05 14:58:18 -0700 | [diff] [blame] | 10516 | final int[] SINGLE_LONG_FORMAT = new int[] { |
| 10517 | Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG |
| 10518 | }; |
| 10519 | long[] longOut = new long[1]; |
| 10520 | Process.readProcFile("/sys/kernel/mm/ksm/pages_shared", |
| 10521 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10522 | long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10523 | longOut[0] = 0; |
| 10524 | Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing", |
| 10525 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10526 | long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10527 | longOut[0] = 0; |
| 10528 | Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared", |
| 10529 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10530 | long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10531 | longOut[0] = 0; |
| 10532 | Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile", |
| 10533 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10534 | long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10535 | pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared "); |
| 10536 | pw.print(shared); pw.println(" kB"); |
| 10537 | pw.print(" "); pw.print(unshared); pw.print(" kB unshared; "); |
| 10538 | pw.print(voltile); pw.println(" kB volatile"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10539 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10540 | } |
| 10541 | |
| 10542 | /** |
| 10543 | * Searches array of arguments for the specified string |
| 10544 | * @param args array of argument strings |
| 10545 | * @param value value to search for |
| 10546 | * @return true if the value is contained in the array |
| 10547 | */ |
| 10548 | private static boolean scanArgs(String[] args, String value) { |
| 10549 | if (args != null) { |
| 10550 | for (String arg : args) { |
| 10551 | if (value.equals(arg)) { |
| 10552 | return true; |
| 10553 | } |
| 10554 | } |
| 10555 | } |
| 10556 | return false; |
| 10557 | } |
| 10558 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10559 | private final boolean removeDyingProviderLocked(ProcessRecord proc, |
| 10560 | ContentProviderRecord cpr, boolean always) { |
| 10561 | final boolean inLaunching = mLaunchingProviders.contains(cpr); |
| 10562 | |
| 10563 | if (!inLaunching || always) { |
| 10564 | synchronized (cpr) { |
| 10565 | cpr.launchingApp = null; |
| 10566 | cpr.notifyAll(); |
| 10567 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10568 | mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid)); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10569 | String names[] = cpr.info.authority.split(";"); |
| 10570 | for (int j = 0; j < names.length; j++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10571 | mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10572 | } |
| 10573 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10574 | |
| 10575 | for (int i=0; i<cpr.connections.size(); i++) { |
| 10576 | ContentProviderConnection conn = cpr.connections.get(i); |
| 10577 | if (conn.waiting) { |
| 10578 | // If this connection is waiting for the provider, then we don't |
| 10579 | // need to mess with its process unless we are always removing |
| 10580 | // or for some reason the provider is not currently launching. |
| 10581 | if (inLaunching && !always) { |
| 10582 | continue; |
| 10583 | } |
| 10584 | } |
| 10585 | ProcessRecord capp = conn.client; |
| 10586 | conn.dead = true; |
| 10587 | if (conn.stableCount > 0) { |
| 10588 | if (!capp.persistent && capp.thread != null |
| 10589 | && capp.pid != 0 |
| 10590 | && capp.pid != MY_PID) { |
| 10591 | Slog.i(TAG, "Kill " + capp.processName |
| 10592 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 10593 | + " in dying process " + (proc != null ? proc.processName : "??")); |
| 10594 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
| 10595 | capp.processName, capp.setAdj, "dying provider " |
| 10596 | + cpr.name.toShortString()); |
| 10597 | Process.killProcessQuiet(capp.pid); |
| 10598 | } |
| 10599 | } else if (capp.thread != null && conn.provider.provider != null) { |
| 10600 | try { |
| 10601 | capp.thread.unstableProviderDied(conn.provider.provider.asBinder()); |
| 10602 | } catch (RemoteException e) { |
| 10603 | } |
| 10604 | // In the protocol here, we don't expect the client to correctly |
| 10605 | // clean up this connection, we'll just remove it. |
| 10606 | cpr.connections.remove(i); |
| 10607 | conn.client.conProviders.remove(conn); |
| 10608 | } |
| 10609 | } |
| 10610 | |
| 10611 | if (inLaunching && always) { |
| 10612 | mLaunchingProviders.remove(cpr); |
| 10613 | } |
| 10614 | return inLaunching; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10615 | } |
| 10616 | |
| 10617 | /** |
| 10618 | * Main code for cleaning up a process when it has gone away. This is |
| 10619 | * called both as a result of the process dying, or directly when stopping |
| 10620 | * a process when running in single process mode. |
| 10621 | */ |
| 10622 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10623 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10624 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10625 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10626 | } |
| 10627 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10628 | mProcessesToGc.remove(app); |
| 10629 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10630 | // Dismiss any open dialogs. |
| 10631 | if (app.crashDialog != null) { |
| 10632 | app.crashDialog.dismiss(); |
| 10633 | app.crashDialog = null; |
| 10634 | } |
| 10635 | if (app.anrDialog != null) { |
| 10636 | app.anrDialog.dismiss(); |
| 10637 | app.anrDialog = null; |
| 10638 | } |
| 10639 | if (app.waitDialog != null) { |
| 10640 | app.waitDialog.dismiss(); |
| 10641 | app.waitDialog = null; |
| 10642 | } |
| 10643 | |
| 10644 | app.crashing = false; |
| 10645 | app.notResponding = false; |
| 10646 | |
| 10647 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10648 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10649 | app.thread = null; |
| 10650 | app.forcingToForeground = null; |
| 10651 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10652 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10653 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10654 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10655 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10656 | mServices.killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10657 | |
| 10658 | boolean restart = false; |
| 10659 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10660 | // Remove published content providers. |
| 10661 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10662 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10663 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10664 | ContentProviderRecord cpr = it.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10665 | |
| 10666 | final boolean always = app.bad || !allowRestart; |
| 10667 | if (removeDyingProviderLocked(app, cpr, always) || always) { |
| 10668 | // We left the provider in the launching list, need to |
| 10669 | // restart it. |
| 10670 | restart = true; |
| 10671 | } |
| 10672 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10673 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10674 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10675 | } |
| 10676 | app.pubProviders.clear(); |
| 10677 | } |
| 10678 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10679 | // Take care of any launching providers waiting for this process. |
| 10680 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10681 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10682 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10684 | // Unregister from connected content providers. |
| 10685 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10686 | for (int i=0; i<app.conProviders.size(); i++) { |
| 10687 | ContentProviderConnection conn = app.conProviders.get(i); |
| 10688 | conn.provider.connections.remove(conn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10689 | } |
| 10690 | app.conProviders.clear(); |
| 10691 | } |
| 10692 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10693 | // At this point there may be remaining entries in mLaunchingProviders |
| 10694 | // where we were the only one waiting, so they are no longer of use. |
| 10695 | // Look for these and clean up if found. |
| 10696 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10697 | // the actual situation to identify what is actually going on. |
| 10698 | if (false) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10699 | for (int i=0; i<mLaunchingProviders.size(); i++) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10700 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10701 | mLaunchingProviders.get(i); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10702 | if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10703 | synchronized (cpr) { |
| 10704 | cpr.launchingApp = null; |
| 10705 | cpr.notifyAll(); |
| 10706 | } |
| 10707 | } |
| 10708 | } |
| 10709 | } |
| 10710 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10711 | skipCurrentReceiverLocked(app); |
| 10712 | |
| 10713 | // Unregister any receivers. |
| 10714 | if (app.receivers.size() > 0) { |
| 10715 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10716 | while (it.hasNext()) { |
| 10717 | removeReceiverLocked(it.next()); |
| 10718 | } |
| 10719 | app.receivers.clear(); |
| 10720 | } |
| 10721 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10722 | // If the app is undergoing backup, tell the backup manager about it |
| 10723 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10724 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10725 | try { |
| 10726 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10727 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10728 | bm.agentDisconnected(app.info.packageName); |
| 10729 | } catch (RemoteException e) { |
| 10730 | // can't happen; backup manager is local |
| 10731 | } |
| 10732 | } |
| 10733 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 10734 | for (int i = mPendingProcessChanges.size()-1; i>=0; i--) { |
| 10735 | ProcessChangeItem item = mPendingProcessChanges.get(i); |
| 10736 | if (item.pid == app.pid) { |
| 10737 | mPendingProcessChanges.remove(i); |
| 10738 | mAvailProcessChanges.add(item); |
| 10739 | } |
| 10740 | } |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10741 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10743 | // If the caller is restarting this app, then leave it in its |
| 10744 | // current lists and let the caller take care of it. |
| 10745 | if (restarting) { |
| 10746 | return; |
| 10747 | } |
| 10748 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10749 | if (!app.persistent || app.isolated) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10750 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10751 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10752 | mProcessNames.remove(app.processName, app.uid); |
| 10753 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10754 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10755 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 10756 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10757 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10758 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10759 | } else if (!app.removed) { |
| 10760 | // This app is persistent, so we need to keep its record around. |
| 10761 | // If it is not already on the pending app list, add it there |
| 10762 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10763 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10764 | mPersistentStartingProcesses.add(app); |
| 10765 | restart = true; |
| 10766 | } |
| 10767 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10768 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 10769 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10770 | mProcessesOnHold.remove(app); |
| 10771 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10772 | if (app == mHomeProcess) { |
| 10773 | mHomeProcess = null; |
| 10774 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10775 | if (app == mPreviousProcess) { |
| 10776 | mPreviousProcess = null; |
| 10777 | } |
| 10778 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10779 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10780 | // We have components that still need to be running in the |
| 10781 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10782 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10783 | startProcessLocked(app, "restart", app.processName); |
| 10784 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10785 | // Goodbye! |
| 10786 | synchronized (mPidsSelfLocked) { |
| 10787 | mPidsSelfLocked.remove(app.pid); |
| 10788 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10789 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10790 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10791 | } |
| 10792 | } |
| 10793 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10794 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10795 | // Look through the content providers we are waiting to have launched, |
| 10796 | // and if any run in this process then either schedule a restart of |
| 10797 | // the process or kill the client waiting for it if this process has |
| 10798 | // gone bad. |
| 10799 | int NL = mLaunchingProviders.size(); |
| 10800 | boolean restart = false; |
| 10801 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10802 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10803 | if (cpr.launchingApp == app) { |
| 10804 | if (!alwaysBad && !app.bad) { |
| 10805 | restart = true; |
| 10806 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10807 | removeDyingProviderLocked(app, cpr, true); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 10808 | // cpr should have been removed from mLaunchingProviders |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10809 | NL = mLaunchingProviders.size(); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 10810 | i--; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10811 | } |
| 10812 | } |
| 10813 | } |
| 10814 | return restart; |
| 10815 | } |
| 10816 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10817 | // ========================================================= |
| 10818 | // SERVICES |
| 10819 | // ========================================================= |
| 10820 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10821 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10822 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10823 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10824 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10825 | return mServices.getRunningServiceInfoLocked(maxNum, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10826 | } |
| 10827 | } |
| 10828 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10829 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10830 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10831 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10832 | return mServices.getRunningServiceControlPanelLocked(name); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10833 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10834 | } |
| 10835 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10836 | public ComponentName startService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10837 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10838 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10839 | // Refuse possible leaked file descriptors |
| 10840 | if (service != null && service.hasFileDescriptors() == true) { |
| 10841 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10842 | } |
| 10843 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10844 | if (DEBUG_SERVICE) |
| 10845 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10846 | synchronized(this) { |
| 10847 | final int callingPid = Binder.getCallingPid(); |
| 10848 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10849 | checkValidCaller(callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10850 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10851 | ComponentName res = mServices.startServiceLocked(caller, service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10852 | resolvedType, callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10853 | Binder.restoreCallingIdentity(origId); |
| 10854 | return res; |
| 10855 | } |
| 10856 | } |
| 10857 | |
| 10858 | ComponentName startServiceInPackage(int uid, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10859 | Intent service, String resolvedType, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10860 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10861 | if (DEBUG_SERVICE) |
| 10862 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10863 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10864 | ComponentName res = mServices.startServiceLocked(null, service, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10865 | resolvedType, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10866 | Binder.restoreCallingIdentity(origId); |
| 10867 | return res; |
| 10868 | } |
| 10869 | } |
| 10870 | |
| 10871 | public int stopService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10872 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10873 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10874 | // Refuse possible leaked file descriptors |
| 10875 | if (service != null && service.hasFileDescriptors() == true) { |
| 10876 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10877 | } |
| 10878 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10879 | checkValidCaller(Binder.getCallingUid(), userId); |
| 10880 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10881 | synchronized(this) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10882 | return mServices.stopServiceLocked(caller, service, resolvedType, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10883 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10884 | } |
| 10885 | |
| 10886 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10887 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10888 | // Refuse possible leaked file descriptors |
| 10889 | if (service != null && service.hasFileDescriptors() == true) { |
| 10890 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10891 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10892 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10893 | return mServices.peekServiceLocked(service, resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10894 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10895 | } |
| 10896 | |
| 10897 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 10898 | int startId) { |
| 10899 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10900 | return mServices.stopServiceTokenLocked(className, token, startId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10902 | } |
| 10903 | |
| 10904 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10905 | int id, Notification notification, boolean removeNotification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10906 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10907 | mServices.setServiceForegroundLocked(className, token, id, notification, |
| 10908 | removeNotification); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10909 | } |
| 10910 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10911 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10912 | public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 10913 | boolean requireFull, String name, String callerPackage) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10914 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 10915 | if (callingUserId != userId) { |
| 10916 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 10917 | if ((requireFull || checkComponentPermission( |
| 10918 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10919 | callingPid, callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) |
| 10920 | && checkComponentPermission( |
| 10921 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 10922 | callingPid, callingUid, -1, true) |
| 10923 | != PackageManager.PERMISSION_GRANTED) { |
| 10924 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 10925 | // In this case, they would like to just execute as their |
| 10926 | // owner user instead of failing. |
| 10927 | userId = callingUserId; |
| 10928 | } else { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10929 | StringBuilder builder = new StringBuilder(128); |
| 10930 | builder.append("Permission Denial: "); |
| 10931 | builder.append(name); |
| 10932 | if (callerPackage != null) { |
| 10933 | builder.append(" from "); |
| 10934 | builder.append(callerPackage); |
| 10935 | } |
| 10936 | builder.append(" asks to run as user "); |
| 10937 | builder.append(userId); |
| 10938 | builder.append(" but is calling from user "); |
| 10939 | builder.append(UserHandle.getUserId(callingUid)); |
| 10940 | builder.append("; this requires "); |
| 10941 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 10942 | if (!requireFull) { |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 10943 | builder.append(" or "); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10944 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 10945 | } |
| 10946 | String msg = builder.toString(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10947 | Slog.w(TAG, msg); |
| 10948 | throw new SecurityException(msg); |
| 10949 | } |
| 10950 | } |
| 10951 | } |
| 10952 | if (userId == UserHandle.USER_CURRENT |
| 10953 | || userId == UserHandle.USER_CURRENT_OR_SELF) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 10954 | // Note that we may be accessing this outside of a lock... |
| 10955 | // shouldn't be a big deal, if this is being called outside |
| 10956 | // of a locked context there is intrinsically a race with |
| 10957 | // the value the caller will receive and someone else changing it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10958 | userId = mCurrentUserId; |
| 10959 | } |
| 10960 | if (!allowAll && userId < 0) { |
| 10961 | throw new IllegalArgumentException( |
| 10962 | "Call does not support special user #" + userId); |
| 10963 | } |
| 10964 | } |
| 10965 | return userId; |
| 10966 | } |
| 10967 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10968 | boolean isSingleton(String componentProcessName, ApplicationInfo aInfo, |
| 10969 | String className, int flags) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10970 | boolean result = false; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10971 | if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10972 | if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) { |
| 10973 | if (ActivityManager.checkUidPermission( |
| 10974 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10975 | aInfo.uid) != PackageManager.PERMISSION_GRANTED) { |
| 10976 | ComponentName comp = new ComponentName(aInfo.packageName, className); |
| 10977 | String msg = "Permission Denial: Component " + comp.flattenToShortString() |
| 10978 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 10979 | + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 10980 | Slog.w(TAG, msg); |
| 10981 | throw new SecurityException(msg); |
| 10982 | } |
| 10983 | result = true; |
| 10984 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10985 | } else if (componentProcessName == aInfo.packageName) { |
| 10986 | result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 10987 | } else if ("system".equals(componentProcessName)) { |
| 10988 | result = true; |
| 10989 | } |
| 10990 | if (DEBUG_MU) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10991 | Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo |
| 10992 | + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10993 | } |
| 10994 | return result; |
| 10995 | } |
| 10996 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10997 | public int bindService(IApplicationThread caller, IBinder token, |
| 10998 | Intent service, String resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10999 | IServiceConnection connection, int flags, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11000 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11001 | // Refuse possible leaked file descriptors |
| 11002 | if (service != null && service.hasFileDescriptors() == true) { |
| 11003 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11004 | } |
| 11005 | |
| 11006 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11007 | return mServices.bindServiceLocked(caller, token, service, resolvedType, |
| 11008 | connection, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11009 | } |
| 11010 | } |
| 11011 | |
| 11012 | public boolean unbindService(IServiceConnection connection) { |
| 11013 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11014 | return mServices.unbindServiceLocked(connection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11015 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11016 | } |
| 11017 | |
| 11018 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 11019 | // Refuse possible leaked file descriptors |
| 11020 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11021 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11022 | } |
| 11023 | |
| 11024 | synchronized(this) { |
| 11025 | if (!(token instanceof ServiceRecord)) { |
| 11026 | throw new IllegalArgumentException("Invalid service token"); |
| 11027 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11028 | mServices.publishServiceLocked((ServiceRecord)token, intent, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11029 | } |
| 11030 | } |
| 11031 | |
| 11032 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 11033 | // Refuse possible leaked file descriptors |
| 11034 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11035 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11036 | } |
| 11037 | |
| 11038 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11039 | mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11040 | } |
| 11041 | } |
| 11042 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11043 | 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] | 11044 | synchronized(this) { |
| 11045 | if (!(token instanceof ServiceRecord)) { |
| 11046 | throw new IllegalArgumentException("Invalid service token"); |
| 11047 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11048 | mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11049 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11050 | } |
| 11051 | |
| 11052 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11053 | // BACKUP AND RESTORE |
| 11054 | // ========================================================= |
| 11055 | |
| 11056 | // Cause the target app to be launched if necessary and its backup agent |
| 11057 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11058 | // activity manager to announce its creation. |
| 11059 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11060 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11061 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 11062 | |
| 11063 | synchronized(this) { |
| 11064 | // !!! TODO: currently no check here that we're already bound |
| 11065 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11066 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11067 | synchronized (stats) { |
| 11068 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11069 | } |
| 11070 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11071 | // Backup agent is now in use, its package can't be stopped. |
| 11072 | try { |
| 11073 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11074 | app.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11075 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11076 | } catch (IllegalArgumentException e) { |
| 11077 | Slog.w(TAG, "Failed trying to unstop package " |
| 11078 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11079 | } |
| 11080 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11081 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11082 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11083 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11084 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11085 | // startProcessLocked() returns existing proc's record if it's already running |
| 11086 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11087 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11088 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11089 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11090 | return false; |
| 11091 | } |
| 11092 | |
| 11093 | r.app = proc; |
| 11094 | mBackupTarget = r; |
| 11095 | mBackupAppName = app.packageName; |
| 11096 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11097 | // Try not to kill the process during backup |
| 11098 | updateOomAdjLocked(proc); |
| 11099 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11100 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11101 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11102 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11103 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11104 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11105 | proc.thread.scheduleCreateBackupAgent(app, |
| 11106 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11107 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11108 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11109 | } |
| 11110 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11111 | 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] | 11112 | } |
| 11113 | // Invariants: at this point, the target app process exists and the application |
| 11114 | // is either already running or in the process of coming up. mBackupTarget and |
| 11115 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11116 | // know that it's scheduled for a backup-agent operation. |
| 11117 | } |
| 11118 | |
| 11119 | return true; |
| 11120 | } |
| 11121 | |
| 11122 | // A backup agent has just come up |
| 11123 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11124 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11125 | + " = " + agent); |
| 11126 | |
| 11127 | synchronized(this) { |
| 11128 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11129 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11130 | return; |
| 11131 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11132 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11133 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11134 | long oldIdent = Binder.clearCallingIdentity(); |
| 11135 | try { |
| 11136 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 11137 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 11138 | bm.agentConnected(agentPackageName, agent); |
| 11139 | } catch (RemoteException e) { |
| 11140 | // can't happen; the backup manager service is local |
| 11141 | } catch (Exception e) { |
| 11142 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 11143 | e.printStackTrace(); |
| 11144 | } finally { |
| 11145 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11146 | } |
| 11147 | } |
| 11148 | |
| 11149 | // done with this agent |
| 11150 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11151 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11152 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11153 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11154 | return; |
| 11155 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11156 | |
| 11157 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11158 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11159 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11160 | return; |
| 11161 | } |
| 11162 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11163 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11164 | 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] | 11165 | return; |
| 11166 | } |
| 11167 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11168 | ProcessRecord proc = mBackupTarget.app; |
| 11169 | mBackupTarget = null; |
| 11170 | mBackupAppName = null; |
| 11171 | |
| 11172 | // Not backing this app up any more; reset its OOM adjustment |
| 11173 | updateOomAdjLocked(proc); |
| 11174 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11175 | // If the app crashed during backup, 'thread' will be null here |
| 11176 | if (proc.thread != null) { |
| 11177 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11178 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 11179 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11180 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11181 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11182 | e.printStackTrace(); |
| 11183 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11184 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11185 | } |
| 11186 | } |
| 11187 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11188 | // BROADCASTS |
| 11189 | // ========================================================= |
| 11190 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11191 | private final List getStickiesLocked(String action, IntentFilter filter, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11192 | List cur, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11193 | final ContentResolver resolver = mContext.getContentResolver(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11194 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11195 | if (stickies == null) { |
| 11196 | return cur; |
| 11197 | } |
| 11198 | final ArrayList<Intent> list = stickies.get(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11199 | if (list == null) { |
| 11200 | return cur; |
| 11201 | } |
| 11202 | int N = list.size(); |
| 11203 | for (int i=0; i<N; i++) { |
| 11204 | Intent intent = list.get(i); |
| 11205 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 11206 | if (cur == null) { |
| 11207 | cur = new ArrayList<Intent>(); |
| 11208 | } |
| 11209 | cur.add(intent); |
| 11210 | } |
| 11211 | } |
| 11212 | return cur; |
| 11213 | } |
| 11214 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11215 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 11216 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 11217 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 11218 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11219 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11220 | void skipPendingBroadcastLocked(int pid) { |
| 11221 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 11222 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11223 | queue.skipPendingBroadcastLocked(pid); |
| 11224 | } |
| 11225 | } |
| 11226 | |
| 11227 | // The app just attached; send any pending broadcasts that it should receive |
| 11228 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 11229 | boolean didSomething = false; |
| 11230 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11231 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11232 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11233 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11234 | } |
| 11235 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11236 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11237 | IIntentReceiver receiver, IntentFilter filter, String permission, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11238 | enforceNotIsolatedCaller("registerReceiver"); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11239 | int callingUid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11240 | int callingPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11241 | synchronized(this) { |
| 11242 | ProcessRecord callerApp = null; |
| 11243 | if (caller != null) { |
| 11244 | callerApp = getRecordForAppLocked(caller); |
| 11245 | if (callerApp == null) { |
| 11246 | throw new SecurityException( |
| 11247 | "Unable to find app for caller " + caller |
| 11248 | + " (pid=" + Binder.getCallingPid() |
| 11249 | + ") when registering receiver " + receiver); |
| 11250 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11251 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 11252 | !callerApp.pkgList.contains(callerPackage)) { |
| 11253 | throw new SecurityException("Given caller package " + callerPackage |
| 11254 | + " is not running in process " + callerApp); |
| 11255 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11256 | callingUid = callerApp.info.uid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11257 | callingPid = callerApp.pid; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11258 | } else { |
| 11259 | callerPackage = null; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11260 | callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11261 | callingPid = Binder.getCallingPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11262 | } |
| 11263 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11264 | userId = this.handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11265 | true, true, "registerReceiver", callerPackage); |
| 11266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11267 | List allSticky = null; |
| 11268 | |
| 11269 | // Look for any matching sticky broadcasts... |
| 11270 | Iterator actions = filter.actionsIterator(); |
| 11271 | if (actions != null) { |
| 11272 | while (actions.hasNext()) { |
| 11273 | String action = (String)actions.next(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11274 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11275 | UserHandle.USER_ALL); |
| 11276 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11277 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11278 | } |
| 11279 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11280 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11281 | UserHandle.USER_ALL); |
| 11282 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11283 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11284 | } |
| 11285 | |
| 11286 | // The first sticky in the list is returned directly back to |
| 11287 | // the client. |
| 11288 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 11289 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11290 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11291 | + ": " + sticky); |
| 11292 | |
| 11293 | if (receiver == null) { |
| 11294 | return sticky; |
| 11295 | } |
| 11296 | |
| 11297 | ReceiverList rl |
| 11298 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11299 | if (rl == null) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11300 | rl = new ReceiverList(this, callerApp, callingPid, callingUid, |
| 11301 | userId, receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11302 | if (rl.app != null) { |
| 11303 | rl.app.receivers.add(rl); |
| 11304 | } else { |
| 11305 | try { |
| 11306 | receiver.asBinder().linkToDeath(rl, 0); |
| 11307 | } catch (RemoteException e) { |
| 11308 | return sticky; |
| 11309 | } |
| 11310 | rl.linkedToDeath = true; |
| 11311 | } |
| 11312 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11313 | } else if (rl.uid != callingUid) { |
| 11314 | throw new IllegalArgumentException( |
| 11315 | "Receiver requested to register for uid " + callingUid |
| 11316 | + " was previously registered for uid " + rl.uid); |
| 11317 | } else if (rl.pid != callingPid) { |
| 11318 | throw new IllegalArgumentException( |
| 11319 | "Receiver requested to register for pid " + callingPid |
| 11320 | + " was previously registered for pid " + rl.pid); |
| 11321 | } else if (rl.userId != userId) { |
| 11322 | throw new IllegalArgumentException( |
| 11323 | "Receiver requested to register for user " + userId |
| 11324 | + " was previously registered for user " + rl.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11325 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11326 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11327 | permission, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11328 | rl.add(bf); |
| 11329 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11330 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11331 | } |
| 11332 | mReceiverResolver.addFilter(bf); |
| 11333 | |
| 11334 | // Enqueue broadcasts for all existing stickies that match |
| 11335 | // this filter. |
| 11336 | if (allSticky != null) { |
| 11337 | ArrayList receivers = new ArrayList(); |
| 11338 | receivers.add(bf); |
| 11339 | |
| 11340 | int N = allSticky.size(); |
| 11341 | for (int i=0; i<N; i++) { |
| 11342 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11343 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11344 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11345 | null, -1, -1, null, receivers, null, 0, null, null, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11346 | false, true, true, -1); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11347 | queue.enqueueParallelBroadcastLocked(r); |
| 11348 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11349 | } |
| 11350 | } |
| 11351 | |
| 11352 | return sticky; |
| 11353 | } |
| 11354 | } |
| 11355 | |
| 11356 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11357 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11358 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11359 | final long origId = Binder.clearCallingIdentity(); |
| 11360 | try { |
| 11361 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11362 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11363 | synchronized(this) { |
| 11364 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11365 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11366 | if (rl != null) { |
| 11367 | if (rl.curBroadcast != null) { |
| 11368 | BroadcastRecord r = rl.curBroadcast; |
| 11369 | final boolean doNext = finishReceiverLocked( |
| 11370 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11371 | r.resultExtras, r.resultAbort, true); |
| 11372 | if (doNext) { |
| 11373 | doTrim = true; |
| 11374 | r.queue.processNextBroadcast(false); |
| 11375 | } |
| 11376 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11377 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11378 | if (rl.app != null) { |
| 11379 | rl.app.receivers.remove(rl); |
| 11380 | } |
| 11381 | removeReceiverLocked(rl); |
| 11382 | if (rl.linkedToDeath) { |
| 11383 | rl.linkedToDeath = false; |
| 11384 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 11385 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11386 | } |
| 11387 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11388 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11389 | // If we actually concluded any broadcasts, we might now be able |
| 11390 | // to trim the recipients' apps from our working set |
| 11391 | if (doTrim) { |
| 11392 | trimApplications(); |
| 11393 | return; |
| 11394 | } |
| 11395 | |
| 11396 | } finally { |
| 11397 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11398 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11399 | } |
| 11400 | |
| 11401 | void removeReceiverLocked(ReceiverList rl) { |
| 11402 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 11403 | int N = rl.size(); |
| 11404 | for (int i=0; i<N; i++) { |
| 11405 | mReceiverResolver.removeFilter(rl.get(i)); |
| 11406 | } |
| 11407 | } |
| 11408 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11409 | private final void sendPackageBroadcastLocked(int cmd, String[] packages, int userId) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11410 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 11411 | ProcessRecord r = mLruProcesses.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11412 | if (r.thread != null && (userId == UserHandle.USER_ALL || r.userId == userId)) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11413 | try { |
| 11414 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 11415 | } catch (RemoteException ex) { |
| 11416 | } |
| 11417 | } |
| 11418 | } |
| 11419 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11420 | |
| 11421 | private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType, |
| 11422 | int[] users) { |
| 11423 | List<ResolveInfo> receivers = null; |
| 11424 | try { |
| 11425 | HashSet<ComponentName> singleUserReceivers = null; |
| 11426 | boolean scannedFirstReceivers = false; |
| 11427 | for (int user : users) { |
| 11428 | List<ResolveInfo> newReceivers = AppGlobals.getPackageManager() |
| 11429 | .queryIntentReceivers(intent, resolvedType, STOCK_PM_FLAGS, user); |
| 11430 | if (newReceivers != null && newReceivers.size() == 0) { |
| 11431 | newReceivers = null; |
| 11432 | } |
| 11433 | if (receivers == null) { |
| 11434 | receivers = newReceivers; |
| 11435 | } else if (newReceivers != null) { |
| 11436 | // We need to concatenate the additional receivers |
| 11437 | // found with what we have do far. This would be easy, |
| 11438 | // but we also need to de-dup any receivers that are |
| 11439 | // singleUser. |
| 11440 | if (!scannedFirstReceivers) { |
| 11441 | // Collect any single user receivers we had already retrieved. |
| 11442 | scannedFirstReceivers = true; |
| 11443 | for (int i=0; i<receivers.size(); i++) { |
| 11444 | ResolveInfo ri = receivers.get(i); |
| 11445 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11446 | ComponentName cn = new ComponentName( |
| 11447 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11448 | if (singleUserReceivers == null) { |
| 11449 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11450 | } |
| 11451 | singleUserReceivers.add(cn); |
| 11452 | } |
| 11453 | } |
| 11454 | } |
| 11455 | // Add the new results to the existing results, tracking |
| 11456 | // and de-dupping single user receivers. |
| 11457 | for (int i=0; i<newReceivers.size(); i++) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 11458 | ResolveInfo ri = newReceivers.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11459 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11460 | ComponentName cn = new ComponentName( |
| 11461 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11462 | if (singleUserReceivers == null) { |
| 11463 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11464 | } |
| 11465 | if (!singleUserReceivers.contains(cn)) { |
| 11466 | singleUserReceivers.add(cn); |
| 11467 | receivers.add(ri); |
| 11468 | } |
| 11469 | } else { |
| 11470 | receivers.add(ri); |
| 11471 | } |
| 11472 | } |
| 11473 | } |
| 11474 | } |
| 11475 | } catch (RemoteException ex) { |
| 11476 | // pm is in same process, this will never happen. |
| 11477 | } |
| 11478 | return receivers; |
| 11479 | } |
| 11480 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11481 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 11482 | String callerPackage, Intent intent, String resolvedType, |
| 11483 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 11484 | Bundle map, String requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11485 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 11486 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11487 | intent = new Intent(intent); |
| 11488 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11489 | // By default broadcasts do not go to stopped apps. |
| 11490 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 11491 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11492 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11493 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 11494 | + " ordered=" + ordered + " userid=" + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11495 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11496 | 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] | 11497 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11498 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11499 | userId = handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11500 | true, false, "broadcast", callerPackage); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11501 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11502 | // Make sure that the user who is receiving this broadcast is started. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11503 | // If not, we will just skip it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11504 | if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 11505 | if (callingUid != Process.SYSTEM_UID || (intent.getFlags() |
| 11506 | & Intent.FLAG_RECEIVER_BOOT_UPGRADE) == 0) { |
| 11507 | Slog.w(TAG, "Skipping broadcast of " + intent |
| 11508 | + ": user " + userId + " is stopped"); |
| 11509 | return ActivityManager.BROADCAST_SUCCESS; |
| 11510 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11511 | } |
| 11512 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11513 | /* |
| 11514 | * Prevent non-system code (defined here to be non-persistent |
| 11515 | * processes) from sending protected broadcasts. |
| 11516 | */ |
| 11517 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 11518 | || callingUid == Process.SHELL_UID || callingUid == Process.BLUETOOTH_UID || |
| 11519 | callingUid == 0) { |
| 11520 | // Always okay. |
| 11521 | } else if (callerApp == null || !callerApp.persistent) { |
| 11522 | try { |
| 11523 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
| 11524 | intent.getAction())) { |
| 11525 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11526 | + intent.getAction() + " from pid=" |
| 11527 | + callingPid + ", uid=" + callingUid; |
| 11528 | Slog.w(TAG, msg); |
| 11529 | throw new SecurityException(msg); |
| 11530 | } |
| 11531 | } catch (RemoteException e) { |
| 11532 | Slog.w(TAG, "Remote exception", e); |
| 11533 | return ActivityManager.BROADCAST_SUCCESS; |
| 11534 | } |
| 11535 | } |
| 11536 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11537 | // Handle special intents: if this broadcast is from the package |
| 11538 | // manager about a package being removed, we need to remove all of |
| 11539 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11540 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11541 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11542 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11543 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11544 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11545 | || uidRemoved) { |
| 11546 | if (checkComponentPermission( |
| 11547 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11548 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11549 | == PackageManager.PERMISSION_GRANTED) { |
| 11550 | if (uidRemoved) { |
| 11551 | final Bundle intentExtras = intent.getExtras(); |
| 11552 | final int uid = intentExtras != null |
| 11553 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11554 | if (uid >= 0) { |
| 11555 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11556 | synchronized (bs) { |
| 11557 | bs.removeUidStatsLocked(uid); |
| 11558 | } |
| 11559 | } |
| 11560 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11561 | // If resources are unavailable just force stop all |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11562 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11563 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11564 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11565 | if (list != null && (list.length > 0)) { |
| 11566 | for (String pkg : list) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11567 | forceStopPackageLocked(pkg, -1, false, true, true, false, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11568 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11569 | sendPackageBroadcastLocked( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11570 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11571 | } |
| 11572 | } else { |
| 11573 | Uri data = intent.getData(); |
| 11574 | String ssp; |
| 11575 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11576 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11577 | forceStopPackageLocked(ssp, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11578 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, |
| 11579 | false, userId); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11580 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11581 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11582 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11583 | new String[] {ssp}, userId); |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11585 | } |
| 11586 | } |
| 11587 | } |
| 11588 | } else { |
| 11589 | String msg = "Permission Denial: " + intent.getAction() |
| 11590 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11591 | + ", uid=" + callingUid + ")" |
| 11592 | + " requires " |
| 11593 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11594 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11595 | throw new SecurityException(msg); |
| 11596 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11597 | |
| 11598 | // Special case for adding a package: by default turn on compatibility |
| 11599 | // mode. |
| 11600 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 11601 | Uri data = intent.getData(); |
| 11602 | String ssp; |
| 11603 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11604 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 11605 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11607 | } |
| 11608 | |
| 11609 | /* |
| 11610 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11611 | * of all currently running processes. This message will get queued up before the broadcast |
| 11612 | * happens. |
| 11613 | */ |
| 11614 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11615 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11616 | } |
| 11617 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11618 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11619 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11620 | } |
| 11621 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11622 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11623 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11624 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11625 | } |
| 11626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11627 | // Add to the sticky list if requested. |
| 11628 | if (sticky) { |
| 11629 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11630 | callingPid, callingUid) |
| 11631 | != PackageManager.PERMISSION_GRANTED) { |
| 11632 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11633 | + callingPid + ", uid=" + callingUid |
| 11634 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11635 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11636 | throw new SecurityException(msg); |
| 11637 | } |
| 11638 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11639 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11640 | + " and enforce permission " + requiredPermission); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11641 | return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11642 | } |
| 11643 | if (intent.getComponent() != null) { |
| 11644 | throw new SecurityException( |
| 11645 | "Sticky broadcasts can't target a specific component"); |
| 11646 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11647 | // We use userId directly here, since the "all" target is maintained |
| 11648 | // as a separate set of sticky broadcasts. |
| 11649 | if (userId != UserHandle.USER_ALL) { |
| 11650 | // But first, if this is not a broadcast to all users, then |
| 11651 | // make sure it doesn't conflict with an existing broadcast to |
| 11652 | // all users. |
| 11653 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get( |
| 11654 | UserHandle.USER_ALL); |
| 11655 | if (stickies != null) { |
| 11656 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11657 | if (list != null) { |
| 11658 | int N = list.size(); |
| 11659 | int i; |
| 11660 | for (i=0; i<N; i++) { |
| 11661 | if (intent.filterEquals(list.get(i))) { |
| 11662 | throw new IllegalArgumentException( |
| 11663 | "Sticky broadcast " + intent + " for user " |
| 11664 | + userId + " conflicts with existing global broadcast"); |
| 11665 | } |
| 11666 | } |
| 11667 | } |
| 11668 | } |
| 11669 | } |
| 11670 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11671 | if (stickies == null) { |
| 11672 | stickies = new HashMap<String, ArrayList<Intent>>(); |
| 11673 | mStickyBroadcasts.put(userId, stickies); |
| 11674 | } |
| 11675 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11676 | if (list == null) { |
| 11677 | list = new ArrayList<Intent>(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11678 | stickies.put(intent.getAction(), list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11679 | } |
| 11680 | int N = list.size(); |
| 11681 | int i; |
| 11682 | for (i=0; i<N; i++) { |
| 11683 | if (intent.filterEquals(list.get(i))) { |
| 11684 | // This sticky already exists, replace it. |
| 11685 | list.set(i, new Intent(intent)); |
| 11686 | break; |
| 11687 | } |
| 11688 | } |
| 11689 | if (i >= N) { |
| 11690 | list.add(new Intent(intent)); |
| 11691 | } |
| 11692 | } |
| 11693 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11694 | int[] users; |
| 11695 | if (userId == UserHandle.USER_ALL) { |
| 11696 | // Caller wants broadcast to go to all started users. |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11697 | users = mStartedUserArray; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11698 | } else { |
| 11699 | // Caller wants broadcast to go to one specific user. |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11700 | users = mCurrentUserArray; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11701 | } |
| 11702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11703 | // Figure out who all will receive this broadcast. |
| 11704 | List receivers = null; |
| 11705 | List<BroadcastFilter> registeredReceivers = null; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11706 | // Need to resolve the intent to interested receivers... |
| 11707 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11708 | == 0) { |
| 11709 | receivers = collectReceiverComponents(intent, resolvedType, users); |
| 11710 | } |
| 11711 | if (intent.getComponent() == null) { |
| 11712 | registeredReceivers = mReceiverResolver.queryIntent(intent, |
| 11713 | resolvedType, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11714 | } |
| 11715 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11716 | final boolean replacePending = |
| 11717 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11718 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11719 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11720 | + " replacePending=" + replacePending); |
| 11721 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11722 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11723 | if (!ordered && NR > 0) { |
| 11724 | // If we are not serializing this broadcast, then send the |
| 11725 | // registered receivers separately so they don't wait for the |
| 11726 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11727 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11728 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11729 | callerPackage, callingPid, callingUid, requiredPermission, |
| 11730 | registeredReceivers, resultTo, resultCode, resultData, map, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11731 | ordered, sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11732 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11733 | TAG, "Enqueueing parallel broadcast " + r); |
| 11734 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11735 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11736 | queue.enqueueParallelBroadcastLocked(r); |
| 11737 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11738 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11739 | registeredReceivers = null; |
| 11740 | NR = 0; |
| 11741 | } |
| 11742 | |
| 11743 | // Merge into one list. |
| 11744 | int ir = 0; |
| 11745 | if (receivers != null) { |
| 11746 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11747 | // being added to see this broadcast. This prevents them from |
| 11748 | // using this as a back door to get run as soon as they are |
| 11749 | // installed. Maybe in the future we want to have a special install |
| 11750 | // broadcast or such for apps, but we'd like to deliberately make |
| 11751 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11752 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11753 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11754 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11755 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11756 | Uri data = intent.getData(); |
| 11757 | if (data != null) { |
| 11758 | String pkgName = data.getSchemeSpecificPart(); |
| 11759 | if (pkgName != null) { |
| 11760 | skipPackages = new String[] { pkgName }; |
| 11761 | } |
| 11762 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11763 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11764 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11765 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11766 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 11767 | for (String skipPackage : skipPackages) { |
| 11768 | if (skipPackage != null) { |
| 11769 | int NT = receivers.size(); |
| 11770 | for (int it=0; it<NT; it++) { |
| 11771 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 11772 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 11773 | receivers.remove(it); |
| 11774 | it--; |
| 11775 | NT--; |
| 11776 | } |
| 11777 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11778 | } |
| 11779 | } |
| 11780 | } |
| 11781 | |
| 11782 | int NT = receivers != null ? receivers.size() : 0; |
| 11783 | int it = 0; |
| 11784 | ResolveInfo curt = null; |
| 11785 | BroadcastFilter curr = null; |
| 11786 | while (it < NT && ir < NR) { |
| 11787 | if (curt == null) { |
| 11788 | curt = (ResolveInfo)receivers.get(it); |
| 11789 | } |
| 11790 | if (curr == null) { |
| 11791 | curr = registeredReceivers.get(ir); |
| 11792 | } |
| 11793 | if (curr.getPriority() >= curt.priority) { |
| 11794 | // Insert this broadcast record into the final list. |
| 11795 | receivers.add(it, curr); |
| 11796 | ir++; |
| 11797 | curr = null; |
| 11798 | it++; |
| 11799 | NT++; |
| 11800 | } else { |
| 11801 | // Skip to the next ResolveInfo in the final list. |
| 11802 | it++; |
| 11803 | curt = null; |
| 11804 | } |
| 11805 | } |
| 11806 | } |
| 11807 | while (ir < NR) { |
| 11808 | if (receivers == null) { |
| 11809 | receivers = new ArrayList(); |
| 11810 | } |
| 11811 | receivers.add(registeredReceivers.get(ir)); |
| 11812 | ir++; |
| 11813 | } |
| 11814 | |
| 11815 | if ((receivers != null && receivers.size() > 0) |
| 11816 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11817 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11818 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11819 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11820 | receivers, resultTo, resultCode, resultData, map, ordered, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11821 | sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11822 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11823 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11824 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11825 | if (DEBUG_BROADCAST) { |
| 11826 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11827 | 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] | 11828 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11829 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11830 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11831 | queue.enqueueOrderedBroadcastLocked(r); |
| 11832 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11833 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11834 | } |
| 11835 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11836 | return ActivityManager.BROADCAST_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11837 | } |
| 11838 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11839 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11840 | // Refuse possible leaked file descriptors |
| 11841 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11842 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11843 | } |
| 11844 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11845 | int flags = intent.getFlags(); |
| 11846 | |
| 11847 | if (!mProcessesReady) { |
| 11848 | // if the caller really truly claims to know what they're doing, go |
| 11849 | // ahead and allow the broadcast without launching any receivers |
| 11850 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 11851 | intent = new Intent(intent); |
| 11852 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 11853 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 11854 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 11855 | + " before boot completion"); |
| 11856 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 11857 | } |
| 11858 | } |
| 11859 | |
| 11860 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 11861 | throw new IllegalArgumentException( |
| 11862 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 11863 | } |
| 11864 | |
| 11865 | return intent; |
| 11866 | } |
| 11867 | |
| 11868 | public final int broadcastIntent(IApplicationThread caller, |
| 11869 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11870 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11871 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11872 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11873 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11874 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11875 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11876 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11877 | final int callingPid = Binder.getCallingPid(); |
| 11878 | final int callingUid = Binder.getCallingUid(); |
| 11879 | final long origId = Binder.clearCallingIdentity(); |
| 11880 | int res = broadcastIntentLocked(callerApp, |
| 11881 | callerApp != null ? callerApp.info.packageName : null, |
| 11882 | intent, resolvedType, resultTo, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11883 | resultCode, resultData, map, requiredPermission, serialized, sticky, |
| 11884 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11885 | Binder.restoreCallingIdentity(origId); |
| 11886 | return res; |
| 11887 | } |
| 11888 | } |
| 11889 | |
| 11890 | int broadcastIntentInPackage(String packageName, int uid, |
| 11891 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11892 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11893 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11894 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11895 | intent = verifyBroadcastLocked(intent); |
| 11896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11897 | final long origId = Binder.clearCallingIdentity(); |
| 11898 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 11899 | resultTo, resultCode, resultData, map, requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11900 | serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11901 | Binder.restoreCallingIdentity(origId); |
| 11902 | return res; |
| 11903 | } |
| 11904 | } |
| 11905 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11906 | 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] | 11907 | // Refuse possible leaked file descriptors |
| 11908 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11909 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11910 | } |
| 11911 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11912 | userId = handleIncomingUser(Binder.getCallingPid(), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11913 | Binder.getCallingUid(), userId, true, false, "removeStickyBroadcast", null); |
| 11914 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11915 | synchronized(this) { |
| 11916 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 11917 | != PackageManager.PERMISSION_GRANTED) { |
| 11918 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 11919 | + Binder.getCallingPid() |
| 11920 | + ", uid=" + Binder.getCallingUid() |
| 11921 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11922 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11923 | throw new SecurityException(msg); |
| 11924 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11925 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11926 | if (stickies != null) { |
| 11927 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11928 | if (list != null) { |
| 11929 | int N = list.size(); |
| 11930 | int i; |
| 11931 | for (i=0; i<N; i++) { |
| 11932 | if (intent.filterEquals(list.get(i))) { |
| 11933 | list.remove(i); |
| 11934 | break; |
| 11935 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11936 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11937 | if (list.size() <= 0) { |
| 11938 | stickies.remove(intent.getAction()); |
| 11939 | } |
| 11940 | } |
| 11941 | if (stickies.size() <= 0) { |
| 11942 | mStickyBroadcasts.remove(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11943 | } |
| 11944 | } |
| 11945 | } |
| 11946 | } |
| 11947 | |
| 11948 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 11949 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 11950 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11951 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 11952 | if (r == null) { |
| 11953 | 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] | 11954 | return false; |
| 11955 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11956 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11957 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 11958 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11959 | } |
| 11960 | |
| 11961 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 11962 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11963 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11964 | |
| 11965 | // Refuse possible leaked file descriptors |
| 11966 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 11967 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11968 | } |
| 11969 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11970 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11971 | try { |
| 11972 | boolean doNext = false; |
| 11973 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11974 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11975 | synchronized(this) { |
| 11976 | r = broadcastRecordForReceiverLocked(who); |
| 11977 | if (r != null) { |
| 11978 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 11979 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11980 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11981 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11982 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11983 | if (doNext) { |
| 11984 | r.queue.processNextBroadcast(false); |
| 11985 | } |
| 11986 | trimApplications(); |
| 11987 | } finally { |
| 11988 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11989 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11990 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11991 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11992 | // ========================================================= |
| 11993 | // INSTRUMENTATION |
| 11994 | // ========================================================= |
| 11995 | |
| 11996 | public boolean startInstrumentation(ComponentName className, |
| 11997 | String profileFile, int flags, Bundle arguments, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 11998 | IInstrumentationWatcher watcher, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11999 | enforceNotIsolatedCaller("startInstrumentation"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 12000 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12001 | userId, false, true, "startInstrumentation", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12002 | // Refuse possible leaked file descriptors |
| 12003 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 12004 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12005 | } |
| 12006 | |
| 12007 | synchronized(this) { |
| 12008 | InstrumentationInfo ii = null; |
| 12009 | ApplicationInfo ai = null; |
| 12010 | try { |
| 12011 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12012 | className, STOCK_PM_FLAGS); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12013 | ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 12014 | ii.targetPackage, STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12015 | } catch (PackageManager.NameNotFoundException e) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12016 | } catch (RemoteException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12017 | } |
| 12018 | if (ii == null) { |
| 12019 | reportStartInstrumentationFailure(watcher, className, |
| 12020 | "Unable to find instrumentation info for: " + className); |
| 12021 | return false; |
| 12022 | } |
| 12023 | if (ai == null) { |
| 12024 | reportStartInstrumentationFailure(watcher, className, |
| 12025 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12026 | return false; |
| 12027 | } |
| 12028 | |
| 12029 | int match = mContext.getPackageManager().checkSignatures( |
| 12030 | ii.targetPackage, ii.packageName); |
| 12031 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12032 | String msg = "Permission Denial: starting instrumentation " |
| 12033 | + className + " from pid=" |
| 12034 | + Binder.getCallingPid() |
| 12035 | + ", uid=" + Binder.getCallingPid() |
| 12036 | + " not allowed because package " + ii.packageName |
| 12037 | + " does not have a signature matching the target " |
| 12038 | + ii.targetPackage; |
| 12039 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12040 | throw new SecurityException(msg); |
| 12041 | } |
| 12042 | |
| 12043 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12044 | // Instrumentation can kill and relaunch even persistent processes |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12045 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 12046 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12047 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12048 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12049 | app.instrumentationProfileFile = profileFile; |
| 12050 | app.instrumentationArguments = arguments; |
| 12051 | app.instrumentationWatcher = watcher; |
| 12052 | app.instrumentationResultClass = className; |
| 12053 | Binder.restoreCallingIdentity(origId); |
| 12054 | } |
| 12055 | |
| 12056 | return true; |
| 12057 | } |
| 12058 | |
| 12059 | /** |
| 12060 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 12061 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 12062 | * the "am" command to report errors with more information. |
| 12063 | * |
| 12064 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 12065 | * @param cn The component name of the instrumentation. |
| 12066 | * @param report The error report. |
| 12067 | */ |
| 12068 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 12069 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12070 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12071 | try { |
| 12072 | if (watcher != null) { |
| 12073 | Bundle results = new Bundle(); |
| 12074 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 12075 | results.putString("Error", report); |
| 12076 | watcher.instrumentationStatus(cn, -1, results); |
| 12077 | } |
| 12078 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12079 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12080 | } |
| 12081 | } |
| 12082 | |
| 12083 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 12084 | if (app.instrumentationWatcher != null) { |
| 12085 | try { |
| 12086 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 12087 | app.instrumentationWatcher.instrumentationFinished( |
| 12088 | app.instrumentationClass, |
| 12089 | resultCode, |
| 12090 | results); |
| 12091 | } catch (RemoteException e) { |
| 12092 | } |
| 12093 | } |
| 12094 | app.instrumentationWatcher = null; |
| 12095 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12096 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12097 | app.instrumentationProfileFile = null; |
| 12098 | app.instrumentationArguments = null; |
| 12099 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 12100 | 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] | 12101 | } |
| 12102 | |
| 12103 | public void finishInstrumentation(IApplicationThread target, |
| 12104 | int resultCode, Bundle results) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 12105 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12106 | // Refuse possible leaked file descriptors |
| 12107 | if (results != null && results.hasFileDescriptors()) { |
| 12108 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12109 | } |
| 12110 | |
| 12111 | synchronized(this) { |
| 12112 | ProcessRecord app = getRecordForAppLocked(target); |
| 12113 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12114 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12115 | return; |
| 12116 | } |
| 12117 | final long origId = Binder.clearCallingIdentity(); |
| 12118 | finishInstrumentationLocked(app, resultCode, results); |
| 12119 | Binder.restoreCallingIdentity(origId); |
| 12120 | } |
| 12121 | } |
| 12122 | |
| 12123 | // ========================================================= |
| 12124 | // CONFIGURATION |
| 12125 | // ========================================================= |
| 12126 | |
| 12127 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 12128 | ConfigurationInfo config = new ConfigurationInfo(); |
| 12129 | synchronized (this) { |
| 12130 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 12131 | config.reqKeyboardType = mConfiguration.keyboard; |
| 12132 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12133 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 12134 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12135 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 12136 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12137 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 12138 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12139 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 12140 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 12141 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12142 | } |
| 12143 | return config; |
| 12144 | } |
| 12145 | |
| 12146 | public Configuration getConfiguration() { |
| 12147 | Configuration ci; |
| 12148 | synchronized(this) { |
| 12149 | ci = new Configuration(mConfiguration); |
| 12150 | } |
| 12151 | return ci; |
| 12152 | } |
| 12153 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12154 | public void updatePersistentConfiguration(Configuration values) { |
| 12155 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12156 | "updateConfiguration()"); |
| 12157 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 12158 | "updateConfiguration()"); |
| 12159 | if (values == null) { |
| 12160 | throw new NullPointerException("Configuration must not be null"); |
| 12161 | } |
| 12162 | |
| 12163 | synchronized(this) { |
| 12164 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12165 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12166 | Binder.restoreCallingIdentity(origId); |
| 12167 | } |
| 12168 | } |
| 12169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12170 | public void updateConfiguration(Configuration values) { |
| 12171 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12172 | "updateConfiguration()"); |
| 12173 | |
| 12174 | synchronized(this) { |
| 12175 | if (values == null && mWindowManager != null) { |
| 12176 | // sentinel: fetch the current configuration from the window manager |
| 12177 | values = mWindowManager.computeNewConfiguration(); |
| 12178 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12179 | |
| 12180 | if (mWindowManager != null) { |
| 12181 | mProcessList.applyDisplaySize(mWindowManager); |
| 12182 | } |
| 12183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12184 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12185 | if (values != null) { |
| 12186 | Settings.System.clearConfiguration(values); |
| 12187 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12188 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12189 | Binder.restoreCallingIdentity(origId); |
| 12190 | } |
| 12191 | } |
| 12192 | |
| 12193 | /** |
| 12194 | * Do either or both things: (1) change the current configuration, and (2) |
| 12195 | * make sure the given activity is running with the (now) current |
| 12196 | * configuration. Returns true if the activity has been left running, or |
| 12197 | * false if <var>starting</var> is being destroyed to match the new |
| 12198 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12199 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12200 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12201 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12202 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 12203 | // do nothing if we are headless |
| 12204 | if (mHeadless) return true; |
| 12205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12206 | int changes = 0; |
| 12207 | |
| 12208 | boolean kept = true; |
| 12209 | |
| 12210 | if (values != null) { |
| 12211 | Configuration newConfig = new Configuration(mConfiguration); |
| 12212 | changes = newConfig.updateFrom(values); |
| 12213 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12214 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12215 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12216 | } |
| 12217 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12218 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12219 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12220 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12221 | saveLocaleLocked(values.locale, |
| 12222 | !values.locale.equals(mConfiguration.locale), |
| 12223 | values.userSetLocale); |
| 12224 | } |
| 12225 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12226 | mConfigurationSeq++; |
| 12227 | if (mConfigurationSeq <= 0) { |
| 12228 | mConfigurationSeq = 1; |
| 12229 | } |
| 12230 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12231 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12232 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12233 | |
| 12234 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12235 | |
| 12236 | // TODO: If our config changes, should we auto dismiss any currently |
| 12237 | // showing dialogs? |
| 12238 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12239 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12240 | AttributeCache ac = AttributeCache.instance(); |
| 12241 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12242 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12243 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12244 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12245 | // Make sure all resources in our process are updated |
| 12246 | // right now, so that anyone who is going to retrieve |
| 12247 | // resource values after we return will be sure to get |
| 12248 | // the new ones. This is especially important during |
| 12249 | // boot, where the first config change needs to guarantee |
| 12250 | // all resources have that config before following boot |
| 12251 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12252 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12253 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12254 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12255 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12256 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12257 | mHandler.sendMessage(msg); |
| 12258 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12259 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12260 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12261 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12262 | try { |
| 12263 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12264 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12265 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12266 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12267 | } |
| 12268 | } catch (Exception e) { |
| 12269 | } |
| 12270 | } |
| 12271 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12272 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 12273 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12274 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12275 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12276 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 12277 | broadcastIntentLocked(null, null, |
| 12278 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 12279 | null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12280 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12281 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12282 | } |
| 12283 | } |
| 12284 | |
| 12285 | if (changes != 0 && starting == null) { |
| 12286 | // If the configuration changed, and the caller is not already |
| 12287 | // in the process of starting an activity, then find the top |
| 12288 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12289 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12290 | } |
| 12291 | |
| 12292 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12293 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12294 | // And we need to make sure at this point that all other activities |
| 12295 | // are made visible with the correct configuration. |
| 12296 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12297 | } |
| 12298 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12299 | if (values != null && mWindowManager != null) { |
| 12300 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12301 | } |
| 12302 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12303 | return kept; |
| 12304 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12305 | |
| 12306 | /** |
| 12307 | * Decide based on the configuration whether we should shouw the ANR, |
| 12308 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 12309 | * press the on-screen buttons, we shouldn't show the dialog. |
| 12310 | * |
| 12311 | * A thought: SystemUI might also want to get told about this, the Power |
| 12312 | * dialog / global actions also might want different behaviors. |
| 12313 | */ |
| 12314 | private static final boolean shouldShowDialogs(Configuration config) { |
| 12315 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 12316 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 12317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12318 | |
| 12319 | /** |
| 12320 | * Save the locale. You must be inside a synchronized (this) block. |
| 12321 | */ |
| 12322 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 12323 | if(isDiff) { |
| 12324 | SystemProperties.set("user.language", l.getLanguage()); |
| 12325 | SystemProperties.set("user.region", l.getCountry()); |
| 12326 | } |
| 12327 | |
| 12328 | if(isPersist) { |
| 12329 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 12330 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 12331 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 12332 | } |
| 12333 | } |
| 12334 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12335 | @Override |
| 12336 | public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) { |
| 12337 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12338 | return srec != null && srec.task.affinity != null && |
| 12339 | srec.task.affinity.equals(destAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12340 | } |
| 12341 | |
| 12342 | public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode, |
| 12343 | Intent resultData) { |
| 12344 | ComponentName dest = destIntent.getComponent(); |
| 12345 | |
| 12346 | synchronized (this) { |
| 12347 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12348 | if (srec == null) { |
| 12349 | return false; |
| 12350 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12351 | ArrayList<ActivityRecord> history = srec.stack.mHistory; |
| 12352 | final int start = history.indexOf(srec); |
| 12353 | if (start < 0) { |
| 12354 | // Current activity is not in history stack; do nothing. |
| 12355 | return false; |
| 12356 | } |
| 12357 | int finishTo = start - 1; |
| 12358 | ActivityRecord parent = null; |
| 12359 | boolean foundParentInTask = false; |
| 12360 | if (dest != null) { |
| 12361 | TaskRecord tr = srec.task; |
| 12362 | for (int i = start - 1; i >= 0; i--) { |
| 12363 | ActivityRecord r = history.get(i); |
| 12364 | if (tr != r.task) { |
| 12365 | // Couldn't find parent in the same task; stop at the one above this. |
| 12366 | // (Root of current task; in-app "home" behavior) |
| 12367 | // Always at least finish the current activity. |
| 12368 | finishTo = Math.min(start - 1, i + 1); |
| 12369 | parent = history.get(finishTo); |
| 12370 | break; |
| 12371 | } else if (r.info.packageName.equals(dest.getPackageName()) && |
| 12372 | r.info.name.equals(dest.getClassName())) { |
| 12373 | finishTo = i; |
| 12374 | parent = r; |
| 12375 | foundParentInTask = true; |
| 12376 | break; |
| 12377 | } |
| 12378 | } |
| 12379 | } |
| 12380 | |
| 12381 | if (mController != null) { |
| 12382 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
| 12383 | if (next != null) { |
| 12384 | // ask watcher if this is allowed |
| 12385 | boolean resumeOK = true; |
| 12386 | try { |
| 12387 | resumeOK = mController.activityResuming(next.packageName); |
| 12388 | } catch (RemoteException e) { |
| 12389 | mController = null; |
| 12390 | } |
| 12391 | |
| 12392 | if (!resumeOK) { |
| 12393 | return false; |
| 12394 | } |
| 12395 | } |
| 12396 | } |
| 12397 | final long origId = Binder.clearCallingIdentity(); |
| 12398 | for (int i = start; i > finishTo; i--) { |
| 12399 | ActivityRecord r = history.get(i); |
| 12400 | mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 12401 | "navigate-up", true); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12402 | // Only return the supplied result for the first activity finished |
| 12403 | resultCode = Activity.RESULT_CANCELED; |
| 12404 | resultData = null; |
| 12405 | } |
| 12406 | |
| 12407 | if (parent != null && foundParentInTask) { |
| 12408 | final int parentLaunchMode = parent.info.launchMode; |
| 12409 | final int destIntentFlags = destIntent.getFlags(); |
| 12410 | if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || |
| 12411 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK || |
| 12412 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP || |
| 12413 | (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
Adam Powell | 69de7e1 | 2012-05-07 18:42:24 -0700 | [diff] [blame] | 12414 | parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12415 | } else { |
| 12416 | try { |
| 12417 | ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo( |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 12418 | destIntent.getComponent(), 0, srec.userId); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12419 | int res = mMainStack.startActivityLocked(srec.app.thread, destIntent, |
| 12420 | null, aInfo, parent.appToken, null, |
| 12421 | 0, -1, parent.launchedFromUid, 0, null, true, null); |
| 12422 | foundParentInTask = res == ActivityManager.START_SUCCESS; |
| 12423 | } catch (RemoteException e) { |
| 12424 | foundParentInTask = false; |
| 12425 | } |
| 12426 | mMainStack.requestFinishActivityLocked(parent.appToken, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 12427 | resultData, "navigate-up", true); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12428 | } |
| 12429 | } |
| 12430 | Binder.restoreCallingIdentity(origId); |
| 12431 | return foundParentInTask; |
| 12432 | } |
| 12433 | } |
| 12434 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 12435 | public int getLaunchedFromUid(IBinder activityToken) { |
| 12436 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 12437 | if (srec == null) { |
| 12438 | return -1; |
| 12439 | } |
| 12440 | return srec.launchedFromUid; |
| 12441 | } |
| 12442 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12443 | // ========================================================= |
| 12444 | // LIFETIME MANAGEMENT |
| 12445 | // ========================================================= |
| 12446 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12447 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 12448 | // on, or 'null' if the app is not an active broadcast recipient. |
| 12449 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 12450 | BroadcastRecord r = app.curReceiver; |
| 12451 | if (r != null) { |
| 12452 | return r.queue; |
| 12453 | } |
| 12454 | |
| 12455 | // It's not the current receiver, but it might be starting up to become one |
| 12456 | synchronized (this) { |
| 12457 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12458 | r = queue.mPendingBroadcast; |
| 12459 | if (r != null && r.curApp == app) { |
| 12460 | // found it; report which queue it's in |
| 12461 | return queue; |
| 12462 | } |
| 12463 | } |
| 12464 | } |
| 12465 | |
| 12466 | return null; |
| 12467 | } |
| 12468 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12469 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12470 | int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12471 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12472 | // This adjustment has already been computed. If we are calling |
| 12473 | // from the top, we may have already computed our adjustment with |
| 12474 | // an earlier hidden adjustment that isn't really for us... if |
| 12475 | // so, use the new hidden adjustment. |
| 12476 | if (!recursed && app.hidden) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12477 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = |
| 12478 | app.hasActivities ? hiddenAdj : emptyAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12479 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12480 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12481 | } |
| 12482 | |
| 12483 | if (app.thread == null) { |
| 12484 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12485 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12486 | 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] | 12487 | } |
| 12488 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12489 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 12490 | app.adjSource = null; |
| 12491 | app.adjTarget = null; |
| 12492 | app.empty = false; |
| 12493 | app.hidden = false; |
| 12494 | |
| 12495 | final int activitiesSize = app.activities.size(); |
| 12496 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12497 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12498 | // The max adjustment doesn't allow this app to be anything |
| 12499 | // below foreground, so it is not worth doing work for it. |
| 12500 | app.adjType = "fixed"; |
| 12501 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12502 | app.curRawAdj = app.nonStoppingAdj = app.maxAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12503 | app.hasActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12504 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12505 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12506 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12507 | // System process can do UI, and when they do we want to have |
| 12508 | // them trim their memory after the user leaves the UI. To |
| 12509 | // facilitate this, here we need to determine whether or not it |
| 12510 | // is currently showing UI. |
| 12511 | app.systemNoUi = true; |
| 12512 | if (app == TOP_APP) { |
| 12513 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12514 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12515 | } else if (activitiesSize > 0) { |
| 12516 | for (int j = 0; j < activitiesSize; j++) { |
| 12517 | final ActivityRecord r = app.activities.get(j); |
| 12518 | if (r.visible) { |
| 12519 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12520 | } |
| 12521 | if (r.app == app) { |
| 12522 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12523 | } |
| 12524 | } |
| 12525 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12526 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12527 | } |
| 12528 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12529 | app.keeping = false; |
| 12530 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12531 | app.hasActivities = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12532 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12533 | // Determine the importance of the process, starting with most |
| 12534 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12535 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12536 | int schedGroup; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12537 | boolean foregroundActivities = false; |
| 12538 | boolean interesting = false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12539 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12540 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12541 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12542 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12543 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12544 | app.adjType = "top-activity"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12545 | foregroundActivities = true; |
| 12546 | interesting = true; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12547 | app.hasActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12548 | } else if (app.instrumentationClass != null) { |
| 12549 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12550 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12551 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12552 | app.adjType = "instrumentation"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12553 | interesting = true; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12554 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12555 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12556 | // counts as being in the foreground for OOM killer purposes. |
| 12557 | // It's placed in a sched group based on the nature of the |
| 12558 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12559 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12560 | schedGroup = (queue == mFgBroadcastQueue) |
| 12561 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12562 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12563 | } else if (app.executingServices.size() > 0) { |
| 12564 | // An app that is currently executing a service callback also |
| 12565 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12566 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12567 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12568 | app.adjType = "exec-service"; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12569 | } else { |
| 12570 | // Assume process is hidden (has activities); we will correct |
| 12571 | // later if this is not the case. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12572 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12573 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12574 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12575 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12576 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12577 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12578 | boolean hasStoppingActivities = false; |
| 12579 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12580 | // Examine all activities if not already foreground. |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12581 | if (!foregroundActivities && activitiesSize > 0) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12582 | for (int j = 0; j < activitiesSize; j++) { |
| 12583 | final ActivityRecord r = app.activities.get(j); |
| 12584 | if (r.visible) { |
| 12585 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12586 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12587 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12588 | app.adjType = "visible"; |
| 12589 | } |
| 12590 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12591 | app.hidden = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12592 | app.hasActivities = true; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12593 | foregroundActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12594 | break; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12595 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12596 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12597 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12598 | app.adjType = "pausing"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12599 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 12600 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12601 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12602 | } else if (r.state == ActivityState.STOPPING) { |
| 12603 | // We will apply the actual adjustment later, because |
| 12604 | // we want to allow this process to immediately go through |
| 12605 | // any memory trimming that is in effect. |
| 12606 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12607 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12608 | hasStoppingActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12609 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12610 | if (r.app == app) { |
| 12611 | app.hasActivities = true; |
| 12612 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12613 | } |
| 12614 | } |
| 12615 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12616 | if (adj == hiddenAdj && !app.hasActivities) { |
| 12617 | // Whoops, this process is completely empty as far as we know |
| 12618 | // at this point. |
| 12619 | adj = emptyAdj; |
| 12620 | app.empty = true; |
| 12621 | app.adjType = "bg-empty"; |
| 12622 | } |
| 12623 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12624 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12625 | if (app.foregroundServices) { |
| 12626 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12627 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12628 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12629 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12630 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12631 | } else if (app.forcingToForeground != null) { |
| 12632 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12633 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12634 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12635 | app.adjType = "force-foreground"; |
| 12636 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12637 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12638 | } |
| 12639 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12640 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12641 | if (app.foregroundServices) { |
| 12642 | interesting = true; |
| 12643 | } |
| 12644 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12645 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12646 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12647 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12648 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12649 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12650 | app.adjType = "heavy"; |
| 12651 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12652 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12653 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12654 | // This process is hosting what we currently consider to be the |
| 12655 | // 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] | 12656 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12657 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12658 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12659 | app.adjType = "home"; |
| 12660 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12661 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 12662 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 12663 | && app.activities.size() > 0) { |
| 12664 | // This was the previous process that showed UI to the user. |
| 12665 | // We want to try to keep it around more aggressively, to give |
| 12666 | // a good experience around switching between two apps. |
| 12667 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 12668 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12669 | app.hidden = false; |
| 12670 | app.adjType = "previous"; |
| 12671 | } |
| 12672 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12673 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 12674 | + " reason=" + app.adjType); |
| 12675 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12676 | // 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] | 12677 | // there are applications dependent on our services or providers, but |
| 12678 | // this gives us a baseline and makes sure we don't get into an |
| 12679 | // infinite recursion. |
| 12680 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12681 | app.curRawAdj = app.nonStoppingAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12682 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12683 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 12684 | // 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] | 12685 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12686 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12687 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12688 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12689 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12690 | } |
| 12691 | } |
| 12692 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12693 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12694 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12695 | final long now = SystemClock.uptimeMillis(); |
| 12696 | // This process is more important if the top activity is |
| 12697 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12698 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12699 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12700 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12701 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12702 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12703 | // If this process has shown some UI, let it immediately |
| 12704 | // go to the LRU list because it may be pretty heavy with |
| 12705 | // UI stuff. We'll tag it with a label just to help |
| 12706 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12707 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12708 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12709 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12710 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12711 | if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12712 | // This service has seen some activity within |
| 12713 | // recent memory, so we will keep its process ahead |
| 12714 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12715 | if (adj > ProcessList.SERVICE_ADJ) { |
| 12716 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12717 | app.adjType = "started-services"; |
| 12718 | app.hidden = false; |
| 12719 | } |
| 12720 | } |
| 12721 | // If we have let the service slide into the background |
| 12722 | // state, still have some text describing what it is doing |
| 12723 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12724 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12725 | app.adjType = "started-bg-services"; |
| 12726 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12727 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12728 | // Don't kill this process because it is doing work; it |
| 12729 | // has said it is doing work. |
| 12730 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12731 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12732 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12733 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12734 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12735 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12736 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12737 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12738 | 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] | 12739 | // XXX should compute this based on the max of |
| 12740 | // all connected clients. |
| 12741 | ConnectionRecord cr = clist.get(i); |
| 12742 | if (cr.binding.client == app) { |
| 12743 | // Binding to ourself is not interesting. |
| 12744 | continue; |
| 12745 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12746 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12747 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12748 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12749 | int myHiddenAdj = hiddenAdj; |
| 12750 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12751 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12752 | myHiddenAdj = client.hiddenAdj; |
| 12753 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12754 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12755 | } |
| 12756 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12757 | int myEmptyAdj = emptyAdj; |
| 12758 | if (myEmptyAdj > client.emptyAdj) { |
| 12759 | if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 12760 | myEmptyAdj = client.emptyAdj; |
| 12761 | } else { |
| 12762 | myEmptyAdj = ProcessList.VISIBLE_APP_ADJ; |
| 12763 | } |
| 12764 | } |
| 12765 | clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
| 12766 | myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12767 | String adjType = null; |
| 12768 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 12769 | // Not doing bind OOM management, so treat |
| 12770 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12771 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12772 | // If this process has shown some UI, let it immediately |
| 12773 | // go to the LRU list because it may be pretty heavy with |
| 12774 | // UI stuff. We'll tag it with a label just to help |
| 12775 | // debug and understand what is going on. |
| 12776 | if (adj > clientAdj) { |
| 12777 | adjType = "bound-bg-ui-services"; |
| 12778 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12779 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12780 | clientAdj = adj; |
| 12781 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12782 | if (now >= (s.lastActivity |
| 12783 | + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12784 | // This service has not seen activity within |
| 12785 | // recent memory, so allow it to drop to the |
| 12786 | // LRU list if there is no other reason to keep |
| 12787 | // it around. We'll also tag it with a label just |
| 12788 | // to help debug and undertand what is going on. |
| 12789 | if (adj > clientAdj) { |
| 12790 | adjType = "bound-bg-services"; |
| 12791 | } |
| 12792 | clientAdj = adj; |
| 12793 | } |
| 12794 | } |
| 12795 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12796 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12797 | // If this process has recently shown UI, and |
| 12798 | // the process that is binding to it is less |
| 12799 | // important than being visible, then we don't |
| 12800 | // care about the binding as much as we care |
| 12801 | // about letting this process get into the LRU |
| 12802 | // list to be killed and restarted if needed for |
| 12803 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12804 | if (app.hasShownUi && app != mHomeProcess |
| 12805 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12806 | adjType = "bound-bg-ui-services"; |
| 12807 | } else { |
| 12808 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 12809 | |Context.BIND_IMPORTANT)) != 0) { |
| 12810 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12811 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 12812 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 12813 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12814 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12815 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12816 | adj = clientAdj; |
| 12817 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12818 | app.pendingUiClean = true; |
| 12819 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12820 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 12821 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12822 | } |
| 12823 | if (!client.hidden) { |
| 12824 | app.hidden = false; |
| 12825 | } |
| 12826 | if (client.keeping) { |
| 12827 | app.keeping = true; |
| 12828 | } |
| 12829 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12830 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12831 | } |
| 12832 | if (adjType != null) { |
| 12833 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12834 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12835 | .REASON_SERVICE_IN_USE; |
| 12836 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12837 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12838 | app.adjTarget = s.name; |
| 12839 | } |
| 12840 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12841 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12842 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12843 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12844 | } |
| 12845 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12846 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
| 12847 | ActivityRecord a = cr.activity; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12848 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12849 | (a.visible || a.state == ActivityState.RESUMED |
| 12850 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12851 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12852 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12853 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12854 | } |
| 12855 | app.hidden = false; |
| 12856 | app.adjType = "service"; |
| 12857 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12858 | .REASON_SERVICE_IN_USE; |
| 12859 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12860 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12861 | app.adjTarget = s.name; |
| 12862 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12863 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12864 | } |
| 12865 | } |
| 12866 | } |
| 12867 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12868 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12869 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12870 | // would like to avoid killing it unless it would prevent the current |
| 12871 | // application from running. By default we put the process in |
| 12872 | // with the rest of the background processes; as we scan through |
| 12873 | // its services we may bump it up from there. |
| 12874 | if (adj > hiddenAdj) { |
| 12875 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12876 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12877 | app.adjType = "bg-services"; |
| 12878 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12879 | } |
| 12880 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12881 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12882 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12883 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12884 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12885 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12886 | ContentProviderRecord cpr = jt.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12887 | for (int i = cpr.connections.size()-1; |
| 12888 | i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
| 12889 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE); |
| 12890 | i--) { |
| 12891 | ContentProviderConnection conn = cpr.connections.get(i); |
| 12892 | ProcessRecord client = conn.client; |
| 12893 | if (client == app) { |
| 12894 | // Being our own client is not interesting. |
| 12895 | continue; |
| 12896 | } |
| 12897 | int myHiddenAdj = hiddenAdj; |
| 12898 | if (myHiddenAdj > client.hiddenAdj) { |
| 12899 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12900 | myHiddenAdj = client.hiddenAdj; |
| 12901 | } else { |
| 12902 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12903 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12904 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12905 | int myEmptyAdj = emptyAdj; |
| 12906 | if (myEmptyAdj > client.emptyAdj) { |
| 12907 | if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12908 | myEmptyAdj = client.emptyAdj; |
| 12909 | } else { |
| 12910 | myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 12911 | } |
| 12912 | } |
| 12913 | int clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
| 12914 | myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12915 | if (adj > clientAdj) { |
| 12916 | if (app.hasShownUi && app != mHomeProcess |
| 12917 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12918 | app.adjType = "bg-ui-provider"; |
| 12919 | } else { |
| 12920 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 12921 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
| 12922 | app.adjType = "provider"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12923 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12924 | if (!client.hidden) { |
| 12925 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12926 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12927 | if (client.keeping) { |
| 12928 | app.keeping = true; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12929 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12930 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12931 | .REASON_PROVIDER_IN_USE; |
| 12932 | app.adjSource = client; |
| 12933 | app.adjSourceOom = clientAdj; |
| 12934 | app.adjTarget = cpr.name; |
| 12935 | } |
| 12936 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12937 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12938 | } |
| 12939 | } |
| 12940 | // If the provider has external (non-framework) process |
| 12941 | // dependencies, ensure that its adjustment is at least |
| 12942 | // FOREGROUND_APP_ADJ. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 12943 | if (cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12944 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12945 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12946 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12947 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12948 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12949 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12950 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12951 | } |
| 12952 | } |
| 12953 | } |
| 12954 | } |
| 12955 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12956 | if (adj == ProcessList.SERVICE_ADJ) { |
| 12957 | if (doingAll) { |
| 12958 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 12959 | mNewNumServiceProcs++; |
| 12960 | } |
| 12961 | if (app.serviceb) { |
| 12962 | adj = ProcessList.SERVICE_B_ADJ; |
| 12963 | } |
| 12964 | } else { |
| 12965 | app.serviceb = false; |
| 12966 | } |
| 12967 | |
| 12968 | app.nonStoppingAdj = adj; |
| 12969 | |
| 12970 | if (hasStoppingActivities) { |
| 12971 | // Only upgrade adjustment. |
| 12972 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12973 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12974 | app.adjType = "stopping"; |
| 12975 | } |
| 12976 | } |
| 12977 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12978 | app.curRawAdj = adj; |
| 12979 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12980 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12981 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 12982 | if (adj > app.maxAdj) { |
| 12983 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12984 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12985 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12986 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12987 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12988 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12989 | app.keeping = true; |
| 12990 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12991 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12992 | if (app.hasAboveClient) { |
| 12993 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 12994 | // then we need to drop its adjustment to be lower than the service's |
| 12995 | // in order to honor the request. We want to drop it by one adjustment |
| 12996 | // level... but there is special meaning applied to various levels so |
| 12997 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12998 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12999 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13000 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 13001 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13002 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13003 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13004 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13005 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13006 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13007 | adj++; |
| 13008 | } |
| 13009 | } |
| 13010 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13011 | int importance = app.memImportance; |
| 13012 | if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) { |
| 13013 | app.curAdj = adj; |
| 13014 | app.curSchedGroup = schedGroup; |
| 13015 | if (!interesting) { |
| 13016 | // For this reporting, if there is not something explicitly |
| 13017 | // interesting in this process then we will push it to the |
| 13018 | // background importance. |
| 13019 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13020 | } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13021 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13022 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 13023 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 13024 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 13025 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13026 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
| 13027 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 13028 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 13029 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 13030 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13031 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 13032 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13033 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 13034 | } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 13035 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 13036 | } else { |
| 13037 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT; |
| 13038 | } |
| 13039 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13040 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13041 | int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0; |
| 13042 | if (foregroundActivities != app.foregroundActivities) { |
| 13043 | changes |= ProcessChangeItem.CHANGE_ACTIVITIES; |
| 13044 | } |
| 13045 | if (changes != 0) { |
| 13046 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes); |
| 13047 | app.memImportance = importance; |
| 13048 | app.foregroundActivities = foregroundActivities; |
| 13049 | int i = mPendingProcessChanges.size()-1; |
| 13050 | ProcessChangeItem item = null; |
| 13051 | while (i >= 0) { |
| 13052 | item = mPendingProcessChanges.get(i); |
| 13053 | if (item.pid == app.pid) { |
| 13054 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item); |
| 13055 | break; |
| 13056 | } |
| 13057 | i--; |
| 13058 | } |
| 13059 | if (i < 0) { |
| 13060 | // No existing item in pending changes; need a new one. |
| 13061 | final int NA = mAvailProcessChanges.size(); |
| 13062 | if (NA > 0) { |
| 13063 | item = mAvailProcessChanges.remove(NA-1); |
| 13064 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item); |
| 13065 | } else { |
| 13066 | item = new ProcessChangeItem(); |
| 13067 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item); |
| 13068 | } |
| 13069 | item.changes = 0; |
| 13070 | item.pid = app.pid; |
| 13071 | item.uid = app.info.uid; |
| 13072 | if (mPendingProcessChanges.size() == 0) { |
| 13073 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, |
| 13074 | "*** Enqueueing dispatch processes changed!"); |
| 13075 | mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget(); |
| 13076 | } |
| 13077 | mPendingProcessChanges.add(item); |
| 13078 | } |
| 13079 | item.changes |= changes; |
| 13080 | item.importance = importance; |
| 13081 | item.foregroundActivities = foregroundActivities; |
| 13082 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item " |
| 13083 | + Integer.toHexString(System.identityHashCode(item)) |
| 13084 | + " " + app.toShortString() + ": changes=" + item.changes |
| 13085 | + " importance=" + item.importance |
| 13086 | + " foreground=" + item.foregroundActivities |
| 13087 | + " type=" + app.adjType + " source=" + app.adjSource |
| 13088 | + " target=" + app.adjTarget); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13089 | } |
| 13090 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13091 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13092 | } |
| 13093 | |
| 13094 | /** |
| 13095 | * Ask a given process to GC right now. |
| 13096 | */ |
| 13097 | final void performAppGcLocked(ProcessRecord app) { |
| 13098 | try { |
| 13099 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13100 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13101 | if (app.reportLowMemory) { |
| 13102 | app.reportLowMemory = false; |
| 13103 | app.thread.scheduleLowMemory(); |
| 13104 | } else { |
| 13105 | app.thread.processInBackground(); |
| 13106 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13107 | } |
| 13108 | } catch (Exception e) { |
| 13109 | // whatever. |
| 13110 | } |
| 13111 | } |
| 13112 | |
| 13113 | /** |
| 13114 | * Returns true if things are idle enough to perform GCs. |
| 13115 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13116 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13117 | boolean processingBroadcasts = false; |
| 13118 | for (BroadcastQueue q : mBroadcastQueues) { |
| 13119 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 13120 | processingBroadcasts = true; |
| 13121 | } |
| 13122 | } |
| 13123 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13124 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13125 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13126 | } |
| 13127 | |
| 13128 | /** |
| 13129 | * Perform GCs on all processes that are waiting for it, but only |
| 13130 | * if things are idle. |
| 13131 | */ |
| 13132 | final void performAppGcsLocked() { |
| 13133 | final int N = mProcessesToGc.size(); |
| 13134 | if (N <= 0) { |
| 13135 | return; |
| 13136 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13137 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13138 | while (mProcessesToGc.size() > 0) { |
| 13139 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13140 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13141 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 13142 | <= SystemClock.uptimeMillis()) { |
| 13143 | // To avoid spamming the system, we will GC processes one |
| 13144 | // at a time, waiting a few seconds between each. |
| 13145 | performAppGcLocked(proc); |
| 13146 | scheduleAppGcsLocked(); |
| 13147 | return; |
| 13148 | } else { |
| 13149 | // It hasn't been long enough since we last GCed this |
| 13150 | // process... put it in the list to wait for its time. |
| 13151 | addProcessToGcListLocked(proc); |
| 13152 | break; |
| 13153 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13154 | } |
| 13155 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13156 | |
| 13157 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13158 | } |
| 13159 | } |
| 13160 | |
| 13161 | /** |
| 13162 | * If all looks good, perform GCs on all processes waiting for them. |
| 13163 | */ |
| 13164 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13165 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13166 | performAppGcsLocked(); |
| 13167 | return; |
| 13168 | } |
| 13169 | // Still not idle, wait some more. |
| 13170 | scheduleAppGcsLocked(); |
| 13171 | } |
| 13172 | |
| 13173 | /** |
| 13174 | * Schedule the execution of all pending app GCs. |
| 13175 | */ |
| 13176 | final void scheduleAppGcsLocked() { |
| 13177 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13178 | |
| 13179 | if (mProcessesToGc.size() > 0) { |
| 13180 | // Schedule a GC for the time to the next process. |
| 13181 | ProcessRecord proc = mProcessesToGc.get(0); |
| 13182 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 13183 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13184 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13185 | long now = SystemClock.uptimeMillis(); |
| 13186 | if (when < (now+GC_TIMEOUT)) { |
| 13187 | when = now + GC_TIMEOUT; |
| 13188 | } |
| 13189 | mHandler.sendMessageAtTime(msg, when); |
| 13190 | } |
| 13191 | } |
| 13192 | |
| 13193 | /** |
| 13194 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 13195 | * list in sorted order by the last GC time. The process can't already be |
| 13196 | * on the list. |
| 13197 | */ |
| 13198 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 13199 | boolean added = false; |
| 13200 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 13201 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 13202 | proc.lastRequestedGc) { |
| 13203 | added = true; |
| 13204 | mProcessesToGc.add(i+1, proc); |
| 13205 | break; |
| 13206 | } |
| 13207 | } |
| 13208 | if (!added) { |
| 13209 | mProcessesToGc.add(0, proc); |
| 13210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13211 | } |
| 13212 | |
| 13213 | /** |
| 13214 | * Set up to ask a process to GC itself. This will either do it |
| 13215 | * immediately, or put it on the list of processes to gc the next |
| 13216 | * time things are idle. |
| 13217 | */ |
| 13218 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 13219 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13220 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13221 | return; |
| 13222 | } |
| 13223 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13224 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13225 | scheduleAppGcsLocked(); |
| 13226 | } |
| 13227 | } |
| 13228 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13229 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 13230 | updateCpuStatsNow(); |
| 13231 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13232 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13233 | boolean doWakeKills = doKills; |
| 13234 | boolean doCpuKills = doKills; |
| 13235 | if (mLastPowerCheckRealtime == 0) { |
| 13236 | doWakeKills = false; |
| 13237 | } |
| 13238 | if (mLastPowerCheckUptime == 0) { |
| 13239 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13240 | } |
| 13241 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13242 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13243 | } |
| 13244 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13245 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 13246 | final long curUptime = SystemClock.uptimeMillis(); |
| 13247 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 13248 | mLastPowerCheckRealtime = curRealtime; |
| 13249 | mLastPowerCheckUptime = curUptime; |
| 13250 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 13251 | doWakeKills = false; |
| 13252 | } |
| 13253 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 13254 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13255 | } |
| 13256 | int i = mLruProcesses.size(); |
| 13257 | while (i > 0) { |
| 13258 | i--; |
| 13259 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13260 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13261 | long wtime; |
| 13262 | synchronized (stats) { |
| 13263 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 13264 | app.pid, curRealtime); |
| 13265 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13266 | long wtimeUsed = wtime - app.lastWakeTime; |
| 13267 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13268 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13269 | StringBuilder sb = new StringBuilder(128); |
| 13270 | sb.append("Wake for "); |
| 13271 | app.toShortString(sb); |
| 13272 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13273 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13274 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13275 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13276 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13277 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13278 | sb.append("%)"); |
| 13279 | Slog.i(TAG, sb.toString()); |
| 13280 | sb.setLength(0); |
| 13281 | sb.append("CPU for "); |
| 13282 | app.toShortString(sb); |
| 13283 | sb.append(": over "); |
| 13284 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13285 | sb.append(" used "); |
| 13286 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13287 | sb.append(" ("); |
| 13288 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13289 | sb.append("%)"); |
| 13290 | Slog.i(TAG, sb.toString()); |
| 13291 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13292 | // If a process has held a wake lock for more |
| 13293 | // than 50% of the time during this period, |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13294 | // that sounds bad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13295 | if (doWakeKills && realtimeSince > 0 |
| 13296 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13297 | synchronized (stats) { |
| 13298 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13299 | realtimeSince, wtimeUsed); |
| 13300 | } |
| 13301 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13302 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13303 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13304 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13305 | app.processName, app.setAdj, "excessive wake lock"); |
| 13306 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13307 | } else if (doCpuKills && uptimeSince > 0 |
| 13308 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13309 | synchronized (stats) { |
| 13310 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13311 | uptimeSince, cputimeUsed); |
| 13312 | } |
| 13313 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13314 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13315 | + " during " + uptimeSince); |
| 13316 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13317 | app.processName, app.setAdj, "excessive cpu"); |
| 13318 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13319 | } else { |
| 13320 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13321 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13322 | } |
| 13323 | } |
| 13324 | } |
| 13325 | } |
| 13326 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13327 | private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj, |
| 13328 | int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13329 | app.hiddenAdj = hiddenAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13330 | app.emptyAdj = emptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13331 | |
| 13332 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13333 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13334 | } |
| 13335 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13336 | final boolean wasKeeping = app.keeping; |
| 13337 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13338 | boolean success = true; |
| 13339 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13340 | computeOomAdjLocked(app, hiddenAdj, emptyAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13341 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13342 | if (app.curRawAdj != app.setRawAdj) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13343 | if (wasKeeping && !app.keeping) { |
| 13344 | // This app is no longer something we want to keep. Note |
| 13345 | // its current wake lock time to later know to kill it if |
| 13346 | // it is not behaving well. |
| 13347 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13348 | synchronized (stats) { |
| 13349 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13350 | app.pid, SystemClock.elapsedRealtime()); |
| 13351 | } |
| 13352 | app.lastCpuTime = app.curCpuTime; |
| 13353 | } |
| 13354 | |
| 13355 | app.setRawAdj = app.curRawAdj; |
| 13356 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13357 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13358 | if (app.curAdj != app.setAdj) { |
| 13359 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 13360 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13361 | TAG, "Set " + app.pid + " " + app.processName + |
| 13362 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13363 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13364 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13365 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13366 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13367 | } |
| 13368 | } |
| 13369 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13370 | app.setSchedGroup = app.curSchedGroup; |
| 13371 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 13372 | "Setting process group of " + app.processName |
| 13373 | + " to " + app.curSchedGroup); |
| 13374 | if (app.waitingToKill != null && |
| 13375 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13376 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
| 13377 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13378 | app.processName, app.setAdj, app.waitingToKill); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 13379 | app.killedBackground = true; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13380 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13381 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13382 | } else { |
| 13383 | if (true) { |
| 13384 | long oldId = Binder.clearCallingIdentity(); |
| 13385 | try { |
| 13386 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13387 | } catch (Exception e) { |
| 13388 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13389 | + " to " + app.curSchedGroup); |
| 13390 | e.printStackTrace(); |
| 13391 | } finally { |
| 13392 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13393 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13394 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13395 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13396 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13397 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13398 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13399 | } |
| 13400 | } |
| 13401 | } |
| 13402 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13403 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13404 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13405 | } |
| 13406 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13407 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13408 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13409 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 13410 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13411 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13412 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13413 | } |
| 13414 | } |
| 13415 | return resumedActivity; |
| 13416 | } |
| 13417 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13418 | final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13419 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13420 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13421 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13422 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13423 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13424 | |
| 13425 | mAdjSeq++; |
| 13426 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13427 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.emptyAdj, |
| 13428 | TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13429 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13430 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13431 | if (nowHidden != wasHidden) { |
| 13432 | // Changed to/from hidden state, so apps after it in the LRU |
| 13433 | // list may also be changed. |
| 13434 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13435 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13436 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13437 | } |
| 13438 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13439 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13440 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13441 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13442 | |
| 13443 | if (false) { |
| 13444 | RuntimeException e = new RuntimeException(); |
| 13445 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13446 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13447 | } |
| 13448 | |
| 13449 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13450 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13451 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13452 | // Let's determine how many processes we have running vs. |
| 13453 | // how many slots we have for background processes; we may want |
| 13454 | // to put multiple processes in a slot of there are enough of |
| 13455 | // them. |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13456 | int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ |
| 13457 | - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2; |
| 13458 | int emptyFactor = (mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs)/numSlots; |
| 13459 | if (emptyFactor < 1) emptyFactor = 1; |
| 13460 | int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots; |
| 13461 | if (hiddenFactor < 1) hiddenFactor = 1; |
| 13462 | int stepHidden = 0; |
| 13463 | int stepEmpty = 0; |
| 13464 | final int emptyProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit; |
| 13465 | final int hiddenProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13466 | int numHidden = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13467 | int numEmpty = 0; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13468 | int numTrimming = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13469 | |
| 13470 | mNumNonHiddenProcs = 0; |
| 13471 | mNumHiddenProcs = 0; |
| 13472 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13473 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13474 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13475 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13476 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13477 | int nextHiddenAdj = curHiddenAdj+1; |
| 13478 | int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
| 13479 | int nextEmptyAdj = curEmptyAdj+2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13480 | while (i > 0) { |
| 13481 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13482 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13483 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13484 | updateOomAdjLocked(app, curHiddenAdj, curEmptyAdj, TOP_APP, true); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13485 | if (!app.killedBackground) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13486 | if (app.curRawAdj == curHiddenAdj && app.hasActivities) { |
| 13487 | // This process was assigned as a hidden process... step the |
| 13488 | // hidden level. |
| 13489 | mNumHiddenProcs++; |
| 13490 | if (curHiddenAdj != nextHiddenAdj) { |
| 13491 | stepHidden++; |
| 13492 | if (stepHidden >= hiddenFactor) { |
| 13493 | stepHidden = 0; |
| 13494 | curHiddenAdj = nextHiddenAdj; |
| 13495 | nextHiddenAdj += 2; |
| 13496 | if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13497 | nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13498 | } |
| 13499 | } |
| 13500 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13501 | numHidden++; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13502 | if (numHidden > hiddenProcessLimit) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13503 | Slog.i(TAG, "No longer want " + app.processName |
| 13504 | + " (pid " + app.pid + "): hidden #" + numHidden); |
| 13505 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13506 | app.processName, app.setAdj, "too many background"); |
| 13507 | app.killedBackground = true; |
| 13508 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13509 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13510 | } else { |
| 13511 | if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) { |
| 13512 | // This process was assigned as an empty process... step the |
| 13513 | // empty level. |
| 13514 | if (curEmptyAdj != nextEmptyAdj) { |
| 13515 | stepEmpty++; |
| 13516 | if (stepEmpty >= emptyFactor) { |
| 13517 | stepEmpty = 0; |
| 13518 | curEmptyAdj = nextEmptyAdj; |
| 13519 | nextEmptyAdj += 2; |
| 13520 | if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13521 | nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13522 | } |
| 13523 | } |
| 13524 | } |
| 13525 | } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13526 | mNumNonHiddenProcs++; |
| 13527 | } |
| 13528 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13529 | numEmpty++; |
| 13530 | if (numEmpty > emptyProcessLimit) { |
| 13531 | Slog.i(TAG, "No longer want " + app.processName |
| 13532 | + " (pid " + app.pid + "): empty #" + numEmpty); |
| 13533 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13534 | app.processName, app.setAdj, "too many background"); |
| 13535 | app.killedBackground = true; |
| 13536 | Process.killProcessQuiet(app.pid); |
| 13537 | } |
| 13538 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13539 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13540 | if (app.isolated && app.services.size() <= 0) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13541 | // If this is an isolated process, and there are no |
| 13542 | // services running in it, then the process is no longer |
| 13543 | // needed. We agressively kill these because we can by |
| 13544 | // definition not re-use the same process again, and it is |
| 13545 | // good to avoid having whatever code was running in them |
| 13546 | // left sitting around after no longer needed. |
| 13547 | Slog.i(TAG, "Isolated process " + app.processName |
| 13548 | + " (pid " + app.pid + ") no longer needed"); |
| 13549 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13550 | app.processName, app.setAdj, "isolated not needed"); |
| 13551 | app.killedBackground = true; |
| 13552 | Process.killProcessQuiet(app.pid); |
| 13553 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13554 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13555 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
| 13556 | && !app.killedBackground) { |
| 13557 | numTrimming++; |
| 13558 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13559 | } |
| 13560 | } |
| 13561 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13562 | mNumServiceProcs = mNewNumServiceProcs; |
| 13563 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13564 | // Now determine the memory trimming level of background processes. |
| 13565 | // Unfortunately we need to start at the back of the list to do this |
| 13566 | // properly. We only do this if the number of background apps we |
| 13567 | // are managing to keep around is less than half the maximum we desire; |
| 13568 | // if we are keeping a good number around, we'll let them use whatever |
| 13569 | // memory they want. |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13570 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/4) |
| 13571 | && numEmpty <= (ProcessList.MAX_HIDDEN_APPS/4)) { |
| 13572 | final int numHiddenAndEmpty = numHidden + numEmpty; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13573 | final int N = mLruProcesses.size(); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13574 | int factor = numTrimming/3; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13575 | int minFactor = 2; |
| 13576 | if (mHomeProcess != null) minFactor++; |
| 13577 | if (mPreviousProcess != null) minFactor++; |
| 13578 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13579 | int step = 0; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13580 | int fgTrimLevel; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13581 | if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/5)) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13582 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13583 | } else if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/3)) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13584 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
| 13585 | } else { |
| 13586 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
| 13587 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13588 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13589 | for (i=0; i<N; i++) { |
| 13590 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13591 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13592 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13593 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13594 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 13595 | try { |
| 13596 | app.thread.scheduleTrimMemory(curLevel); |
| 13597 | } catch (RemoteException e) { |
| 13598 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13599 | if (false) { |
| 13600 | // For now we won't do this; our memory trimming seems |
| 13601 | // to be good enough at this point that destroying |
| 13602 | // activities causes more harm than good. |
| 13603 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 13604 | && app != mHomeProcess && app != mPreviousProcess) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13605 | // Need to do this on its own message because the stack may not |
| 13606 | // be in a consistent state at this point. |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13607 | // For these apps we will also finish their activities |
| 13608 | // to help them free memory. |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13609 | mMainStack.scheduleDestroyActivities(app, false, "trim"); |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13610 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13611 | } |
| 13612 | } |
| 13613 | app.trimMemoryLevel = curLevel; |
| 13614 | step++; |
| 13615 | if (step >= factor) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13616 | step = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13617 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13618 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 13619 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13620 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13621 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 13622 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13623 | break; |
| 13624 | } |
| 13625 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13626 | } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13627 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13628 | && app.thread != null) { |
| 13629 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13630 | app.thread.scheduleTrimMemory( |
| 13631 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13632 | } catch (RemoteException e) { |
| 13633 | } |
| 13634 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13635 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13636 | } else { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13637 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13638 | && app.pendingUiClean) { |
| 13639 | // If this application is now in the background and it |
| 13640 | // had done UI, then give it the special trim level to |
| 13641 | // have it free UI resources. |
| 13642 | final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13643 | if (app.trimMemoryLevel < level && app.thread != null) { |
| 13644 | try { |
| 13645 | app.thread.scheduleTrimMemory(level); |
| 13646 | } catch (RemoteException e) { |
| 13647 | } |
| 13648 | } |
| 13649 | app.pendingUiClean = false; |
| 13650 | } |
| 13651 | if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13652 | try { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13653 | app.thread.scheduleTrimMemory(fgTrimLevel); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13654 | } catch (RemoteException e) { |
| 13655 | } |
| 13656 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13657 | app.trimMemoryLevel = fgTrimLevel; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13658 | } |
| 13659 | } |
| 13660 | } else { |
| 13661 | final int N = mLruProcesses.size(); |
| 13662 | for (i=0; i<N; i++) { |
| 13663 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13664 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13665 | && app.pendingUiClean) { |
| 13666 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13667 | && app.thread != null) { |
| 13668 | try { |
| 13669 | app.thread.scheduleTrimMemory( |
| 13670 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 13671 | } catch (RemoteException e) { |
| 13672 | } |
| 13673 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13674 | app.pendingUiClean = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13675 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13676 | app.trimMemoryLevel = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13677 | } |
| 13678 | } |
| 13679 | |
| 13680 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13681 | // Need to do this on its own message because the stack may not |
| 13682 | // be in a consistent state at this point. |
| 13683 | mMainStack.scheduleDestroyActivities(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13684 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13685 | } |
| 13686 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13687 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13688 | synchronized (this) { |
| 13689 | int i; |
| 13690 | |
| 13691 | // First remove any unused application processes whose package |
| 13692 | // has been removed. |
| 13693 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 13694 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 13695 | if (app.activities.size() == 0 |
| 13696 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13697 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13698 | TAG, "Exiting empty application process " |
| 13699 | + app.processName + " (" |
| 13700 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13701 | + ")\n"); |
| 13702 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13703 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13704 | app.processName, app.setAdj, "empty"); |
| 13705 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13706 | } else { |
| 13707 | try { |
| 13708 | app.thread.scheduleExit(); |
| 13709 | } catch (Exception e) { |
| 13710 | // Ignore exceptions. |
| 13711 | } |
| 13712 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13713 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13714 | mRemovedProcesses.remove(i); |
| 13715 | |
| 13716 | if (app.persistent) { |
| 13717 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13718 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13719 | } |
| 13720 | } |
| 13721 | } |
| 13722 | } |
| 13723 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13724 | // Now update the oom adj for all processes. |
| 13725 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13726 | } |
| 13727 | } |
| 13728 | |
| 13729 | /** This method sends the specified signal to each of the persistent apps */ |
| 13730 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 13731 | if (sig != Process.SIGNAL_USR1) { |
| 13732 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 13733 | } |
| 13734 | |
| 13735 | synchronized (this) { |
| 13736 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 13737 | != PackageManager.PERMISSION_GRANTED) { |
| 13738 | throw new SecurityException("Requires permission " |
| 13739 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 13740 | } |
| 13741 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13742 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 13743 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13744 | if (r.thread != null && r.persistent) { |
| 13745 | Process.sendSignal(r.pid, sig); |
| 13746 | } |
| 13747 | } |
| 13748 | } |
| 13749 | } |
| 13750 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13751 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 13752 | if (proc == null || proc == mProfileProc) { |
| 13753 | proc = mProfileProc; |
| 13754 | path = mProfileFile; |
| 13755 | profileType = mProfileType; |
| 13756 | clearProfilerLocked(); |
| 13757 | } |
| 13758 | if (proc == null) { |
| 13759 | return; |
| 13760 | } |
| 13761 | try { |
| 13762 | proc.thread.profilerControl(false, path, null, profileType); |
| 13763 | } catch (RemoteException e) { |
| 13764 | throw new IllegalStateException("Process disappeared"); |
| 13765 | } |
| 13766 | } |
| 13767 | |
| 13768 | private void clearProfilerLocked() { |
| 13769 | if (mProfileFd != null) { |
| 13770 | try { |
| 13771 | mProfileFd.close(); |
| 13772 | } catch (IOException e) { |
| 13773 | } |
| 13774 | } |
| 13775 | mProfileApp = null; |
| 13776 | mProfileProc = null; |
| 13777 | mProfileFile = null; |
| 13778 | mProfileType = 0; |
| 13779 | mAutoStopProfiler = false; |
| 13780 | } |
| 13781 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13782 | public boolean profileControl(String process, int userId, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 13783 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13784 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13785 | try { |
| 13786 | synchronized (this) { |
| 13787 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13788 | // its own permission. |
| 13789 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13790 | != PackageManager.PERMISSION_GRANTED) { |
| 13791 | throw new SecurityException("Requires permission " |
| 13792 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13793 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13794 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13795 | if (start && fd == null) { |
| 13796 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13797 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13798 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13799 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13800 | if (process != null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13801 | proc = findProcessLocked(process, userId, "profileControl"); |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13802 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13803 | |
| 13804 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13805 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13806 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13807 | |
| 13808 | if (start) { |
| 13809 | stopProfilerLocked(null, null, 0); |
| 13810 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 13811 | mProfileProc = proc; |
| 13812 | mProfileType = profileType; |
| 13813 | try { |
| 13814 | fd = fd.dup(); |
| 13815 | } catch (IOException e) { |
| 13816 | fd = null; |
| 13817 | } |
| 13818 | proc.thread.profilerControl(start, path, fd, profileType); |
| 13819 | fd = null; |
| 13820 | mProfileFd = null; |
| 13821 | } else { |
| 13822 | stopProfilerLocked(proc, path, profileType); |
| 13823 | if (fd != null) { |
| 13824 | try { |
| 13825 | fd.close(); |
| 13826 | } catch (IOException e) { |
| 13827 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13828 | } |
| 13829 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13830 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13831 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13832 | } |
| 13833 | } catch (RemoteException e) { |
| 13834 | throw new IllegalStateException("Process disappeared"); |
| 13835 | } finally { |
| 13836 | if (fd != null) { |
| 13837 | try { |
| 13838 | fd.close(); |
| 13839 | } catch (IOException e) { |
| 13840 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13841 | } |
| 13842 | } |
| 13843 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13844 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13845 | private ProcessRecord findProcessLocked(String process, int userId, String callName) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 13846 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13847 | userId, true, true, callName, null); |
| 13848 | ProcessRecord proc = null; |
| 13849 | try { |
| 13850 | int pid = Integer.parseInt(process); |
| 13851 | synchronized (mPidsSelfLocked) { |
| 13852 | proc = mPidsSelfLocked.get(pid); |
| 13853 | } |
| 13854 | } catch (NumberFormatException e) { |
| 13855 | } |
| 13856 | |
| 13857 | if (proc == null) { |
| 13858 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13859 | = mProcessNames.getMap(); |
| 13860 | SparseArray<ProcessRecord> procs = all.get(process); |
| 13861 | if (procs != null && procs.size() > 0) { |
| 13862 | proc = procs.valueAt(0); |
| 13863 | if (userId != UserHandle.USER_ALL && proc.userId != userId) { |
| 13864 | for (int i=1; i<procs.size(); i++) { |
| 13865 | ProcessRecord thisProc = procs.valueAt(i); |
| 13866 | if (thisProc.userId == userId) { |
| 13867 | proc = thisProc; |
| 13868 | break; |
| 13869 | } |
| 13870 | } |
| 13871 | } |
| 13872 | } |
| 13873 | } |
| 13874 | |
| 13875 | return proc; |
| 13876 | } |
| 13877 | |
| 13878 | public boolean dumpHeap(String process, int userId, boolean managed, |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13879 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 13880 | |
| 13881 | try { |
| 13882 | synchronized (this) { |
| 13883 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13884 | // its own permission (same as profileControl). |
| 13885 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13886 | != PackageManager.PERMISSION_GRANTED) { |
| 13887 | throw new SecurityException("Requires permission " |
| 13888 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 13889 | } |
| 13890 | |
| 13891 | if (fd == null) { |
| 13892 | throw new IllegalArgumentException("null fd"); |
| 13893 | } |
| 13894 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13895 | ProcessRecord proc = findProcessLocked(process, userId, "dumpHeap"); |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13896 | if (proc == null || proc.thread == null) { |
| 13897 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13898 | } |
| 13899 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 13900 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 13901 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13902 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 13903 | throw new SecurityException("Process not debuggable: " + proc); |
| 13904 | } |
| 13905 | } |
| 13906 | |
| 13907 | proc.thread.dumpHeap(managed, path, fd); |
| 13908 | fd = null; |
| 13909 | return true; |
| 13910 | } |
| 13911 | } catch (RemoteException e) { |
| 13912 | throw new IllegalStateException("Process disappeared"); |
| 13913 | } finally { |
| 13914 | if (fd != null) { |
| 13915 | try { |
| 13916 | fd.close(); |
| 13917 | } catch (IOException e) { |
| 13918 | } |
| 13919 | } |
| 13920 | } |
| 13921 | } |
| 13922 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13923 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 13924 | public void monitor() { |
| 13925 | synchronized (this) { } |
| 13926 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13927 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 13928 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13929 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 13930 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 13931 | try { |
| 13932 | if (processRecord.thread != null) { |
| 13933 | processRecord.thread.setCoreSettings(settings); |
| 13934 | } |
| 13935 | } catch (RemoteException re) { |
| 13936 | /* ignore */ |
| 13937 | } |
| 13938 | } |
| 13939 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 13940 | |
| 13941 | // Multi-user methods |
| 13942 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13943 | @Override |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13944 | public boolean switchUser(int userId) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13945 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 13946 | != PackageManager.PERMISSION_GRANTED) { |
| 13947 | String msg = "Permission Denial: switchUser() from pid=" |
| 13948 | + Binder.getCallingPid() |
| 13949 | + ", uid=" + Binder.getCallingUid() |
| 13950 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 13951 | Slog.w(TAG, msg); |
| 13952 | throw new SecurityException(msg); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13953 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13954 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13955 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13956 | try { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13957 | synchronized (this) { |
| 13958 | final int oldUserId = mCurrentUserId; |
| 13959 | if (oldUserId == userId) { |
| 13960 | return true; |
| 13961 | } |
| 13962 | |
| 13963 | final UserInfo userInfo = getUserManagerLocked().getUserInfo(userId); |
| 13964 | if (userInfo == null) { |
| 13965 | Slog.w(TAG, "No user info for user #" + userId); |
| 13966 | return false; |
| 13967 | } |
| 13968 | |
| 13969 | mWindowManager.startFreezingScreen(R.anim.screen_user_exit, |
| 13970 | R.anim.screen_user_enter); |
| 13971 | |
| 13972 | // If the user we are switching to is not currently started, then |
| 13973 | // we need to start it now. |
| 13974 | if (mStartedUsers.get(userId) == null) { |
| 13975 | mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 13976 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13977 | } |
| 13978 | |
| 13979 | mCurrentUserId = userId; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 13980 | mCurrentUserArray = new int[] { userId }; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13981 | final Integer userIdInt = Integer.valueOf(userId); |
| 13982 | mUserLru.remove(userIdInt); |
| 13983 | mUserLru.add(userIdInt); |
| 13984 | |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 13985 | mWindowManager.setCurrentUser(userId); |
| 13986 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13987 | final UserStartedState uss = mStartedUsers.get(userId); |
| 13988 | |
| 13989 | mHandler.removeMessages(REPORT_USER_SWITCH_MSG); |
| 13990 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 13991 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG, |
| 13992 | oldUserId, userId, uss)); |
| 13993 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG, |
| 13994 | oldUserId, userId, uss), USER_SWITCH_TIMEOUT); |
| 13995 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
| 13996 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 13997 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 13998 | broadcastIntentLocked(null, null, intent, |
| 13999 | null, null, 0, null, null, null, |
| 14000 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14001 | |
| 14002 | if ((userInfo.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
| 14003 | if (userId != 0) { |
| 14004 | intent = new Intent(Intent.ACTION_USER_INITIALIZE); |
| 14005 | broadcastIntentLocked(null, null, intent, null, |
| 14006 | new IIntentReceiver.Stub() { |
| 14007 | public void performReceive(Intent intent, int resultCode, |
| 14008 | String data, Bundle extras, boolean ordered, |
| 14009 | boolean sticky, int sendingUser) { |
| 14010 | synchronized (ActivityManagerService.this) { |
| 14011 | getUserManagerLocked().makeInitialized(userInfo.id); |
| 14012 | } |
| 14013 | } |
| 14014 | }, 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 14015 | userId); |
| 14016 | } else { |
| 14017 | getUserManagerLocked().makeInitialized(userInfo.id); |
| 14018 | } |
| 14019 | } |
| 14020 | |
| 14021 | boolean haveActivities = mMainStack.switchUserLocked(userId, uss); |
| 14022 | if (!haveActivities) { |
| 14023 | startHomeActivityLocked(userId); |
| 14024 | } |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14025 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 14026 | getUserManagerLocked().userForeground(userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14027 | sendUserSwitchBroadcastsLocked(oldUserId, userId); |
| 14028 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14029 | } finally { |
| 14030 | Binder.restoreCallingIdentity(ident); |
| 14031 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14032 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14033 | return true; |
| 14034 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14035 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14036 | void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) { |
| 14037 | long ident = Binder.clearCallingIdentity(); |
| 14038 | try { |
| 14039 | Intent intent; |
| 14040 | if (oldUserId >= 0) { |
| 14041 | intent = new Intent(Intent.ACTION_USER_BACKGROUND); |
| 14042 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14043 | intent.putExtra(Intent.EXTRA_USER_HANDLE, oldUserId); |
| 14044 | broadcastIntentLocked(null, null, intent, |
| 14045 | null, null, 0, null, null, null, |
| 14046 | false, false, MY_PID, Process.SYSTEM_UID, oldUserId); |
| 14047 | } |
| 14048 | if (newUserId >= 0) { |
| 14049 | intent = new Intent(Intent.ACTION_USER_FOREGROUND); |
| 14050 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14051 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14052 | broadcastIntentLocked(null, null, intent, |
| 14053 | null, null, 0, null, null, null, |
| 14054 | false, false, MY_PID, Process.SYSTEM_UID, newUserId); |
| 14055 | intent = new Intent(Intent.ACTION_USER_SWITCHED); |
| 14056 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14057 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14058 | broadcastIntentLocked(null, null, intent, |
| 14059 | null, null, 0, null, null, |
| 14060 | android.Manifest.permission.MANAGE_USERS, |
| 14061 | false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
| 14062 | } |
| 14063 | } finally { |
| 14064 | Binder.restoreCallingIdentity(ident); |
| 14065 | } |
| 14066 | } |
| 14067 | |
| 14068 | void dispatchUserSwitch(final UserStartedState uss, final int oldUserId, |
| 14069 | final int newUserId) { |
| 14070 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14071 | if (N > 0) { |
| 14072 | final IRemoteCallback callback = new IRemoteCallback.Stub() { |
| 14073 | int mCount = 0; |
| 14074 | @Override |
| 14075 | public void sendResult(Bundle data) throws RemoteException { |
| 14076 | synchronized (ActivityManagerService.this) { |
| 14077 | if (mCurUserSwitchCallback == this) { |
| 14078 | mCount++; |
| 14079 | if (mCount == N) { |
| 14080 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14081 | } |
| 14082 | } |
| 14083 | } |
| 14084 | } |
| 14085 | }; |
| 14086 | synchronized (this) { |
| 14087 | mCurUserSwitchCallback = callback; |
| 14088 | } |
| 14089 | for (int i=0; i<N; i++) { |
| 14090 | try { |
| 14091 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitching( |
| 14092 | newUserId, callback); |
| 14093 | } catch (RemoteException e) { |
| 14094 | } |
| 14095 | } |
| 14096 | } else { |
| 14097 | synchronized (this) { |
| 14098 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14099 | } |
| 14100 | } |
| 14101 | mUserSwitchObservers.finishBroadcast(); |
| 14102 | } |
| 14103 | |
| 14104 | void timeoutUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
| 14105 | synchronized (this) { |
| 14106 | Slog.w(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId); |
| 14107 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14108 | } |
| 14109 | } |
| 14110 | |
| 14111 | void sendContinueUserSwitchLocked(UserStartedState uss, int oldUserId, int newUserId) { |
| 14112 | mCurUserSwitchCallback = null; |
| 14113 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14114 | mHandler.sendMessage(mHandler.obtainMessage(CONTINUE_USER_SWITCH_MSG, |
| 14115 | oldUserId, newUserId, uss)); |
| 14116 | } |
| 14117 | |
| 14118 | void continueUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
| 14119 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14120 | for (int i=0; i<N; i++) { |
| 14121 | try { |
| 14122 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(newUserId); |
| 14123 | } catch (RemoteException e) { |
| 14124 | } |
| 14125 | } |
| 14126 | mUserSwitchObservers.finishBroadcast(); |
| 14127 | synchronized (this) { |
| 14128 | mWindowManager.stopFreezingScreen(); |
| 14129 | } |
| 14130 | } |
| 14131 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14132 | void finishUserSwitch(UserStartedState uss) { |
| 14133 | synchronized (this) { |
| 14134 | if (uss.mState == UserStartedState.STATE_BOOTING |
| 14135 | && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) { |
| 14136 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14137 | final int userId = uss.mHandle.getIdentifier(); |
| 14138 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 14139 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14140 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14141 | null, null, 0, null, null, |
| 14142 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14143 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14144 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14145 | int num = mUserLru.size(); |
| 14146 | int i = 0; |
| 14147 | while (num > MAX_RUNNING_USERS && i < mUserLru.size()) { |
| 14148 | Integer oldUserId = mUserLru.get(i); |
| 14149 | UserStartedState oldUss = mStartedUsers.get(oldUserId); |
| 14150 | if (oldUss == null) { |
| 14151 | // Shouldn't happen, but be sane if it does. |
| 14152 | mUserLru.remove(i); |
| 14153 | num--; |
| 14154 | continue; |
| 14155 | } |
| 14156 | if (oldUss.mState == UserStartedState.STATE_STOPPING) { |
| 14157 | // This user is already stopping, doesn't count. |
| 14158 | num--; |
| 14159 | i++; |
| 14160 | continue; |
| 14161 | } |
| 14162 | if (oldUserId == UserHandle.USER_OWNER || oldUserId == mCurrentUserId) { |
| 14163 | // Owner and current can't be stopped, but count as running. |
| 14164 | i++; |
| 14165 | continue; |
| 14166 | } |
| 14167 | // This is a user to be stopped. |
| 14168 | stopUserLocked(oldUserId, null); |
| 14169 | num--; |
| 14170 | i++; |
| 14171 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14172 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14173 | } |
| 14174 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14175 | @Override |
| 14176 | public int stopUser(final int userId, final IStopUserCallback callback) { |
| 14177 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14178 | != PackageManager.PERMISSION_GRANTED) { |
| 14179 | String msg = "Permission Denial: switchUser() from pid=" |
| 14180 | + Binder.getCallingPid() |
| 14181 | + ", uid=" + Binder.getCallingUid() |
| 14182 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14183 | Slog.w(TAG, msg); |
| 14184 | throw new SecurityException(msg); |
| 14185 | } |
| 14186 | if (userId <= 0) { |
| 14187 | throw new IllegalArgumentException("Can't stop primary user " + userId); |
| 14188 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14189 | synchronized (this) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14190 | return stopUserLocked(userId, callback); |
| 14191 | } |
| 14192 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14193 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14194 | private int stopUserLocked(final int userId, final IStopUserCallback callback) { |
| 14195 | if (mCurrentUserId == userId) { |
| 14196 | return ActivityManager.USER_OP_IS_CURRENT; |
| 14197 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14198 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14199 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14200 | if (uss == null) { |
| 14201 | // User is not started, nothing to do... but we do need to |
| 14202 | // callback if requested. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14203 | if (callback != null) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14204 | mHandler.post(new Runnable() { |
| 14205 | @Override |
| 14206 | public void run() { |
| 14207 | try { |
| 14208 | callback.userStopped(userId); |
| 14209 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14210 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14211 | } |
| 14212 | }); |
| 14213 | } |
| 14214 | return ActivityManager.USER_OP_SUCCESS; |
| 14215 | } |
| 14216 | |
| 14217 | if (callback != null) { |
| 14218 | uss.mStopCallbacks.add(callback); |
| 14219 | } |
| 14220 | |
| 14221 | if (uss.mState != UserStartedState.STATE_STOPPING) { |
| 14222 | uss.mState = UserStartedState.STATE_STOPPING; |
| 14223 | |
| 14224 | long ident = Binder.clearCallingIdentity(); |
| 14225 | try { |
| 14226 | // Inform of user switch |
| 14227 | Intent intent = new Intent(Intent.ACTION_SHUTDOWN); |
| 14228 | final IIntentReceiver resultReceiver = new IIntentReceiver.Stub() { |
| 14229 | @Override |
| 14230 | public void performReceive(Intent intent, int resultCode, String data, |
| 14231 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 14232 | finishUserStop(uss); |
| 14233 | } |
| 14234 | }; |
| 14235 | broadcastIntentLocked(null, null, intent, |
| 14236 | null, resultReceiver, 0, null, null, null, |
| 14237 | true, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14238 | } finally { |
| 14239 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14240 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14241 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14242 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14243 | return ActivityManager.USER_OP_SUCCESS; |
| 14244 | } |
| 14245 | |
| 14246 | void finishUserStop(UserStartedState uss) { |
| 14247 | final int userId = uss.mHandle.getIdentifier(); |
| 14248 | boolean stopped; |
| 14249 | ArrayList<IStopUserCallback> callbacks; |
| 14250 | synchronized (this) { |
| 14251 | callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks); |
| 14252 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14253 | || mStartedUsers.get(userId) != uss) { |
| 14254 | stopped = false; |
| 14255 | } else { |
| 14256 | stopped = true; |
| 14257 | // User can no longer run. |
| 14258 | mStartedUsers.remove(userId); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14259 | mUserLru.remove(Integer.valueOf(userId)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14260 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14261 | |
| 14262 | // Clean up all state and processes associated with the user. |
| 14263 | // Kill all the processes for the user. |
| 14264 | forceStopUserLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14265 | } |
| 14266 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14267 | |
| 14268 | for (int i=0; i<callbacks.size(); i++) { |
| 14269 | try { |
| 14270 | if (stopped) callbacks.get(i).userStopped(userId); |
| 14271 | else callbacks.get(i).userStopAborted(userId); |
| 14272 | } catch (RemoteException e) { |
| 14273 | } |
| 14274 | } |
| 14275 | } |
| 14276 | |
| 14277 | @Override |
| 14278 | public UserInfo getCurrentUser() { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14279 | if ((checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14280 | != PackageManager.PERMISSION_GRANTED) && ( |
| 14281 | checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14282 | != PackageManager.PERMISSION_GRANTED)) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14283 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 14284 | + Binder.getCallingPid() |
| 14285 | + ", uid=" + Binder.getCallingUid() |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14286 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14287 | Slog.w(TAG, msg); |
| 14288 | throw new SecurityException(msg); |
| 14289 | } |
| 14290 | synchronized (this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14291 | return getUserManagerLocked().getUserInfo(mCurrentUserId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14292 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14293 | } |
| 14294 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 14295 | int getCurrentUserIdLocked() { |
| 14296 | return mCurrentUserId; |
| 14297 | } |
| 14298 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14299 | @Override |
| 14300 | public boolean isUserRunning(int userId) { |
| 14301 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14302 | != PackageManager.PERMISSION_GRANTED) { |
| 14303 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14304 | + Binder.getCallingPid() |
| 14305 | + ", uid=" + Binder.getCallingUid() |
| 14306 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14307 | Slog.w(TAG, msg); |
| 14308 | throw new SecurityException(msg); |
| 14309 | } |
| 14310 | synchronized (this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14311 | return isUserRunningLocked(userId); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14312 | } |
| 14313 | } |
| 14314 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14315 | boolean isUserRunningLocked(int userId) { |
| 14316 | UserStartedState state = mStartedUsers.get(userId); |
| 14317 | return state != null && state.mState != UserStartedState.STATE_STOPPING; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14318 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14319 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14320 | @Override |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14321 | public int[] getRunningUserIds() { |
| 14322 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14323 | != PackageManager.PERMISSION_GRANTED) { |
| 14324 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14325 | + Binder.getCallingPid() |
| 14326 | + ", uid=" + Binder.getCallingUid() |
| 14327 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14328 | Slog.w(TAG, msg); |
| 14329 | throw new SecurityException(msg); |
| 14330 | } |
| 14331 | synchronized (this) { |
| 14332 | return mStartedUserArray; |
| 14333 | } |
| 14334 | } |
| 14335 | |
| 14336 | private void updateStartedUserArrayLocked() { |
| 14337 | mStartedUserArray = new int[mStartedUsers.size()]; |
| 14338 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 14339 | mStartedUserArray[i] = mStartedUsers.keyAt(i); |
| 14340 | } |
| 14341 | } |
| 14342 | |
| 14343 | @Override |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14344 | public void registerUserSwitchObserver(IUserSwitchObserver observer) { |
| 14345 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14346 | != PackageManager.PERMISSION_GRANTED) { |
| 14347 | String msg = "Permission Denial: registerUserSwitchObserver() from pid=" |
| 14348 | + Binder.getCallingPid() |
| 14349 | + ", uid=" + Binder.getCallingUid() |
| 14350 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14351 | Slog.w(TAG, msg); |
| 14352 | throw new SecurityException(msg); |
| 14353 | } |
| 14354 | |
| 14355 | mUserSwitchObservers.register(observer); |
| 14356 | } |
| 14357 | |
| 14358 | @Override |
| 14359 | public void unregisterUserSwitchObserver(IUserSwitchObserver observer) { |
| 14360 | mUserSwitchObservers.unregister(observer); |
| 14361 | } |
| 14362 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14363 | private boolean userExists(int userId) { |
| 14364 | if (userId == 0) { |
| 14365 | return true; |
| 14366 | } |
| 14367 | UserManagerService ums = getUserManagerLocked(); |
| 14368 | return ums != null ? (ums.getUserInfo(userId) != null) : false; |
| 14369 | } |
| 14370 | |
| 14371 | int[] getUsersLocked() { |
| 14372 | UserManagerService ums = getUserManagerLocked(); |
| 14373 | return ums != null ? ums.getUserIds() : new int[] { 0 }; |
| 14374 | } |
| 14375 | |
| 14376 | UserManagerService getUserManagerLocked() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14377 | if (mUserManager == null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14378 | IBinder b = ServiceManager.getService(Context.USER_SERVICE); |
| 14379 | mUserManager = (UserManagerService)IUserManager.Stub.asInterface(b); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14380 | } |
| 14381 | return mUserManager; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14382 | } |
| 14383 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14384 | private void checkValidCaller(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14385 | if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14386 | |
| 14387 | throw new SecurityException("Caller uid=" + uid |
| 14388 | + " is not privileged to communicate with user=" + userId); |
| 14389 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14390 | |
| 14391 | private int applyUserId(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14392 | return UserHandle.getUid(userId, uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14393 | } |
| 14394 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 14395 | ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14396 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14397 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 14398 | newInfo.uid = applyUserId(info.uid, userId); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14399 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 14400 | + info.packageName; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14401 | return newInfo; |
| 14402 | } |
| 14403 | |
| 14404 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14405 | if (aInfo == null |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14406 | || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14407 | return aInfo; |
| 14408 | } |
| 14409 | |
| 14410 | ActivityInfo info = new ActivityInfo(aInfo); |
| 14411 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 14412 | return info; |
| 14413 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14414 | } |