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 | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 24 | import com.android.server.AppOpsService; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 25 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import com.android.server.IntentResolver; |
| 27 | import com.android.server.ProcessMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import com.android.server.SystemServer; |
| 29 | import com.android.server.Watchdog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 30 | import com.android.server.am.ActivityStack.ActivityState; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 31 | import com.android.server.pm.UserManagerService; |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 32 | import com.android.server.wm.AppTransition; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 33 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 35 | import dalvik.system.Zygote; |
| 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.app.Activity; |
| 38 | import android.app.ActivityManager; |
| 39 | import android.app.ActivityManagerNative; |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 40 | import android.app.ActivityOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.app.ActivityThread; |
| 42 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 43 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 44 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 46 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.app.IApplicationThread; |
| 48 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 49 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 50 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.app.IServiceConnection; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 52 | import android.app.IStopUserCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.app.IThumbnailReceiver; |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 54 | import android.app.IUiAutomationConnection; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 55 | import android.app.IUserSwitchObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 57 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 58 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.app.PendingIntent; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 60 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 61 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 62 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 63 | import android.content.ClipData; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 64 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.content.ComponentName; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 66 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.content.ContentResolver; |
| 68 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 69 | import android.content.DialogInterface; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 70 | import android.content.IContentProvider; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 71 | import android.content.IIntentReceiver; |
| 72 | import android.content.IIntentSender; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 73 | import android.content.Intent; |
| 74 | import android.content.IntentFilter; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 75 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.content.pm.ActivityInfo; |
| 77 | import android.content.pm.ApplicationInfo; |
| 78 | import android.content.pm.ConfigurationInfo; |
| 79 | import android.content.pm.IPackageDataObserver; |
| 80 | import android.content.pm.IPackageManager; |
| 81 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 82 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | import android.content.pm.PackageManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 84 | import android.content.pm.UserInfo; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 85 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 86 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.content.pm.ProviderInfo; |
| 88 | import android.content.pm.ResolveInfo; |
| 89 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 90 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.content.res.Configuration; |
| 92 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 93 | import android.net.Proxy; |
| 94 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.net.Uri; |
| 96 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 97 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 98 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 99 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 100 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 102 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.os.FileUtils; |
| 104 | import android.os.Handler; |
| 105 | import android.os.IBinder; |
| 106 | import android.os.IPermissionController; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 107 | import android.os.IRemoteCallback; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 108 | import android.os.IUserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.os.Looper; |
| 110 | import android.os.Message; |
| 111 | import android.os.Parcel; |
| 112 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 114 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.os.RemoteException; |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 116 | import android.os.SELinux; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 118 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | import android.os.SystemClock; |
| 120 | import android.os.SystemProperties; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 121 | import android.os.UpdateLock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 122 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | import android.provider.Settings; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 124 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import android.util.EventLog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 126 | import android.util.Log; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 127 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | import android.util.PrintWriterPrinter; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 129 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | import android.util.SparseArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 131 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | import android.view.Gravity; |
| 133 | import android.view.LayoutInflater; |
| 134 | import android.view.View; |
| 135 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 137 | import java.io.BufferedInputStream; |
| 138 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 139 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 140 | import java.io.DataInputStream; |
| 141 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | import java.io.File; |
| 143 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 144 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 146 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 147 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 148 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 150 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | import java.lang.ref.WeakReference; |
| 152 | import java.util.ArrayList; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 153 | import java.util.Arrays; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 154 | import java.util.Collections; |
| 155 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | import java.util.HashMap; |
| 157 | import java.util.HashSet; |
| 158 | import java.util.Iterator; |
| 159 | import java.util.List; |
| 160 | import java.util.Locale; |
| 161 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 162 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 163 | import java.util.concurrent.atomic.AtomicBoolean; |
| 164 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 166 | public final class ActivityManagerService extends ActivityManagerNative |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 167 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 168 | private static final String USER_DATA_DIR = "/data/user/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | static final String TAG = "ActivityManager"; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 170 | static final String TAG_MU = "ActivityManagerServiceMU"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 172 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 174 | static final boolean DEBUG_TASKS = localLOGV || false; |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 175 | static final boolean DEBUG_THUMBNAILS = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 177 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 178 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 179 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 180 | static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 181 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 183 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 185 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 186 | static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false; |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 187 | static final boolean DEBUG_CLEANUP = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 188 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 189 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 191 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 192 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 193 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 194 | static final boolean DEBUG_POWER = localLOGV || false; |
| 195 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 196 | static final boolean DEBUG_MU = localLOGV || false; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 197 | static final boolean DEBUG_IMMERSIVE = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | static final boolean VALIDATE_TOKENS = false; |
| 199 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 200 | |
| 201 | // Control over CPU and battery monitoring. |
| 202 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 203 | static final boolean MONITOR_CPU_USAGE = true; |
| 204 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 205 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 206 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 207 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 208 | // 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] | 209 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 210 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 211 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 213 | static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); |
| 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | // Maximum number of recent tasks that we can remember. |
| 216 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 217 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 218 | // Amount of time after a call to stopAppSwitches() during which we will |
| 219 | // prevent further untrusted switches from happening. |
| 220 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | |
| 222 | // How long we wait for a launched process to attach to the activity manager |
| 223 | // before we decide it's never going to come up for real. |
| 224 | static final int PROC_START_TIMEOUT = 10*1000; |
| 225 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 226 | // How long we wait for a launched process to attach to the activity manager |
| 227 | // before we decide it's never going to come up for real, when the process was |
| 228 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 229 | // could take much longer than usual. |
| 230 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | // How long to wait after going idle before forcing apps to GC. |
| 233 | static final int GC_TIMEOUT = 5*1000; |
| 234 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 235 | // The minimum amount of time between successive GC requests for a process. |
| 236 | static final int GC_MIN_INTERVAL = 60*1000; |
| 237 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 238 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 239 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 240 | |
| 241 | // The minimum sample duration we will allow before deciding we have |
| 242 | // enough data on wake locks to start killing things. |
| 243 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 244 | |
| 245 | // The minimum sample duration we will allow before deciding we have |
| 246 | // enough data on CPU usage to start killing things. |
| 247 | 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] | 248 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | // 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] | 250 | static final int BROADCAST_FG_TIMEOUT = 10*1000; |
| 251 | static final int BROADCAST_BG_TIMEOUT = 60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | // How long we wait until we timeout on key dispatching. |
| 254 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 257 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 258 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 259 | // Amount of time we wait for observers to handle a user switch before |
| 260 | // giving up on them and unfreezing the screen. |
| 261 | static final int USER_SWITCH_TIMEOUT = 2*1000; |
| 262 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 263 | // Maximum number of users we allow to be running at a time. |
| 264 | static final int MAX_RUNNING_USERS = 3; |
| 265 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 266 | // How long to wait in getTopActivityExtras for the activity to respond with the result. |
| 267 | static final int PENDING_ACTIVITY_RESULT_TIMEOUT = 2*2000; |
| 268 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 269 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | |
| 271 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 272 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 273 | public ActivityStack mMainStack; |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 274 | |
| 275 | private final boolean mHeadless; |
| 276 | |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 277 | // Whether we should show our dialogs (ANR, crash, etc) or just perform their |
| 278 | // default actuion automatically. Important for devices without direct input |
| 279 | // devices. |
| 280 | private boolean mShowDialogs = true; |
| 281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 283 | * Description of a request to start a new activity, which has been held |
| 284 | * due to app switches being disabled. |
| 285 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 286 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 287 | ActivityRecord r; |
| 288 | ActivityRecord sourceRecord; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 289 | int startFlags; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 293 | = new ArrayList<PendingActivityLaunch>(); |
| 294 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 296 | BroadcastQueue mFgBroadcastQueue; |
| 297 | BroadcastQueue mBgBroadcastQueue; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 298 | // Convenient for easy iteration over the queues. Foreground is first |
| 299 | // so that dispatch of foreground broadcasts gets precedence. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 300 | final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 301 | |
| 302 | BroadcastQueue broadcastQueueForIntent(Intent intent) { |
| 303 | final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; |
| 304 | if (DEBUG_BACKGROUND_BROADCAST) { |
| 305 | Slog.i(TAG, "Broadcast intent " + intent + " on " |
| 306 | + (isFg ? "foreground" : "background") |
| 307 | + " queue"); |
| 308 | } |
| 309 | return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; |
| 310 | } |
| 311 | |
| 312 | BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) { |
| 313 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 314 | BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver); |
| 315 | if (r != null) { |
| 316 | return r; |
| 317 | } |
| 318 | } |
| 319 | return null; |
| 320 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | |
| 322 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | * Activity we have told the window manager to have key focus. |
| 324 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 325 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 326 | |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 327 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | * List of intents that were used to start the most recent tasks. |
| 329 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 330 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 332 | public class PendingActivityExtras extends Binder implements Runnable { |
| 333 | public final ActivityRecord activity; |
| 334 | public boolean haveResult = false; |
| 335 | public Bundle result = null; |
| 336 | public PendingActivityExtras(ActivityRecord _activity) { |
| 337 | activity = _activity; |
| 338 | } |
| 339 | @Override |
| 340 | public void run() { |
| 341 | Slog.w(TAG, "getTopActivityExtras failed: timeout retrieving from " + activity); |
| 342 | synchronized (this) { |
| 343 | haveResult = true; |
| 344 | notifyAll(); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | final ArrayList<PendingActivityExtras> mPendingActivityExtras |
| 350 | = new ArrayList<PendingActivityExtras>(); |
| 351 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 353 | * Process management. |
| 354 | */ |
| 355 | final ProcessList mProcessList = new ProcessList(); |
| 356 | |
| 357 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | * All of the applications we currently have running organized by name. |
| 359 | * The keys are strings of the application package name (as |
| 360 | * returned by the package manager), and the keys are ApplicationRecord |
| 361 | * objects. |
| 362 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 363 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | |
| 365 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 366 | * The currently running isolated processes. |
| 367 | */ |
| 368 | final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>(); |
| 369 | |
| 370 | /** |
| 371 | * Counter for assigning isolated process uids, to avoid frequently reusing the |
| 372 | * same ones. |
| 373 | */ |
| 374 | int mNextIsolatedProcessUid = 0; |
| 375 | |
| 376 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 377 | * The currently running heavy-weight process, if any. |
| 378 | */ |
| 379 | ProcessRecord mHeavyWeightProcess = null; |
| 380 | |
| 381 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | * The last time that various processes have crashed. |
| 383 | */ |
| 384 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 385 | |
| 386 | /** |
| 387 | * Set of applications that we consider to be bad, and will reject |
| 388 | * incoming broadcasts from (which the user has no control over). |
| 389 | * Processes are added to this set when they have crashed twice within |
| 390 | * a minimum amount of time; they are removed from it when they are |
| 391 | * later restarted (hopefully due to some user action). The value is the |
| 392 | * time it was added to the list. |
| 393 | */ |
| 394 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 395 | |
| 396 | /** |
| 397 | * All of the processes we currently have running organized by pid. |
| 398 | * The keys are the pid running the application. |
| 399 | * |
| 400 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 401 | * activity manager lock! |
| 402 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 403 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | |
| 405 | /** |
| 406 | * All of the processes that have been forced to be foreground. The key |
| 407 | * is the pid of the caller who requested it (we hold a death |
| 408 | * link on it). |
| 409 | */ |
| 410 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 411 | int pid; |
| 412 | IBinder token; |
| 413 | } |
| 414 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 415 | = new SparseArray<ForegroundToken>(); |
| 416 | |
| 417 | /** |
| 418 | * List of records for processes that someone had tried to start before the |
| 419 | * system was ready. We don't start them at that point, but ensure they |
| 420 | * are started by the time booting is complete. |
| 421 | */ |
| 422 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 423 | = new ArrayList<ProcessRecord>(); |
| 424 | |
| 425 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | * List of persistent applications that are in the process |
| 427 | * of being started. |
| 428 | */ |
| 429 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 430 | = new ArrayList<ProcessRecord>(); |
| 431 | |
| 432 | /** |
| 433 | * Processes that are being forcibly torn down. |
| 434 | */ |
| 435 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 436 | = new ArrayList<ProcessRecord>(); |
| 437 | |
| 438 | /** |
| 439 | * List of running applications, sorted by recent usage. |
| 440 | * The first entry in the list is the least recently used. |
| 441 | * It contains ApplicationRecord objects. This list does NOT include |
| 442 | * any persistent application records (since we never want to exit them). |
| 443 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 444 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | = new ArrayList<ProcessRecord>(); |
| 446 | |
| 447 | /** |
| 448 | * List of processes that should gc as soon as things are idle. |
| 449 | */ |
| 450 | final ArrayList<ProcessRecord> mProcessesToGc |
| 451 | = new ArrayList<ProcessRecord>(); |
| 452 | |
| 453 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 454 | * This is the process holding what we currently consider to be |
| 455 | * the "home" activity. |
| 456 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 457 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 458 | |
| 459 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 460 | * This is the process holding the activity the user last visited that |
| 461 | * is in a different process from the one they are currently in. |
| 462 | */ |
| 463 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 464 | |
| 465 | /** |
| 466 | * The time at which the previous process was last visible. |
| 467 | */ |
| 468 | long mPreviousProcessVisibleTime; |
| 469 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 470 | /** |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 471 | * Which uses have been started, so are allowed to run code. |
| 472 | */ |
| 473 | final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>(); |
| 474 | |
| 475 | /** |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 476 | * LRU list of history of current users. Most recently current is at the end. |
| 477 | */ |
| 478 | final ArrayList<Integer> mUserLru = new ArrayList<Integer>(); |
| 479 | |
| 480 | /** |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 481 | * Constant array of the users that are currently started. |
| 482 | */ |
| 483 | int[] mStartedUserArray = new int[] { 0 }; |
| 484 | |
| 485 | /** |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 486 | * Registered observers of the user switching mechanics. |
| 487 | */ |
| 488 | final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers |
| 489 | = new RemoteCallbackList<IUserSwitchObserver>(); |
| 490 | |
| 491 | /** |
| 492 | * Currently active user switch. |
| 493 | */ |
| 494 | Object mCurUserSwitchCallback; |
| 495 | |
| 496 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 497 | * Packages that the user has asked to have run in screen size |
| 498 | * compatibility mode instead of filling the screen. |
| 499 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 500 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 501 | |
| 502 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | * Set of PendingResultRecord objects that are currently active. |
| 504 | */ |
| 505 | final HashSet mPendingResultRecords = new HashSet(); |
| 506 | |
| 507 | /** |
| 508 | * Set of IntentSenderRecord objects that are currently active. |
| 509 | */ |
| 510 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 511 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 512 | |
| 513 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 514 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 515 | * already logged DropBox entries for. Guarded by itself. If |
| 516 | * something (rogue user app) forces this over |
| 517 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 518 | */ |
| 519 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 520 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 521 | |
| 522 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 523 | * Strict Mode background batched logging state. |
| 524 | * |
| 525 | * The string buffer is guarded by itself, and its lock is also |
| 526 | * used to determine if another batched write is already |
| 527 | * in-flight. |
| 528 | */ |
| 529 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 530 | |
| 531 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | * Keeps track of all IIntentReceivers that have been registered for |
| 533 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 534 | * a ReceiverList. |
| 535 | */ |
| 536 | final HashMap mRegisteredReceivers = new HashMap(); |
| 537 | |
| 538 | /** |
| 539 | * Resolver for broadcast intents to registered receivers. |
| 540 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 541 | */ |
| 542 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 543 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 544 | @Override |
| 545 | protected boolean allowFilterResult( |
| 546 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 547 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 548 | for (int i=dest.size()-1; i>=0; i--) { |
| 549 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 550 | return false; |
| 551 | } |
| 552 | } |
| 553 | return true; |
| 554 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 555 | |
| 556 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 557 | protected BroadcastFilter newResult(BroadcastFilter filter, int match, int userId) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 558 | if (userId == UserHandle.USER_ALL || filter.owningUserId == UserHandle.USER_ALL |
| 559 | || userId == filter.owningUserId) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 560 | return super.newResult(filter, match, userId); |
| 561 | } |
| 562 | return null; |
| 563 | } |
| 564 | |
| 565 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 566 | protected BroadcastFilter[] newArray(int size) { |
| 567 | return new BroadcastFilter[size]; |
| 568 | } |
| 569 | |
| 570 | @Override |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 571 | protected String packageForFilter(BroadcastFilter filter) { |
| 572 | return filter.packageName; |
| 573 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | /** |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 577 | * 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] | 578 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 579 | * that action (which should usually be one). The SparseArray is keyed |
| 580 | * by the user ID the sticky is for, and can include UserHandle.USER_ALL |
| 581 | * for stickies that are sent to all users. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | */ |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 583 | final SparseArray<HashMap<String, ArrayList<Intent>>> mStickyBroadcasts = |
| 584 | new SparseArray<HashMap<String, ArrayList<Intent>>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 586 | final ActiveServices mServices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | |
| 588 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 589 | * Backup/restore process management |
| 590 | */ |
| 591 | String mBackupAppName = null; |
| 592 | BackupRecord mBackupTarget = null; |
| 593 | |
| 594 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | * List of PendingThumbnailsRecord objects of clients who are still |
| 596 | * waiting to receive all of the thumbnails for a task. |
| 597 | */ |
| 598 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 599 | |
| 600 | /** |
| 601 | * List of HistoryRecord objects that have been finished and must |
| 602 | * still report back to a pending thumbnail receiver. |
| 603 | */ |
| 604 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 605 | |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 606 | final ProviderMap mProviderMap; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | /** |
| 609 | * List of content providers who have clients waiting for them. The |
| 610 | * application is currently being launched and the provider will be |
| 611 | * removed from this list once it is published. |
| 612 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 613 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 614 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | |
| 616 | /** |
| 617 | * Global set of specific Uri permissions that have been granted. |
| 618 | */ |
| 619 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 620 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 621 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 622 | CoreSettingsObserver mCoreSettingsObserver; |
| 623 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | /** |
| 625 | * Thread-local storage used to carry caller permissions over through |
| 626 | * indirect content-provider access. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | */ |
| 628 | private class Identity { |
| 629 | public int pid; |
| 630 | public int uid; |
| 631 | |
| 632 | Identity(int _pid, int _uid) { |
| 633 | pid = _pid; |
| 634 | uid = _uid; |
| 635 | } |
| 636 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 637 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 639 | |
| 640 | /** |
| 641 | * All information we have collected about the runtime performance of |
| 642 | * any user id that can impact battery performance. |
| 643 | */ |
| 644 | final BatteryStatsService mBatteryStatsService; |
| 645 | |
| 646 | /** |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 647 | * Information about component usage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | */ |
| 649 | final UsageStatsService mUsageStatsService; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 650 | |
| 651 | /** |
| 652 | * Information about and control over application operations |
| 653 | */ |
| 654 | final AppOpsService mAppOpsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | |
| 656 | /** |
| 657 | * Current configuration information. HistoryRecord objects are given |
| 658 | * a reference to this object to indicate which configuration they are |
| 659 | * currently running in, so this object must be kept immutable. |
| 660 | */ |
| 661 | Configuration mConfiguration = new Configuration(); |
| 662 | |
| 663 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 664 | * Current sequencing integer of the configuration, for skipping old |
| 665 | * configurations. |
| 666 | */ |
| 667 | int mConfigurationSeq = 0; |
| 668 | |
| 669 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 670 | * Hardware-reported OpenGLES version. |
| 671 | */ |
| 672 | final int GL_ES_VERSION; |
| 673 | |
| 674 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 676 | * For example, references to the commonly used services. |
| 677 | */ |
| 678 | HashMap<String, IBinder> mAppBindArgs; |
| 679 | |
| 680 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 681 | * Temporary to avoid allocations. Protected by main lock. |
| 682 | */ |
| 683 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 684 | |
| 685 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | * Used to control how we initialize the service. |
| 687 | */ |
| 688 | boolean mStartRunning = false; |
| 689 | ComponentName mTopComponent; |
| 690 | String mTopAction; |
| 691 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 692 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | boolean mSystemReady = false; |
| 694 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 695 | boolean mWaitingUpdate = false; |
| 696 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 697 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 698 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | |
| 700 | Context mContext; |
| 701 | |
| 702 | int mFactoryTest; |
| 703 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 704 | boolean mCheckedForSetup; |
| 705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 707 | * The time at which we will allow normal application switches again, |
| 708 | * after a call to {@link #stopAppSwitches()}. |
| 709 | */ |
| 710 | long mAppSwitchesAllowedTime; |
| 711 | |
| 712 | /** |
| 713 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 714 | * is set; any switches after that will clear the time. |
| 715 | */ |
| 716 | boolean mDidAppSwitch; |
| 717 | |
| 718 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 719 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 720 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 721 | long mLastPowerCheckRealtime; |
| 722 | |
| 723 | /** |
| 724 | * Last time (in uptime) at which we checked for power usage. |
| 725 | */ |
| 726 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 727 | |
| 728 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | * Set while we are wanting to sleep, to prevent any |
| 730 | * activities from being started/resumed. |
| 731 | */ |
| 732 | boolean mSleeping = false; |
| 733 | |
| 734 | /** |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 735 | * State of external calls telling us if the device is asleep. |
| 736 | */ |
| 737 | boolean mWentToSleep = false; |
| 738 | |
| 739 | /** |
| 740 | * State of external call telling us if the lock screen is shown. |
| 741 | */ |
| 742 | boolean mLockScreenShown = false; |
| 743 | |
| 744 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 745 | * Set if we are shutting down the system, similar to sleeping. |
| 746 | */ |
| 747 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | |
| 749 | /** |
| 750 | * Task identifier that activities are currently being started |
| 751 | * in. Incremented each time a new task is created. |
| 752 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 753 | * integers that won't wrap. |
| 754 | */ |
| 755 | int mCurTask = 1; |
| 756 | |
| 757 | /** |
| 758 | * Current sequence id for oom_adj computation traversal. |
| 759 | */ |
| 760 | int mAdjSeq = 0; |
| 761 | |
| 762 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 763 | * Current sequence id for process LRU updating. |
| 764 | */ |
| 765 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | |
| 767 | /** |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 768 | * Keep track of the non-hidden/empty process we last found, to help |
| 769 | * determine how to distribute hidden/empty processes next time. |
| 770 | */ |
| 771 | int mNumNonHiddenProcs = 0; |
| 772 | |
| 773 | /** |
| 774 | * Keep track of the number of hidden procs, to balance oom adj |
| 775 | * distribution between those and empty procs. |
| 776 | */ |
| 777 | int mNumHiddenProcs = 0; |
| 778 | |
| 779 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 780 | * Keep track of the number of service processes we last found, to |
| 781 | * determine on the next iteration which should be B services. |
| 782 | */ |
| 783 | int mNumServiceProcs = 0; |
| 784 | int mNewNumServiceProcs = 0; |
| 785 | |
| 786 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | * System monitoring: number of processes that died since the last |
| 788 | * N procs were started. |
| 789 | */ |
| 790 | int[] mProcDeaths = new int[20]; |
| 791 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 792 | /** |
| 793 | * This is set if we had to do a delayed dexopt of an app before launching |
| 794 | * it, to increasing the ANR timeouts in that case. |
| 795 | */ |
| 796 | boolean mDidDexOpt; |
| 797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | String mDebugApp = null; |
| 799 | boolean mWaitForDebugger = false; |
| 800 | boolean mDebugTransient = false; |
| 801 | String mOrigDebugApp = null; |
| 802 | boolean mOrigWaitForDebugger = false; |
| 803 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 804 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 805 | String mProfileApp = null; |
| 806 | ProcessRecord mProfileProc = null; |
| 807 | String mProfileFile; |
| 808 | ParcelFileDescriptor mProfileFd; |
| 809 | int mProfileType = 0; |
| 810 | boolean mAutoStopProfiler = false; |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 811 | String mOpenGlTraceApp = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 813 | static class ProcessChangeItem { |
| 814 | static final int CHANGE_ACTIVITIES = 1<<0; |
| 815 | static final int CHANGE_IMPORTANCE= 1<<1; |
| 816 | int changes; |
| 817 | int uid; |
| 818 | int pid; |
| 819 | int importance; |
| 820 | boolean foregroundActivities; |
| 821 | } |
| 822 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 823 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 824 | = new RemoteCallbackList<IProcessObserver>(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 825 | ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5]; |
| 826 | |
| 827 | final ArrayList<ProcessChangeItem> mPendingProcessChanges |
| 828 | = new ArrayList<ProcessChangeItem>(); |
| 829 | final ArrayList<ProcessChangeItem> mAvailProcessChanges |
| 830 | = new ArrayList<ProcessChangeItem>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 831 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | * Runtime statistics collection thread. This object's lock is used to |
| 834 | * protect all related state. |
| 835 | */ |
| 836 | final Thread mProcessStatsThread; |
| 837 | |
| 838 | /** |
| 839 | * Used to collect process stats when showing not responding dialog. |
| 840 | * Protected by mProcessStatsThread. |
| 841 | */ |
| 842 | final ProcessStats mProcessStats = new ProcessStats( |
| 843 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 844 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 845 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | long mLastWriteTime = 0; |
| 848 | |
| 849 | /** |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 850 | * Used to retain an update lock when the foreground activity is in |
| 851 | * immersive mode. |
| 852 | */ |
| 853 | final UpdateLock mUpdateLock = new UpdateLock("immersive"); |
| 854 | |
| 855 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | * Set to true after the system has finished booting. |
| 857 | */ |
| 858 | boolean mBooted = false; |
| 859 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 860 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 861 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | |
| 863 | WindowManagerService mWindowManager; |
| 864 | |
| 865 | static ActivityManagerService mSelf; |
| 866 | static ActivityThread mSystemThread; |
| 867 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 868 | private int mCurrentUserId = 0; |
| 869 | private int[] mCurrentUserArray = new int[] { 0 }; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 870 | private UserManagerService mUserManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 871 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 873 | final ProcessRecord mApp; |
| 874 | final int mPid; |
| 875 | final IApplicationThread mAppThread; |
| 876 | |
| 877 | AppDeathRecipient(ProcessRecord app, int pid, |
| 878 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 879 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | TAG, "New death recipient " + this |
| 881 | + " for thread " + thread.asBinder()); |
| 882 | mApp = app; |
| 883 | mPid = pid; |
| 884 | mAppThread = thread; |
| 885 | } |
| 886 | |
| 887 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 888 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | TAG, "Death received in " + this |
| 890 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | synchronized(ActivityManagerService.this) { |
| 892 | appDiedLocked(mApp, mPid, mAppThread); |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | static final int SHOW_ERROR_MSG = 1; |
| 898 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 899 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 900 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 901 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 902 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 904 | static final int UPDATE_TIME_ZONE = 13; |
| 905 | static final int SHOW_UID_ERROR_MSG = 14; |
| 906 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 908 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 909 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 910 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 911 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 912 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 913 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 914 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 915 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 916 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 917 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 918 | static final int DISPATCH_PROCESSES_CHANGED = 31; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 919 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 920 | static final int REPORT_MEM_USAGE = 33; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 921 | static final int REPORT_USER_SWITCH_MSG = 34; |
| 922 | static final int CONTINUE_USER_SWITCH_MSG = 35; |
| 923 | static final int USER_SWITCH_TIMEOUT_MSG = 36; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 924 | static final int IMMERSIVE_MODE_LOCK_MSG = 37; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 926 | static final int FIRST_ACTIVITY_STACK_MSG = 100; |
| 927 | static final int FIRST_BROADCAST_QUEUE_MSG = 200; |
| 928 | static final int FIRST_COMPAT_MODE_MSG = 300; |
| 929 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 931 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 932 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | |
| 934 | final Handler mHandler = new Handler() { |
| 935 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 936 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | //} |
| 938 | |
| 939 | public void handleMessage(Message msg) { |
| 940 | switch (msg.what) { |
| 941 | case SHOW_ERROR_MSG: { |
| 942 | HashMap data = (HashMap) msg.obj; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 943 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 944 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | synchronized (ActivityManagerService.this) { |
| 946 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 947 | AppErrorResult res = (AppErrorResult) data.get("result"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 949 | Slog.e(TAG, "App already has crash dialog: " + proc); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 950 | if (res != null) { |
| 951 | res.set(0); |
| 952 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | return; |
| 954 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 955 | if (!showBackground && UserHandle.getAppId(proc.uid) |
| 956 | >= Process.FIRST_APPLICATION_UID && proc.userId != mCurrentUserId |
| 957 | && proc.pid != MY_PID) { |
| 958 | Slog.w(TAG, "Skipping crash dialog of " + proc + ": background"); |
| 959 | if (res != null) { |
| 960 | res.set(0); |
| 961 | } |
| 962 | return; |
| 963 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 964 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 965 | Dialog d = new AppErrorDialog(mContext, |
| 966 | ActivityManagerService.this, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | d.show(); |
| 968 | proc.crashDialog = d; |
| 969 | } else { |
| 970 | // The device is asleep, so just pretend that the user |
| 971 | // saw a crash dialog and hit "force quit". |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 972 | if (res != null) { |
| 973 | res.set(0); |
| 974 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | } |
| 976 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 977 | |
| 978 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | } break; |
| 980 | case SHOW_NOT_RESPONDING_MSG: { |
| 981 | synchronized (ActivityManagerService.this) { |
| 982 | HashMap data = (HashMap) msg.obj; |
| 983 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 984 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 985 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | return; |
| 987 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 988 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 989 | Intent intent = new Intent("android.intent.action.ANR"); |
| 990 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 991 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 992 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 993 | } |
| 994 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 995 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 996 | 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] | 997 | |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 998 | if (mShowDialogs) { |
| 999 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 1000 | mContext, proc, (ActivityRecord)data.get("activity"), |
| 1001 | msg.arg1 != 0); |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 1002 | d.show(); |
| 1003 | proc.anrDialog = d; |
| 1004 | } else { |
| 1005 | // Just kill the app if there is no dialog to be shown. |
| 1006 | killAppAtUsersRequest(proc, null); |
| 1007 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1009 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1010 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1012 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 1013 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 1014 | synchronized (ActivityManagerService.this) { |
| 1015 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 1016 | if (proc == null) { |
| 1017 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 1018 | break; |
| 1019 | } |
| 1020 | if (proc.crashDialog != null) { |
| 1021 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 1022 | return; |
| 1023 | } |
| 1024 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1025 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1026 | Dialog d = new StrictModeViolationDialog(mContext, |
| 1027 | ActivityManagerService.this, res, proc); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1028 | d.show(); |
| 1029 | proc.crashDialog = d; |
| 1030 | } else { |
| 1031 | // The device is asleep, so just pretend that the user |
| 1032 | // saw a crash dialog and hit "force quit". |
| 1033 | res.set(0); |
| 1034 | } |
| 1035 | } |
| 1036 | ensureBootCompleted(); |
| 1037 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | case SHOW_FACTORY_ERROR_MSG: { |
| 1039 | Dialog d = new FactoryErrorDialog( |
| 1040 | mContext, msg.getData().getCharSequence("msg")); |
| 1041 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1042 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | } break; |
| 1044 | case UPDATE_CONFIGURATION_MSG: { |
| 1045 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1046 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 1047 | } break; |
| 1048 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 1049 | synchronized (ActivityManagerService.this) { |
| 1050 | performAppGcsIfAppropriateLocked(); |
| 1051 | } |
| 1052 | } break; |
| 1053 | case WAIT_FOR_DEBUGGER_MSG: { |
| 1054 | synchronized (ActivityManagerService.this) { |
| 1055 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1056 | if (msg.arg1 != 0) { |
| 1057 | if (!app.waitedForDebugger) { |
| 1058 | Dialog d = new AppWaitingForDebuggerDialog( |
| 1059 | ActivityManagerService.this, |
| 1060 | mContext, app); |
| 1061 | app.waitDialog = d; |
| 1062 | app.waitedForDebugger = true; |
| 1063 | d.show(); |
| 1064 | } |
| 1065 | } else { |
| 1066 | if (app.waitDialog != null) { |
| 1067 | app.waitDialog.dismiss(); |
| 1068 | app.waitDialog = null; |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1074 | if (mDidDexOpt) { |
| 1075 | mDidDexOpt = false; |
| 1076 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 1077 | nmsg.obj = msg.obj; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1078 | mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1079 | return; |
| 1080 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1081 | mServices.serviceTimeout((ProcessRecord)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | } break; |
| 1083 | case UPDATE_TIME_ZONE: { |
| 1084 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1085 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1086 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | if (r.thread != null) { |
| 1088 | try { |
| 1089 | r.thread.updateTimeZone(); |
| 1090 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1091 | 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] | 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1096 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1097 | case CLEAR_DNS_CACHE: { |
| 1098 | synchronized (ActivityManagerService.this) { |
| 1099 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1100 | ProcessRecord r = mLruProcesses.get(i); |
| 1101 | if (r.thread != null) { |
| 1102 | try { |
| 1103 | r.thread.clearDnsCache(); |
| 1104 | } catch (RemoteException ex) { |
| 1105 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1111 | case UPDATE_HTTP_PROXY: { |
| 1112 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1113 | String host = ""; |
| 1114 | String port = ""; |
| 1115 | String exclList = ""; |
| 1116 | if (proxy != null) { |
| 1117 | host = proxy.getHost(); |
| 1118 | port = Integer.toString(proxy.getPort()); |
| 1119 | exclList = proxy.getExclusionList(); |
| 1120 | } |
| 1121 | synchronized (ActivityManagerService.this) { |
| 1122 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1123 | ProcessRecord r = mLruProcesses.get(i); |
| 1124 | if (r.thread != null) { |
| 1125 | try { |
| 1126 | r.thread.setHttpProxy(host, port, exclList); |
| 1127 | } catch (RemoteException ex) { |
| 1128 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1129 | r.info.processName); |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1136 | String title = "System UIDs Inconsistent"; |
| 1137 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1138 | + " data partition or your device will be unstable."; |
| 1139 | Log.e(TAG, title + ": " + text); |
| 1140 | if (mShowDialogs) { |
| 1141 | // XXX This is a temporary dialog, no need to localize. |
| 1142 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1143 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1144 | d.setCancelable(false); |
| 1145 | d.setTitle(title); |
| 1146 | d.setMessage(text); |
| 1147 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1148 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1149 | mUidAlert = d; |
| 1150 | d.show(); |
| 1151 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | } break; |
| 1153 | case IM_FEELING_LUCKY_MSG: { |
| 1154 | if (mUidAlert != null) { |
| 1155 | mUidAlert.dismiss(); |
| 1156 | mUidAlert = null; |
| 1157 | } |
| 1158 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1160 | if (mDidDexOpt) { |
| 1161 | mDidDexOpt = false; |
| 1162 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1163 | nmsg.obj = msg.obj; |
| 1164 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1165 | return; |
| 1166 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1168 | synchronized (ActivityManagerService.this) { |
| 1169 | processStartTimedOutLocked(app); |
| 1170 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1171 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1172 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1173 | synchronized (ActivityManagerService.this) { |
| 1174 | doPendingActivityLaunchesLocked(true); |
| 1175 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1176 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1177 | case KILL_APPLICATION_MSG: { |
| 1178 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1179 | int appid = msg.arg1; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1180 | boolean restart = (msg.arg2 == 1); |
| 1181 | String pkg = (String) msg.obj; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1182 | forceStopPackageLocked(pkg, appid, restart, false, true, false, |
| 1183 | UserHandle.USER_ALL); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1184 | } |
| 1185 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1186 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1187 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1188 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1189 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1190 | INotificationManager inm = NotificationManager.getService(); |
| 1191 | if (inm == null) { |
| 1192 | return; |
| 1193 | } |
| 1194 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1195 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1196 | ProcessRecord process = root.app; |
| 1197 | if (process == null) { |
| 1198 | return; |
| 1199 | } |
| 1200 | |
| 1201 | try { |
| 1202 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1203 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1204 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1205 | Notification notification = new Notification(); |
| 1206 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1207 | notification.when = 0; |
| 1208 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1209 | notification.tickerText = text; |
| 1210 | notification.defaults = 0; // please be quiet |
| 1211 | notification.sound = null; |
| 1212 | notification.vibrate = null; |
| 1213 | notification.setLatestEventInfo(context, text, |
| 1214 | mContext.getText(R.string.heavy_weight_notification_detail), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1215 | PendingIntent.getActivityAsUser(mContext, 0, root.intent, |
| 1216 | PendingIntent.FLAG_CANCEL_CURRENT, null, |
| 1217 | new UserHandle(root.userId))); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1218 | |
| 1219 | try { |
| 1220 | int[] outId = new int[1]; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1221 | inm.enqueueNotificationWithTag("android", "android", null, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1222 | R.string.heavy_weight_notification, |
| 1223 | notification, outId, root.userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1224 | } catch (RuntimeException e) { |
| 1225 | Slog.w(ActivityManagerService.TAG, |
| 1226 | "Error showing notification for heavy-weight app", e); |
| 1227 | } catch (RemoteException e) { |
| 1228 | } |
| 1229 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1230 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1231 | } |
| 1232 | } break; |
| 1233 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1234 | INotificationManager inm = NotificationManager.getService(); |
| 1235 | if (inm == null) { |
| 1236 | return; |
| 1237 | } |
| 1238 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1239 | inm.cancelNotificationWithTag("android", null, |
| 1240 | R.string.heavy_weight_notification, msg.arg1); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1241 | } catch (RuntimeException e) { |
| 1242 | Slog.w(ActivityManagerService.TAG, |
| 1243 | "Error canceling notification for service", e); |
| 1244 | } catch (RemoteException e) { |
| 1245 | } |
| 1246 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1247 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1248 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1249 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1250 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1251 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1252 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1253 | } |
| 1254 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1255 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1256 | synchronized (ActivityManagerService.this) { |
| 1257 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1258 | if (mCompatModeDialog != null) { |
| 1259 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1260 | ar.info.applicationInfo.packageName)) { |
| 1261 | return; |
| 1262 | } |
| 1263 | mCompatModeDialog.dismiss(); |
| 1264 | mCompatModeDialog = null; |
| 1265 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1266 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1267 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1268 | ar.packageName)) { |
| 1269 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1270 | ar.info.applicationInfo); |
| 1271 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1272 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1273 | mCompatModeDialog = new CompatModeDialog( |
| 1274 | ActivityManagerService.this, mContext, |
| 1275 | ar.info.applicationInfo); |
| 1276 | mCompatModeDialog.show(); |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1281 | break; |
| 1282 | } |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 1283 | case DISPATCH_PROCESSES_CHANGED: { |
| 1284 | dispatchProcessesChanged(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1285 | break; |
| 1286 | } |
| 1287 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1288 | final int pid = msg.arg1; |
| 1289 | final int uid = msg.arg2; |
| 1290 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1291 | break; |
| 1292 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1293 | case REPORT_MEM_USAGE: { |
| 1294 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1295 | if (!isDebuggable) { |
| 1296 | return; |
| 1297 | } |
| 1298 | synchronized (ActivityManagerService.this) { |
| 1299 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1300 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1301 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1302 | // avoid spamming. |
| 1303 | return; |
| 1304 | } |
| 1305 | mLastMemUsageReportTime = now; |
| 1306 | } |
| 1307 | Thread thread = new Thread() { |
| 1308 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1309 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1310 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1311 | StringWriter oomSw = new StringWriter(); |
| 1312 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1313 | StringWriter catSw = new StringWriter(); |
| 1314 | PrintWriter catPw = new PrintWriter(catSw); |
| 1315 | String[] emptyArgs = new String[] { }; |
| 1316 | StringBuilder tag = new StringBuilder(128); |
| 1317 | StringBuilder stack = new StringBuilder(128); |
| 1318 | tag.append("Low on memory -- "); |
| 1319 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1320 | tag, stack); |
| 1321 | dropBuilder.append(stack); |
| 1322 | dropBuilder.append('\n'); |
| 1323 | dropBuilder.append('\n'); |
| 1324 | String oomString = oomSw.toString(); |
| 1325 | dropBuilder.append(oomString); |
| 1326 | dropBuilder.append('\n'); |
| 1327 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1328 | try { |
| 1329 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1330 | "procrank", }); |
| 1331 | final InputStreamReader converter = new InputStreamReader( |
| 1332 | proc.getInputStream()); |
| 1333 | BufferedReader in = new BufferedReader(converter); |
| 1334 | String line; |
| 1335 | while (true) { |
| 1336 | line = in.readLine(); |
| 1337 | if (line == null) { |
| 1338 | break; |
| 1339 | } |
| 1340 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1341 | logBuilder.append(line); |
| 1342 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1343 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1344 | dropBuilder.append(line); |
| 1345 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1346 | } |
| 1347 | converter.close(); |
| 1348 | } catch (IOException e) { |
| 1349 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1350 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1351 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1352 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1353 | catPw.println(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1354 | mServices.dumpServicesLocked(null, catPw, emptyArgs, 0, |
| 1355 | false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1356 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1357 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1358 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1359 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1360 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1361 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1362 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1363 | synchronized (ActivityManagerService.this) { |
| 1364 | long now = SystemClock.uptimeMillis(); |
| 1365 | if (mLastMemUsageReportTime < now) { |
| 1366 | mLastMemUsageReportTime = now; |
| 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | }; |
| 1371 | thread.start(); |
| 1372 | break; |
| 1373 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1374 | case REPORT_USER_SWITCH_MSG: { |
| 1375 | dispatchUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1376 | break; |
| 1377 | } |
| 1378 | case CONTINUE_USER_SWITCH_MSG: { |
| 1379 | continueUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1380 | break; |
| 1381 | } |
| 1382 | case USER_SWITCH_TIMEOUT_MSG: { |
| 1383 | timeoutUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1384 | break; |
| 1385 | } |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1386 | case IMMERSIVE_MODE_LOCK_MSG: { |
| 1387 | final boolean nextState = (msg.arg1 != 0); |
| 1388 | if (mUpdateLock.isHeld() != nextState) { |
| 1389 | if (DEBUG_IMMERSIVE) { |
| 1390 | final ActivityRecord r = (ActivityRecord) msg.obj; |
| 1391 | Slog.d(TAG, "Applying new update lock state '" + nextState + "' for " + r); |
| 1392 | } |
| 1393 | if (nextState) { |
| 1394 | mUpdateLock.acquire(); |
| 1395 | } else { |
| 1396 | mUpdateLock.release(); |
| 1397 | } |
| 1398 | } |
| 1399 | break; |
| 1400 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | }; |
| 1404 | |
| 1405 | public static void setSystemProcess() { |
| 1406 | try { |
| 1407 | ActivityManagerService m = mSelf; |
| 1408 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1409 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1411 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1412 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | if (MONITOR_CPU_USAGE) { |
| 1414 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1415 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1417 | |
| 1418 | ApplicationInfo info = |
| 1419 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1420 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1421 | mSystemThread.installSystemApplicationInfo(info); |
| 1422 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | synchronized (mSelf) { |
| 1424 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1425 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1426 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1428 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1429 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1430 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | synchronized (mSelf.mPidsSelfLocked) { |
| 1432 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1433 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1434 | mSelf.updateLruProcessLocked(app, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | } |
| 1436 | } catch (PackageManager.NameNotFoundException e) { |
| 1437 | throw new RuntimeException( |
| 1438 | "Unable to find android system package", e); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | public void setWindowManager(WindowManagerService wm) { |
| 1443 | mWindowManager = wm; |
| 1444 | } |
| 1445 | |
| 1446 | public static final Context main(int factoryTest) { |
| 1447 | AThread thr = new AThread(); |
| 1448 | thr.start(); |
| 1449 | |
| 1450 | synchronized (thr) { |
| 1451 | while (thr.mService == null) { |
| 1452 | try { |
| 1453 | thr.wait(); |
| 1454 | } catch (InterruptedException e) { |
| 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | ActivityManagerService m = thr.mService; |
| 1460 | mSelf = m; |
| 1461 | ActivityThread at = ActivityThread.systemMain(); |
| 1462 | mSystemThread = at; |
| 1463 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1464 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | m.mContext = context; |
| 1466 | m.mFactoryTest = factoryTest; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1467 | m.mMainStack = new ActivityStack(m, context, true, thr.mLooper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | |
| 1469 | m.mBatteryStatsService.publish(context); |
| 1470 | m.mUsageStatsService.publish(context); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1471 | m.mAppOpsService.publish(context); |
| 1472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | synchronized (thr) { |
| 1474 | thr.mReady = true; |
| 1475 | thr.notifyAll(); |
| 1476 | } |
| 1477 | |
| 1478 | m.startRunning(null, null, null, null); |
| 1479 | |
| 1480 | return context; |
| 1481 | } |
| 1482 | |
| 1483 | public static ActivityManagerService self() { |
| 1484 | return mSelf; |
| 1485 | } |
| 1486 | |
| 1487 | static class AThread extends Thread { |
| 1488 | ActivityManagerService mService; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1489 | Looper mLooper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | boolean mReady = false; |
| 1491 | |
| 1492 | public AThread() { |
| 1493 | super("ActivityManager"); |
| 1494 | } |
| 1495 | |
| 1496 | public void run() { |
| 1497 | Looper.prepare(); |
| 1498 | |
| 1499 | android.os.Process.setThreadPriority( |
| 1500 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1501 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | |
| 1503 | ActivityManagerService m = new ActivityManagerService(); |
| 1504 | |
| 1505 | synchronized (this) { |
| 1506 | mService = m; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1507 | mLooper = Looper.myLooper(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | notifyAll(); |
| 1509 | } |
| 1510 | |
| 1511 | synchronized (this) { |
| 1512 | while (!mReady) { |
| 1513 | try { |
| 1514 | wait(); |
| 1515 | } catch (InterruptedException e) { |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1520 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1521 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1522 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1523 | } |
| 1524 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | Looper.loop(); |
| 1526 | } |
| 1527 | } |
| 1528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | static class MemBinder extends Binder { |
| 1530 | ActivityManagerService mActivityManagerService; |
| 1531 | MemBinder(ActivityManagerService activityManagerService) { |
| 1532 | mActivityManagerService = activityManagerService; |
| 1533 | } |
| 1534 | |
| 1535 | @Override |
| 1536 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1537 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1538 | != PackageManager.PERMISSION_GRANTED) { |
| 1539 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1540 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1541 | + " without permission " + android.Manifest.permission.DUMP); |
| 1542 | return; |
| 1543 | } |
| 1544 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1545 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1546 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1550 | static class GraphicsBinder extends Binder { |
| 1551 | ActivityManagerService mActivityManagerService; |
| 1552 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1553 | mActivityManagerService = activityManagerService; |
| 1554 | } |
| 1555 | |
| 1556 | @Override |
| 1557 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1558 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1559 | != PackageManager.PERMISSION_GRANTED) { |
| 1560 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1561 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1562 | + " without permission " + android.Manifest.permission.DUMP); |
| 1563 | return; |
| 1564 | } |
| 1565 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1566 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1570 | static class DbBinder extends Binder { |
| 1571 | ActivityManagerService mActivityManagerService; |
| 1572 | DbBinder(ActivityManagerService activityManagerService) { |
| 1573 | mActivityManagerService = activityManagerService; |
| 1574 | } |
| 1575 | |
| 1576 | @Override |
| 1577 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1578 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1579 | != PackageManager.PERMISSION_GRANTED) { |
| 1580 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1581 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1582 | + " without permission " + android.Manifest.permission.DUMP); |
| 1583 | return; |
| 1584 | } |
| 1585 | |
| 1586 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1587 | } |
| 1588 | } |
| 1589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | static class CpuBinder extends Binder { |
| 1591 | ActivityManagerService mActivityManagerService; |
| 1592 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1593 | mActivityManagerService = activityManagerService; |
| 1594 | } |
| 1595 | |
| 1596 | @Override |
| 1597 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1598 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1599 | != PackageManager.PERMISSION_GRANTED) { |
| 1600 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1601 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1602 | + " without permission " + android.Manifest.permission.DUMP); |
| 1603 | return; |
| 1604 | } |
| 1605 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1607 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1608 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1609 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1615 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1616 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1617 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1618 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1619 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1620 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 1621 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1622 | mServices = new ActiveServices(this); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1623 | mProviderMap = new ProviderMap(this); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1624 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1625 | File dataDir = Environment.getDataDirectory(); |
| 1626 | File systemDir = new File(dataDir, "system"); |
| 1627 | systemDir.mkdirs(); |
| 1628 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1629 | systemDir, "batterystats.bin").toString()); |
| 1630 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1631 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1632 | mOnBattery = DEBUG_POWER ? true |
| 1633 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1634 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1635 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1636 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1637 | systemDir, "usagestats").toString()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1638 | mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml")); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1639 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1640 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1641 | // User 0 is the first and only user that runs at boot. |
| 1642 | mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true)); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1643 | mUserLru.add(Integer.valueOf(0)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1644 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1645 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1646 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1647 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1648 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1649 | mConfiguration.setToDefaults(); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1650 | mConfiguration.setLocale(Locale.getDefault()); |
| 1651 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1652 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | mProcessStats.init(); |
| 1654 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1655 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | // Add ourself to the Watchdog monitors. |
| 1658 | Watchdog.getInstance().addMonitor(this); |
| 1659 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1660 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1661 | public void run() { |
| 1662 | while (true) { |
| 1663 | try { |
| 1664 | try { |
| 1665 | synchronized(this) { |
| 1666 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1667 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1669 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | // + ", write delay=" + nextWriteDelay); |
| 1671 | if (nextWriteDelay < nextCpuDelay) { |
| 1672 | nextCpuDelay = nextWriteDelay; |
| 1673 | } |
| 1674 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1675 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1676 | this.wait(nextCpuDelay); |
| 1677 | } |
| 1678 | } |
| 1679 | } catch (InterruptedException e) { |
| 1680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1681 | updateCpuStatsNow(); |
| 1682 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1683 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | }; |
| 1688 | mProcessStatsThread.start(); |
| 1689 | } |
| 1690 | |
| 1691 | @Override |
| 1692 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1693 | throws RemoteException { |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 1694 | if (code == SYSPROPS_TRANSACTION) { |
| 1695 | // We need to tell all apps about the system property change. |
| 1696 | ArrayList<IBinder> procs = new ArrayList<IBinder>(); |
| 1697 | synchronized(this) { |
| 1698 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 1699 | final int NA = apps.size(); |
| 1700 | for (int ia=0; ia<NA; ia++) { |
| 1701 | ProcessRecord app = apps.valueAt(ia); |
| 1702 | if (app.thread != null) { |
| 1703 | procs.add(app.thread.asBinder()); |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | int N = procs.size(); |
| 1710 | for (int i=0; i<N; i++) { |
| 1711 | Parcel data2 = Parcel.obtain(); |
| 1712 | try { |
| 1713 | procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0); |
| 1714 | } catch (RemoteException e) { |
| 1715 | } |
| 1716 | data2.recycle(); |
| 1717 | } |
| 1718 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1719 | try { |
| 1720 | return super.onTransact(code, data, reply, flags); |
| 1721 | } catch (RuntimeException e) { |
| 1722 | // The activity manager only throws security exceptions, so let's |
| 1723 | // log all others. |
| 1724 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1725 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | } |
| 1727 | throw e; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1732 | final long now = SystemClock.uptimeMillis(); |
| 1733 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1734 | return; |
| 1735 | } |
| 1736 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1737 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | mProcessStatsThread.notify(); |
| 1739 | } |
| 1740 | } |
| 1741 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | void updateCpuStatsNow() { |
| 1744 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1745 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | final long now = SystemClock.uptimeMillis(); |
| 1747 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1748 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1749 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1750 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1751 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | haveNewCpuStats = true; |
| 1753 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1754 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1755 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1756 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1757 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1758 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1759 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1760 | int user = mProcessStats.getLastUserTime(); |
| 1761 | int system = mProcessStats.getLastSystemTime(); |
| 1762 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1763 | int irq = mProcessStats.getLastIrqTime(); |
| 1764 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1765 | int idle = mProcessStats.getLastIdleTime(); |
| 1766 | |
| 1767 | int total = user + system + iowait + irq + softIrq + idle; |
| 1768 | if (total == 0) total = 1; |
| 1769 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1770 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1772 | (user * 100) / total, |
| 1773 | (system * 100) / total, |
| 1774 | (iowait * 100) / total, |
| 1775 | (irq * 100) / total, |
| 1776 | (softIrq * 100) / total); |
| 1777 | } |
| 1778 | } |
| 1779 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1780 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1781 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1782 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | synchronized(mPidsSelfLocked) { |
| 1784 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1785 | if (mOnBattery) { |
| 1786 | int perc = bstats.startAddingCpuLocked(); |
| 1787 | int totalUTime = 0; |
| 1788 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1789 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1790 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1791 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1792 | if (!st.working) { |
| 1793 | continue; |
| 1794 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1796 | int otherUTime = (st.rel_utime*perc)/100; |
| 1797 | int otherSTime = (st.rel_stime*perc)/100; |
| 1798 | totalUTime += otherUTime; |
| 1799 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | if (pr != null) { |
| 1801 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1802 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1803 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1804 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1805 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1806 | } else { |
| 1807 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1808 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1809 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1810 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1811 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1812 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1813 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1814 | } |
| 1815 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1816 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1817 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1821 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1823 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1824 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | } |
| 1829 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1830 | @Override |
| 1831 | public void batteryNeedsCpuUpdate() { |
| 1832 | updateCpuStatsNow(); |
| 1833 | } |
| 1834 | |
| 1835 | @Override |
| 1836 | public void batteryPowerChanged(boolean onBattery) { |
| 1837 | // When plugging in, update the CPU stats first before changing |
| 1838 | // the plug state. |
| 1839 | updateCpuStatsNow(); |
| 1840 | synchronized (this) { |
| 1841 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1842 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1847 | /** |
| 1848 | * Initialize the application bind args. These are passed to each |
| 1849 | * process when the bindApplication() IPC is sent to the process. They're |
| 1850 | * lazily setup to make sure the services are running when they're asked for. |
| 1851 | */ |
| 1852 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1853 | if (mAppBindArgs == null) { |
| 1854 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1855 | |
| 1856 | // Setup the application init args |
| 1857 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1858 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1859 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1860 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1861 | } |
| 1862 | return mAppBindArgs; |
| 1863 | } |
| 1864 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1865 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1866 | if (mFocusedActivity != r) { |
| 1867 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1868 | if (r != null) { |
| 1869 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1870 | } |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1871 | applyUpdateLockStateLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1875 | final void applyUpdateLockStateLocked(ActivityRecord r) { |
| 1876 | // Modifications to the UpdateLock state are done on our handler, outside |
| 1877 | // the activity manager's locks. The new state is determined based on the |
| 1878 | // state *now* of the relevant activity record. The object is passed to |
| 1879 | // the handler solely for logging detail, not to be consulted/modified. |
| 1880 | final boolean nextState = r != null && r.immersive; |
| 1881 | mHandler.sendMessage( |
| 1882 | mHandler.obtainMessage(IMMERSIVE_MODE_LOCK_MSG, (nextState) ? 1 : 0, 0, r)); |
| 1883 | } |
| 1884 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1885 | private final void updateLruProcessInternalLocked(ProcessRecord app, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1886 | // 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] | 1887 | int lrui = mLruProcesses.indexOf(app); |
| 1888 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1889 | |
| 1890 | int i = mLruProcesses.size()-1; |
| 1891 | int skipTop = 0; |
| 1892 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1893 | app.lruSeq = mLruSeq; |
| 1894 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1895 | // compute the new weight for this process. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1896 | app.lastActivityTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1897 | if (app.activities.size() > 0) { |
| 1898 | // If this process has activities, we more strongly want to keep |
| 1899 | // it around. |
| 1900 | app.lruWeight = app.lastActivityTime; |
| 1901 | } else if (app.pubProviders.size() > 0) { |
| 1902 | // If this process contains content providers, we want to keep |
| 1903 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1904 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1905 | // 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] | 1906 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1907 | } else { |
| 1908 | // If this process doesn't have activities, we less strongly |
| 1909 | // want to keep it around, and generally want to avoid getting |
| 1910 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1911 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1912 | // 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] | 1913 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1914 | } |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1915 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1916 | while (i >= 0) { |
| 1917 | ProcessRecord p = mLruProcesses.get(i); |
| 1918 | // If this app shouldn't be in front of the first N background |
| 1919 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1920 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1921 | skipTop--; |
| 1922 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1923 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1924 | mLruProcesses.add(i+1, app); |
| 1925 | break; |
| 1926 | } |
| 1927 | i--; |
| 1928 | } |
| 1929 | if (i < 0) { |
| 1930 | mLruProcesses.add(0, app); |
| 1931 | } |
| 1932 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1933 | // If the app is currently using a content provider or service, |
| 1934 | // bump those processes as well. |
| 1935 | if (app.connections.size() > 0) { |
| 1936 | for (ConnectionRecord cr : app.connections) { |
| 1937 | if (cr.binding != null && cr.binding.service != null |
| 1938 | && cr.binding.service.app != null |
| 1939 | && cr.binding.service.app.lruSeq != mLruSeq) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1940 | updateLruProcessInternalLocked(cr.binding.service.app, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1944 | for (int j=app.conProviders.size()-1; j>=0; j--) { |
| 1945 | ContentProviderRecord cpr = app.conProviders.get(j).provider; |
| 1946 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1947 | updateLruProcessInternalLocked(cpr.proc, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1948 | } |
| 1949 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1950 | } |
| 1951 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1952 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1953 | boolean oomAdj) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1954 | mLruSeq++; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1955 | updateLruProcessInternalLocked(app, 0); |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1956 | |
| 1957 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
| 1958 | if (oomAdj) { |
| 1959 | updateOomAdjLocked(); |
| 1960 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1961 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1962 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1963 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | String processName, int uid) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1965 | if (uid == Process.SYSTEM_UID) { |
Amith Yamasani | 0184ce9 | 2012-03-28 22:41:41 -0700 | [diff] [blame] | 1966 | // The system gets to run in any process. If there are multiple |
| 1967 | // processes with the same uid, just pick the first (this |
| 1968 | // should never happen). |
| 1969 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1970 | processName); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1971 | if (procs == null) return null; |
| 1972 | final int N = procs.size(); |
| 1973 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1974 | if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1975 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | } |
| 1977 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1978 | return proc; |
| 1979 | } |
| 1980 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1981 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1982 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1983 | try { |
| 1984 | if (pm.performDexOpt(packageName)) { |
| 1985 | mDidDexOpt = true; |
| 1986 | } |
| 1987 | } catch (RemoteException e) { |
| 1988 | } |
| 1989 | } |
| 1990 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1991 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1992 | int transit = mWindowManager.getPendingAppTransition(); |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1993 | return transit == AppTransition.TRANSIT_ACTIVITY_OPEN |
| 1994 | || transit == AppTransition.TRANSIT_TASK_OPEN |
| 1995 | || transit == AppTransition.TRANSIT_TASK_TO_FRONT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | } |
| 1997 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1998 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1999 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2000 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 2001 | boolean isolated) { |
| 2002 | ProcessRecord app; |
| 2003 | if (!isolated) { |
| 2004 | app = getProcessRecordLocked(processName, info.uid); |
| 2005 | } else { |
| 2006 | // If this is an isolated process, it can't re-use an existing process. |
| 2007 | app = null; |
| 2008 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2009 | // We don't have to do anything more if: |
| 2010 | // (1) There is an existing application record; and |
| 2011 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 2012 | // object attached to it so we know it couldn't have crashed; and |
| 2013 | // (3) There is a pid assigned to it, so it is either starting or |
| 2014 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2015 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 2017 | + " thread=" + (app != null ? app.thread : null) |
| 2018 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2019 | if (app != null && app.pid > 0) { |
| 2020 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 2021 | // We already have the app running, or are waiting for it to |
| 2022 | // 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] | 2023 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 2024 | // If this is a new package in the process, add the package to the list |
| 2025 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2026 | return app; |
| 2027 | } else { |
| 2028 | // An application record is attached to a previous process, |
| 2029 | // clean it up now. |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 2030 | if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2031 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2032 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2033 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2034 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2035 | String hostingNameStr = hostingName != null |
| 2036 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2037 | |
| 2038 | if (!isolated) { |
| 2039 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 2040 | // If we are in the background, then check to see if this process |
| 2041 | // is bad. If so, we will just silently fail. |
| 2042 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 2043 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 2044 | + "/" + info.processName); |
| 2045 | return null; |
| 2046 | } |
| 2047 | } else { |
| 2048 | // When the user is explicitly starting a process, then clear its |
| 2049 | // crash count so that we won't make it bad until they see at |
| 2050 | // least one crash dialog again, and make the process good again |
| 2051 | // if it had been bad. |
| 2052 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2053 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2054 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 2055 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2056 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, |
| 2057 | UserHandle.getUserId(info.uid), info.uid, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2058 | info.processName); |
| 2059 | mBadProcesses.remove(info.processName, info.uid); |
| 2060 | if (app != null) { |
| 2061 | app.bad = false; |
| 2062 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2066 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2067 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2068 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 2069 | if (app == null) { |
| 2070 | Slog.w(TAG, "Failed making new process record for " |
| 2071 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 2072 | return null; |
| 2073 | } |
| 2074 | mProcessNames.put(processName, app.uid, app); |
| 2075 | if (isolated) { |
| 2076 | mIsolatedProcesses.put(app.uid, app); |
| 2077 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2078 | } else { |
| 2079 | // If this is a new package in the process, add the package to the list |
| 2080 | app.addPackage(info.packageName); |
| 2081 | } |
| 2082 | |
| 2083 | // If the system is not ready yet, then hold off on starting this |
| 2084 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2085 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2086 | && !isAllowedWhileBooting(info) |
| 2087 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | if (!mProcessesOnHold.contains(app)) { |
| 2089 | mProcessesOnHold.add(app); |
| 2090 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2091 | 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] | 2092 | return app; |
| 2093 | } |
| 2094 | |
| 2095 | startProcessLocked(app, hostingType, hostingNameStr); |
| 2096 | return (app.pid != 0) ? app : null; |
| 2097 | } |
| 2098 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2099 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 2100 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 2101 | } |
| 2102 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2103 | private final void startProcessLocked(ProcessRecord app, |
| 2104 | String hostingType, String hostingNameStr) { |
| 2105 | if (app.pid > 0 && app.pid != MY_PID) { |
| 2106 | synchronized (mPidsSelfLocked) { |
| 2107 | mPidsSelfLocked.remove(app.pid); |
| 2108 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 2109 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2110 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2111 | } |
| 2112 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2113 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 2114 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2115 | mProcessesOnHold.remove(app); |
| 2116 | |
| 2117 | updateCpuStats(); |
| 2118 | |
| 2119 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 2120 | mProcDeaths[0] = 0; |
| 2121 | |
| 2122 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2123 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2125 | int[] gids = null; |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2126 | int mountExternal = Zygote.MOUNT_EXTERNAL_NONE; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2127 | if (!app.isolated) { |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2128 | int[] permGids = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2129 | try { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2130 | final PackageManager pm = mContext.getPackageManager(); |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2131 | permGids = pm.getPackageGids(app.info.packageName); |
Jeff Sharkey | e217ee4 | 2012-08-28 16:23:01 -0700 | [diff] [blame] | 2132 | |
| 2133 | if (Environment.isExternalStorageEmulated()) { |
| 2134 | if (pm.checkPermission( |
| 2135 | android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE, |
| 2136 | app.info.packageName) == PERMISSION_GRANTED) { |
| 2137 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL; |
| 2138 | } else { |
| 2139 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER; |
| 2140 | } |
| 2141 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2142 | } catch (PackageManager.NameNotFoundException e) { |
| 2143 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 2144 | } |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2145 | |
| 2146 | /* |
| 2147 | * Add shared application GID so applications can share some |
| 2148 | * resources like shared libraries |
| 2149 | */ |
| 2150 | if (permGids == null) { |
| 2151 | gids = new int[1]; |
| 2152 | } else { |
| 2153 | gids = new int[permGids.length + 1]; |
| 2154 | System.arraycopy(permGids, 0, gids, 1, permGids.length); |
| 2155 | } |
| 2156 | gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2157 | } |
| 2158 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 2159 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2160 | && mTopComponent != null |
| 2161 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 2162 | uid = 0; |
| 2163 | } |
| 2164 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 2165 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 2166 | uid = 0; |
| 2167 | } |
| 2168 | } |
| 2169 | int debugFlags = 0; |
| 2170 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 2171 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 2172 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 2173 | // awkward to turn on otherwise. |
| 2174 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2175 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 2176 | // Run the app in safe mode if its manifest requests so or the |
| 2177 | // system is booted in safe mode. |
| 2178 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 2179 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 2180 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 2181 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2182 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 2183 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 2184 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 2185 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 2186 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 2187 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2188 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 2189 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 2190 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2191 | |
| 2192 | // Start the process. It will either succeed and return a result containing |
| 2193 | // the PID of the new process, or else throw a RuntimeException. |
| 2194 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2195 | app.processName, uid, uid, gids, debugFlags, mountExternal, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 2196 | app.info.targetSdkVersion, null, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2198 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2199 | synchronized (bs) { |
| 2200 | if (bs.isOnBattery()) { |
| 2201 | app.batteryStats.incStartsLocked(); |
| 2202 | } |
| 2203 | } |
| 2204 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2205 | EventLog.writeEvent(EventLogTags.AM_PROC_START, |
| 2206 | UserHandle.getUserId(uid), startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2207 | app.processName, hostingType, |
| 2208 | hostingNameStr != null ? hostingNameStr : ""); |
| 2209 | |
| 2210 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2211 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2212 | } |
| 2213 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2214 | StringBuilder buf = mStringBuilder; |
| 2215 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2216 | buf.append("Start proc "); |
| 2217 | buf.append(app.processName); |
| 2218 | buf.append(" for "); |
| 2219 | buf.append(hostingType); |
| 2220 | if (hostingNameStr != null) { |
| 2221 | buf.append(" "); |
| 2222 | buf.append(hostingNameStr); |
| 2223 | } |
| 2224 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2225 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2226 | buf.append(" uid="); |
| 2227 | buf.append(uid); |
| 2228 | buf.append(" gids={"); |
| 2229 | if (gids != null) { |
| 2230 | for (int gi=0; gi<gids.length; gi++) { |
| 2231 | if (gi != 0) buf.append(", "); |
| 2232 | buf.append(gids[gi]); |
| 2233 | |
| 2234 | } |
| 2235 | } |
| 2236 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2237 | Slog.i(TAG, buf.toString()); |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2238 | app.setPid(startResult.pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2239 | app.usingWrapper = startResult.usingWrapper; |
| 2240 | app.removed = false; |
| 2241 | synchronized (mPidsSelfLocked) { |
| 2242 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2243 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2244 | msg.obj = app; |
| 2245 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2246 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2247 | } |
| 2248 | } catch (RuntimeException e) { |
| 2249 | // XXX do better error recovery. |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2250 | app.setPid(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2251 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2255 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2256 | if (resumed) { |
| 2257 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2258 | } else { |
| 2259 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2260 | } |
| 2261 | } |
| 2262 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2263 | boolean startHomeActivityLocked(int userId) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2264 | if (mHeadless) { |
| 2265 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2266 | // when running headless. |
| 2267 | ensureBootCompleted(); |
| 2268 | return false; |
| 2269 | } |
| 2270 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2271 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2272 | && mTopAction == null) { |
| 2273 | // We are running in factory test mode, but unable to find |
| 2274 | // the factory test app, so just sit around displaying the |
| 2275 | // error message and don't try to start anything. |
| 2276 | return false; |
| 2277 | } |
| 2278 | Intent intent = new Intent( |
| 2279 | mTopAction, |
| 2280 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2281 | intent.setComponent(mTopComponent); |
| 2282 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2283 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2284 | } |
| 2285 | ActivityInfo aInfo = |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2286 | resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2287 | if (aInfo != null) { |
| 2288 | intent.setComponent(new ComponentName( |
| 2289 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2290 | // Don't do this if the home app is currently being |
| 2291 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2292 | aInfo = new ActivityInfo(aInfo); |
| 2293 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2294 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2295 | aInfo.applicationInfo.uid); |
| 2296 | if (app == null || app.instrumentationClass == null) { |
| 2297 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2298 | mMainStack.startActivityLocked(null, intent, null, aInfo, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2299 | null, null, 0, 0, 0, null, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2300 | } |
| 2301 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2302 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2303 | return true; |
| 2304 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2305 | |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2306 | private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) { |
| 2307 | ActivityInfo ai = null; |
| 2308 | ComponentName comp = intent.getComponent(); |
| 2309 | try { |
| 2310 | if (comp != null) { |
| 2311 | ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId); |
| 2312 | } else { |
| 2313 | ResolveInfo info = AppGlobals.getPackageManager().resolveIntent( |
| 2314 | intent, |
| 2315 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
| 2316 | flags, userId); |
| 2317 | |
| 2318 | if (info != null) { |
| 2319 | ai = info.activityInfo; |
| 2320 | } |
| 2321 | } |
| 2322 | } catch (RemoteException e) { |
| 2323 | // ignore |
| 2324 | } |
| 2325 | |
| 2326 | return ai; |
| 2327 | } |
| 2328 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2329 | /** |
| 2330 | * Starts the "new version setup screen" if appropriate. |
| 2331 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2332 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2333 | // Only do this once per boot. |
| 2334 | if (mCheckedForSetup) { |
| 2335 | return; |
| 2336 | } |
| 2337 | |
| 2338 | // We will show this screen if the current one is a different |
| 2339 | // version than the last one shown, and we are not running in |
| 2340 | // low-level factory test mode. |
| 2341 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2342 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2343 | Settings.Global.getInt(resolver, |
| 2344 | Settings.Global.DEVICE_PROVISIONED, 0) != 0) { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2345 | mCheckedForSetup = true; |
| 2346 | |
| 2347 | // See if we should be showing the platform update setup UI. |
| 2348 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2349 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2350 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2351 | |
| 2352 | // We don't allow third party apps to replace this. |
| 2353 | ResolveInfo ri = null; |
| 2354 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2355 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2356 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2357 | ri = ris.get(i); |
| 2358 | break; |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | if (ri != null) { |
| 2363 | String vers = ri.activityInfo.metaData != null |
| 2364 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2365 | : null; |
| 2366 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2367 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2368 | Intent.METADATA_SETUP_VERSION); |
| 2369 | } |
| 2370 | String lastVers = Settings.Secure.getString( |
| 2371 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2372 | if (vers != null && !vers.equals(lastVers)) { |
| 2373 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2374 | intent.setComponent(new ComponentName( |
| 2375 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2376 | mMainStack.startActivityLocked(null, intent, null, ri.activityInfo, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2377 | null, null, 0, 0, 0, null, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2378 | } |
| 2379 | } |
| 2380 | } |
| 2381 | } |
| 2382 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2383 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2384 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2385 | } |
| 2386 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2387 | void enforceNotIsolatedCaller(String caller) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2388 | if (UserHandle.isIsolated(Binder.getCallingUid())) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2389 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2390 | } |
| 2391 | } |
| 2392 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2393 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2394 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2395 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2396 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2397 | } |
| 2398 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2399 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2400 | public void setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2401 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2402 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2403 | synchronized (this) { |
| 2404 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2405 | } |
| 2406 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2407 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2408 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2409 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2410 | synchronized (this) { |
| 2411 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2412 | } |
| 2413 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2414 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2415 | public void setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2416 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2417 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2418 | synchronized (this) { |
| 2419 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2423 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2424 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2425 | synchronized (this) { |
| 2426 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2431 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2432 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2433 | synchronized (this) { |
| 2434 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2435 | } |
| 2436 | } |
| 2437 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2438 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2439 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2440 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2441 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2442 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2443 | private void dispatchProcessesChanged() { |
| 2444 | int N; |
| 2445 | synchronized (this) { |
| 2446 | N = mPendingProcessChanges.size(); |
| 2447 | if (mActiveProcessChanges.length < N) { |
| 2448 | mActiveProcessChanges = new ProcessChangeItem[N]; |
| 2449 | } |
| 2450 | mPendingProcessChanges.toArray(mActiveProcessChanges); |
| 2451 | mAvailProcessChanges.addAll(mPendingProcessChanges); |
| 2452 | mPendingProcessChanges.clear(); |
| 2453 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes"); |
| 2454 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2455 | int i = mProcessObservers.beginBroadcast(); |
| 2456 | while (i > 0) { |
| 2457 | i--; |
| 2458 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2459 | if (observer != null) { |
| 2460 | try { |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2461 | for (int j=0; j<N; j++) { |
| 2462 | ProcessChangeItem item = mActiveProcessChanges[j]; |
| 2463 | if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) { |
| 2464 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid=" |
| 2465 | + item.pid + " uid=" + item.uid + ": " |
| 2466 | + item.foregroundActivities); |
| 2467 | observer.onForegroundActivitiesChanged(item.pid, item.uid, |
| 2468 | item.foregroundActivities); |
| 2469 | } |
| 2470 | if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) { |
| 2471 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid=" |
| 2472 | + item.pid + " uid=" + item.uid + ": " + item.importance); |
| 2473 | observer.onImportanceChanged(item.pid, item.uid, |
| 2474 | item.importance); |
| 2475 | } |
| 2476 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2477 | } catch (RemoteException e) { |
| 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | mProcessObservers.finishBroadcast(); |
| 2482 | } |
| 2483 | |
| 2484 | private void dispatchProcessDied(int pid, int uid) { |
| 2485 | int i = mProcessObservers.beginBroadcast(); |
| 2486 | while (i > 0) { |
| 2487 | i--; |
| 2488 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2489 | if (observer != null) { |
| 2490 | try { |
| 2491 | observer.onProcessDied(pid, uid); |
| 2492 | } catch (RemoteException e) { |
| 2493 | } |
| 2494 | } |
| 2495 | } |
| 2496 | mProcessObservers.finishBroadcast(); |
| 2497 | } |
| 2498 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2499 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2500 | final int N = mPendingActivityLaunches.size(); |
| 2501 | if (N <= 0) { |
| 2502 | return; |
| 2503 | } |
| 2504 | for (int i=0; i<N; i++) { |
| 2505 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2506 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2507 | pal.startFlags, doResume && i == (N-1), null); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2508 | } |
| 2509 | mPendingActivityLaunches.clear(); |
| 2510 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2511 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2512 | public final int startActivity(IApplicationThread caller, String callingPackage, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2513 | Intent intent, String resolvedType, IBinder resultTo, |
| 2514 | String resultWho, int requestCode, int startFlags, |
| 2515 | String profileFile, ParcelFileDescriptor profileFd, Bundle options) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2516 | return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo, |
| 2517 | resultWho, requestCode, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2518 | startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId()); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2521 | public final int startActivityAsUser(IApplicationThread caller, String callingPackage, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2522 | Intent intent, String resolvedType, IBinder resultTo, |
| 2523 | String resultWho, int requestCode, int startFlags, |
| 2524 | String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2525 | enforceNotIsolatedCaller("startActivity"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2526 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | d888399 | 2012-09-07 15:58:52 -0700 | [diff] [blame] | 2527 | false, true, "startActivity", null); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2528 | return mMainStack.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2529 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2530 | null, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2531 | } |
| 2532 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2533 | public final WaitResult startActivityAndWait(IApplicationThread caller, String callingPackage, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2534 | Intent intent, String resolvedType, IBinder resultTo, |
| 2535 | String resultWho, int requestCode, int startFlags, String profileFile, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2536 | ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2537 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2538 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2539 | false, true, "startActivityAndWait", null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2540 | WaitResult res = new WaitResult(); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2541 | mMainStack.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2542 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2543 | res, null, options, UserHandle.getCallingUserId()); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2544 | return res; |
| 2545 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 2546 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2547 | public final int startActivityWithConfig(IApplicationThread caller, String callingPackage, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2548 | Intent intent, String resolvedType, IBinder resultTo, |
| 2549 | String resultWho, int requestCode, int startFlags, Configuration config, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2550 | Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2551 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2552 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2553 | false, true, "startActivityWithConfig", null); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2554 | int ret = mMainStack.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2555 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2556 | null, null, null, config, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2557 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2560 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2561 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2562 | IBinder resultTo, String resultWho, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2563 | int flagsMask, int flagsValues, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2564 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2565 | // Refuse possible leaked file descriptors |
| 2566 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2567 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2568 | } |
| 2569 | |
| 2570 | IIntentSender sender = intent.getTarget(); |
| 2571 | if (!(sender instanceof PendingIntentRecord)) { |
| 2572 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2573 | } |
| 2574 | |
| 2575 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2576 | |
| 2577 | synchronized (this) { |
| 2578 | // If this is coming from the currently resumed activity, it is |
| 2579 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2580 | if (mMainStack.mResumedActivity != null |
| 2581 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2582 | Binder.getCallingUid()) { |
| 2583 | mAppSwitchesAllowedTime = 0; |
| 2584 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2585 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2586 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null, |
| 2587 | resultTo, resultWho, requestCode, flagsMask, flagsValues, options); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2588 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2591 | public boolean startNextMatchingActivity(IBinder callingActivity, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2592 | Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2593 | // Refuse possible leaked file descriptors |
| 2594 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2595 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2596 | } |
| 2597 | |
| 2598 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2599 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2600 | if (r == null) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2601 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | return false; |
| 2603 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | if (r.app == null || r.app.thread == null) { |
| 2605 | // The caller is not running... d'oh! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2606 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2607 | return false; |
| 2608 | } |
| 2609 | intent = new Intent(intent); |
| 2610 | // The caller is not allowed to change the data. |
| 2611 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2612 | // And we are resetting to find the next component... |
| 2613 | intent.setComponent(null); |
| 2614 | |
| 2615 | ActivityInfo aInfo = null; |
| 2616 | try { |
| 2617 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2618 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2619 | intent, r.resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2620 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2621 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2622 | |
| 2623 | // Look for the original activity in the list... |
| 2624 | final int N = resolves != null ? resolves.size() : 0; |
| 2625 | for (int i=0; i<N; i++) { |
| 2626 | ResolveInfo rInfo = resolves.get(i); |
| 2627 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2628 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2629 | // We found the current one... the next matching is |
| 2630 | // after it. |
| 2631 | i++; |
| 2632 | if (i<N) { |
| 2633 | aInfo = resolves.get(i).activityInfo; |
| 2634 | } |
| 2635 | break; |
| 2636 | } |
| 2637 | } |
| 2638 | } catch (RemoteException e) { |
| 2639 | } |
| 2640 | |
| 2641 | if (aInfo == null) { |
| 2642 | // Nobody who is next! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2643 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2644 | return false; |
| 2645 | } |
| 2646 | |
| 2647 | intent.setComponent(new ComponentName( |
| 2648 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2649 | intent.setFlags(intent.getFlags()&~( |
| 2650 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2651 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2652 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2653 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2654 | |
| 2655 | // Okay now we need to start the new activity, replacing the |
| 2656 | // currently running activity. This is a little tricky because |
| 2657 | // we want to start the new one as if the current one is finished, |
| 2658 | // but not finish the current one first so that there is no flicker. |
| 2659 | // And thus... |
| 2660 | final boolean wasFinishing = r.finishing; |
| 2661 | r.finishing = true; |
| 2662 | |
| 2663 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2664 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2665 | final String resultWho = r.resultWho; |
| 2666 | final int requestCode = r.requestCode; |
| 2667 | r.resultTo = null; |
| 2668 | if (resultTo != null) { |
| 2669 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2670 | } |
| 2671 | |
| 2672 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2673 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2674 | r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2675 | resultWho, requestCode, -1, r.launchedFromUid, r.launchedFromPackage, 0, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2676 | options, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2677 | Binder.restoreCallingIdentity(origId); |
| 2678 | |
| 2679 | r.finishing = wasFinishing; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2680 | if (res != ActivityManager.START_SUCCESS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2681 | return false; |
| 2682 | } |
| 2683 | return true; |
| 2684 | } |
| 2685 | } |
| 2686 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2687 | final int startActivityInPackage(int uid, String callingPackage, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2688 | Intent intent, String resolvedType, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2689 | String resultWho, int requestCode, int startFlags, Bundle options, int userId) { |
| 2690 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2691 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2692 | false, true, "startActivityInPackage", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2693 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2694 | int ret = mMainStack.startActivityMayWait(null, uid, callingPackage, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2695 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2696 | null, null, null, null, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2697 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2698 | } |
| 2699 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2700 | public final int startActivities(IApplicationThread caller, String callingPackage, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2701 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options, |
| 2702 | int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2703 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 16979ea | 2012-09-24 18:14:47 -0700 | [diff] [blame] | 2704 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2705 | false, true, "startActivity", null); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2706 | int ret = mMainStack.startActivities(caller, -1, callingPackage, intents, |
| 2707 | resolvedTypes, resultTo, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2708 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2709 | } |
| 2710 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2711 | final int startActivitiesInPackage(int uid, String callingPackage, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2712 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2713 | Bundle options, int userId) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2714 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2715 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2716 | false, true, "startActivityInPackage", null); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2717 | int ret = mMainStack.startActivities(null, uid, callingPackage, intents, resolvedTypes, |
| 2718 | resultTo, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2719 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | } |
| 2721 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2722 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2723 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2724 | // Quick case: check if the top-most recent task is the same. |
| 2725 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2726 | return; |
| 2727 | } |
| 2728 | // 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] | 2729 | for (int i=0; i<N; i++) { |
| 2730 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2731 | if (task.userId == tr.userId |
| 2732 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2733 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2734 | mRecentTasks.remove(i); |
| 2735 | i--; |
| 2736 | N--; |
| 2737 | if (task.intent == null) { |
| 2738 | // If the new recent task we are adding is not fully |
| 2739 | // specified, then replace it with the existing recent task. |
| 2740 | task = tr; |
| 2741 | } |
| 2742 | } |
| 2743 | } |
| 2744 | if (N >= MAX_RECENT_TASKS) { |
| 2745 | mRecentTasks.remove(N-1); |
| 2746 | } |
| 2747 | mRecentTasks.add(0, task); |
| 2748 | } |
| 2749 | |
| 2750 | public void setRequestedOrientation(IBinder token, |
| 2751 | int requestedOrientation) { |
| 2752 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2753 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2754 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2755 | return; |
| 2756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2757 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2758 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2759 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2760 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2761 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2762 | if (config != null) { |
| 2763 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2764 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2765 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2766 | } |
| 2767 | } |
| 2768 | Binder.restoreCallingIdentity(origId); |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | public int getRequestedOrientation(IBinder token) { |
| 2773 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2774 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2775 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2776 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2777 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2778 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2779 | } |
| 2780 | } |
| 2781 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2782 | /** |
| 2783 | * This is the internal entry point for handling Activity.finish(). |
| 2784 | * |
| 2785 | * @param token The Binder token referencing the Activity we want to finish. |
| 2786 | * @param resultCode Result code, if any, from this Activity. |
| 2787 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2788 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2789 | * @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] | 2790 | */ |
| 2791 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2792 | // Refuse possible leaked file descriptors |
| 2793 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2794 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2795 | } |
| 2796 | |
| 2797 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2798 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2799 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2800 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2801 | if (next != null) { |
| 2802 | // ask watcher if this is allowed |
| 2803 | boolean resumeOK = true; |
| 2804 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2805 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2806 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2807 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | if (!resumeOK) { |
| 2811 | return false; |
| 2812 | } |
| 2813 | } |
| 2814 | } |
| 2815 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2816 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2817 | resultData, "app-request", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2818 | Binder.restoreCallingIdentity(origId); |
| 2819 | return res; |
| 2820 | } |
| 2821 | } |
| 2822 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2823 | public final void finishHeavyWeightApp() { |
| 2824 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2825 | != PackageManager.PERMISSION_GRANTED) { |
| 2826 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2827 | + Binder.getCallingPid() |
| 2828 | + ", uid=" + Binder.getCallingUid() |
| 2829 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2830 | Slog.w(TAG, msg); |
| 2831 | throw new SecurityException(msg); |
| 2832 | } |
| 2833 | |
| 2834 | synchronized(this) { |
| 2835 | if (mHeavyWeightProcess == null) { |
| 2836 | return; |
| 2837 | } |
| 2838 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2839 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2840 | mHeavyWeightProcess.activities); |
| 2841 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2842 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2843 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2844 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2845 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2846 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2847 | null, "finish-heavy", true); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | } |
| 2851 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2852 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 2853 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2854 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2858 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2859 | String message) { |
| 2860 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2861 | != PackageManager.PERMISSION_GRANTED) { |
| 2862 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2863 | + Binder.getCallingPid() |
| 2864 | + ", uid=" + Binder.getCallingUid() |
| 2865 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2866 | Slog.w(TAG, msg); |
| 2867 | throw new SecurityException(msg); |
| 2868 | } |
| 2869 | |
| 2870 | synchronized(this) { |
| 2871 | ProcessRecord proc = null; |
| 2872 | |
| 2873 | // Figure out which process to kill. We don't trust that initialPid |
| 2874 | // still has any relation to current pids, so must scan through the |
| 2875 | // list. |
| 2876 | synchronized (mPidsSelfLocked) { |
| 2877 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2878 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2879 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2880 | continue; |
| 2881 | } |
| 2882 | if (p.pid == initialPid) { |
| 2883 | proc = p; |
| 2884 | break; |
| 2885 | } |
| 2886 | for (String str : p.pkgList) { |
| 2887 | if (str.equals(packageName)) { |
| 2888 | proc = p; |
| 2889 | } |
| 2890 | } |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2895 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2896 | + " initialPid=" + initialPid |
| 2897 | + " packageName=" + packageName); |
| 2898 | return; |
| 2899 | } |
| 2900 | |
| 2901 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2902 | if (proc.pid == Process.myPid()) { |
| 2903 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2904 | return; |
| 2905 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2906 | long ident = Binder.clearCallingIdentity(); |
| 2907 | try { |
| 2908 | proc.thread.scheduleCrash(message); |
| 2909 | } catch (RemoteException e) { |
| 2910 | } |
| 2911 | Binder.restoreCallingIdentity(ident); |
| 2912 | } |
| 2913 | } |
| 2914 | } |
| 2915 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2916 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2917 | int requestCode) { |
| 2918 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2919 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2920 | mMainStack.finishSubActivityLocked(token, resultWho, requestCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2921 | Binder.restoreCallingIdentity(origId); |
| 2922 | } |
| 2923 | } |
| 2924 | |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2925 | public boolean finishActivityAffinity(IBinder token) { |
| 2926 | synchronized(this) { |
| 2927 | final long origId = Binder.clearCallingIdentity(); |
| 2928 | boolean res = mMainStack.finishActivityAffinityLocked(token); |
| 2929 | Binder.restoreCallingIdentity(origId); |
| 2930 | return res; |
| 2931 | } |
| 2932 | } |
| 2933 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2934 | public boolean willActivityBeVisible(IBinder token) { |
| 2935 | synchronized(this) { |
| 2936 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2937 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2938 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2939 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2940 | return true; |
| 2941 | } |
| 2942 | if (r.fullscreen && !r.finishing) { |
| 2943 | return false; |
| 2944 | } |
| 2945 | } |
| 2946 | return true; |
| 2947 | } |
| 2948 | } |
| 2949 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2950 | public void overridePendingTransition(IBinder token, String packageName, |
| 2951 | int enterAnim, int exitAnim) { |
| 2952 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2953 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2954 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2955 | return; |
| 2956 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2957 | |
| 2958 | final long origId = Binder.clearCallingIdentity(); |
| 2959 | |
| 2960 | if (self.state == ActivityState.RESUMED |
| 2961 | || self.state == ActivityState.PAUSING) { |
| 2962 | mWindowManager.overridePendingAppTransition(packageName, |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2963 | enterAnim, exitAnim, null); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | Binder.restoreCallingIdentity(origId); |
| 2967 | } |
| 2968 | } |
| 2969 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2970 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2971 | * Main function for removing an existing process from the activity manager |
| 2972 | * as a result of that process going away. Clears out all connections |
| 2973 | * to the process. |
| 2974 | */ |
| 2975 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2976 | boolean restarting, boolean allowRestart) { |
| 2977 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2978 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2979 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2980 | } |
| 2981 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2982 | if (mProfileProc == app) { |
| 2983 | clearProfilerLocked(); |
| 2984 | } |
| 2985 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2986 | // Just in case... |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2987 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 2988 | if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG, |
| 2989 | "App died while pausing: " + mMainStack.mPausingActivity); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2990 | mMainStack.mPausingActivity = null; |
| 2991 | } |
| 2992 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2993 | mMainStack.mLastPausedActivity = null; |
| 2994 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2995 | |
| 2996 | // Remove this application's activities from active lists. |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 2997 | boolean hasVisibleActivities = mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2998 | |
| 2999 | app.activities.clear(); |
| 3000 | |
| 3001 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3002 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3003 | + " running instrumentation " + app.instrumentationClass); |
| 3004 | Bundle info = new Bundle(); |
| 3005 | info.putString("shortMsg", "Process crashed."); |
| 3006 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 3007 | } |
| 3008 | |
| 3009 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3010 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3011 | // If there was nothing to resume, and we are not already |
| 3012 | // restarting this process, but there is a visible activity that |
| 3013 | // is hosted by the process... then make sure all visible |
| 3014 | // activities are running, taking care of restarting this |
| 3015 | // process. |
| 3016 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3017 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3018 | } |
| 3019 | } |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 3024 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3025 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3026 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3027 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3028 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 3029 | return i; |
| 3030 | } |
| 3031 | } |
| 3032 | return -1; |
| 3033 | } |
| 3034 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3035 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3036 | IApplicationThread thread) { |
| 3037 | if (thread == null) { |
| 3038 | return null; |
| 3039 | } |
| 3040 | |
| 3041 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3042 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3043 | } |
| 3044 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3045 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3046 | IApplicationThread thread) { |
| 3047 | |
| 3048 | mProcDeaths[0]++; |
| 3049 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3050 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 3051 | synchronized (stats) { |
| 3052 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 3053 | } |
| 3054 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3055 | // Clean up already done if the process has been re-started. |
| 3056 | if (app.pid == pid && app.thread != null && |
| 3057 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 3058 | if (!app.killedBackground) { |
| 3059 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 3060 | + ") has died."); |
| 3061 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3062 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3063 | if (DEBUG_CLEANUP) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3064 | TAG, "Dying app: " + app + ", pid: " + pid |
| 3065 | + ", thread: " + thread.asBinder()); |
| 3066 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3067 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3068 | |
| 3069 | if (doLowMem) { |
| 3070 | // If there are no longer any background processes running, |
| 3071 | // and the app that died was not running instrumentation, |
| 3072 | // then tell everyone we are now low on memory. |
| 3073 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3074 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3075 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3076 | 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] | 3077 | haveBg = true; |
| 3078 | break; |
| 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3083 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3084 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3085 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3086 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 3087 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3088 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 3089 | // The low memory report is overriding any current |
| 3090 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 3091 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3092 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3093 | rec.lastRequestedGc = 0; |
| 3094 | } else { |
| 3095 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3096 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3097 | rec.reportLowMemory = true; |
| 3098 | rec.lastLowMemory = now; |
| 3099 | mProcessesToGc.remove(rec); |
| 3100 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3101 | } |
| 3102 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 3103 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3104 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3105 | } |
| 3106 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3107 | } else if (app.pid != pid) { |
| 3108 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3109 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3110 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3111 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3112 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3113 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3114 | + thread.asBinder()); |
| 3115 | } |
| 3116 | } |
| 3117 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3118 | /** |
| 3119 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3120 | * @param clearTraces causes the dump file to be erased prior to the new |
| 3121 | * traces being written, if true; when false, the new traces will be |
| 3122 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3123 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 3124 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3125 | * @param nativeProcs optional list of native process names to dump stack crawls |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3126 | * @return file containing stack traces, or null if no dump file is configured |
| 3127 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3128 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3129 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3130 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3131 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3132 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3133 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3134 | |
| 3135 | File tracesFile = new File(tracesPath); |
| 3136 | try { |
| 3137 | File tracesDir = tracesFile.getParentFile(); |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3138 | if (!tracesDir.exists()) { |
| 3139 | tracesFile.mkdirs(); |
| 3140 | if (!SELinux.restorecon(tracesDir)) { |
| 3141 | return null; |
| 3142 | } |
| 3143 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3144 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3145 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3146 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3147 | tracesFile.createNewFile(); |
| 3148 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3149 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3150 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3151 | return null; |
| 3152 | } |
| 3153 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3154 | dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3155 | return tracesFile; |
| 3156 | } |
| 3157 | |
| 3158 | private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3159 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3160 | // Use a FileObserver to detect when traces finish writing. |
| 3161 | // The order of traces is considered important to maintain for legibility. |
| 3162 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 3163 | public synchronized void onEvent(int event, String path) { notify(); } |
| 3164 | }; |
| 3165 | |
| 3166 | try { |
| 3167 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3168 | |
| 3169 | // First collect all of the stacks of the most important pids. |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3170 | if (firstPids != null) { |
| 3171 | try { |
| 3172 | int num = firstPids.size(); |
| 3173 | for (int i = 0; i < num; i++) { |
| 3174 | synchronized (observer) { |
| 3175 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3176 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3177 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3178 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3179 | } catch (InterruptedException e) { |
| 3180 | Log.wtf(TAG, e); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3181 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | // Next measure CPU usage. |
| 3185 | if (processStats != null) { |
| 3186 | processStats.init(); |
| 3187 | System.gc(); |
| 3188 | processStats.update(); |
| 3189 | try { |
| 3190 | synchronized (processStats) { |
| 3191 | processStats.wait(500); // measure over 1/2 second. |
| 3192 | } |
| 3193 | } catch (InterruptedException e) { |
| 3194 | } |
| 3195 | processStats.update(); |
| 3196 | |
| 3197 | // We'll take the stack crawls of just the top apps using CPU. |
| 3198 | final int N = processStats.countWorkingStats(); |
| 3199 | int numProcs = 0; |
| 3200 | for (int i=0; i<N && numProcs<5; i++) { |
| 3201 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3202 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3203 | numProcs++; |
| 3204 | try { |
| 3205 | synchronized (observer) { |
| 3206 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3207 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3208 | } |
| 3209 | } catch (InterruptedException e) { |
| 3210 | Log.wtf(TAG, e); |
| 3211 | } |
| 3212 | |
| 3213 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3214 | } |
| 3215 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3216 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3217 | } finally { |
| 3218 | observer.stopWatching(); |
| 3219 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3220 | |
| 3221 | if (nativeProcs != null) { |
| 3222 | int[] pids = Process.getPidsForCommands(nativeProcs); |
| 3223 | if (pids != null) { |
| 3224 | for (int pid : pids) { |
| 3225 | Debug.dumpNativeBacktraceToFile(pid, tracesPath); |
| 3226 | } |
| 3227 | } |
| 3228 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3229 | } |
| 3230 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3231 | final void logAppTooSlow(ProcessRecord app, long startTime, String msg) { |
Dianne Hackborn | 69dc66e | 2012-03-26 10:50:54 -0700 | [diff] [blame] | 3232 | if (true || IS_USER_BUILD) { |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3233 | return; |
| 3234 | } |
| 3235 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3236 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3237 | return; |
| 3238 | } |
| 3239 | |
| 3240 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 3241 | StrictMode.allowThreadDiskWrites(); |
| 3242 | try { |
| 3243 | final File tracesFile = new File(tracesPath); |
| 3244 | final File tracesDir = tracesFile.getParentFile(); |
| 3245 | final File tracesTmp = new File(tracesDir, "__tmp__"); |
| 3246 | try { |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3247 | if (!tracesDir.exists()) { |
| 3248 | tracesFile.mkdirs(); |
| 3249 | if (!SELinux.restorecon(tracesDir.getPath())) { |
| 3250 | return; |
| 3251 | } |
| 3252 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3253 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3254 | |
| 3255 | if (tracesFile.exists()) { |
| 3256 | tracesTmp.delete(); |
| 3257 | tracesFile.renameTo(tracesTmp); |
| 3258 | } |
| 3259 | StringBuilder sb = new StringBuilder(); |
| 3260 | Time tobj = new Time(); |
| 3261 | tobj.set(System.currentTimeMillis()); |
| 3262 | sb.append(tobj.format("%Y-%m-%d %H:%M:%S")); |
| 3263 | sb.append(": "); |
| 3264 | TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb); |
| 3265 | sb.append(" since "); |
| 3266 | sb.append(msg); |
| 3267 | FileOutputStream fos = new FileOutputStream(tracesFile); |
| 3268 | fos.write(sb.toString().getBytes()); |
| 3269 | if (app == null) { |
| 3270 | fos.write("\n*** No application process!".getBytes()); |
| 3271 | } |
| 3272 | fos.close(); |
| 3273 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3274 | } catch (IOException e) { |
| 3275 | Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e); |
| 3276 | return; |
| 3277 | } |
| 3278 | |
| 3279 | if (app != null) { |
| 3280 | ArrayList<Integer> firstPids = new ArrayList<Integer>(); |
| 3281 | firstPids.add(app.pid); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3282 | dumpStackTraces(tracesPath, firstPids, null, null, null); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | File lastTracesFile = null; |
| 3286 | File curTracesFile = null; |
| 3287 | for (int i=9; i>=0; i--) { |
| 3288 | String name = String.format("slow%02d.txt", i); |
| 3289 | curTracesFile = new File(tracesDir, name); |
| 3290 | if (curTracesFile.exists()) { |
| 3291 | if (lastTracesFile != null) { |
| 3292 | curTracesFile.renameTo(lastTracesFile); |
| 3293 | } else { |
| 3294 | curTracesFile.delete(); |
| 3295 | } |
| 3296 | } |
| 3297 | lastTracesFile = curTracesFile; |
| 3298 | } |
| 3299 | tracesFile.renameTo(curTracesFile); |
| 3300 | if (tracesTmp.exists()) { |
| 3301 | tracesTmp.renameTo(tracesFile); |
| 3302 | } |
| 3303 | } finally { |
| 3304 | StrictMode.setThreadPolicy(oldPolicy); |
| 3305 | } |
| 3306 | } |
| 3307 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3308 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 3309 | ActivityRecord parent, boolean aboveSystem, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3310 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3311 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3312 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3313 | if (mController != null) { |
| 3314 | try { |
| 3315 | // 0 == continue, -1 = kill process immediately |
| 3316 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3317 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3318 | } catch (RemoteException e) { |
| 3319 | mController = null; |
| 3320 | } |
| 3321 | } |
| 3322 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3323 | long anrTime = SystemClock.uptimeMillis(); |
| 3324 | if (MONITOR_CPU_USAGE) { |
| 3325 | updateCpuStatsNow(); |
| 3326 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3327 | |
| 3328 | synchronized (this) { |
| 3329 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3330 | if (mShuttingDown) { |
| 3331 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3332 | return; |
| 3333 | } else if (app.notResponding) { |
| 3334 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3335 | return; |
| 3336 | } else if (app.crashing) { |
| 3337 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3338 | return; |
| 3339 | } |
| 3340 | |
| 3341 | // In case we come through here for the same app before completing |
| 3342 | // this one, mark as anring now so we will bail out. |
| 3343 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3344 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3345 | // Log the ANR to the event log. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3346 | EventLog.writeEvent(EventLogTags.AM_ANR, app.userId, app.pid, |
| 3347 | app.processName, app.info.flags, annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3348 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3349 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3350 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3351 | |
| 3352 | int parentPid = app.pid; |
| 3353 | 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] | 3354 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3355 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3356 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3357 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3358 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3359 | ProcessRecord r = mLruProcesses.get(i); |
| 3360 | if (r != null && r.thread != null) { |
| 3361 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3362 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3363 | if (r.persistent) { |
| 3364 | firstPids.add(pid); |
| 3365 | } else { |
| 3366 | lastPids.put(pid, Boolean.TRUE); |
| 3367 | } |
| 3368 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3369 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3370 | } |
| 3371 | } |
| 3372 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3373 | // Log the ANR to the main log. |
Jeff Brown | deb6ed8 | 2012-04-10 14:26:26 -0700 | [diff] [blame] | 3374 | StringBuilder info = new StringBuilder(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3375 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3376 | info.append("ANR in ").append(app.processName); |
| 3377 | if (activity != null && activity.shortComponentName != null) { |
| 3378 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3379 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3380 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3381 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3382 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3383 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3384 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3385 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3386 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3387 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3388 | final ProcessStats processStats = new ProcessStats(true); |
| 3389 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3390 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3391 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3392 | String cpuInfo = null; |
| 3393 | if (MONITOR_CPU_USAGE) { |
| 3394 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3395 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3396 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3397 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3398 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3399 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3400 | } |
| 3401 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3402 | info.append(processStats.printCurrentState(anrTime)); |
| 3403 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3404 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3405 | if (tracesFile == null) { |
| 3406 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3407 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3408 | } |
| 3409 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3410 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3411 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3412 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3413 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3414 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3415 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3416 | 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] | 3417 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3418 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3419 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3420 | } |
| 3421 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3422 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3423 | } |
| 3424 | } |
| 3425 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3426 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3427 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3428 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3429 | |
| 3430 | synchronized (this) { |
| 3431 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3432 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3433 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3434 | app.processName, app.setAdj, "background ANR"); |
| 3435 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3436 | return; |
| 3437 | } |
| 3438 | |
| 3439 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3440 | makeAppNotRespondingLocked(app, |
| 3441 | activity != null ? activity.shortComponentName : null, |
| 3442 | annotation != null ? "ANR " + annotation : "ANR", |
| 3443 | info.toString()); |
| 3444 | |
| 3445 | // Bring up the infamous App Not Responding dialog |
| 3446 | Message msg = Message.obtain(); |
| 3447 | HashMap map = new HashMap(); |
| 3448 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3449 | msg.obj = map; |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 3450 | msg.arg1 = aboveSystem ? 1 : 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3451 | map.put("app", app); |
| 3452 | if (activity != null) { |
| 3453 | map.put("activity", activity); |
| 3454 | } |
| 3455 | |
| 3456 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3457 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3458 | } |
| 3459 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3460 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3461 | if (!mLaunchWarningShown) { |
| 3462 | mLaunchWarningShown = true; |
| 3463 | mHandler.post(new Runnable() { |
| 3464 | @Override |
| 3465 | public void run() { |
| 3466 | synchronized (ActivityManagerService.this) { |
| 3467 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3468 | d.show(); |
| 3469 | mHandler.postDelayed(new Runnable() { |
| 3470 | @Override |
| 3471 | public void run() { |
| 3472 | synchronized (ActivityManagerService.this) { |
| 3473 | d.dismiss(); |
| 3474 | mLaunchWarningShown = false; |
| 3475 | } |
| 3476 | } |
| 3477 | }, 4000); |
| 3478 | } |
| 3479 | } |
| 3480 | }); |
| 3481 | } |
| 3482 | } |
| 3483 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3484 | public boolean clearApplicationUserData(final String packageName, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3485 | final IPackageDataObserver observer, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3486 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3487 | int uid = Binder.getCallingUid(); |
| 3488 | int pid = Binder.getCallingPid(); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3489 | userId = handleIncomingUser(pid, uid, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3490 | userId, false, true, "clearApplicationUserData", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3491 | long callingId = Binder.clearCallingIdentity(); |
| 3492 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3493 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3494 | int pkgUid = -1; |
| 3495 | synchronized(this) { |
| 3496 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3497 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3498 | } catch (RemoteException e) { |
| 3499 | } |
| 3500 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3501 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3502 | return false; |
| 3503 | } |
| 3504 | if (uid == pkgUid || checkComponentPermission( |
| 3505 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3506 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3507 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3508 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3509 | } else { |
| 3510 | throw new SecurityException(pid+" does not have permission:"+ |
| 3511 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3512 | "for process:"+packageName); |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | try { |
| 3517 | //clear application user data |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3518 | pm.clearApplicationUserData(packageName, observer, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3519 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3520 | Uri.fromParts("package", packageName, null)); |
| 3521 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3522 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3523 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3524 | } catch (RemoteException e) { |
| 3525 | } |
| 3526 | } finally { |
| 3527 | Binder.restoreCallingIdentity(callingId); |
| 3528 | } |
| 3529 | return true; |
| 3530 | } |
| 3531 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3532 | public void killBackgroundProcesses(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3533 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3534 | != PackageManager.PERMISSION_GRANTED && |
| 3535 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3536 | != PackageManager.PERMISSION_GRANTED) { |
| 3537 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3538 | + Binder.getCallingPid() |
| 3539 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3540 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3541 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3542 | throw new SecurityException(msg); |
| 3543 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3544 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3545 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3546 | userId, true, true, "killBackgroundProcesses", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3547 | long callingId = Binder.clearCallingIdentity(); |
| 3548 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3549 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3551 | int appId = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3552 | try { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3553 | appId = UserHandle.getAppId(pm.getPackageUid(packageName, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3554 | } catch (RemoteException e) { |
| 3555 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3556 | if (appId == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3557 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3558 | return; |
| 3559 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3560 | killPackageProcessesLocked(packageName, appId, userId, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3561 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3562 | } |
| 3563 | } finally { |
| 3564 | Binder.restoreCallingIdentity(callingId); |
| 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | public void killAllBackgroundProcesses() { |
| 3569 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3570 | != PackageManager.PERMISSION_GRANTED) { |
| 3571 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3572 | + Binder.getCallingPid() |
| 3573 | + ", uid=" + Binder.getCallingUid() |
| 3574 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3575 | Slog.w(TAG, msg); |
| 3576 | throw new SecurityException(msg); |
| 3577 | } |
| 3578 | |
| 3579 | long callingId = Binder.clearCallingIdentity(); |
| 3580 | try { |
| 3581 | synchronized(this) { |
| 3582 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3583 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3584 | final int NA = apps.size(); |
| 3585 | for (int ia=0; ia<NA; ia++) { |
| 3586 | ProcessRecord app = apps.valueAt(ia); |
| 3587 | if (app.persistent) { |
| 3588 | // we don't kill persistent processes |
| 3589 | continue; |
| 3590 | } |
| 3591 | if (app.removed) { |
| 3592 | procs.add(app); |
| 3593 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3594 | app.removed = true; |
| 3595 | procs.add(app); |
| 3596 | } |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | int N = procs.size(); |
| 3601 | for (int i=0; i<N; i++) { |
| 3602 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3603 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3604 | } |
| 3605 | } finally { |
| 3606 | Binder.restoreCallingIdentity(callingId); |
| 3607 | } |
| 3608 | } |
| 3609 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3610 | public void forceStopPackage(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3611 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3612 | != PackageManager.PERMISSION_GRANTED) { |
| 3613 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3614 | + Binder.getCallingPid() |
| 3615 | + ", uid=" + Binder.getCallingUid() |
| 3616 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3617 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3618 | throw new SecurityException(msg); |
| 3619 | } |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3620 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3621 | userId, true, true, "forceStopPackage", null); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3622 | long callingId = Binder.clearCallingIdentity(); |
| 3623 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3624 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3625 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3626 | int[] users = userId == UserHandle.USER_ALL |
| 3627 | ? getUsersLocked() : new int[] { userId }; |
| 3628 | for (int user : users) { |
| 3629 | int pkgUid = -1; |
| 3630 | try { |
| 3631 | pkgUid = pm.getPackageUid(packageName, user); |
| 3632 | } catch (RemoteException e) { |
| 3633 | } |
| 3634 | if (pkgUid == -1) { |
| 3635 | Slog.w(TAG, "Invalid packageName: " + packageName); |
| 3636 | continue; |
| 3637 | } |
| 3638 | try { |
| 3639 | pm.setPackageStoppedState(packageName, true, user); |
| 3640 | } catch (RemoteException e) { |
| 3641 | } catch (IllegalArgumentException e) { |
| 3642 | Slog.w(TAG, "Failed trying to unstop package " |
| 3643 | + packageName + ": " + e); |
| 3644 | } |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 3645 | if (isUserRunningLocked(user, false)) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3646 | forceStopPackageLocked(packageName, pkgUid); |
| 3647 | } |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3648 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3649 | } |
| 3650 | } finally { |
| 3651 | Binder.restoreCallingIdentity(callingId); |
| 3652 | } |
| 3653 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3654 | |
| 3655 | /* |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3656 | * The pkg name and app id have to be specified. |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3657 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3658 | public void killApplicationWithAppId(String pkg, int appid) { |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3659 | if (pkg == null) { |
| 3660 | return; |
| 3661 | } |
| 3662 | // Make sure the uid is valid. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3663 | if (appid < 0) { |
| 3664 | Slog.w(TAG, "Invalid appid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3665 | return; |
| 3666 | } |
| 3667 | int callerUid = Binder.getCallingUid(); |
| 3668 | // Only the system server can kill an application |
| 3669 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3670 | // Post an aysnc message to kill the application |
| 3671 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3672 | msg.arg1 = appid; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3673 | msg.arg2 = 0; |
| 3674 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3675 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3676 | } else { |
| 3677 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3678 | pkg); |
| 3679 | } |
| 3680 | } |
| 3681 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3682 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3683 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3684 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3685 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3686 | final int uid = Binder.getCallingUid(); |
| 3687 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3688 | try { |
| 3689 | synchronized (this) { |
| 3690 | // Only allow this from foreground processes, so that background |
| 3691 | // applications can't abuse it to prevent system UI from being shown. |
| 3692 | if (uid >= Process.FIRST_APPLICATION_UID) { |
| 3693 | ProcessRecord proc; |
| 3694 | synchronized (mPidsSelfLocked) { |
| 3695 | proc = mPidsSelfLocked.get(pid); |
| 3696 | } |
| 3697 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 3698 | Slog.w(TAG, "Ignoring closeSystemDialogs " + reason |
| 3699 | + " from background process " + proc); |
| 3700 | return; |
| 3701 | } |
| 3702 | } |
| 3703 | closeSystemDialogsLocked(reason); |
| 3704 | } |
| 3705 | } finally { |
| 3706 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3707 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3708 | } |
| 3709 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3710 | void closeSystemDialogsLocked(String reason) { |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3711 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3712 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3713 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3714 | if (reason != null) { |
| 3715 | intent.putExtra("reason", reason); |
| 3716 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3717 | mWindowManager.closeSystemDialogs(reason); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3718 | |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3719 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3720 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 3721 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3722 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3723 | Activity.RESULT_CANCELED, null, "close-sys", true); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3724 | } |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3725 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3726 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3727 | broadcastIntentLocked(null, null, intent, null, |
| 3728 | null, 0, null, null, null, false, false, -1, |
| 3729 | Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3730 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3731 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3732 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3733 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3734 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3735 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3736 | for (int i=pids.length-1; i>=0; i--) { |
| 3737 | infos[i] = new Debug.MemoryInfo(); |
| 3738 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3739 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3740 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3741 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3742 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3743 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3744 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3745 | long[] pss = new long[pids.length]; |
| 3746 | for (int i=pids.length-1; i>=0; i--) { |
| 3747 | pss[i] = Debug.getPss(pids[i]); |
| 3748 | } |
| 3749 | return pss; |
| 3750 | } |
| 3751 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3752 | public void killApplicationProcess(String processName, int uid) { |
| 3753 | if (processName == null) { |
| 3754 | return; |
| 3755 | } |
| 3756 | |
| 3757 | int callerUid = Binder.getCallingUid(); |
| 3758 | // Only the system server can kill an application |
| 3759 | if (callerUid == Process.SYSTEM_UID) { |
| 3760 | synchronized (this) { |
| 3761 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3762 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3763 | try { |
| 3764 | app.thread.scheduleSuicide(); |
| 3765 | } catch (RemoteException e) { |
| 3766 | // If the other end already died, then our work here is done. |
| 3767 | } |
| 3768 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3769 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3770 | + processName + " / " + uid); |
| 3771 | } |
| 3772 | } |
| 3773 | } else { |
| 3774 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3775 | processName); |
| 3776 | } |
| 3777 | } |
| 3778 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3779 | private void forceStopPackageLocked(final String packageName, int uid) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3780 | forceStopPackageLocked(packageName, UserHandle.getAppId(uid), false, |
| 3781 | false, true, false, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3782 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3783 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3784 | if (!mProcessesReady) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3785 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3786 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3787 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3788 | intent.putExtra(Intent.EXTRA_UID, uid); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 3789 | intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3790 | broadcastIntentLocked(null, null, intent, |
| 3791 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3792 | false, false, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3793 | MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3794 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3795 | |
| 3796 | private void forceStopUserLocked(int userId) { |
| 3797 | forceStopPackageLocked(null, -1, false, false, true, false, userId); |
| 3798 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 3799 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3800 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3801 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 3802 | broadcastIntentLocked(null, null, intent, |
| 3803 | null, null, 0, null, null, null, |
| 3804 | false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 3805 | MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3806 | } |
| 3807 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3808 | private final boolean killPackageProcessesLocked(String packageName, int appId, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3809 | int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart, |
| 3810 | boolean doit, boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3811 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3812 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3813 | // Remove all processes this package may have touched: all with the |
| 3814 | // same UID (except for the system or root user), and all whose name |
| 3815 | // matches the package name. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3816 | final String procNamePrefix = packageName != null ? (packageName + ":") : null; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3817 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3818 | final int NA = apps.size(); |
| 3819 | for (int ia=0; ia<NA; ia++) { |
| 3820 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3821 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3822 | // we don't kill persistent processes |
| 3823 | continue; |
| 3824 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3825 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3826 | if (doit) { |
| 3827 | procs.add(app); |
| 3828 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3829 | continue; |
| 3830 | } |
| 3831 | |
| 3832 | // Skip process if it doesn't meet our oom adj requirement. |
| 3833 | if (app.setAdj < minOomAdj) { |
| 3834 | continue; |
| 3835 | } |
| 3836 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3837 | // If no package is specified, we call all processes under the |
| 3838 | // give user id. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3839 | if (packageName == null) { |
| 3840 | if (app.userId != userId) { |
| 3841 | continue; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3842 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3843 | // Package has been specified, we want to hit all processes |
| 3844 | // that match it. We need to qualify this by the processes |
| 3845 | // that are running under the specified app and user ID. |
| 3846 | } else { |
| 3847 | if (UserHandle.getAppId(app.uid) != appId) { |
| 3848 | continue; |
| 3849 | } |
| 3850 | if (userId != UserHandle.USER_ALL && app.userId != userId) { |
| 3851 | continue; |
| 3852 | } |
| 3853 | if (!app.pkgList.contains(packageName)) { |
| 3854 | continue; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3855 | } |
| 3856 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3857 | |
| 3858 | // Process has passed all conditions, kill it! |
| 3859 | if (!doit) { |
| 3860 | return true; |
| 3861 | } |
| 3862 | app.removed = true; |
| 3863 | procs.add(app); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | int N = procs.size(); |
| 3868 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3869 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3870 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3871 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3872 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3873 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3874 | private final boolean forceStopPackageLocked(String name, int appId, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3875 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3876 | boolean evenPersistent, int userId) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3877 | int i; |
| 3878 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3879 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3880 | if (userId == UserHandle.USER_ALL && name == null) { |
| 3881 | Slog.w(TAG, "Can't force stop all processes of all users, that is insane!"); |
| 3882 | } |
| 3883 | |
| 3884 | if (appId < 0 && name != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3885 | try { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3886 | appId = UserHandle.getAppId( |
| 3887 | AppGlobals.getPackageManager().getPackageUid(name, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3888 | } catch (RemoteException e) { |
| 3889 | } |
| 3890 | } |
| 3891 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3892 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3893 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3894 | Slog.i(TAG, "Force stopping package " + name + " appid=" + appId |
| 3895 | + " user=" + userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3896 | } else { |
| 3897 | Slog.i(TAG, "Force stopping user " + userId); |
| 3898 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3899 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3900 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3901 | while (badApps.hasNext()) { |
| 3902 | SparseArray<Long> ba = badApps.next(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3903 | for (i=ba.size()-1; i>=0; i--) { |
| 3904 | boolean remove = false; |
| 3905 | final int entUid = ba.keyAt(i); |
| 3906 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3907 | if (userId == UserHandle.USER_ALL) { |
| 3908 | if (UserHandle.getAppId(entUid) == appId) { |
| 3909 | remove = true; |
| 3910 | } |
| 3911 | } else { |
| 3912 | if (entUid == UserHandle.getUid(userId, appId)) { |
| 3913 | remove = true; |
| 3914 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3915 | } |
| 3916 | } else if (UserHandle.getUserId(entUid) == userId) { |
| 3917 | remove = true; |
| 3918 | } |
| 3919 | if (remove) { |
| 3920 | ba.removeAt(i); |
| 3921 | } |
| 3922 | } |
| 3923 | if (ba.size() == 0) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3924 | badApps.remove(); |
| 3925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3926 | } |
| 3927 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3928 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3929 | boolean didSomething = killPackageProcessesLocked(name, appId, userId, |
Dianne Hackborn | e62fa82 | 2013-01-09 18:31:37 -0800 | [diff] [blame] | 3930 | -100, callerWillRestart, true, doit, evenPersistent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3931 | name == null ? ("force stop user " + userId) : ("force stop " + name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3932 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3933 | TaskRecord lastTask = null; |
| 3934 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3935 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3936 | final boolean samePackage = r.packageName.equals(name) |
| 3937 | || (name == null && r.userId == userId); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3938 | if ((userId == UserHandle.USER_ALL || r.userId == userId) |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3939 | && (samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3940 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3941 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3942 | if (r.finishing) { |
| 3943 | // If this activity is just finishing, then it is not |
| 3944 | // interesting as far as something to stop. |
| 3945 | continue; |
| 3946 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3947 | return true; |
| 3948 | } |
| 3949 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3950 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3951 | if (samePackage) { |
| 3952 | if (r.app != null) { |
| 3953 | r.app.removed = true; |
| 3954 | } |
| 3955 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3956 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3957 | lastTask = r.task; |
| 3958 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3959 | null, "force-stop", true)) { |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3960 | i--; |
| 3961 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3962 | } |
| 3963 | } |
| 3964 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3965 | if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) { |
| 3966 | if (!doit) { |
| 3967 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3968 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3969 | didSomething = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3970 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3971 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3972 | if (name == null) { |
| 3973 | // Remove all sticky broadcasts from this user. |
| 3974 | mStickyBroadcasts.remove(userId); |
| 3975 | } |
| 3976 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3977 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3978 | if (mProviderMap.collectForceStopProviders(name, appId, doit, evenPersistent, |
| 3979 | userId, providers)) { |
| 3980 | if (!doit) { |
| 3981 | return true; |
| 3982 | } |
| 3983 | didSomething = true; |
| 3984 | } |
| 3985 | N = providers.size(); |
| 3986 | for (i=0; i<N; i++) { |
| 3987 | removeDyingProviderLocked(null, providers.get(i), true); |
| 3988 | } |
| 3989 | |
Dianne Hackborn | 861a3b2 | 2012-10-12 15:25:20 -0700 | [diff] [blame] | 3990 | if (name == null) { |
| 3991 | // Remove pending intents. For now we only do this when force |
| 3992 | // stopping users, because we have some problems when doing this |
| 3993 | // for packages -- app widgets are not currently cleaned up for |
| 3994 | // such packages, so they can be left with bad pending intents. |
| 3995 | if (mIntentSenderRecords.size() > 0) { |
| 3996 | Iterator<WeakReference<PendingIntentRecord>> it |
| 3997 | = mIntentSenderRecords.values().iterator(); |
| 3998 | while (it.hasNext()) { |
| 3999 | WeakReference<PendingIntentRecord> wpir = it.next(); |
| 4000 | if (wpir == null) { |
| 4001 | it.remove(); |
| 4002 | continue; |
| 4003 | } |
| 4004 | PendingIntentRecord pir = wpir.get(); |
| 4005 | if (pir == null) { |
| 4006 | it.remove(); |
| 4007 | continue; |
| 4008 | } |
| 4009 | if (name == null) { |
| 4010 | // Stopping user, remove all objects for the user. |
| 4011 | if (pir.key.userId != userId) { |
| 4012 | // Not the same user, skip it. |
| 4013 | continue; |
| 4014 | } |
| 4015 | } else { |
| 4016 | if (UserHandle.getAppId(pir.uid) != appId) { |
| 4017 | // Different app id, skip it. |
| 4018 | continue; |
| 4019 | } |
| 4020 | if (userId != UserHandle.USER_ALL && pir.key.userId != userId) { |
| 4021 | // Different user, skip it. |
| 4022 | continue; |
| 4023 | } |
| 4024 | if (!pir.key.packageName.equals(name)) { |
| 4025 | // Different package, skip it. |
| 4026 | continue; |
| 4027 | } |
| 4028 | } |
| 4029 | if (!doit) { |
| 4030 | return true; |
| 4031 | } |
| 4032 | didSomething = true; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4033 | it.remove(); |
Dianne Hackborn | 861a3b2 | 2012-10-12 15:25:20 -0700 | [diff] [blame] | 4034 | pir.canceled = true; |
| 4035 | if (pir.key.activity != null) { |
| 4036 | pir.key.activity.pendingResults.remove(pir.ref); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4037 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4038 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 4039 | } |
| 4040 | } |
| 4041 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4042 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4043 | if (purgeCache && name != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4044 | AttributeCache ac = AttributeCache.instance(); |
| 4045 | if (ac != null) { |
| 4046 | ac.removePackage(name); |
| 4047 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4048 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 4049 | if (mBooted) { |
| 4050 | mMainStack.resumeTopActivityLocked(null); |
| 4051 | mMainStack.scheduleIdleLocked(); |
| 4052 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4053 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4054 | |
| 4055 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4056 | } |
| 4057 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4058 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4059 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4060 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4061 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4062 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4063 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4064 | + "/" + uid + ")"); |
| 4065 | |
| 4066 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4067 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4068 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4069 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4070 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4071 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4072 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4073 | boolean needRestart = false; |
| 4074 | if (app.pid > 0 && app.pid != MY_PID) { |
| 4075 | int pid = app.pid; |
| 4076 | synchronized (mPidsSelfLocked) { |
| 4077 | mPidsSelfLocked.remove(pid); |
| 4078 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4079 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4080 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4081 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 4082 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4083 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4084 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4085 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4086 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4087 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4088 | } else { |
| 4089 | needRestart = true; |
| 4090 | } |
| 4091 | } |
| 4092 | } else { |
| 4093 | mRemovedProcesses.add(app); |
| 4094 | } |
| 4095 | |
| 4096 | return needRestart; |
| 4097 | } |
| 4098 | |
| 4099 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 4100 | final int pid = app.pid; |
| 4101 | boolean gone = false; |
| 4102 | synchronized (mPidsSelfLocked) { |
| 4103 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 4104 | if (knownApp != null && knownApp.thread == null) { |
| 4105 | mPidsSelfLocked.remove(pid); |
| 4106 | gone = true; |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4111 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4112 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, app.userId, |
| 4113 | pid, app.uid, app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4114 | mProcessNames.remove(app.processName, app.uid); |
| 4115 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4116 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4117 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4118 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4119 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4120 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4121 | // Take care of any launching providers waiting for this process. |
| 4122 | checkAppInLaunchingProvidersLocked(app, true); |
| 4123 | // Take care of any services that are waiting for the process. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4124 | mServices.processStartTimedOutLocked(app); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4125 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4126 | app.processName, app.setAdj, "start timeout"); |
| 4127 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4128 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4129 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4130 | try { |
| 4131 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 4132 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 4133 | bm.agentDisconnected(app.info.packageName); |
| 4134 | } catch (RemoteException e) { |
| 4135 | // Can't happen; the backup manager is local |
| 4136 | } |
| 4137 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4138 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4139 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4140 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4141 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4142 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4143 | 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] | 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 4148 | int pid) { |
| 4149 | |
| 4150 | // Find the application record that is being attached... either via |
| 4151 | // the pid if we are running in multiple processes, or just pull the |
| 4152 | // next app record if we are emulating process with anonymous threads. |
| 4153 | ProcessRecord app; |
| 4154 | if (pid != MY_PID && pid >= 0) { |
| 4155 | synchronized (mPidsSelfLocked) { |
| 4156 | app = mPidsSelfLocked.get(pid); |
| 4157 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4158 | } else { |
| 4159 | app = null; |
| 4160 | } |
| 4161 | |
| 4162 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4163 | 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] | 4164 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4165 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4166 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4167 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4168 | } else { |
| 4169 | try { |
| 4170 | thread.scheduleExit(); |
| 4171 | } catch (Exception e) { |
| 4172 | // Ignore exceptions. |
| 4173 | } |
| 4174 | } |
| 4175 | return false; |
| 4176 | } |
| 4177 | |
| 4178 | // If this application record is still attached to a previous |
| 4179 | // process, clean it up now. |
| 4180 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4181 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | // Tell the process all about itself. |
| 4185 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4186 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4187 | TAG, "Binding process pid " + pid + " to record " + app); |
| 4188 | |
| 4189 | String processName = app.processName; |
| 4190 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4191 | AppDeathRecipient adr = new AppDeathRecipient( |
| 4192 | app, pid, thread); |
| 4193 | thread.asBinder().linkToDeath(adr, 0); |
| 4194 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4195 | } catch (RemoteException e) { |
| 4196 | app.resetPackageList(); |
| 4197 | startProcessLocked(app, "link fail", processName); |
| 4198 | return false; |
| 4199 | } |
| 4200 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4201 | EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.userId, app.pid, app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4202 | |
| 4203 | app.thread = thread; |
| 4204 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 4205 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 4206 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4207 | app.forcingToForeground = null; |
| 4208 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 4209 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4210 | app.debugging = false; |
| 4211 | |
| 4212 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4213 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4214 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4215 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4216 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4217 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4218 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4219 | } |
| 4220 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4221 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4222 | TAG, "New app record " + app |
| 4223 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 4224 | try { |
| 4225 | int testMode = IApplicationThread.DEBUG_OFF; |
| 4226 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 4227 | testMode = mWaitForDebugger |
| 4228 | ? IApplicationThread.DEBUG_WAIT |
| 4229 | : IApplicationThread.DEBUG_ON; |
| 4230 | app.debugging = true; |
| 4231 | if (mDebugTransient) { |
| 4232 | mDebugApp = mOrigDebugApp; |
| 4233 | mWaitForDebugger = mOrigWaitForDebugger; |
| 4234 | } |
| 4235 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4236 | String profileFile = app.instrumentationProfileFile; |
| 4237 | ParcelFileDescriptor profileFd = null; |
| 4238 | boolean profileAutoStop = false; |
| 4239 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 4240 | mProfileProc = app; |
| 4241 | profileFile = mProfileFile; |
| 4242 | profileFd = mProfileFd; |
| 4243 | profileAutoStop = mAutoStopProfiler; |
| 4244 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4245 | boolean enableOpenGlTrace = false; |
| 4246 | if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) { |
| 4247 | enableOpenGlTrace = true; |
| 4248 | mOpenGlTraceApp = null; |
| 4249 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4250 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4251 | // If the app is being launched for restore or full backup, set it up specially |
| 4252 | boolean isRestrictedBackupMode = false; |
| 4253 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 4254 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4255 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4256 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 4257 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4258 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 4259 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 4260 | ? app.instrumentationInfo.packageName |
| 4261 | : app.info.packageName); |
| 4262 | if (app.instrumentationClass != null) { |
| 4263 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 4264 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4265 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 4266 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4267 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 4268 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 4269 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4270 | if (profileFd != null) { |
| 4271 | profileFd = profileFd.dup(); |
| 4272 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4273 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4274 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 4275 | app.instrumentationArguments, app.instrumentationWatcher, |
| 4276 | app.instrumentationUiAutomationConnection, testMode, enableOpenGlTrace, |
| 4277 | isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 4278 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 4279 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4280 | updateLruProcessLocked(app, false); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 4281 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4282 | } catch (Exception e) { |
| 4283 | // todo: Yikes! What should we do? For now we will try to |
| 4284 | // start another process, but that could easily get us in |
| 4285 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4286 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4287 | |
| 4288 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4289 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4290 | startProcessLocked(app, "bind fail", processName); |
| 4291 | return false; |
| 4292 | } |
| 4293 | |
| 4294 | // Remove this record from the list of starting applications. |
| 4295 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4296 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 4297 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4298 | mProcessesOnHold.remove(app); |
| 4299 | |
| 4300 | boolean badApp = false; |
| 4301 | boolean didSomething = false; |
| 4302 | |
| 4303 | // 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] | 4304 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 4305 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4306 | 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] | 4307 | && processName.equals(hr.processName)) { |
| 4308 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 4309 | if (mHeadless) { |
| 4310 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 4311 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4312 | didSomething = true; |
| 4313 | } |
| 4314 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4315 | 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] | 4316 | + hr.intent.getComponent().flattenToShortString(), e); |
| 4317 | badApp = true; |
| 4318 | } |
| 4319 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4320 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4321 | } |
| 4322 | } |
| 4323 | |
| 4324 | // Find any services that should be running in this process... |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4325 | if (!badApp) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4326 | try { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4327 | didSomething |= mServices.attachApplicationLocked(app, processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4328 | } catch (Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4329 | badApp = true; |
| 4330 | } |
| 4331 | } |
| 4332 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4333 | // Check if a next-broadcast receiver is in this process... |
| 4334 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4335 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4336 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4337 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4338 | // 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] | 4339 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4340 | } |
| 4341 | } |
| 4342 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4343 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4344 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4345 | 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] | 4346 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4347 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4348 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 4349 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 4350 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4351 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4352 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4353 | e.printStackTrace(); |
| 4354 | } |
| 4355 | } |
| 4356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4357 | if (badApp) { |
| 4358 | // todo: Also need to kill application to deal with all |
| 4359 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4360 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4361 | return false; |
| 4362 | } |
| 4363 | |
| 4364 | if (!didSomething) { |
| 4365 | updateOomAdjLocked(); |
| 4366 | } |
| 4367 | |
| 4368 | return true; |
| 4369 | } |
| 4370 | |
| 4371 | public final void attachApplication(IApplicationThread thread) { |
| 4372 | synchronized (this) { |
| 4373 | int callingPid = Binder.getCallingPid(); |
| 4374 | final long origId = Binder.clearCallingIdentity(); |
| 4375 | attachApplicationLocked(thread, callingPid); |
| 4376 | Binder.restoreCallingIdentity(origId); |
| 4377 | } |
| 4378 | } |
| 4379 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4380 | 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] | 4381 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4382 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4383 | if (stopProfiling) { |
| 4384 | synchronized (this) { |
| 4385 | if (mProfileProc == r.app) { |
| 4386 | if (mProfileFd != null) { |
| 4387 | try { |
| 4388 | mProfileFd.close(); |
| 4389 | } catch (IOException e) { |
| 4390 | } |
| 4391 | clearProfilerLocked(); |
| 4392 | } |
| 4393 | } |
| 4394 | } |
| 4395 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4396 | Binder.restoreCallingIdentity(origId); |
| 4397 | } |
| 4398 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4399 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4400 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4401 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4402 | mWindowManager.enableScreenAfterBoot(); |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 4403 | |
| 4404 | synchronized (this) { |
| 4405 | updateEventDispatchingLocked(); |
| 4406 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4407 | } |
| 4408 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4409 | public void showBootMessage(final CharSequence msg, final boolean always) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4410 | enforceNotIsolatedCaller("showBootMessage"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4411 | mWindowManager.showBootMessage(msg, always); |
| 4412 | } |
| 4413 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4414 | public void dismissKeyguardOnNextActivity() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4415 | enforceNotIsolatedCaller("dismissKeyguardOnNextActivity"); |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4416 | final long token = Binder.clearCallingIdentity(); |
| 4417 | try { |
| 4418 | synchronized (this) { |
| 4419 | if (mLockScreenShown) { |
| 4420 | mLockScreenShown = false; |
| 4421 | comeOutOfSleepIfNeededLocked(); |
| 4422 | } |
| 4423 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
Dianne Hackborn | 1e88e98 | 2012-04-24 18:35:55 -0700 | [diff] [blame] | 4424 | } |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4425 | } finally { |
| 4426 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4427 | } |
| 4428 | } |
| 4429 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4430 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4431 | IntentFilter pkgFilter = new IntentFilter(); |
| 4432 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4433 | pkgFilter.addDataScheme("package"); |
| 4434 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4435 | @Override |
| 4436 | public void onReceive(Context context, Intent intent) { |
| 4437 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4438 | if (pkgs != null) { |
| 4439 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4440 | synchronized (ActivityManagerService.this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4441 | if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) { |
| 4442 | setResultCode(Activity.RESULT_OK); |
| 4443 | return; |
| 4444 | } |
| 4445 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4446 | } |
| 4447 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4448 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4449 | }, pkgFilter); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4450 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4451 | synchronized (this) { |
| 4452 | // Ensure that any processes we had put on hold are now started |
| 4453 | // up. |
| 4454 | final int NP = mProcessesOnHold.size(); |
| 4455 | if (NP > 0) { |
| 4456 | ArrayList<ProcessRecord> procs = |
| 4457 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4458 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4459 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4460 | + procs.get(ip)); |
| 4461 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4466 | // Start looking for apps that are abusing wake locks. |
| 4467 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4468 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4469 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4470 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4471 | SystemProperties.set("dev.bootcomplete", "1"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4472 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 4473 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 4474 | if (uss.mState == UserStartedState.STATE_BOOTING) { |
| 4475 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4476 | final int userId = mStartedUsers.keyAt(i); |
| 4477 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 4478 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 4479 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4480 | null, null, 0, null, null, |
| 4481 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4482 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4483 | } |
Amith Yamasani | 4860cfc | 2012-08-08 19:14:18 -0700 | [diff] [blame] | 4484 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4485 | } |
| 4486 | } |
| 4487 | } |
| 4488 | |
| 4489 | final void ensureBootCompleted() { |
| 4490 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4491 | boolean enableScreen; |
| 4492 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4493 | booting = mBooting; |
| 4494 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4495 | enableScreen = !mBooted; |
| 4496 | mBooted = true; |
| 4497 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4498 | |
| 4499 | if (booting) { |
| 4500 | finishBooting(); |
| 4501 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4502 | |
| 4503 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4504 | enableScreenAfterBoot(); |
| 4505 | } |
| 4506 | } |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 4507 | |
| 4508 | public final void activityResumed(IBinder token) { |
| 4509 | final long origId = Binder.clearCallingIdentity(); |
| 4510 | mMainStack.activityResumed(token); |
| 4511 | Binder.restoreCallingIdentity(origId); |
| 4512 | } |
| 4513 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4514 | public final void activityPaused(IBinder token) { |
| 4515 | final long origId = Binder.clearCallingIdentity(); |
| 4516 | mMainStack.activityPaused(token, false); |
| 4517 | Binder.restoreCallingIdentity(origId); |
| 4518 | } |
| 4519 | |
| 4520 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4521 | CharSequence description) { |
| 4522 | if (localLOGV) Slog.v( |
| 4523 | TAG, "Activity stopped: token=" + token); |
| 4524 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4525 | // Refuse possible leaked file descriptors |
| 4526 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4527 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4528 | } |
| 4529 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4530 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4531 | |
| 4532 | final long origId = Binder.clearCallingIdentity(); |
| 4533 | |
| 4534 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4535 | r = mMainStack.isInStackLocked(token); |
| 4536 | if (r != null) { |
| 4537 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4538 | } |
| 4539 | } |
| 4540 | |
| 4541 | if (r != null) { |
| 4542 | sendPendingThumbnail(r, null, null, null, false); |
| 4543 | } |
| 4544 | |
| 4545 | trimApplications(); |
| 4546 | |
| 4547 | Binder.restoreCallingIdentity(origId); |
| 4548 | } |
| 4549 | |
| 4550 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4551 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4552 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | public String getCallingPackage(IBinder token) { |
| 4556 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4557 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4558 | 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] | 4559 | } |
| 4560 | } |
| 4561 | |
| 4562 | public ComponentName getCallingActivity(IBinder token) { |
| 4563 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4564 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4565 | return r != null ? r.intent.getComponent() : null; |
| 4566 | } |
| 4567 | } |
| 4568 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4569 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4570 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4571 | if (r == null) { |
| 4572 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4573 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4574 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4578 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4579 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4580 | if (r == null) { |
| 4581 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4582 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4583 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | public String getPackageForToken(IBinder token) { |
| 4588 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4589 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4590 | if (r == null) { |
| 4591 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4592 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4593 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4594 | } |
| 4595 | } |
| 4596 | |
| 4597 | public IIntentSender getIntentSender(int type, |
| 4598 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4599 | int requestCode, Intent[] intents, String[] resolvedTypes, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4600 | int flags, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4601 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4602 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4603 | if (intents != null) { |
| 4604 | if (intents.length < 1) { |
| 4605 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4606 | } |
| 4607 | for (int i=0; i<intents.length; i++) { |
| 4608 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4609 | if (intent != null) { |
| 4610 | if (intent.hasFileDescriptors()) { |
| 4611 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4612 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4613 | if (type == ActivityManager.INTENT_SENDER_BROADCAST && |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4614 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4615 | throw new IllegalArgumentException( |
| 4616 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4617 | } |
| 4618 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4619 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4620 | } |
| 4621 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4622 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4623 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4624 | } |
| 4625 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4626 | if (options != null) { |
| 4627 | if (options.hasFileDescriptors()) { |
| 4628 | throw new IllegalArgumentException("File descriptors passed in options"); |
| 4629 | } |
| 4630 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4631 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4632 | synchronized(this) { |
| 4633 | int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4634 | int origUserId = userId; |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 4635 | userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId, |
Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 4636 | type == ActivityManager.INTENT_SENDER_BROADCAST, false, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4637 | "getIntentSender", null); |
| 4638 | if (origUserId == UserHandle.USER_CURRENT) { |
| 4639 | // We don't want to evaluate this until the pending intent is |
| 4640 | // actually executed. However, we do want to always do the |
| 4641 | // security checking for it above. |
| 4642 | userId = UserHandle.USER_CURRENT; |
| 4643 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4644 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4645 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4646 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4647 | .getPackageUid(packageName, UserHandle.getUserId(callingUid)); |
| 4648 | if (!UserHandle.isSameApp(callingUid, uid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4649 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4650 | + Binder.getCallingPid() |
| 4651 | + ", uid=" + Binder.getCallingUid() |
| 4652 | + ", (need uid=" + uid + ")" |
| 4653 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4654 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4655 | throw new SecurityException(msg); |
| 4656 | } |
| 4657 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4658 | |
| 4659 | return getIntentSenderLocked(type, packageName, callingUid, userId, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4660 | token, resultWho, requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4661 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4662 | } catch (RemoteException e) { |
| 4663 | throw new SecurityException(e); |
| 4664 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4665 | } |
| 4666 | } |
| 4667 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4668 | IIntentSender getIntentSenderLocked(int type, String packageName, |
| 4669 | int callingUid, int userId, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4670 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags, |
| 4671 | Bundle options) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4672 | if (DEBUG_MU) |
| 4673 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4674 | ActivityRecord activity = null; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4675 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4676 | activity = mMainStack.isInStackLocked(token); |
| 4677 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4678 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4679 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4680 | if (activity.finishing) { |
| 4681 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4682 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4686 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4687 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4688 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4689 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4690 | |
| 4691 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4692 | type, packageName, activity, resultWho, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4693 | requestCode, intents, resolvedTypes, flags, options, userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4694 | WeakReference<PendingIntentRecord> ref; |
| 4695 | ref = mIntentSenderRecords.get(key); |
| 4696 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4697 | if (rec != null) { |
| 4698 | if (!cancelCurrent) { |
| 4699 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4700 | if (rec.key.requestIntent != null) { |
Adam Powell | 501d4a5 | 2012-04-30 15:03:57 -0700 | [diff] [blame] | 4701 | rec.key.requestIntent.replaceExtras(intents != null ? |
| 4702 | intents[intents.length - 1] : null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4703 | } |
| 4704 | if (intents != null) { |
| 4705 | intents[intents.length-1] = rec.key.requestIntent; |
| 4706 | rec.key.allIntents = intents; |
| 4707 | rec.key.allResolvedTypes = resolvedTypes; |
| 4708 | } else { |
| 4709 | rec.key.allIntents = null; |
| 4710 | rec.key.allResolvedTypes = null; |
| 4711 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4712 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4713 | return rec; |
| 4714 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4715 | rec.canceled = true; |
| 4716 | mIntentSenderRecords.remove(key); |
| 4717 | } |
| 4718 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4719 | return rec; |
| 4720 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4721 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4722 | mIntentSenderRecords.put(key, rec.ref); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4723 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4724 | if (activity.pendingResults == null) { |
| 4725 | activity.pendingResults |
| 4726 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4727 | } |
| 4728 | activity.pendingResults.add(rec.ref); |
| 4729 | } |
| 4730 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4731 | } |
| 4732 | |
| 4733 | public void cancelIntentSender(IIntentSender sender) { |
| 4734 | if (!(sender instanceof PendingIntentRecord)) { |
| 4735 | return; |
| 4736 | } |
| 4737 | synchronized(this) { |
| 4738 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4739 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4740 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4741 | .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId()); |
| 4742 | if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4743 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4744 | + Binder.getCallingPid() |
| 4745 | + ", uid=" + Binder.getCallingUid() |
| 4746 | + " is not allowed to cancel packges " |
| 4747 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4748 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4749 | throw new SecurityException(msg); |
| 4750 | } |
| 4751 | } catch (RemoteException e) { |
| 4752 | throw new SecurityException(e); |
| 4753 | } |
| 4754 | cancelIntentSenderLocked(rec, true); |
| 4755 | } |
| 4756 | } |
| 4757 | |
| 4758 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4759 | rec.canceled = true; |
| 4760 | mIntentSenderRecords.remove(rec.key); |
| 4761 | if (cleanActivity && rec.key.activity != null) { |
| 4762 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4763 | } |
| 4764 | } |
| 4765 | |
| 4766 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4767 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4768 | return null; |
| 4769 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4770 | try { |
| 4771 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4772 | return res.key.packageName; |
| 4773 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4774 | } |
| 4775 | return null; |
| 4776 | } |
| 4777 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4778 | public int getUidForIntentSender(IIntentSender sender) { |
| 4779 | if (sender instanceof PendingIntentRecord) { |
| 4780 | try { |
| 4781 | PendingIntentRecord res = (PendingIntentRecord)sender; |
| 4782 | return res.uid; |
| 4783 | } catch (ClassCastException e) { |
| 4784 | } |
| 4785 | } |
| 4786 | return -1; |
| 4787 | } |
| 4788 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4789 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4790 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4791 | return false; |
| 4792 | } |
| 4793 | try { |
| 4794 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4795 | if (res.key.allIntents == null) { |
| 4796 | return false; |
| 4797 | } |
| 4798 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4799 | Intent intent = res.key.allIntents[i]; |
| 4800 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4801 | return false; |
| 4802 | } |
| 4803 | } |
| 4804 | return true; |
| 4805 | } catch (ClassCastException e) { |
| 4806 | } |
| 4807 | return false; |
| 4808 | } |
| 4809 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 4810 | public boolean isIntentSenderAnActivity(IIntentSender pendingResult) { |
| 4811 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4812 | return false; |
| 4813 | } |
| 4814 | try { |
| 4815 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4816 | if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) { |
| 4817 | return true; |
| 4818 | } |
| 4819 | return false; |
| 4820 | } catch (ClassCastException e) { |
| 4821 | } |
| 4822 | return false; |
| 4823 | } |
| 4824 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 4825 | public Intent getIntentForIntentSender(IIntentSender pendingResult) { |
| 4826 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4827 | return null; |
| 4828 | } |
| 4829 | try { |
| 4830 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4831 | return res.key.requestIntent != null ? new Intent(res.key.requestIntent) : null; |
| 4832 | } catch (ClassCastException e) { |
| 4833 | } |
| 4834 | return null; |
| 4835 | } |
| 4836 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4837 | public void setProcessLimit(int max) { |
| 4838 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4839 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4840 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4841 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4842 | mProcessLimitOverride = max; |
| 4843 | } |
| 4844 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4848 | synchronized (this) { |
| 4849 | return mProcessLimitOverride; |
| 4850 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | void foregroundTokenDied(ForegroundToken token) { |
| 4854 | synchronized (ActivityManagerService.this) { |
| 4855 | synchronized (mPidsSelfLocked) { |
| 4856 | ForegroundToken cur |
| 4857 | = mForegroundProcesses.get(token.pid); |
| 4858 | if (cur != token) { |
| 4859 | return; |
| 4860 | } |
| 4861 | mForegroundProcesses.remove(token.pid); |
| 4862 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4863 | if (pr == null) { |
| 4864 | return; |
| 4865 | } |
| 4866 | pr.forcingToForeground = null; |
| 4867 | pr.foregroundServices = false; |
| 4868 | } |
| 4869 | updateOomAdjLocked(); |
| 4870 | } |
| 4871 | } |
| 4872 | |
| 4873 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4874 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4875 | "setProcessForeground()"); |
| 4876 | synchronized(this) { |
| 4877 | boolean changed = false; |
| 4878 | |
| 4879 | synchronized (mPidsSelfLocked) { |
| 4880 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4881 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4882 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4883 | return; |
| 4884 | } |
| 4885 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4886 | if (oldToken != null) { |
| 4887 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4888 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4889 | if (pr != null) { |
| 4890 | pr.forcingToForeground = null; |
| 4891 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4892 | changed = true; |
| 4893 | } |
| 4894 | if (isForeground && token != null) { |
| 4895 | ForegroundToken newToken = new ForegroundToken() { |
| 4896 | public void binderDied() { |
| 4897 | foregroundTokenDied(this); |
| 4898 | } |
| 4899 | }; |
| 4900 | newToken.pid = pid; |
| 4901 | newToken.token = token; |
| 4902 | try { |
| 4903 | token.linkToDeath(newToken, 0); |
| 4904 | mForegroundProcesses.put(pid, newToken); |
| 4905 | pr.forcingToForeground = token; |
| 4906 | changed = true; |
| 4907 | } catch (RemoteException e) { |
| 4908 | // If the process died while doing this, we will later |
| 4909 | // do the cleanup with the process death link. |
| 4910 | } |
| 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | if (changed) { |
| 4915 | updateOomAdjLocked(); |
| 4916 | } |
| 4917 | } |
| 4918 | } |
| 4919 | |
| 4920 | // ========================================================= |
| 4921 | // PERMISSIONS |
| 4922 | // ========================================================= |
| 4923 | |
| 4924 | static class PermissionController extends IPermissionController.Stub { |
| 4925 | ActivityManagerService mActivityManagerService; |
| 4926 | PermissionController(ActivityManagerService activityManagerService) { |
| 4927 | mActivityManagerService = activityManagerService; |
| 4928 | } |
| 4929 | |
| 4930 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4931 | return mActivityManagerService.checkPermission(permission, pid, |
| 4932 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4933 | } |
| 4934 | } |
| 4935 | |
| 4936 | /** |
| 4937 | * This can be called with or without the global lock held. |
| 4938 | */ |
| 4939 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4940 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4941 | // We might be performing an operation on behalf of an indirect binder |
| 4942 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4943 | // client identity accordingly before proceeding. |
| 4944 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4945 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4946 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4947 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4948 | uid = tlsIdentity.uid; |
| 4949 | pid = tlsIdentity.pid; |
| 4950 | } |
| 4951 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4952 | if (pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4953 | return PackageManager.PERMISSION_GRANTED; |
| 4954 | } |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4955 | |
| 4956 | return ActivityManager.checkComponentPermission(permission, uid, |
| 4957 | owningUid, exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | /** |
| 4961 | * As the only public entry point for permissions checking, this method |
| 4962 | * can enforce the semantic that requesting a check on a null global |
| 4963 | * permission is automatically denied. (Internally a null permission |
| 4964 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4965 | * when only uid-based security is needed.) |
| 4966 | * |
| 4967 | * This can be called with or without the global lock held. |
| 4968 | */ |
| 4969 | public int checkPermission(String permission, int pid, int uid) { |
| 4970 | if (permission == null) { |
| 4971 | return PackageManager.PERMISSION_DENIED; |
| 4972 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4973 | return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | /** |
| 4977 | * Binder IPC calls go through the public entry point. |
| 4978 | * This can be called with or without the global lock held. |
| 4979 | */ |
| 4980 | int checkCallingPermission(String permission) { |
| 4981 | return checkPermission(permission, |
| 4982 | Binder.getCallingPid(), |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4983 | UserHandle.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | /** |
| 4987 | * This can be called with or without the global lock held. |
| 4988 | */ |
| 4989 | void enforceCallingPermission(String permission, String func) { |
| 4990 | if (checkCallingPermission(permission) |
| 4991 | == PackageManager.PERMISSION_GRANTED) { |
| 4992 | return; |
| 4993 | } |
| 4994 | |
| 4995 | String msg = "Permission Denial: " + func + " from pid=" |
| 4996 | + Binder.getCallingPid() |
| 4997 | + ", uid=" + Binder.getCallingUid() |
| 4998 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4999 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5000 | throw new SecurityException(msg); |
| 5001 | } |
| 5002 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5003 | /** |
| 5004 | * Determine if UID is holding permissions required to access {@link Uri} in |
| 5005 | * the given {@link ProviderInfo}. Final permission checking is always done |
| 5006 | * in {@link ContentProvider}. |
| 5007 | */ |
| 5008 | private final boolean checkHoldingPermissionsLocked( |
| 5009 | IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5010 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5011 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5012 | |
| 5013 | if (pi.applicationInfo.uid == uid) { |
| 5014 | return true; |
| 5015 | } else if (!pi.exported) { |
| 5016 | return false; |
| 5017 | } |
| 5018 | |
| 5019 | boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 5020 | 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] | 5021 | try { |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5022 | // check if target holds top-level <provider> permissions |
| 5023 | if (!readMet && pi.readPermission != null |
| 5024 | && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) { |
| 5025 | readMet = true; |
| 5026 | } |
| 5027 | if (!writeMet && pi.writePermission != null |
| 5028 | && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) { |
| 5029 | writeMet = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5030 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5031 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5032 | // track if unprotected read/write is allowed; any denied |
| 5033 | // <path-permission> below removes this ability |
| 5034 | boolean allowDefaultRead = pi.readPermission == null; |
| 5035 | boolean allowDefaultWrite = pi.writePermission == null; |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5036 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5037 | // check if target holds any <path-permission> that match uri |
| 5038 | final PathPermission[] pps = pi.pathPermissions; |
| 5039 | if (pps != null) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5040 | final String path = uri.getPath(); |
| 5041 | int i = pps.length; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5042 | while (i > 0 && (!readMet || !writeMet)) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5043 | i--; |
| 5044 | PathPermission pp = pps[i]; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5045 | if (pp.match(path)) { |
| 5046 | if (!readMet) { |
| 5047 | final String pprperm = pp.getReadPermission(); |
| 5048 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 5049 | + pprperm + " for " + pp.getPath() |
| 5050 | + ": match=" + pp.match(path) |
| 5051 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 5052 | if (pprperm != null) { |
| 5053 | if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) { |
| 5054 | readMet = true; |
| 5055 | } else { |
| 5056 | allowDefaultRead = false; |
| 5057 | } |
| 5058 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5059 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5060 | if (!writeMet) { |
| 5061 | final String ppwperm = pp.getWritePermission(); |
| 5062 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 5063 | + ppwperm + " for " + pp.getPath() |
| 5064 | + ": match=" + pp.match(path) |
| 5065 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 5066 | if (ppwperm != null) { |
| 5067 | if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) { |
| 5068 | writeMet = true; |
| 5069 | } else { |
| 5070 | allowDefaultWrite = false; |
| 5071 | } |
| 5072 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5073 | } |
| 5074 | } |
| 5075 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5076 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5077 | |
| 5078 | // grant unprotected <provider> read/write, if not blocked by |
| 5079 | // <path-permission> above |
| 5080 | if (allowDefaultRead) readMet = true; |
| 5081 | if (allowDefaultWrite) writeMet = true; |
| 5082 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5083 | } catch (RemoteException e) { |
| 5084 | return false; |
| 5085 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5086 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5087 | return readMet && writeMet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 5091 | int modeFlags) { |
| 5092 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5093 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5094 | return true; |
| 5095 | } |
| 5096 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 5097 | if (perms == null) return false; |
| 5098 | UriPermission perm = perms.get(uri); |
| 5099 | if (perm == null) return false; |
| 5100 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 5101 | } |
| 5102 | |
| 5103 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5104 | enforceNotIsolatedCaller("checkUriPermission"); |
| 5105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5106 | // Another redirected-binder-call permissions check as in |
| 5107 | // {@link checkComponentPermission}. |
| 5108 | Identity tlsIdentity = sCallerIdentity.get(); |
| 5109 | if (tlsIdentity != null) { |
| 5110 | uid = tlsIdentity.uid; |
| 5111 | pid = tlsIdentity.pid; |
| 5112 | } |
| 5113 | |
| 5114 | // Our own process gets to do everything. |
| 5115 | if (pid == MY_PID) { |
| 5116 | return PackageManager.PERMISSION_GRANTED; |
| 5117 | } |
| 5118 | synchronized(this) { |
| 5119 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 5120 | ? PackageManager.PERMISSION_GRANTED |
| 5121 | : PackageManager.PERMISSION_DENIED; |
| 5122 | } |
| 5123 | } |
| 5124 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5125 | /** |
| 5126 | * Check if the targetPkg can be granted permission to access uri by |
| 5127 | * the callingUid using the given modeFlags. Throws a security exception |
| 5128 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 5129 | * if the URI permission grant should be performed; returns -1 if it is not |
| 5130 | * needed (for example targetPkg already has permission to access the URI). |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5131 | * If you already know the uid of the target, you can supply it in |
| 5132 | * lastTargetUid else set that to -1. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5133 | */ |
| 5134 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5135 | Uri uri, int modeFlags, int lastTargetUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5136 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5137 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5138 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5139 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5140 | } |
| 5141 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5142 | if (targetPkg != null) { |
| 5143 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5144 | "Checking grant " + targetPkg + " permission to " + uri); |
| 5145 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5146 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5147 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5148 | |
| 5149 | // If this is not a content: uri, we can't do anything with it. |
| 5150 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5151 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5152 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5153 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | String name = uri.getAuthority(); |
| 5157 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5158 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5159 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5160 | if (cpr != null) { |
| 5161 | pi = cpr.info; |
| 5162 | } else { |
| 5163 | try { |
| 5164 | pi = pm.resolveContentProvider(name, |
Dianne Hackborn | 11f0cb7 | 2012-09-16 17:12:34 -0700 | [diff] [blame] | 5165 | PackageManager.GET_URI_PERMISSION_PATTERNS, |
| 5166 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5167 | } catch (RemoteException ex) { |
| 5168 | } |
| 5169 | } |
| 5170 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5171 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5172 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5173 | } |
| 5174 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5175 | int targetUid = lastTargetUid; |
| 5176 | if (targetUid < 0 && targetPkg != null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5177 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5178 | targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5179 | if (targetUid < 0) { |
| 5180 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5181 | "Can't grant URI permission no uid for: " + targetPkg); |
| 5182 | return -1; |
| 5183 | } |
| 5184 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5185 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5186 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5187 | } |
| 5188 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5189 | if (targetUid >= 0) { |
| 5190 | // First... does the target actually need this permission? |
| 5191 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 5192 | // No need to grant the target this permission. |
| 5193 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5194 | "Target " + targetPkg + " already has full permission to " + uri); |
| 5195 | return -1; |
| 5196 | } |
| 5197 | } else { |
| 5198 | // First... there is no target package, so can anyone access it? |
| 5199 | boolean allowed = pi.exported; |
| 5200 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5201 | if (pi.readPermission != null) { |
| 5202 | allowed = false; |
| 5203 | } |
| 5204 | } |
| 5205 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5206 | if (pi.writePermission != null) { |
| 5207 | allowed = false; |
| 5208 | } |
| 5209 | } |
| 5210 | if (allowed) { |
| 5211 | return -1; |
| 5212 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5213 | } |
| 5214 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5215 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5216 | if (!pi.grantUriPermissions) { |
| 5217 | throw new SecurityException("Provider " + pi.packageName |
| 5218 | + "/" + pi.name |
| 5219 | + " does not allow granting of Uri permissions (uri " |
| 5220 | + uri + ")"); |
| 5221 | } |
| 5222 | if (pi.uriPermissionPatterns != null) { |
| 5223 | final int N = pi.uriPermissionPatterns.length; |
| 5224 | boolean allowed = false; |
| 5225 | for (int i=0; i<N; i++) { |
| 5226 | if (pi.uriPermissionPatterns[i] != null |
| 5227 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 5228 | allowed = true; |
| 5229 | break; |
| 5230 | } |
| 5231 | } |
| 5232 | if (!allowed) { |
| 5233 | throw new SecurityException("Provider " + pi.packageName |
| 5234 | + "/" + pi.name |
| 5235 | + " does not allow granting of permission to path of Uri " |
| 5236 | + uri); |
| 5237 | } |
| 5238 | } |
| 5239 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5240 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5241 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5242 | if (callingUid != Process.myUid()) { |
| 5243 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 5244 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5245 | throw new SecurityException("Uid " + callingUid |
| 5246 | + " does not have permission to uri " + uri); |
| 5247 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5248 | } |
| 5249 | } |
| 5250 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5251 | return targetUid; |
| 5252 | } |
| 5253 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5254 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 5255 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5256 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5257 | synchronized(this) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5258 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5259 | } |
| 5260 | } |
| 5261 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5262 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 5263 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 5264 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5265 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5266 | if (modeFlags == 0) { |
| 5267 | return; |
| 5268 | } |
| 5269 | |
| 5270 | // 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] | 5271 | // to the uri, and the target doesn't. Let's now give this to |
| 5272 | // the target. |
| 5273 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5274 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5275 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5277 | HashMap<Uri, UriPermission> targetUris |
| 5278 | = mGrantedUriPermissions.get(targetUid); |
| 5279 | if (targetUris == null) { |
| 5280 | targetUris = new HashMap<Uri, UriPermission>(); |
| 5281 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 5282 | } |
| 5283 | |
| 5284 | UriPermission perm = targetUris.get(uri); |
| 5285 | if (perm == null) { |
| 5286 | perm = new UriPermission(targetUid, uri); |
| 5287 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5288 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5290 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5291 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5292 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 5293 | } else { |
| 5294 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5295 | perm.readOwners.add(owner); |
| 5296 | owner.addReadPermission(perm); |
| 5297 | } |
| 5298 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5299 | perm.writeOwners.add(owner); |
| 5300 | owner.addWritePermission(perm); |
| 5301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5302 | } |
| 5303 | } |
| 5304 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5305 | void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri, |
| 5306 | int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5307 | if (targetPkg == null) { |
| 5308 | throw new NullPointerException("targetPkg"); |
| 5309 | } |
| 5310 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5311 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5312 | if (targetUid < 0) { |
| 5313 | return; |
| 5314 | } |
| 5315 | |
| 5316 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 5317 | } |
| 5318 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5319 | static class NeededUriGrants extends ArrayList<Uri> { |
| 5320 | final String targetPkg; |
| 5321 | final int targetUid; |
| 5322 | final int flags; |
| 5323 | |
| 5324 | NeededUriGrants(String _targetPkg, int _targetUid, int _flags) { |
| 5325 | targetPkg = _targetPkg; |
| 5326 | targetUid = _targetUid; |
| 5327 | flags = _flags; |
| 5328 | } |
| 5329 | } |
| 5330 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5331 | /** |
| 5332 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 5333 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5334 | NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 5335 | String targetPkg, Intent intent, int mode, NeededUriGrants needed) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5336 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5337 | "Checking URI perm to data=" + (intent != null ? intent.getData() : null) |
| 5338 | + " clip=" + (intent != null ? intent.getClipData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5339 | + " from " + intent + "; flags=0x" |
| 5340 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 5341 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5342 | if (targetPkg == null) { |
| 5343 | throw new NullPointerException("targetPkg"); |
| 5344 | } |
| 5345 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5346 | if (intent == null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5347 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5348 | } |
| 5349 | Uri data = intent.getData(); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5350 | ClipData clip = intent.getClipData(); |
| 5351 | if (data == null && clip == null) { |
| 5352 | return null; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5353 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5354 | if (data != null) { |
| 5355 | int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 5356 | mode, needed != null ? needed.targetUid : -1); |
| 5357 | if (target > 0) { |
| 5358 | if (needed == null) { |
| 5359 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5360 | } |
| 5361 | needed.add(data); |
| 5362 | } |
| 5363 | } |
| 5364 | if (clip != null) { |
| 5365 | for (int i=0; i<clip.getItemCount(); i++) { |
| 5366 | Uri uri = clip.getItemAt(i).getUri(); |
| 5367 | if (uri != null) { |
| 5368 | int target = -1; |
| 5369 | target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, |
| 5370 | mode, needed != null ? needed.targetUid : -1); |
| 5371 | if (target > 0) { |
| 5372 | if (needed == null) { |
| 5373 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5374 | } |
| 5375 | needed.add(uri); |
| 5376 | } |
| 5377 | } else { |
| 5378 | Intent clipIntent = clip.getItemAt(i).getIntent(); |
| 5379 | if (clipIntent != null) { |
| 5380 | NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked( |
| 5381 | callingUid, targetPkg, clipIntent, mode, needed); |
| 5382 | if (newNeeded != null) { |
| 5383 | needed = newNeeded; |
| 5384 | } |
| 5385 | } |
| 5386 | } |
| 5387 | } |
| 5388 | } |
| 5389 | |
| 5390 | return needed; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5391 | } |
| 5392 | |
| 5393 | /** |
| 5394 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 5395 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5396 | void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed, |
| 5397 | UriPermissionOwner owner) { |
| 5398 | if (needed != null) { |
| 5399 | for (int i=0; i<needed.size(); i++) { |
| 5400 | grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg, |
| 5401 | needed.get(i), needed.flags, owner); |
| 5402 | } |
| 5403 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 5407 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5408 | NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, |
Dianne Hackborn | d9781fe | 2012-03-08 18:04:18 -0800 | [diff] [blame] | 5409 | intent, intent != null ? intent.getFlags() : 0, null); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5410 | if (needed == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5411 | return; |
| 5412 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5413 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5414 | grantUriPermissionUncheckedFromIntentLocked(needed, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 5418 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5419 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5420 | synchronized(this) { |
| 5421 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5422 | if (r == null) { |
| 5423 | throw new SecurityException("Unable to find app for caller " |
| 5424 | + caller |
| 5425 | + " when granting permission to uri " + uri); |
| 5426 | } |
| 5427 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5428 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5429 | } |
| 5430 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5431 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5432 | } |
| 5433 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5434 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5435 | null); |
| 5436 | } |
| 5437 | } |
| 5438 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5439 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5440 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5441 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 5442 | HashMap<Uri, UriPermission> perms |
| 5443 | = mGrantedUriPermissions.get(perm.uid); |
| 5444 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5445 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5446 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5447 | perms.remove(perm.uri); |
| 5448 | if (perms.size() == 0) { |
| 5449 | mGrantedUriPermissions.remove(perm.uid); |
| 5450 | } |
| 5451 | } |
| 5452 | } |
| 5453 | } |
| 5454 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5455 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 5456 | int modeFlags) { |
| 5457 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5458 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5459 | if (modeFlags == 0) { |
| 5460 | return; |
| 5461 | } |
| 5462 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5463 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5464 | "Revoking all granted permissions to " + uri); |
| 5465 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5466 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5467 | |
| 5468 | final String authority = uri.getAuthority(); |
| 5469 | ProviderInfo pi = null; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5470 | int userId = UserHandle.getUserId(callingUid); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5471 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5472 | if (cpr != null) { |
| 5473 | pi = cpr.info; |
| 5474 | } else { |
| 5475 | try { |
| 5476 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5477 | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5478 | } catch (RemoteException ex) { |
| 5479 | } |
| 5480 | } |
| 5481 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5482 | 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] | 5483 | return; |
| 5484 | } |
| 5485 | |
| 5486 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5487 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5488 | // Right now, if you are not the original owner of the permission, |
| 5489 | // you are not allowed to revoke it. |
| 5490 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5491 | throw new SecurityException("Uid " + callingUid |
| 5492 | + " does not have permission to uri " + uri); |
| 5493 | //} |
| 5494 | } |
| 5495 | |
| 5496 | // Go through all of the permissions and remove any that match. |
| 5497 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 5498 | if (SEGMENTS != null) { |
| 5499 | final int NS = SEGMENTS.size(); |
| 5500 | int N = mGrantedUriPermissions.size(); |
| 5501 | for (int i=0; i<N; i++) { |
| 5502 | HashMap<Uri, UriPermission> perms |
| 5503 | = mGrantedUriPermissions.valueAt(i); |
| 5504 | Iterator<UriPermission> it = perms.values().iterator(); |
| 5505 | toploop: |
| 5506 | while (it.hasNext()) { |
| 5507 | UriPermission perm = it.next(); |
| 5508 | Uri targetUri = perm.uri; |
| 5509 | if (!authority.equals(targetUri.getAuthority())) { |
| 5510 | continue; |
| 5511 | } |
| 5512 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5513 | if (targetSegments == null) { |
| 5514 | continue; |
| 5515 | } |
| 5516 | if (targetSegments.size() < NS) { |
| 5517 | continue; |
| 5518 | } |
| 5519 | for (int j=0; j<NS; j++) { |
| 5520 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5521 | continue toploop; |
| 5522 | } |
| 5523 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5524 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5525 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5526 | perm.clearModes(modeFlags); |
| 5527 | if (perm.modeFlags == 0) { |
| 5528 | it.remove(); |
| 5529 | } |
| 5530 | } |
| 5531 | if (perms.size() == 0) { |
| 5532 | mGrantedUriPermissions.remove( |
| 5533 | mGrantedUriPermissions.keyAt(i)); |
| 5534 | N--; |
| 5535 | i--; |
| 5536 | } |
| 5537 | } |
| 5538 | } |
| 5539 | } |
| 5540 | |
| 5541 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5542 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5543 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5544 | synchronized(this) { |
| 5545 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5546 | if (r == null) { |
| 5547 | throw new SecurityException("Unable to find app for caller " |
| 5548 | + caller |
| 5549 | + " when revoking permission to uri " + uri); |
| 5550 | } |
| 5551 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5552 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5553 | return; |
| 5554 | } |
| 5555 | |
| 5556 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5557 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5558 | if (modeFlags == 0) { |
| 5559 | return; |
| 5560 | } |
| 5561 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5562 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5563 | |
| 5564 | final String authority = uri.getAuthority(); |
| 5565 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5566 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5567 | if (cpr != null) { |
| 5568 | pi = cpr.info; |
| 5569 | } else { |
| 5570 | try { |
| 5571 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5572 | PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5573 | } catch (RemoteException ex) { |
| 5574 | } |
| 5575 | } |
| 5576 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5577 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5578 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5579 | return; |
| 5580 | } |
| 5581 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5582 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5583 | } |
| 5584 | } |
| 5585 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5586 | @Override |
| 5587 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5588 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5589 | synchronized(this) { |
| 5590 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5591 | return owner.getExternalTokenLocked(); |
| 5592 | } |
| 5593 | } |
| 5594 | |
| 5595 | @Override |
| 5596 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5597 | Uri uri, int modeFlags) { |
| 5598 | synchronized(this) { |
| 5599 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5600 | if (owner == null) { |
| 5601 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5602 | } |
| 5603 | if (fromUid != Binder.getCallingUid()) { |
| 5604 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5605 | // Only system code can grant URI permissions on behalf |
| 5606 | // of other users. |
| 5607 | throw new SecurityException("nice try"); |
| 5608 | } |
| 5609 | } |
| 5610 | if (targetPkg == null) { |
| 5611 | throw new IllegalArgumentException("null target"); |
| 5612 | } |
| 5613 | if (uri == null) { |
| 5614 | throw new IllegalArgumentException("null uri"); |
| 5615 | } |
| 5616 | |
| 5617 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5618 | } |
| 5619 | } |
| 5620 | |
| 5621 | @Override |
| 5622 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5623 | synchronized(this) { |
| 5624 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5625 | if (owner == null) { |
| 5626 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5627 | } |
| 5628 | |
| 5629 | if (uri == null) { |
| 5630 | owner.removeUriPermissionsLocked(mode); |
| 5631 | } else { |
| 5632 | owner.removeUriPermissionLocked(uri, mode); |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5637 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5638 | synchronized (this) { |
| 5639 | ProcessRecord app = |
| 5640 | who != null ? getRecordForAppLocked(who) : null; |
| 5641 | if (app == null) return; |
| 5642 | |
| 5643 | Message msg = Message.obtain(); |
| 5644 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5645 | msg.obj = app; |
| 5646 | msg.arg1 = waiting ? 1 : 0; |
| 5647 | mHandler.sendMessage(msg); |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5652 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5653 | 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] | 5654 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 5655 | outInfo.totalMem = Process.getTotalMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5656 | outInfo.threshold = homeAppMem; |
| 5657 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5658 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5659 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5660 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5661 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5662 | ProcessList.VISIBLE_APP_ADJ); |
| 5663 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5664 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5665 | } |
| 5666 | |
| 5667 | // ========================================================= |
| 5668 | // TASK MANAGEMENT |
| 5669 | // ========================================================= |
| 5670 | |
| 5671 | public List getTasks(int maxNum, int flags, |
| 5672 | IThumbnailReceiver receiver) { |
| 5673 | ArrayList list = new ArrayList(); |
| 5674 | |
| 5675 | PendingThumbnailsRecord pending = null; |
| 5676 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5677 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5678 | |
| 5679 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5680 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5681 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5682 | + ", receiver=" + receiver); |
| 5683 | |
| 5684 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5685 | != PackageManager.PERMISSION_GRANTED) { |
| 5686 | if (receiver != null) { |
| 5687 | // If the caller wants to wait for pending thumbnails, |
| 5688 | // it ain't gonna get them. |
| 5689 | try { |
| 5690 | receiver.finished(); |
| 5691 | } catch (RemoteException ex) { |
| 5692 | } |
| 5693 | } |
| 5694 | String msg = "Permission Denial: getTasks() from pid=" |
| 5695 | + Binder.getCallingPid() |
| 5696 | + ", uid=" + Binder.getCallingUid() |
| 5697 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5698 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5699 | throw new SecurityException(msg); |
| 5700 | } |
| 5701 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5702 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5703 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5704 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5705 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5706 | TaskRecord curTask = null; |
| 5707 | int numActivities = 0; |
| 5708 | int numRunning = 0; |
| 5709 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5710 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5711 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5712 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5713 | |
| 5714 | // Initialize state for next task if needed. |
| 5715 | if (top == null || |
| 5716 | (top.state == ActivityState.INITIALIZING |
| 5717 | && top.task == r.task)) { |
| 5718 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5719 | curTask = r.task; |
| 5720 | numActivities = numRunning = 0; |
| 5721 | } |
| 5722 | |
| 5723 | // Add 'r' into the current task. |
| 5724 | numActivities++; |
| 5725 | if (r.app != null && r.app.thread != null) { |
| 5726 | numRunning++; |
| 5727 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5728 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5729 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5730 | TAG, r.intent.getComponent().flattenToShortString() |
| 5731 | + ": task=" + r.task); |
| 5732 | |
| 5733 | // If the next one is a different task, generate a new |
| 5734 | // TaskInfo entry for what we have. |
| 5735 | if (next == null || next.task != curTask) { |
| 5736 | ActivityManager.RunningTaskInfo ci |
| 5737 | = new ActivityManager.RunningTaskInfo(); |
| 5738 | ci.id = curTask.taskId; |
| 5739 | ci.baseActivity = r.intent.getComponent(); |
| 5740 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5741 | if (top.thumbHolder != null) { |
| 5742 | ci.description = top.thumbHolder.lastDescription; |
| 5743 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5744 | ci.numActivities = numActivities; |
| 5745 | ci.numRunning = numRunning; |
| 5746 | //System.out.println( |
| 5747 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5748 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5749 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5750 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5751 | + " app=" + top.app |
| 5752 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5753 | if (top.state == ActivityState.RESUMED |
| 5754 | || top.state == ActivityState.PAUSING) { |
| 5755 | if (top.idle && top.app != null |
| 5756 | && top.app.thread != null) { |
| 5757 | topRecord = top; |
| 5758 | topThumbnail = top.app.thread; |
| 5759 | } else { |
| 5760 | top.thumbnailNeeded = true; |
| 5761 | } |
| 5762 | } |
| 5763 | if (pending == null) { |
| 5764 | pending = new PendingThumbnailsRecord(receiver); |
| 5765 | } |
| 5766 | pending.pendingRecords.add(top); |
| 5767 | } |
| 5768 | list.add(ci); |
| 5769 | maxNum--; |
| 5770 | top = null; |
| 5771 | } |
| 5772 | } |
| 5773 | |
| 5774 | if (pending != null) { |
| 5775 | mPendingThumbnails.add(pending); |
| 5776 | } |
| 5777 | } |
| 5778 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5779 | 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] | 5780 | |
| 5781 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5782 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5783 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5784 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5785 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5786 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5787 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5788 | } |
| 5789 | } |
| 5790 | |
| 5791 | if (pending == null && receiver != null) { |
| 5792 | // In this case all thumbnails were available and the client |
| 5793 | // is being asked to be told when the remaining ones come in... |
| 5794 | // which is unusually, since the top-most currently running |
| 5795 | // activity should never have a canned thumbnail! Oh well. |
| 5796 | try { |
| 5797 | receiver.finished(); |
| 5798 | } catch (RemoteException ex) { |
| 5799 | } |
| 5800 | } |
| 5801 | |
| 5802 | return list; |
| 5803 | } |
| 5804 | |
| 5805 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5806 | int flags, int userId) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 5807 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 5808 | false, true, "getRecentTasks", null); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5809 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5810 | synchronized (this) { |
| 5811 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5812 | "getRecentTasks()"); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5813 | final boolean detailed = checkCallingPermission( |
| 5814 | android.Manifest.permission.GET_DETAILED_TASKS) |
| 5815 | == PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5816 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5817 | IPackageManager pm = AppGlobals.getPackageManager(); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5818 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5819 | final int N = mRecentTasks.size(); |
| 5820 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5821 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5822 | maxNum < N ? maxNum : N); |
| 5823 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5824 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5825 | // Only add calling user's recent tasks |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5826 | if (tr.userId != userId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5827 | // Return the entry if desired by the caller. We always return |
| 5828 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5829 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5830 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5831 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5832 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5833 | if (i == 0 |
| 5834 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5835 | || (tr.intent == null) |
| 5836 | || ((tr.intent.getFlags() |
| 5837 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5838 | ActivityManager.RecentTaskInfo rti |
| 5839 | = new ActivityManager.RecentTaskInfo(); |
| 5840 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5841 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5842 | rti.baseIntent = new Intent( |
| 5843 | tr.intent != null ? tr.intent : tr.affinityIntent); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5844 | if (!detailed) { |
| 5845 | rti.baseIntent.replaceExtras((Bundle)null); |
| 5846 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5847 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5848 | rti.description = tr.lastDescription; |
| 5849 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5850 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5851 | // Check whether this activity is currently available. |
| 5852 | try { |
| 5853 | if (rti.origActivity != null) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5854 | if (pm.getActivityInfo(rti.origActivity, 0, userId) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5855 | == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5856 | continue; |
| 5857 | } |
| 5858 | } else if (rti.baseIntent != null) { |
| 5859 | if (pm.queryIntentActivities(rti.baseIntent, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5860 | null, 0, userId) == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5861 | continue; |
| 5862 | } |
| 5863 | } |
| 5864 | } catch (RemoteException e) { |
| 5865 | // Will never happen. |
| 5866 | } |
| 5867 | } |
| 5868 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5869 | res.add(rti); |
| 5870 | maxNum--; |
| 5871 | } |
| 5872 | } |
| 5873 | return res; |
| 5874 | } |
| 5875 | } |
| 5876 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5877 | private TaskRecord taskForIdLocked(int id) { |
| 5878 | final int N = mRecentTasks.size(); |
| 5879 | for (int i=0; i<N; i++) { |
| 5880 | TaskRecord tr = mRecentTasks.get(i); |
| 5881 | if (tr.taskId == id) { |
| 5882 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5883 | } |
| 5884 | } |
| 5885 | return null; |
| 5886 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5887 | |
| 5888 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5889 | synchronized (this) { |
| 5890 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5891 | "getTaskThumbnails()"); |
| 5892 | TaskRecord tr = taskForIdLocked(id); |
| 5893 | if (tr != null) { |
| 5894 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5895 | } |
| 5896 | } |
| 5897 | return null; |
| 5898 | } |
| 5899 | |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 5900 | public Bitmap getTaskTopThumbnail(int id) { |
| 5901 | synchronized (this) { |
| 5902 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5903 | "getTaskTopThumbnail()"); |
| 5904 | TaskRecord tr = taskForIdLocked(id); |
| 5905 | if (tr != null) { |
| 5906 | return mMainStack.getTaskTopThumbnailLocked(tr); |
| 5907 | } |
| 5908 | } |
| 5909 | return null; |
| 5910 | } |
| 5911 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5912 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5913 | synchronized (this) { |
| 5914 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5915 | "removeSubTask()"); |
| 5916 | long ident = Binder.clearCallingIdentity(); |
| 5917 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5918 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex, |
| 5919 | true) != null; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5920 | } finally { |
| 5921 | Binder.restoreCallingIdentity(ident); |
| 5922 | } |
| 5923 | } |
| 5924 | } |
| 5925 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5926 | private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) { |
| 5927 | final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5928 | Intent baseIntent = new Intent( |
| 5929 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5930 | ComponentName component = baseIntent.getComponent(); |
| 5931 | if (component == null) { |
| 5932 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5933 | return; |
| 5934 | } |
| 5935 | |
| 5936 | // Find any running services associated with this app. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 5937 | mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5938 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5939 | if (killProcesses) { |
| 5940 | // Find any running processes associated with this app. |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5941 | final String pkg = component.getPackageName(); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5942 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5943 | HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap(); |
| 5944 | for (SparseArray<ProcessRecord> uids : pmap.values()) { |
| 5945 | for (int i=0; i<uids.size(); i++) { |
| 5946 | ProcessRecord proc = uids.valueAt(i); |
| 5947 | if (proc.userId != tr.userId) { |
| 5948 | continue; |
| 5949 | } |
| 5950 | if (!proc.pkgList.contains(pkg)) { |
| 5951 | continue; |
| 5952 | } |
| 5953 | procs.add(proc); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5954 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5955 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5956 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5957 | // Kill the running processes. |
| 5958 | for (int i=0; i<procs.size(); i++) { |
| 5959 | ProcessRecord pr = procs.get(i); |
| 5960 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5961 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 5962 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.userId, pr.pid, |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5963 | pr.processName, pr.setAdj, "remove task"); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5964 | pr.killedBackground = true; |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5965 | Process.killProcessQuiet(pr.pid); |
| 5966 | } else { |
| 5967 | pr.waitingToKill = "remove task"; |
| 5968 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5969 | } |
| 5970 | } |
| 5971 | } |
| 5972 | |
| 5973 | public boolean removeTask(int taskId, int flags) { |
| 5974 | synchronized (this) { |
| 5975 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5976 | "removeTask()"); |
| 5977 | long ident = Binder.clearCallingIdentity(); |
| 5978 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5979 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1, |
| 5980 | false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5981 | if (r != null) { |
| 5982 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5983 | cleanUpRemovedTaskLocked(r.task, flags); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5984 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5985 | } else { |
| 5986 | TaskRecord tr = null; |
| 5987 | int i=0; |
| 5988 | while (i < mRecentTasks.size()) { |
| 5989 | TaskRecord t = mRecentTasks.get(i); |
| 5990 | if (t.taskId == taskId) { |
| 5991 | tr = t; |
| 5992 | break; |
| 5993 | } |
| 5994 | i++; |
| 5995 | } |
| 5996 | if (tr != null) { |
| 5997 | if (tr.numActivities <= 0) { |
| 5998 | // Caller is just removing a recent task that is |
| 5999 | // not actively running. That is easy! |
| 6000 | mRecentTasks.remove(i); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 6001 | cleanUpRemovedTaskLocked(tr, flags); |
| 6002 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 6003 | } else { |
| 6004 | Slog.w(TAG, "removeTask: task " + taskId |
| 6005 | + " does not have activities to remove, " |
| 6006 | + " but numActivities=" + tr.numActivities |
| 6007 | + ": " + tr); |
| 6008 | } |
| 6009 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6010 | } |
| 6011 | } finally { |
| 6012 | Binder.restoreCallingIdentity(ident); |
| 6013 | } |
| 6014 | } |
| 6015 | return false; |
| 6016 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 6017 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6018 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 6019 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6020 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6021 | TaskRecord jt = startTask; |
| 6022 | |
| 6023 | // First look backwards |
| 6024 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6025 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6026 | if (r.task != jt) { |
| 6027 | jt = r.task; |
| 6028 | if (affinity.equals(jt.affinity)) { |
| 6029 | return j; |
| 6030 | } |
| 6031 | } |
| 6032 | } |
| 6033 | |
| 6034 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6035 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6036 | jt = startTask; |
| 6037 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6038 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6039 | if (r.task != jt) { |
| 6040 | if (affinity.equals(jt.affinity)) { |
| 6041 | return j; |
| 6042 | } |
| 6043 | jt = r.task; |
| 6044 | } |
| 6045 | } |
| 6046 | |
| 6047 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6048 | 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] | 6049 | return N-1; |
| 6050 | } |
| 6051 | |
| 6052 | return -1; |
| 6053 | } |
| 6054 | |
| 6055 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6056 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6057 | */ |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6058 | public void moveTaskToFront(int task, int flags, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6059 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6060 | "moveTaskToFront()"); |
| 6061 | |
| 6062 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6063 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6064 | Binder.getCallingUid(), "Task to front")) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6065 | ActivityOptions.abort(options); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6066 | return; |
| 6067 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6068 | final long origId = Binder.clearCallingIdentity(); |
| 6069 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6070 | TaskRecord tr = taskForIdLocked(task); |
| 6071 | if (tr != null) { |
| 6072 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 6073 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6074 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6075 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 6076 | // Caller wants the home activity moved with it. To accomplish this, |
| 6077 | // we'll just move the home task to the top first. |
| 6078 | mMainStack.moveHomeToFrontLocked(); |
| 6079 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6080 | mMainStack.moveTaskToFrontLocked(tr, null, options); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6081 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6082 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6083 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 6084 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6085 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 6086 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 6087 | mMainStack.mUserLeaving = true; |
| 6088 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 6089 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 6090 | // Caller wants the home activity moved with it. To accomplish this, |
| 6091 | // we'll just move the home task to the top first. |
| 6092 | mMainStack.moveHomeToFrontLocked(); |
| 6093 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6094 | mMainStack.moveTaskToFrontLocked(hr.task, null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6095 | return; |
| 6096 | } |
| 6097 | } |
| 6098 | } finally { |
| 6099 | Binder.restoreCallingIdentity(origId); |
| 6100 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6101 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6102 | } |
| 6103 | } |
| 6104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6105 | public void moveTaskToBack(int task) { |
| 6106 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6107 | "moveTaskToBack()"); |
| 6108 | |
| 6109 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6110 | if (mMainStack.mResumedActivity != null |
| 6111 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6112 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6113 | Binder.getCallingUid(), "Task to back")) { |
| 6114 | return; |
| 6115 | } |
| 6116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6117 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6118 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6119 | Binder.restoreCallingIdentity(origId); |
| 6120 | } |
| 6121 | } |
| 6122 | |
| 6123 | /** |
| 6124 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 6125 | * of the history stack. The activity's order within the task is unchanged. |
| 6126 | * |
| 6127 | * @param token A reference to the activity we wish to move |
| 6128 | * @param nonRoot If false then this only works if the activity is the root |
| 6129 | * of a task; if true it will work for any activity in a task. |
| 6130 | * @return Returns true if the move completed, false if not. |
| 6131 | */ |
| 6132 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6133 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6134 | synchronized(this) { |
| 6135 | final long origId = Binder.clearCallingIdentity(); |
| 6136 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 6137 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6138 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6139 | } |
| 6140 | Binder.restoreCallingIdentity(origId); |
| 6141 | } |
| 6142 | return false; |
| 6143 | } |
| 6144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6145 | public void moveTaskBackwards(int task) { |
| 6146 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6147 | "moveTaskBackwards()"); |
| 6148 | |
| 6149 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6150 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6151 | Binder.getCallingUid(), "Task backwards")) { |
| 6152 | return; |
| 6153 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6154 | final long origId = Binder.clearCallingIdentity(); |
| 6155 | moveTaskBackwardsLocked(task); |
| 6156 | Binder.restoreCallingIdentity(origId); |
| 6157 | } |
| 6158 | } |
| 6159 | |
| 6160 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6161 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6162 | } |
| 6163 | |
| 6164 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 6165 | synchronized(this) { |
| 6166 | return getTaskForActivityLocked(token, onlyRoot); |
| 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6171 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6172 | TaskRecord lastTask = null; |
| 6173 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6174 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 6175 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6176 | if (!onlyRoot || lastTask != r.task) { |
| 6177 | return r.task.taskId; |
| 6178 | } |
| 6179 | return -1; |
| 6180 | } |
| 6181 | lastTask = r.task; |
| 6182 | } |
| 6183 | |
| 6184 | return -1; |
| 6185 | } |
| 6186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6187 | // ========================================================= |
| 6188 | // THUMBNAILS |
| 6189 | // ========================================================= |
| 6190 | |
| 6191 | public void reportThumbnail(IBinder token, |
| 6192 | Bitmap thumbnail, CharSequence description) { |
| 6193 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 6194 | final long origId = Binder.clearCallingIdentity(); |
| 6195 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 6196 | Binder.restoreCallingIdentity(origId); |
| 6197 | } |
| 6198 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6199 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6200 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 6201 | TaskRecord task = null; |
| 6202 | ArrayList receivers = null; |
| 6203 | |
| 6204 | //System.out.println("Send pending thumbnail: " + r); |
| 6205 | |
| 6206 | synchronized(this) { |
| 6207 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6208 | r = mMainStack.isInStackLocked(token); |
| 6209 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6210 | return; |
| 6211 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6212 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 6213 | if (thumbnail == null && r.thumbHolder != null) { |
| 6214 | thumbnail = r.thumbHolder.lastThumbnail; |
| 6215 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6216 | } |
| 6217 | if (thumbnail == null && !always) { |
| 6218 | // If there is no thumbnail, and this entry is not actually |
| 6219 | // going away, then abort for now and pick up the next |
| 6220 | // thumbnail we get. |
| 6221 | return; |
| 6222 | } |
| 6223 | task = r.task; |
| 6224 | |
| 6225 | int N = mPendingThumbnails.size(); |
| 6226 | int i=0; |
| 6227 | while (i<N) { |
| 6228 | PendingThumbnailsRecord pr = |
| 6229 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 6230 | //System.out.println("Looking in " + pr.pendingRecords); |
| 6231 | if (pr.pendingRecords.remove(r)) { |
| 6232 | if (receivers == null) { |
| 6233 | receivers = new ArrayList(); |
| 6234 | } |
| 6235 | receivers.add(pr); |
| 6236 | if (pr.pendingRecords.size() == 0) { |
| 6237 | pr.finished = true; |
| 6238 | mPendingThumbnails.remove(i); |
| 6239 | N--; |
| 6240 | continue; |
| 6241 | } |
| 6242 | } |
| 6243 | i++; |
| 6244 | } |
| 6245 | } |
| 6246 | |
| 6247 | if (receivers != null) { |
| 6248 | final int N = receivers.size(); |
| 6249 | for (int i=0; i<N; i++) { |
| 6250 | try { |
| 6251 | PendingThumbnailsRecord pr = |
| 6252 | (PendingThumbnailsRecord)receivers.get(i); |
| 6253 | pr.receiver.newThumbnail( |
| 6254 | task != null ? task.taskId : -1, thumbnail, description); |
| 6255 | if (pr.finished) { |
| 6256 | pr.receiver.finished(); |
| 6257 | } |
| 6258 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6259 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6260 | } |
| 6261 | } |
| 6262 | } |
| 6263 | } |
| 6264 | |
| 6265 | // ========================================================= |
| 6266 | // CONTENT PROVIDERS |
| 6267 | // ========================================================= |
| 6268 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6269 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 6270 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6271 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6272 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6273 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6274 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6275 | } catch (RemoteException ex) { |
| 6276 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6277 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6278 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 6279 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6280 | if (providers != null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6281 | int N = providers.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6282 | for (int i=0; i<N; i++) { |
| 6283 | ProviderInfo cpi = |
| 6284 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6285 | boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6286 | cpi.name, cpi.flags); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6287 | if (singleton && UserHandle.getUserId(app.uid) != 0) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6288 | // This is a singleton provider, but a user besides the |
| 6289 | // default user is asking to initialize a process it runs |
| 6290 | // in... well, no, it doesn't actually run in this process, |
| 6291 | // it runs in the process of the default user. Get rid of it. |
| 6292 | providers.remove(i); |
| 6293 | N--; |
| 6294 | continue; |
| 6295 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6296 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6297 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6298 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6299 | if (cpr == null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6300 | cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6301 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6302 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6303 | if (DEBUG_MU) |
| 6304 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6305 | app.pubProviders.put(cpi.name, cpr); |
| 6306 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 6307 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6308 | } |
| 6309 | } |
| 6310 | return providers; |
| 6311 | } |
| 6312 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 6313 | /** |
| 6314 | * Check if {@link ProcessRecord} has a possible chance at accessing the |
| 6315 | * given {@link ProviderInfo}. Final permission checking is always done |
| 6316 | * in {@link ContentProvider}. |
| 6317 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6318 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6319 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6320 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6321 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6322 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6323 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6324 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6325 | return null; |
| 6326 | } |
| 6327 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6328 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6329 | == PackageManager.PERMISSION_GRANTED) { |
| 6330 | return null; |
| 6331 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6332 | |
| 6333 | PathPermission[] pps = cpi.pathPermissions; |
| 6334 | if (pps != null) { |
| 6335 | int i = pps.length; |
| 6336 | while (i > 0) { |
| 6337 | i--; |
| 6338 | PathPermission pp = pps[i]; |
| 6339 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6340 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6341 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6342 | return null; |
| 6343 | } |
| 6344 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6345 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6346 | == PackageManager.PERMISSION_GRANTED) { |
| 6347 | return null; |
| 6348 | } |
| 6349 | } |
| 6350 | } |
| 6351 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6352 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 6353 | if (perms != null) { |
| 6354 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 6355 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 6356 | return null; |
| 6357 | } |
| 6358 | } |
| 6359 | } |
| 6360 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6361 | String msg; |
| 6362 | if (!cpi.exported) { |
| 6363 | msg = "Permission Denial: opening provider " + cpi.name |
| 6364 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6365 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 6366 | + cpi.applicationInfo.uid; |
| 6367 | } else { |
| 6368 | msg = "Permission Denial: opening provider " + cpi.name |
| 6369 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6370 | + ", uid=" + callingUid + ") requires " |
| 6371 | + cpi.readPermission + " or " + cpi.writePermission; |
| 6372 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6373 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6374 | return msg; |
| 6375 | } |
| 6376 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6377 | ContentProviderConnection incProviderCountLocked(ProcessRecord r, |
| 6378 | final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6379 | if (r != null) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6380 | for (int i=0; i<r.conProviders.size(); i++) { |
| 6381 | ContentProviderConnection conn = r.conProviders.get(i); |
| 6382 | if (conn.provider == cpr) { |
| 6383 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6384 | "Adding provider requested by " |
| 6385 | + r.processName + " from process " |
| 6386 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6387 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6388 | if (stable) { |
| 6389 | conn.stableCount++; |
| 6390 | conn.numStableIncs++; |
| 6391 | } else { |
| 6392 | conn.unstableCount++; |
| 6393 | conn.numUnstableIncs++; |
| 6394 | } |
| 6395 | return conn; |
| 6396 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6397 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6398 | ContentProviderConnection conn = new ContentProviderConnection(cpr, r); |
| 6399 | if (stable) { |
| 6400 | conn.stableCount = 1; |
| 6401 | conn.numStableIncs = 1; |
| 6402 | } else { |
| 6403 | conn.unstableCount = 1; |
| 6404 | conn.numUnstableIncs = 1; |
| 6405 | } |
| 6406 | cpr.connections.add(conn); |
| 6407 | r.conProviders.add(conn); |
| 6408 | return conn; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6409 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6410 | cpr.addExternalProcessHandleLocked(externalProcessToken); |
| 6411 | return null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6412 | } |
| 6413 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6414 | boolean decProviderCountLocked(ContentProviderConnection conn, |
| 6415 | ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
| 6416 | if (conn != null) { |
| 6417 | cpr = conn.provider; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6418 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6419 | "Removing provider requested by " |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6420 | + conn.client.processName + " from process " |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6421 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6422 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6423 | if (stable) { |
| 6424 | conn.stableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6425 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6426 | conn.unstableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6427 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6428 | if (conn.stableCount == 0 && conn.unstableCount == 0) { |
| 6429 | cpr.connections.remove(conn); |
| 6430 | conn.client.conProviders.remove(conn); |
| 6431 | return true; |
| 6432 | } |
| 6433 | return false; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6434 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6435 | cpr.removeExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6436 | return false; |
| 6437 | } |
| 6438 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6439 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6440 | String name, IBinder token, boolean stable, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6441 | ContentProviderRecord cpr; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6442 | ContentProviderConnection conn = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6443 | ProviderInfo cpi = null; |
| 6444 | |
| 6445 | synchronized(this) { |
| 6446 | ProcessRecord r = null; |
| 6447 | if (caller != null) { |
| 6448 | r = getRecordForAppLocked(caller); |
| 6449 | if (r == null) { |
| 6450 | throw new SecurityException( |
| 6451 | "Unable to find app for caller " + caller |
| 6452 | + " (pid=" + Binder.getCallingPid() |
| 6453 | + ") when getting content provider " + name); |
| 6454 | } |
| 6455 | } |
| 6456 | |
| 6457 | // First check if this content provider has been published... |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6458 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6459 | boolean providerRunning = cpr != null; |
| 6460 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6461 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6462 | String msg; |
| 6463 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6464 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6465 | } |
| 6466 | |
| 6467 | if (r != null && cpr.canRunHere(r)) { |
| 6468 | // This provider has been published or is in the process |
| 6469 | // of being published... but it is also allowed to run |
| 6470 | // in the caller's process, so don't make a connection |
| 6471 | // and just let the caller instantiate its own instance. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6472 | ContentProviderHolder holder = cpr.newHolder(null); |
| 6473 | // don't give caller the provider object, it needs |
| 6474 | // to make its own. |
| 6475 | holder.provider = null; |
| 6476 | return holder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | final long origId = Binder.clearCallingIdentity(); |
| 6480 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6481 | // 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] | 6482 | // return it right away. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6483 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6484 | if (conn != null && (conn.stableCount+conn.unstableCount) == 1) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6485 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 6486 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6487 | // make sure to count it as being accessed and thus |
| 6488 | // back up on the LRU list. This is good because |
| 6489 | // content providers are often expensive to start. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6490 | updateLruProcessLocked(cpr.proc, false); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6491 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 6492 | } |
| 6493 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6494 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6495 | if (false) { |
| 6496 | if (cpr.name.flattenToShortString().equals( |
| 6497 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 6498 | Slog.v(TAG, "****************** KILLING " |
| 6499 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6500 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6501 | } |
| 6502 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6503 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6504 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 6505 | // NOTE: there is still a race here where a signal could be |
| 6506 | // pending on the process even though we managed to update its |
| 6507 | // adj level. Not sure what to do about this, but at least |
| 6508 | // the race is now smaller. |
| 6509 | if (!success) { |
| 6510 | // Uh oh... it looks like the provider's process |
| 6511 | // has been killed on us. We need to wait for a new |
| 6512 | // process to be started, and make sure its death |
| 6513 | // doesn't kill our process. |
| 6514 | Slog.i(TAG, |
| 6515 | "Existing provider " + cpr.name.flattenToShortString() |
| 6516 | + " is crashing; detaching " + r); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6517 | boolean lastRef = decProviderCountLocked(conn, cpr, token, stable); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6518 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6519 | if (!lastRef) { |
| 6520 | // This wasn't the last ref our process had on |
| 6521 | // the provider... we have now been killed, bail. |
| 6522 | return null; |
| 6523 | } |
| 6524 | providerRunning = false; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6525 | conn = null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6530 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6531 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6532 | boolean singleton; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6533 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6534 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6535 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6536 | resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6537 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6538 | } catch (RemoteException ex) { |
| 6539 | } |
| 6540 | if (cpi == null) { |
| 6541 | return null; |
| 6542 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6543 | singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6544 | cpi.name, cpi.flags); |
| 6545 | if (singleton) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 6546 | userId = 0; |
| 6547 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6548 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6549 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6550 | String msg; |
| 6551 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6552 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6553 | } |
| 6554 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6555 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6556 | && !cpi.processName.equals("system")) { |
| 6557 | // If this content provider does not run in the system |
| 6558 | // process, and the system is not yet ready to run other |
| 6559 | // processes, then fail fast instead of hanging. |
| 6560 | throw new IllegalArgumentException( |
| 6561 | "Attempt to launch content provider before system ready"); |
| 6562 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6563 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 6564 | // Make sure that the user who owns this provider is started. If not, |
| 6565 | // we don't want to allow it to run. |
| 6566 | if (mStartedUsers.get(userId) == null) { |
| 6567 | Slog.w(TAG, "Unable to launch app " |
| 6568 | + cpi.applicationInfo.packageName + "/" |
| 6569 | + cpi.applicationInfo.uid + " for provider " |
| 6570 | + name + ": user " + userId + " is stopped"); |
| 6571 | return null; |
| 6572 | } |
| 6573 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6574 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6575 | cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6576 | final boolean firstClass = cpr == null; |
| 6577 | if (firstClass) { |
| 6578 | try { |
| 6579 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6580 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6581 | getApplicationInfo( |
| 6582 | cpi.applicationInfo.packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6583 | STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6584 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6585 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6586 | + cpi.name); |
| 6587 | return null; |
| 6588 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6589 | ai = getAppInfoForUser(ai, userId); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6590 | cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6591 | } catch (RemoteException ex) { |
| 6592 | // pm is in same process, this will never happen. |
| 6593 | } |
| 6594 | } |
| 6595 | |
| 6596 | if (r != null && cpr.canRunHere(r)) { |
| 6597 | // If this is a multiprocess provider, then just return its |
| 6598 | // info and allow the caller to instantiate it. Only do |
| 6599 | // this if the provider is the same user as the caller's |
| 6600 | // process, or can run as root (so can be in any process). |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6601 | return cpr.newHolder(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6602 | } |
| 6603 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6604 | if (DEBUG_PROVIDER) { |
| 6605 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6606 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6607 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6608 | } |
| 6609 | |
| 6610 | // This is single process, and our app is now connecting to it. |
| 6611 | // See if we are already in the process of launching this |
| 6612 | // provider. |
| 6613 | final int N = mLaunchingProviders.size(); |
| 6614 | int i; |
| 6615 | for (i=0; i<N; i++) { |
| 6616 | if (mLaunchingProviders.get(i) == cpr) { |
| 6617 | break; |
| 6618 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6619 | } |
| 6620 | |
| 6621 | // If the provider is not already being launched, then get it |
| 6622 | // started. |
| 6623 | if (i >= N) { |
| 6624 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6625 | |
| 6626 | try { |
| 6627 | // Content provider is now in use, its package can't be stopped. |
| 6628 | try { |
| 6629 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6630 | cpr.appInfo.packageName, false, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6631 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6632 | } catch (IllegalArgumentException e) { |
| 6633 | Slog.w(TAG, "Failed trying to unstop package " |
| 6634 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6635 | } |
| 6636 | |
| 6637 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6638 | cpr.appInfo, false, 0, "content provider", |
| 6639 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6640 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6641 | if (proc == null) { |
| 6642 | Slog.w(TAG, "Unable to launch app " |
| 6643 | + cpi.applicationInfo.packageName + "/" |
| 6644 | + cpi.applicationInfo.uid + " for provider " |
| 6645 | + name + ": process is bad"); |
| 6646 | return null; |
| 6647 | } |
| 6648 | cpr.launchingApp = proc; |
| 6649 | mLaunchingProviders.add(cpr); |
| 6650 | } finally { |
| 6651 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6652 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6653 | } |
| 6654 | |
| 6655 | // Make sure the provider is published (the same provider class |
| 6656 | // may be published under multiple names). |
| 6657 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6658 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6659 | } |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6660 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6661 | mProviderMap.putProviderByName(name, cpr); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6662 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6663 | if (conn != null) { |
| 6664 | conn.waiting = true; |
| 6665 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6666 | } |
| 6667 | } |
| 6668 | |
| 6669 | // Wait for the provider to be published... |
| 6670 | synchronized (cpr) { |
| 6671 | while (cpr.provider == null) { |
| 6672 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6673 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6674 | + cpi.applicationInfo.packageName + "/" |
| 6675 | + cpi.applicationInfo.uid + " for provider " |
| 6676 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6677 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6678 | UserHandle.getUserId(cpi.applicationInfo.uid), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6679 | cpi.applicationInfo.packageName, |
| 6680 | cpi.applicationInfo.uid, name); |
| 6681 | return null; |
| 6682 | } |
| 6683 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6684 | if (DEBUG_MU) { |
| 6685 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6686 | + cpr.launchingApp); |
| 6687 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6688 | if (conn != null) { |
| 6689 | conn.waiting = true; |
| 6690 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6691 | cpr.wait(); |
| 6692 | } catch (InterruptedException ex) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6693 | } finally { |
| 6694 | if (conn != null) { |
| 6695 | conn.waiting = false; |
| 6696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6697 | } |
| 6698 | } |
| 6699 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6700 | return cpr != null ? cpr.newHolder(conn) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6701 | } |
| 6702 | |
| 6703 | public final ContentProviderHolder getContentProvider( |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6704 | IApplicationThread caller, String name, int userId, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6705 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6706 | if (caller == null) { |
| 6707 | String msg = "null IApplicationThread when getting content provider " |
| 6708 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6709 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6710 | throw new SecurityException(msg); |
| 6711 | } |
| 6712 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6713 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6714 | false, true, "getContentProvider", null); |
| 6715 | return getContentProviderImpl(caller, name, null, stable, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6716 | } |
| 6717 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6718 | public ContentProviderHolder getContentProviderExternal( |
| 6719 | String name, int userId, IBinder token) { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6720 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6721 | "Do not have permission in call getContentProviderExternal()"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6722 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6723 | false, true, "getContentProvider", null); |
| 6724 | return getContentProviderExternalUnchecked(name, token, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6725 | } |
| 6726 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6727 | private ContentProviderHolder getContentProviderExternalUnchecked(String name, |
| 6728 | IBinder token, int userId) { |
| 6729 | return getContentProviderImpl(null, name, token, true, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6730 | } |
| 6731 | |
| 6732 | /** |
| 6733 | * Drop a content provider from a ProcessRecord's bookkeeping |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6734 | */ |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6735 | public void removeContentProvider(IBinder connection, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6736 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6737 | synchronized (this) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6738 | ContentProviderConnection conn; |
| 6739 | try { |
| 6740 | conn = (ContentProviderConnection)connection; |
| 6741 | } catch (ClassCastException e) { |
| 6742 | String msg ="removeContentProvider: " + connection |
| 6743 | + " not a ContentProviderConnection"; |
| 6744 | Slog.w(TAG, msg); |
| 6745 | throw new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6746 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6747 | if (conn == null) { |
| 6748 | throw new NullPointerException("connection is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6749 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6750 | if (decProviderCountLocked(conn, null, null, stable)) { |
| 6751 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6752 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6753 | } |
| 6754 | } |
| 6755 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6756 | public void removeContentProviderExternal(String name, IBinder token) { |
| 6757 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6758 | "Do not have permission in call removeContentProviderExternal()"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6759 | removeContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6760 | } |
| 6761 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6762 | private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6763 | synchronized (this) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6764 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6765 | if(cpr == null) { |
| 6766 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6767 | 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] | 6768 | return; |
| 6769 | } |
| 6770 | |
| 6771 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6772 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6773 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6774 | if (localCpr.hasExternalProcessHandles()) { |
| 6775 | if (localCpr.removeExternalProcessHandleLocked(token)) { |
| 6776 | updateOomAdjLocked(); |
| 6777 | } else { |
| 6778 | Slog.e(TAG, "Attmpt to remove content provider " + localCpr |
| 6779 | + " with no external reference for token: " |
| 6780 | + token + "."); |
| 6781 | } |
| 6782 | } else { |
| 6783 | Slog.e(TAG, "Attmpt to remove content provider: " + localCpr |
| 6784 | + " with no external references."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6785 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6786 | } |
| 6787 | } |
| 6788 | |
| 6789 | public final void publishContentProviders(IApplicationThread caller, |
| 6790 | List<ContentProviderHolder> providers) { |
| 6791 | if (providers == null) { |
| 6792 | return; |
| 6793 | } |
| 6794 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6795 | enforceNotIsolatedCaller("publishContentProviders"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6796 | synchronized (this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6797 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6798 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6799 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6800 | if (r == null) { |
| 6801 | throw new SecurityException( |
| 6802 | "Unable to find app for caller " + caller |
| 6803 | + " (pid=" + Binder.getCallingPid() |
| 6804 | + ") when publishing content providers"); |
| 6805 | } |
| 6806 | |
| 6807 | final long origId = Binder.clearCallingIdentity(); |
| 6808 | |
| 6809 | final int N = providers.size(); |
| 6810 | for (int i=0; i<N; i++) { |
| 6811 | ContentProviderHolder src = providers.get(i); |
| 6812 | if (src == null || src.info == null || src.provider == null) { |
| 6813 | continue; |
| 6814 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6815 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6816 | if (DEBUG_MU) |
| 6817 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6818 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6819 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6820 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6821 | String names[] = dst.info.authority.split(";"); |
| 6822 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6823 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | int NL = mLaunchingProviders.size(); |
| 6827 | int j; |
| 6828 | for (j=0; j<NL; j++) { |
| 6829 | if (mLaunchingProviders.get(j) == dst) { |
| 6830 | mLaunchingProviders.remove(j); |
| 6831 | j--; |
| 6832 | NL--; |
| 6833 | } |
| 6834 | } |
| 6835 | synchronized (dst) { |
| 6836 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6837 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6838 | dst.notifyAll(); |
| 6839 | } |
| 6840 | updateOomAdjLocked(r); |
| 6841 | } |
| 6842 | } |
| 6843 | |
| 6844 | Binder.restoreCallingIdentity(origId); |
| 6845 | } |
| 6846 | } |
| 6847 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6848 | public boolean refContentProvider(IBinder connection, int stable, int unstable) { |
| 6849 | ContentProviderConnection conn; |
| 6850 | try { |
| 6851 | conn = (ContentProviderConnection)connection; |
| 6852 | } catch (ClassCastException e) { |
| 6853 | String msg ="refContentProvider: " + connection |
| 6854 | + " not a ContentProviderConnection"; |
| 6855 | Slog.w(TAG, msg); |
| 6856 | throw new IllegalArgumentException(msg); |
| 6857 | } |
| 6858 | if (conn == null) { |
| 6859 | throw new NullPointerException("connection is null"); |
| 6860 | } |
| 6861 | |
| 6862 | synchronized (this) { |
| 6863 | if (stable > 0) { |
| 6864 | conn.numStableIncs += stable; |
| 6865 | } |
| 6866 | stable = conn.stableCount + stable; |
| 6867 | if (stable < 0) { |
| 6868 | throw new IllegalStateException("stableCount < 0: " + stable); |
| 6869 | } |
| 6870 | |
| 6871 | if (unstable > 0) { |
| 6872 | conn.numUnstableIncs += unstable; |
| 6873 | } |
| 6874 | unstable = conn.unstableCount + unstable; |
| 6875 | if (unstable < 0) { |
| 6876 | throw new IllegalStateException("unstableCount < 0: " + unstable); |
| 6877 | } |
| 6878 | |
| 6879 | if ((stable+unstable) <= 0) { |
| 6880 | throw new IllegalStateException("ref counts can't go to zero here: stable=" |
| 6881 | + stable + " unstable=" + unstable); |
| 6882 | } |
| 6883 | conn.stableCount = stable; |
| 6884 | conn.unstableCount = unstable; |
| 6885 | return !conn.dead; |
| 6886 | } |
| 6887 | } |
| 6888 | |
| 6889 | public void unstableProviderDied(IBinder connection) { |
| 6890 | ContentProviderConnection conn; |
| 6891 | try { |
| 6892 | conn = (ContentProviderConnection)connection; |
| 6893 | } catch (ClassCastException e) { |
| 6894 | String msg ="refContentProvider: " + connection |
| 6895 | + " not a ContentProviderConnection"; |
| 6896 | Slog.w(TAG, msg); |
| 6897 | throw new IllegalArgumentException(msg); |
| 6898 | } |
| 6899 | if (conn == null) { |
| 6900 | throw new NullPointerException("connection is null"); |
| 6901 | } |
| 6902 | |
| 6903 | // Safely retrieve the content provider associated with the connection. |
| 6904 | IContentProvider provider; |
| 6905 | synchronized (this) { |
| 6906 | provider = conn.provider.provider; |
| 6907 | } |
| 6908 | |
| 6909 | if (provider == null) { |
| 6910 | // Um, yeah, we're way ahead of you. |
| 6911 | return; |
| 6912 | } |
| 6913 | |
| 6914 | // Make sure the caller is being honest with us. |
| 6915 | if (provider.asBinder().pingBinder()) { |
| 6916 | // Er, no, still looks good to us. |
| 6917 | synchronized (this) { |
| 6918 | Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid() |
| 6919 | + " says " + conn + " died, but we don't agree"); |
| 6920 | return; |
| 6921 | } |
| 6922 | } |
| 6923 | |
| 6924 | // Well look at that! It's dead! |
| 6925 | synchronized (this) { |
| 6926 | if (conn.provider.provider != provider) { |
| 6927 | // But something changed... good enough. |
| 6928 | return; |
| 6929 | } |
| 6930 | |
| 6931 | ProcessRecord proc = conn.provider.proc; |
| 6932 | if (proc == null || proc.thread == null) { |
| 6933 | // Seems like the process is already cleaned up. |
| 6934 | return; |
| 6935 | } |
| 6936 | |
| 6937 | // As far as we're concerned, this is just like receiving a |
| 6938 | // death notification... just a bit prematurely. |
| 6939 | Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid |
| 6940 | + ") early provider death"); |
Dianne Hackborn | bd145db | 2012-06-05 16:20:46 -0700 | [diff] [blame] | 6941 | final long ident = Binder.clearCallingIdentity(); |
| 6942 | try { |
| 6943 | appDiedLocked(proc, proc.pid, proc.thread); |
| 6944 | } finally { |
| 6945 | Binder.restoreCallingIdentity(ident); |
| 6946 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6947 | } |
| 6948 | } |
| 6949 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6950 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6951 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6952 | synchronized (mSelf) { |
| 6953 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6954 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6955 | if (providers != null) { |
| 6956 | for (int i=providers.size()-1; i>=0; i--) { |
| 6957 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6958 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6959 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6960 | + ": not system .apk"); |
| 6961 | providers.remove(i); |
| 6962 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6963 | } |
| 6964 | } |
| 6965 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6966 | if (providers != null) { |
| 6967 | mSystemThread.installSystemProviders(providers); |
| 6968 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6969 | |
| 6970 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6971 | |
| 6972 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6973 | } |
| 6974 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6975 | /** |
| 6976 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6977 | * to access its content provider. |
| 6978 | * |
| 6979 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6980 | * |
| 6981 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6982 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6983 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6984 | public String getProviderMimeType(Uri uri, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6985 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6986 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6987 | userId, false, true, "getProviderMimeType", null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6988 | final String name = uri.getAuthority(); |
| 6989 | final long ident = Binder.clearCallingIdentity(); |
| 6990 | ContentProviderHolder holder = null; |
| 6991 | |
| 6992 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6993 | holder = getContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6994 | if (holder != null) { |
| 6995 | return holder.provider.getType(uri); |
| 6996 | } |
| 6997 | } catch (RemoteException e) { |
| 6998 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6999 | return null; |
| 7000 | } finally { |
| 7001 | if (holder != null) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7002 | removeContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 7003 | } |
| 7004 | Binder.restoreCallingIdentity(ident); |
| 7005 | } |
| 7006 | |
| 7007 | return null; |
| 7008 | } |
| 7009 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7010 | // ========================================================= |
| 7011 | // GLOBAL MANAGEMENT |
| 7012 | // ========================================================= |
| 7013 | |
| 7014 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7015 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7016 | String proc = customProcess != null ? customProcess : info.processName; |
| 7017 | BatteryStatsImpl.Uid.Proc ps = null; |
| 7018 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7019 | int uid = info.uid; |
| 7020 | if (isolated) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7021 | int userId = UserHandle.getUserId(uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7022 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 7023 | uid = 0; |
| 7024 | while (true) { |
| 7025 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 7026 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 7027 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 7028 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7029 | uid = UserHandle.getUid(userId, mNextIsolatedProcessUid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7030 | mNextIsolatedProcessUid++; |
| 7031 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 7032 | // No process for this uid, use it. |
| 7033 | break; |
| 7034 | } |
| 7035 | stepsLeft--; |
| 7036 | if (stepsLeft <= 0) { |
| 7037 | return null; |
| 7038 | } |
| 7039 | } |
| 7040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7041 | synchronized (stats) { |
| 7042 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 7043 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7044 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7045 | } |
| 7046 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7047 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 7048 | ProcessRecord app; |
| 7049 | if (!isolated) { |
| 7050 | app = getProcessRecordLocked(info.processName, info.uid); |
| 7051 | } else { |
| 7052 | app = null; |
| 7053 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7054 | |
| 7055 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7056 | app = newProcessRecordLocked(null, info, null, isolated); |
| 7057 | mProcessNames.put(info.processName, app.uid, app); |
| 7058 | if (isolated) { |
| 7059 | mIsolatedProcesses.put(app.uid, app); |
| 7060 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7061 | updateLruProcessLocked(app, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7062 | } |
| 7063 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7064 | // This package really, really can not be stopped. |
| 7065 | try { |
| 7066 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7067 | info.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7068 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 7069 | } catch (IllegalArgumentException e) { |
| 7070 | Slog.w(TAG, "Failed trying to unstop package " |
| 7071 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7072 | } |
| 7073 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7074 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 7075 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 7076 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7077 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7078 | } |
| 7079 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 7080 | mPersistentStartingProcesses.add(app); |
| 7081 | startProcessLocked(app, "added application", app.processName); |
| 7082 | } |
| 7083 | |
| 7084 | return app; |
| 7085 | } |
| 7086 | |
| 7087 | public void unhandledBack() { |
| 7088 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 7089 | "unhandledBack()"); |
| 7090 | |
| 7091 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7092 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7093 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7094 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 7095 | if (count > 1) { |
| 7096 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7097 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 7098 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7099 | Binder.restoreCallingIdentity(origId); |
| 7100 | } |
| 7101 | } |
| 7102 | } |
| 7103 | |
| 7104 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7105 | enforceNotIsolatedCaller("openContentUri"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7106 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7107 | String name = uri.getAuthority(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7108 | ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7109 | ParcelFileDescriptor pfd = null; |
| 7110 | if (cph != null) { |
| 7111 | // We record the binder invoker's uid in thread-local storage before |
| 7112 | // going to the content provider to open the file. Later, in the code |
| 7113 | // that handles all permissions checks, we look for this uid and use |
| 7114 | // that rather than the Activity Manager's own uid. The effect is that |
| 7115 | // we do the check against the caller's permissions even though it looks |
| 7116 | // to the content provider like the Activity Manager itself is making |
| 7117 | // the request. |
| 7118 | sCallerIdentity.set(new Identity( |
| 7119 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 7120 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 7121 | pfd = cph.provider.openFile(null, uri, "r"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7122 | } catch (FileNotFoundException e) { |
| 7123 | // do nothing; pfd will be returned null |
| 7124 | } finally { |
| 7125 | // Ensure that whatever happens, we clean up the identity state |
| 7126 | sCallerIdentity.remove(); |
| 7127 | } |
| 7128 | |
| 7129 | // 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] | 7130 | removeContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7131 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7132 | 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] | 7133 | } |
| 7134 | return pfd; |
| 7135 | } |
| 7136 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7137 | // Actually is sleeping or shutting down or whatever else in the future |
| 7138 | // is an inactive state. |
| 7139 | public boolean isSleeping() { |
| 7140 | return mSleeping || mShuttingDown; |
| 7141 | } |
| 7142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7143 | public void goingToSleep() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7144 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7145 | != PackageManager.PERMISSION_GRANTED) { |
| 7146 | throw new SecurityException("Requires permission " |
| 7147 | + android.Manifest.permission.DEVICE_POWER); |
| 7148 | } |
| 7149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7150 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7151 | mWentToSleep = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7152 | updateEventDispatchingLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7153 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7154 | if (!mSleeping) { |
| 7155 | mSleeping = true; |
| 7156 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 7157 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7158 | // Initialize the wake times of all processes. |
| 7159 | checkExcessivePowerUsageLocked(false); |
| 7160 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 7161 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 7162 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
| 7163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7164 | } |
| 7165 | } |
| 7166 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7167 | public boolean shutdown(int timeout) { |
| 7168 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 7169 | != PackageManager.PERMISSION_GRANTED) { |
| 7170 | throw new SecurityException("Requires permission " |
| 7171 | + android.Manifest.permission.SHUTDOWN); |
| 7172 | } |
| 7173 | |
| 7174 | boolean timedout = false; |
| 7175 | |
| 7176 | synchronized(this) { |
| 7177 | mShuttingDown = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7178 | updateEventDispatchingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7179 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7180 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7181 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7182 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7183 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 7184 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7185 | long delay = endTime - System.currentTimeMillis(); |
| 7186 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7187 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7188 | timedout = true; |
| 7189 | break; |
| 7190 | } |
| 7191 | try { |
| 7192 | this.wait(); |
| 7193 | } catch (InterruptedException e) { |
| 7194 | } |
| 7195 | } |
| 7196 | } |
| 7197 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 7198 | |
| 7199 | mAppOpsService.shutdown(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7200 | mUsageStatsService.shutdown(); |
| 7201 | mBatteryStatsService.shutdown(); |
| 7202 | |
| 7203 | return timedout; |
| 7204 | } |
| 7205 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7206 | public final void activitySlept(IBinder token) { |
| 7207 | if (localLOGV) Slog.v( |
| 7208 | TAG, "Activity slept: token=" + token); |
| 7209 | |
| 7210 | ActivityRecord r = null; |
| 7211 | |
| 7212 | final long origId = Binder.clearCallingIdentity(); |
| 7213 | |
| 7214 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7215 | r = mMainStack.isInStackLocked(token); |
| 7216 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7217 | mMainStack.activitySleptLocked(r); |
| 7218 | } |
| 7219 | } |
| 7220 | |
| 7221 | Binder.restoreCallingIdentity(origId); |
| 7222 | } |
| 7223 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7224 | private void comeOutOfSleepIfNeededLocked() { |
| 7225 | if (!mWentToSleep && !mLockScreenShown) { |
| 7226 | if (mSleeping) { |
| 7227 | mSleeping = false; |
| 7228 | mMainStack.awakeFromSleepingLocked(); |
| 7229 | mMainStack.resumeTopActivityLocked(null); |
| 7230 | } |
| 7231 | } |
| 7232 | } |
| 7233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7234 | public void wakingUp() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7235 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7236 | != PackageManager.PERMISSION_GRANTED) { |
| 7237 | throw new SecurityException("Requires permission " |
| 7238 | + android.Manifest.permission.DEVICE_POWER); |
| 7239 | } |
| 7240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7241 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7242 | mWentToSleep = false; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7243 | updateEventDispatchingLocked(); |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7244 | comeOutOfSleepIfNeededLocked(); |
| 7245 | } |
| 7246 | } |
| 7247 | |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7248 | private void updateEventDispatchingLocked() { |
| 7249 | mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown); |
| 7250 | } |
| 7251 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7252 | public void setLockScreenShown(boolean shown) { |
| 7253 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7254 | != PackageManager.PERMISSION_GRANTED) { |
| 7255 | throw new SecurityException("Requires permission " |
| 7256 | + android.Manifest.permission.DEVICE_POWER); |
| 7257 | } |
| 7258 | |
| 7259 | synchronized(this) { |
| 7260 | mLockScreenShown = shown; |
| 7261 | comeOutOfSleepIfNeededLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7262 | } |
| 7263 | } |
| 7264 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7265 | public void stopAppSwitches() { |
| 7266 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7267 | != PackageManager.PERMISSION_GRANTED) { |
| 7268 | throw new SecurityException("Requires permission " |
| 7269 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7270 | } |
| 7271 | |
| 7272 | synchronized(this) { |
| 7273 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 7274 | + APP_SWITCH_DELAY_TIME; |
| 7275 | mDidAppSwitch = false; |
| 7276 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7277 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7278 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 7279 | } |
| 7280 | } |
| 7281 | |
| 7282 | public void resumeAppSwitches() { |
| 7283 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7284 | != PackageManager.PERMISSION_GRANTED) { |
| 7285 | throw new SecurityException("Requires permission " |
| 7286 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7287 | } |
| 7288 | |
| 7289 | synchronized(this) { |
| 7290 | // Note that we don't execute any pending app switches... we will |
| 7291 | // let those wait until either the timeout, or the next start |
| 7292 | // activity request. |
| 7293 | mAppSwitchesAllowedTime = 0; |
| 7294 | } |
| 7295 | } |
| 7296 | |
| 7297 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 7298 | String name) { |
| 7299 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 7300 | return true; |
| 7301 | } |
| 7302 | |
| 7303 | final int perm = checkComponentPermission( |
| 7304 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 7305 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7306 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 7307 | return true; |
| 7308 | } |
| 7309 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7310 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7311 | return false; |
| 7312 | } |
| 7313 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7314 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 7315 | boolean persistent) { |
| 7316 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 7317 | "setDebugApp()"); |
| 7318 | |
| 7319 | // Note that this is not really thread safe if there are multiple |
| 7320 | // callers into it at the same time, but that's not a situation we |
| 7321 | // care about. |
| 7322 | if (persistent) { |
| 7323 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7324 | Settings.Global.putString( |
| 7325 | resolver, Settings.Global.DEBUG_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7326 | packageName); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7327 | Settings.Global.putInt( |
| 7328 | resolver, Settings.Global.WAIT_FOR_DEBUGGER, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7329 | waitForDebugger ? 1 : 0); |
| 7330 | } |
| 7331 | |
| 7332 | synchronized (this) { |
| 7333 | if (!persistent) { |
| 7334 | mOrigDebugApp = mDebugApp; |
| 7335 | mOrigWaitForDebugger = mWaitForDebugger; |
| 7336 | } |
| 7337 | mDebugApp = packageName; |
| 7338 | mWaitForDebugger = waitForDebugger; |
| 7339 | mDebugTransient = !persistent; |
| 7340 | if (packageName != null) { |
| 7341 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 7342 | forceStopPackageLocked(packageName, -1, false, false, true, true, |
| 7343 | UserHandle.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7344 | Binder.restoreCallingIdentity(origId); |
| 7345 | } |
| 7346 | } |
| 7347 | } |
| 7348 | |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 7349 | void setOpenGlTraceApp(ApplicationInfo app, String processName) { |
| 7350 | synchronized (this) { |
| 7351 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7352 | if (!isDebuggable) { |
| 7353 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7354 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7355 | } |
| 7356 | } |
| 7357 | |
| 7358 | mOpenGlTraceApp = processName; |
| 7359 | } |
| 7360 | } |
| 7361 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 7362 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 7363 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 7364 | synchronized (this) { |
| 7365 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7366 | if (!isDebuggable) { |
| 7367 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7368 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7369 | } |
| 7370 | } |
| 7371 | mProfileApp = processName; |
| 7372 | mProfileFile = profileFile; |
| 7373 | if (mProfileFd != null) { |
| 7374 | try { |
| 7375 | mProfileFd.close(); |
| 7376 | } catch (IOException e) { |
| 7377 | } |
| 7378 | mProfileFd = null; |
| 7379 | } |
| 7380 | mProfileFd = profileFd; |
| 7381 | mProfileType = 0; |
| 7382 | mAutoStopProfiler = autoStopProfiler; |
| 7383 | } |
| 7384 | } |
| 7385 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7386 | public void setAlwaysFinish(boolean enabled) { |
| 7387 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 7388 | "setAlwaysFinish()"); |
| 7389 | |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7390 | Settings.Global.putInt( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7391 | mContext.getContentResolver(), |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7392 | Settings.Global.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7393 | |
| 7394 | synchronized (this) { |
| 7395 | mAlwaysFinishActivities = enabled; |
| 7396 | } |
| 7397 | } |
| 7398 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7399 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7400 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7401 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7402 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7403 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7404 | } |
| 7405 | } |
| 7406 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 7407 | public boolean isUserAMonkey() { |
| 7408 | // For now the fact that there is a controller implies |
| 7409 | // we have a monkey. |
| 7410 | synchronized (this) { |
| 7411 | return mController != null; |
| 7412 | } |
| 7413 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7414 | |
| 7415 | public void requestBugReport() { |
Jeff Sharkey | b932319 | 2013-02-05 13:32:18 -0800 | [diff] [blame^] | 7416 | enforceCallingPermission(android.Manifest.permission.DUMP, "requestBugReport"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7417 | SystemProperties.set("ctl.start", "bugreport"); |
| 7418 | } |
| 7419 | |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 7420 | public long inputDispatchingTimedOut(int pid, boolean aboveSystem) { |
| 7421 | if (checkCallingPermission(android.Manifest.permission.FILTER_EVENTS) |
| 7422 | != PackageManager.PERMISSION_GRANTED) { |
| 7423 | throw new SecurityException("Requires permission " |
| 7424 | + android.Manifest.permission.FILTER_EVENTS); |
| 7425 | } |
| 7426 | |
| 7427 | ProcessRecord proc; |
| 7428 | |
| 7429 | // TODO: Unify this code with ActivityRecord.keyDispatchingTimedOut(). |
| 7430 | synchronized (this) { |
| 7431 | synchronized (mPidsSelfLocked) { |
| 7432 | proc = mPidsSelfLocked.get(pid); |
| 7433 | } |
| 7434 | if (proc != null) { |
| 7435 | if (proc.debugging) { |
| 7436 | return -1; |
| 7437 | } |
| 7438 | |
| 7439 | if (mDidDexOpt) { |
| 7440 | // Give more time since we were dexopting. |
| 7441 | mDidDexOpt = false; |
| 7442 | return -1; |
| 7443 | } |
| 7444 | |
| 7445 | if (proc.instrumentationClass != null) { |
| 7446 | Bundle info = new Bundle(); |
| 7447 | info.putString("shortMsg", "keyDispatchingTimedOut"); |
| 7448 | info.putString("longMsg", "Timed out while dispatching key event"); |
| 7449 | finishInstrumentationLocked(proc, Activity.RESULT_CANCELED, info); |
| 7450 | proc = null; |
| 7451 | } |
| 7452 | } |
| 7453 | } |
| 7454 | |
| 7455 | if (proc != null) { |
| 7456 | appNotResponding(proc, null, null, aboveSystem, "keyDispatchingTimedOut"); |
| 7457 | if (proc.instrumentationClass != null || proc.usingWrapper) { |
| 7458 | return INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT; |
| 7459 | } |
| 7460 | } |
| 7461 | |
| 7462 | return KEY_DISPATCHING_TIMEOUT; |
| 7463 | } |
| 7464 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 7465 | public Bundle getTopActivityExtras(int requestType) { |
| 7466 | enforceCallingPermission(android.Manifest.permission.GET_TOP_ACTIVITY_INFO, |
| 7467 | "getTopActivityExtras()"); |
| 7468 | PendingActivityExtras pae; |
| 7469 | Bundle extras = new Bundle(); |
| 7470 | synchronized (this) { |
| 7471 | ActivityRecord activity = mMainStack.mResumedActivity; |
| 7472 | if (activity == null) { |
| 7473 | Slog.w(TAG, "getTopActivityExtras failed: no resumed activity"); |
| 7474 | return null; |
| 7475 | } |
| 7476 | extras.putString(Intent.EXTRA_ASSIST_PACKAGE, activity.packageName); |
| 7477 | if (activity.app == null || activity.app.thread == null) { |
| 7478 | Slog.w(TAG, "getTopActivityExtras failed: no process for " + activity); |
| 7479 | return extras; |
| 7480 | } |
| 7481 | if (activity.app.pid == Binder.getCallingPid()) { |
| 7482 | Slog.w(TAG, "getTopActivityExtras failed: request process same as " + activity); |
| 7483 | return extras; |
| 7484 | } |
| 7485 | pae = new PendingActivityExtras(activity); |
| 7486 | try { |
| 7487 | activity.app.thread.requestActivityExtras(activity.appToken, pae, requestType); |
| 7488 | mPendingActivityExtras.add(pae); |
| 7489 | mHandler.postDelayed(pae, PENDING_ACTIVITY_RESULT_TIMEOUT); |
| 7490 | } catch (RemoteException e) { |
| 7491 | Slog.w(TAG, "getTopActivityExtras failed: crash calling " + activity); |
| 7492 | return extras; |
| 7493 | } |
| 7494 | } |
| 7495 | synchronized (pae) { |
| 7496 | while (!pae.haveResult) { |
| 7497 | try { |
| 7498 | pae.wait(); |
| 7499 | } catch (InterruptedException e) { |
| 7500 | } |
| 7501 | } |
| 7502 | if (pae.result != null) { |
| 7503 | extras.putBundle(Intent.EXTRA_ASSIST_CONTEXT, pae.result); |
| 7504 | } |
| 7505 | } |
| 7506 | synchronized (this) { |
| 7507 | mPendingActivityExtras.remove(pae); |
| 7508 | mHandler.removeCallbacks(pae); |
| 7509 | } |
| 7510 | return extras; |
| 7511 | } |
| 7512 | |
| 7513 | public void reportTopActivityExtras(IBinder token, Bundle extras) { |
| 7514 | PendingActivityExtras pae = (PendingActivityExtras)token; |
| 7515 | synchronized (pae) { |
| 7516 | pae.result = extras; |
| 7517 | pae.haveResult = true; |
| 7518 | pae.notifyAll(); |
| 7519 | } |
| 7520 | } |
| 7521 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7522 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7523 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 7524 | "registerProcessObserver()"); |
| 7525 | synchronized (this) { |
| 7526 | mProcessObservers.register(observer); |
| 7527 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7528 | } |
| 7529 | |
| 7530 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7531 | synchronized (this) { |
| 7532 | mProcessObservers.unregister(observer); |
| 7533 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7534 | } |
| 7535 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7536 | public void setImmersive(IBinder token, boolean immersive) { |
| 7537 | synchronized(this) { |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 7538 | final ActivityRecord r = mMainStack.isInStackLocked(token); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7539 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7540 | throw new IllegalArgumentException(); |
| 7541 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7542 | r.immersive = immersive; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 7543 | |
| 7544 | // update associated state if we're frontmost |
| 7545 | if (r == mFocusedActivity) { |
| 7546 | if (DEBUG_IMMERSIVE) { |
| 7547 | Slog.d(TAG, "Frontmost changed immersion: "+ r); |
| 7548 | } |
| 7549 | applyUpdateLockStateLocked(r); |
| 7550 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7551 | } |
| 7552 | } |
| 7553 | |
| 7554 | public boolean isImmersive(IBinder token) { |
| 7555 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7556 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7557 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7558 | throw new IllegalArgumentException(); |
| 7559 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7560 | return r.immersive; |
| 7561 | } |
| 7562 | } |
| 7563 | |
| 7564 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7565 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7566 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7567 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7568 | return (r != null) ? r.immersive : false; |
| 7569 | } |
| 7570 | } |
| 7571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7572 | public final void enterSafeMode() { |
| 7573 | synchronized(this) { |
| 7574 | // It only makes sense to do this before the system is ready |
| 7575 | // and started launching other packages. |
| 7576 | if (!mSystemReady) { |
| 7577 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7578 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7579 | } catch (RemoteException e) { |
| 7580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7581 | } |
| 7582 | } |
| 7583 | } |
| 7584 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7585 | public final void showSafeModeOverlay() { |
| 7586 | View v = LayoutInflater.from(mContext).inflate( |
| 7587 | com.android.internal.R.layout.safe_mode, null); |
| 7588 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 7589 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 7590 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 7591 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 7592 | lp.gravity = Gravity.BOTTOM | Gravity.START; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7593 | lp.format = v.getBackground().getOpacity(); |
| 7594 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 7595 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
Craig Mautner | 5962b12 | 2012-10-05 14:45:52 -0700 | [diff] [blame] | 7596 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7597 | ((WindowManager)mContext.getSystemService( |
| 7598 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 7599 | } |
| 7600 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7601 | public void noteWakeupAlarm(IIntentSender sender) { |
| 7602 | if (!(sender instanceof PendingIntentRecord)) { |
| 7603 | return; |
| 7604 | } |
| 7605 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 7606 | synchronized (stats) { |
| 7607 | if (mBatteryStatsService.isOnBattery()) { |
| 7608 | mBatteryStatsService.enforceCallingPermission(); |
| 7609 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 7610 | int MY_UID = Binder.getCallingUid(); |
| 7611 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 7612 | BatteryStatsImpl.Uid.Pkg pkg = |
| 7613 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 7614 | pkg.incWakeupsLocked(); |
| 7615 | } |
| 7616 | } |
| 7617 | } |
| 7618 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7619 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7620 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7621 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7622 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7623 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7624 | // XXX Note: don't acquire main activity lock here, because the window |
| 7625 | // manager calls in with its locks held. |
| 7626 | |
| 7627 | boolean killed = false; |
| 7628 | synchronized (mPidsSelfLocked) { |
| 7629 | int[] types = new int[pids.length]; |
| 7630 | int worstType = 0; |
| 7631 | for (int i=0; i<pids.length; i++) { |
| 7632 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7633 | if (proc != null) { |
| 7634 | int type = proc.setAdj; |
| 7635 | types[i] = type; |
| 7636 | if (type > worstType) { |
| 7637 | worstType = type; |
| 7638 | } |
| 7639 | } |
| 7640 | } |
| 7641 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7642 | // 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] | 7643 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7644 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 7645 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7646 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7647 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7648 | |
| 7649 | // If this is not a secure call, don't let it kill processes that |
| 7650 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7651 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 7652 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7653 | } |
| 7654 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7655 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7656 | for (int i=0; i<pids.length; i++) { |
| 7657 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7658 | if (proc == null) { |
| 7659 | continue; |
| 7660 | } |
| 7661 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7662 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7663 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7664 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.userId, proc.pid, |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7665 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7666 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7667 | proc.killedBackground = true; |
| 7668 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7669 | } |
| 7670 | } |
| 7671 | } |
| 7672 | return killed; |
| 7673 | } |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7674 | |
| 7675 | @Override |
| 7676 | public boolean killProcessesBelowForeground(String reason) { |
| 7677 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7678 | throw new SecurityException("killProcessesBelowForeground() only available to system"); |
| 7679 | } |
| 7680 | |
| 7681 | return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason); |
| 7682 | } |
| 7683 | |
| 7684 | private boolean killProcessesBelowAdj(int belowAdj, String reason) { |
| 7685 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7686 | throw new SecurityException("killProcessesBelowAdj() only available to system"); |
| 7687 | } |
| 7688 | |
| 7689 | boolean killed = false; |
| 7690 | synchronized (mPidsSelfLocked) { |
| 7691 | final int size = mPidsSelfLocked.size(); |
| 7692 | for (int i = 0; i < size; i++) { |
| 7693 | final int pid = mPidsSelfLocked.keyAt(i); |
| 7694 | final ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7695 | if (proc == null) continue; |
| 7696 | |
| 7697 | final int adj = proc.setAdj; |
| 7698 | if (adj > belowAdj && !proc.killedBackground) { |
| 7699 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7700 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.userId, |
| 7701 | proc.pid, proc.processName, adj, reason); |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7702 | killed = true; |
| 7703 | proc.killedBackground = true; |
| 7704 | Process.killProcessQuiet(pid); |
| 7705 | } |
| 7706 | } |
| 7707 | } |
| 7708 | return killed; |
| 7709 | } |
| 7710 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7711 | public final void startRunning(String pkg, String cls, String action, |
| 7712 | String data) { |
| 7713 | synchronized(this) { |
| 7714 | if (mStartRunning) { |
| 7715 | return; |
| 7716 | } |
| 7717 | mStartRunning = true; |
| 7718 | mTopComponent = pkg != null && cls != null |
| 7719 | ? new ComponentName(pkg, cls) : null; |
| 7720 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 7721 | mTopData = data; |
| 7722 | if (!mSystemReady) { |
| 7723 | return; |
| 7724 | } |
| 7725 | } |
| 7726 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7727 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7728 | } |
| 7729 | |
| 7730 | private void retrieveSettings() { |
| 7731 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7732 | String debugApp = Settings.Global.getString( |
| 7733 | resolver, Settings.Global.DEBUG_APP); |
| 7734 | boolean waitForDebugger = Settings.Global.getInt( |
| 7735 | resolver, Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0; |
| 7736 | boolean alwaysFinishActivities = Settings.Global.getInt( |
| 7737 | resolver, Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7738 | |
| 7739 | Configuration configuration = new Configuration(); |
| 7740 | Settings.System.getConfiguration(resolver, configuration); |
| 7741 | |
| 7742 | synchronized (this) { |
| 7743 | mDebugApp = mOrigDebugApp = debugApp; |
| 7744 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 7745 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 7746 | // This happens before any activities are started, so we can |
| 7747 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 7748 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7749 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7750 | } |
| 7751 | } |
| 7752 | |
| 7753 | public boolean testIsSystemReady() { |
| 7754 | // no need to synchronize(this) just to read & return the value |
| 7755 | return mSystemReady; |
| 7756 | } |
| 7757 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7758 | private static File getCalledPreBootReceiversFile() { |
| 7759 | File dataDir = Environment.getDataDirectory(); |
| 7760 | File systemDir = new File(dataDir, "system"); |
| 7761 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 7762 | return fname; |
| 7763 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7764 | |
| 7765 | static final int LAST_DONE_VERSION = 10000; |
| 7766 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7767 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 7768 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 7769 | File file = getCalledPreBootReceiversFile(); |
| 7770 | FileInputStream fis = null; |
| 7771 | try { |
| 7772 | fis = new FileInputStream(file); |
| 7773 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7774 | int fvers = dis.readInt(); |
| 7775 | if (fvers == LAST_DONE_VERSION) { |
| 7776 | String vers = dis.readUTF(); |
| 7777 | String codename = dis.readUTF(); |
| 7778 | String build = dis.readUTF(); |
| 7779 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 7780 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 7781 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 7782 | int num = dis.readInt(); |
| 7783 | while (num > 0) { |
| 7784 | num--; |
| 7785 | String pkg = dis.readUTF(); |
| 7786 | String cls = dis.readUTF(); |
| 7787 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 7788 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7789 | } |
| 7790 | } |
| 7791 | } catch (FileNotFoundException e) { |
| 7792 | } catch (IOException e) { |
| 7793 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 7794 | } finally { |
| 7795 | if (fis != null) { |
| 7796 | try { |
| 7797 | fis.close(); |
| 7798 | } catch (IOException e) { |
| 7799 | } |
| 7800 | } |
| 7801 | } |
| 7802 | return lastDoneReceivers; |
| 7803 | } |
| 7804 | |
| 7805 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 7806 | File file = getCalledPreBootReceiversFile(); |
| 7807 | FileOutputStream fos = null; |
| 7808 | DataOutputStream dos = null; |
| 7809 | try { |
| 7810 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 7811 | fos = new FileOutputStream(file); |
| 7812 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7813 | dos.writeInt(LAST_DONE_VERSION); |
| 7814 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7815 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7816 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7817 | dos.writeInt(list.size()); |
| 7818 | for (int i=0; i<list.size(); i++) { |
| 7819 | dos.writeUTF(list.get(i).getPackageName()); |
| 7820 | dos.writeUTF(list.get(i).getClassName()); |
| 7821 | } |
| 7822 | } catch (IOException e) { |
| 7823 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 7824 | file.delete(); |
| 7825 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 7826 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7827 | if (dos != null) { |
| 7828 | try { |
| 7829 | dos.close(); |
| 7830 | } catch (IOException e) { |
| 7831 | // TODO Auto-generated catch block |
| 7832 | e.printStackTrace(); |
| 7833 | } |
| 7834 | } |
| 7835 | } |
| 7836 | } |
| 7837 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7838 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7839 | synchronized(this) { |
| 7840 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7841 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7842 | return; |
| 7843 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7844 | |
| 7845 | // Check to see if there are any update receivers to run. |
| 7846 | if (!mDidUpdate) { |
| 7847 | if (mWaitingUpdate) { |
| 7848 | return; |
| 7849 | } |
| 7850 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 7851 | List<ResolveInfo> ris = null; |
| 7852 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7853 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7854 | intent, null, 0, 0); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7855 | } catch (RemoteException e) { |
| 7856 | } |
| 7857 | if (ris != null) { |
| 7858 | for (int i=ris.size()-1; i>=0; i--) { |
| 7859 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 7860 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7861 | ris.remove(i); |
| 7862 | } |
| 7863 | } |
| 7864 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7865 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7866 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7867 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7868 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7869 | for (int i=0; i<ris.size(); i++) { |
| 7870 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7871 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7872 | if (lastDoneReceivers.contains(comp)) { |
| 7873 | ris.remove(i); |
| 7874 | i--; |
| 7875 | } |
| 7876 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 7877 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7878 | final int[] users = getUsersLocked(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7879 | for (int i=0; i<ris.size(); i++) { |
| 7880 | ActivityInfo ai = ris.get(i).activityInfo; |
| 7881 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7882 | doneReceivers.add(comp); |
| 7883 | intent.setComponent(comp); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7884 | for (int j=0; j<users.length; j++) { |
| 7885 | IIntentReceiver finisher = null; |
| 7886 | if (i == ris.size()-1 && j == users.length-1) { |
| 7887 | finisher = new IIntentReceiver.Stub() { |
| 7888 | public void performReceive(Intent intent, int resultCode, |
| 7889 | String data, Bundle extras, boolean ordered, |
| 7890 | boolean sticky, int sendingUser) { |
| 7891 | // The raw IIntentReceiver interface is called |
| 7892 | // with the AM lock held, so redispatch to |
| 7893 | // execute our code without the lock. |
| 7894 | mHandler.post(new Runnable() { |
| 7895 | public void run() { |
| 7896 | synchronized (ActivityManagerService.this) { |
| 7897 | mDidUpdate = true; |
| 7898 | } |
| 7899 | writeLastDonePreBootReceivers(doneReceivers); |
| 7900 | showBootMessage(mContext.getText( |
| 7901 | R.string.android_upgrading_complete), |
| 7902 | false); |
| 7903 | systemReady(goingCallback); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7904 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7905 | }); |
| 7906 | } |
| 7907 | }; |
| 7908 | } |
| 7909 | Slog.i(TAG, "Sending system update to " + intent.getComponent() |
| 7910 | + " for user " + users[j]); |
| 7911 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 7912 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 7913 | users[j]); |
| 7914 | if (finisher != null) { |
| 7915 | mWaitingUpdate = true; |
| 7916 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7917 | } |
| 7918 | } |
| 7919 | } |
| 7920 | if (mWaitingUpdate) { |
| 7921 | return; |
| 7922 | } |
| 7923 | mDidUpdate = true; |
| 7924 | } |
| 7925 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7926 | mSystemReady = true; |
| 7927 | if (!mStartRunning) { |
| 7928 | return; |
| 7929 | } |
| 7930 | } |
| 7931 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7932 | ArrayList<ProcessRecord> procsToKill = null; |
| 7933 | synchronized(mPidsSelfLocked) { |
| 7934 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7935 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7936 | if (!isAllowedWhileBooting(proc.info)){ |
| 7937 | if (procsToKill == null) { |
| 7938 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7939 | } |
| 7940 | procsToKill.add(proc); |
| 7941 | } |
| 7942 | } |
| 7943 | } |
| 7944 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7945 | synchronized(this) { |
| 7946 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7947 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7948 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7949 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7950 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7951 | } |
| 7952 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7953 | |
| 7954 | // Now that we have cleaned up any update processes, we |
| 7955 | // are ready to start launching real processes and know that |
| 7956 | // we won't trample on them any more. |
| 7957 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7958 | } |
| 7959 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7960 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7961 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7962 | SystemClock.uptimeMillis()); |
| 7963 | |
| 7964 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7965 | // Make sure we have no pre-ready processes sitting around. |
| 7966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7967 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7968 | ResolveInfo ri = mContext.getPackageManager() |
| 7969 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7970 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7971 | CharSequence errorMsg = null; |
| 7972 | if (ri != null) { |
| 7973 | ActivityInfo ai = ri.activityInfo; |
| 7974 | ApplicationInfo app = ai.applicationInfo; |
| 7975 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7976 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7977 | mTopData = null; |
| 7978 | mTopComponent = new ComponentName(app.packageName, |
| 7979 | ai.name); |
| 7980 | } else { |
| 7981 | errorMsg = mContext.getResources().getText( |
| 7982 | com.android.internal.R.string.factorytest_not_system); |
| 7983 | } |
| 7984 | } else { |
| 7985 | errorMsg = mContext.getResources().getText( |
| 7986 | com.android.internal.R.string.factorytest_no_action); |
| 7987 | } |
| 7988 | if (errorMsg != null) { |
| 7989 | mTopAction = null; |
| 7990 | mTopData = null; |
| 7991 | mTopComponent = null; |
| 7992 | Message msg = Message.obtain(); |
| 7993 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7994 | msg.getData().putCharSequence("msg", errorMsg); |
| 7995 | mHandler.sendMessage(msg); |
| 7996 | } |
| 7997 | } |
| 7998 | } |
| 7999 | |
| 8000 | retrieveSettings(); |
| 8001 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 8002 | if (goingCallback != null) goingCallback.run(); |
| 8003 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8004 | synchronized (this) { |
| 8005 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 8006 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8007 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 8008 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8009 | if (apps != null) { |
| 8010 | int N = apps.size(); |
| 8011 | int i; |
| 8012 | for (i=0; i<N; i++) { |
| 8013 | ApplicationInfo info |
| 8014 | = (ApplicationInfo)apps.get(i); |
| 8015 | if (info != null && |
| 8016 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8017 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8018 | } |
| 8019 | } |
| 8020 | } |
| 8021 | } catch (RemoteException ex) { |
| 8022 | // pm is in same process, this will never happen. |
| 8023 | } |
| 8024 | } |
| 8025 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 8026 | // Start up initial activity. |
| 8027 | mBooting = true; |
| 8028 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8029 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8030 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8031 | Message msg = Message.obtain(); |
| 8032 | msg.what = SHOW_UID_ERROR_MSG; |
| 8033 | mHandler.sendMessage(msg); |
| 8034 | } |
| 8035 | } catch (RemoteException e) { |
| 8036 | } |
| 8037 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 8038 | long ident = Binder.clearCallingIdentity(); |
| 8039 | try { |
| 8040 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 8041 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 8042 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 8043 | intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId); |
| 8044 | broadcastIntentLocked(null, null, intent, |
| 8045 | null, null, 0, null, null, null, |
| 8046 | false, false, MY_PID, Process.SYSTEM_UID, mCurrentUserId); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 8047 | intent = new Intent(Intent.ACTION_USER_STARTING); |
| 8048 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 8049 | intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId); |
| 8050 | broadcastIntentLocked(null, null, intent, |
| 8051 | null, new IIntentReceiver.Stub() { |
| 8052 | @Override |
| 8053 | public void performReceive(Intent intent, int resultCode, String data, |
| 8054 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) |
| 8055 | throws RemoteException { |
| 8056 | } |
| 8057 | }, 0, null, null, |
| 8058 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 8059 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 8060 | } finally { |
| 8061 | Binder.restoreCallingIdentity(ident); |
| 8062 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8063 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 8064 | sendUserSwitchBroadcastsLocked(-1, mCurrentUserId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8065 | } |
| 8066 | } |
| 8067 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8068 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8069 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8070 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8071 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8072 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8073 | startAppProblemLocked(app); |
| 8074 | app.stopFreezingAllLocked(); |
| 8075 | return handleAppCrashLocked(app); |
| 8076 | } |
| 8077 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8078 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8079 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8080 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8081 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8082 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 8083 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8084 | startAppProblemLocked(app); |
| 8085 | app.stopFreezingAllLocked(); |
| 8086 | } |
| 8087 | |
| 8088 | /** |
| 8089 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 8090 | * |
| 8091 | * @param app The ProcessRecord in which the error occurred. |
| 8092 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 8093 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8094 | * @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] | 8095 | * @param shortMsg Short message describing the crash. |
| 8096 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8097 | * @param stackTrace Full crash stack trace, may be null. |
| 8098 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8099 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 8100 | */ |
| 8101 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8102 | 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] | 8103 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8105 | report.condition = condition; |
| 8106 | report.processName = app.processName; |
| 8107 | report.pid = app.pid; |
| 8108 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8109 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8110 | report.shortMsg = shortMsg; |
| 8111 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8112 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8113 | |
| 8114 | return report; |
| 8115 | } |
| 8116 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8117 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8118 | synchronized (this) { |
| 8119 | app.crashing = false; |
| 8120 | app.crashingReport = null; |
| 8121 | app.notResponding = false; |
| 8122 | app.notRespondingReport = null; |
| 8123 | if (app.anrDialog == fromDialog) { |
| 8124 | app.anrDialog = null; |
| 8125 | } |
| 8126 | if (app.waitDialog == fromDialog) { |
| 8127 | app.waitDialog = null; |
| 8128 | } |
| 8129 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8130 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 8131 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8132 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 8133 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 8134 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8135 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8136 | } |
| 8137 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8138 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8139 | private boolean handleAppCrashLocked(ProcessRecord app) { |
Mike Lockwood | 86548c4 | 2011-09-13 17:21:46 -0400 | [diff] [blame] | 8140 | if (mHeadless) { |
| 8141 | Log.e(TAG, "handleAppCrashLocked: " + app.processName); |
| 8142 | return false; |
| 8143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8144 | long now = SystemClock.uptimeMillis(); |
| 8145 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8146 | Long crashTime; |
| 8147 | if (!app.isolated) { |
| 8148 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 8149 | } else { |
| 8150 | crashTime = null; |
| 8151 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8152 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8153 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8154 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8155 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 8156 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8157 | app.userId, app.info.processName, app.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8158 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 8159 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8160 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8161 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8162 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8163 | r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
| 8164 | null, "crashed", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8165 | } |
| 8166 | } |
| 8167 | if (!app.persistent) { |
| 8168 | // We don't want to start this process again until the user |
| 8169 | // explicitly does so... but for persistent process, we really |
| 8170 | // need to keep it running. If a persistent process is actually |
| 8171 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8172 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.userId, app.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8173 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8174 | if (!app.isolated) { |
| 8175 | // XXX We don't have a way to mark isolated processes |
| 8176 | // as bad, since they don't have a peristent identity. |
| 8177 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 8178 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 8179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8180 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8181 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 8182 | // Don't let services in this process be restarted and potentially |
| 8183 | // annoy the user repeatedly. Unless it is persistent, since those |
| 8184 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 8185 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8186 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8187 | return false; |
| 8188 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8189 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8190 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8191 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Kevin Hester-Chow | d87a9be | 2012-03-05 08:01:00 -0800 | [diff] [blame] | 8192 | if (r != null && r.app == app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8193 | // If the top running activity is from this crashing |
| 8194 | // process, then terminate it to avoid getting in a loop. |
| 8195 | Slog.w(TAG, " Force finishing activity " |
| 8196 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 8197 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8198 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8199 | Activity.RESULT_CANCELED, null, "crashed", false); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 8200 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8201 | // stopped, to avoid a situation where one will get |
| 8202 | // re-start our crashing activity once it gets resumed again. |
| 8203 | index--; |
| 8204 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8205 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8206 | if (r.state == ActivityState.RESUMED |
| 8207 | || r.state == ActivityState.PAUSING |
| 8208 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 8209 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8210 | Slog.w(TAG, " Force finishing activity " |
| 8211 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8212 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8213 | Activity.RESULT_CANCELED, null, "crashed", false); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8214 | } |
| 8215 | } |
| 8216 | } |
| 8217 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8218 | } |
| 8219 | |
| 8220 | // Bump up the crash count of any services currently running in the proc. |
| 8221 | if (app.services.size() != 0) { |
| 8222 | // Any services running in the application need to be placed |
| 8223 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8224 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8225 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8226 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8227 | sr.crashCount++; |
| 8228 | } |
| 8229 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8230 | |
| 8231 | // If the crashing process is what we consider to be the "home process" and it has been |
| 8232 | // replaced by a third-party app, clear the package preferred activities from packages |
| 8233 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 8234 | // from blocking the user to manually clear the list. |
| 8235 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 8236 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 8237 | Iterator it = mHomeProcess.activities.iterator(); |
| 8238 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 8239 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8240 | if (r.isHomeActivity) { |
| 8241 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 8242 | try { |
| 8243 | ActivityThread.getPackageManager() |
| 8244 | .clearPackagePreferredActivities(r.packageName); |
| 8245 | } catch (RemoteException c) { |
| 8246 | // pm is in same process, this will never happen. |
| 8247 | } |
| 8248 | } |
| 8249 | } |
| 8250 | } |
| 8251 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8252 | if (!app.isolated) { |
| 8253 | // XXX Can't keep track of crash times for isolated processes, |
| 8254 | // because they don't have a perisistent identity. |
| 8255 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 8256 | } |
| 8257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8258 | return true; |
| 8259 | } |
| 8260 | |
| 8261 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 8262 | if (app.userId == mCurrentUserId) { |
| 8263 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 8264 | mContext, app.info.packageName, app.info.flags); |
| 8265 | } else { |
| 8266 | // If this app is not running under the current user, then we |
| 8267 | // can't give it a report button because that would require |
| 8268 | // launching the report UI under a different user. |
| 8269 | app.errorReportReceiver = null; |
| 8270 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8271 | skipCurrentReceiverLocked(app); |
| 8272 | } |
| 8273 | |
| 8274 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 8275 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 8276 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8277 | } |
| 8278 | } |
| 8279 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8280 | /** |
| 8281 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 8282 | * The application process will exit immediately after this call returns. |
| 8283 | * @param app object of the crashing app, null for the system server |
| 8284 | * @param crashInfo describing the exception |
| 8285 | */ |
| 8286 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8287 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8288 | final String processName = app == null ? "system_server" |
| 8289 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8290 | |
| 8291 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8292 | UserHandle.getUserId(Binder.getCallingUid()), processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8293 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8294 | crashInfo.exceptionClassName, |
| 8295 | crashInfo.exceptionMessage, |
| 8296 | crashInfo.throwFileName, |
| 8297 | crashInfo.throwLineNumber); |
| 8298 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8299 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8300 | |
| 8301 | crashApplication(r, crashInfo); |
| 8302 | } |
| 8303 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8304 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8305 | IBinder app, |
| 8306 | int violationMask, |
| 8307 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8308 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 8309 | if (r == null) { |
| 8310 | return; |
| 8311 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8312 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8313 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 8314 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8315 | boolean logIt = true; |
| 8316 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 8317 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 8318 | logIt = false; |
| 8319 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8320 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8321 | // the relative pain numbers, without logging all |
| 8322 | // the stack traces repeatedly. We'd want to do |
| 8323 | // likewise in the client code, which also does |
| 8324 | // dup suppression, before the Binder call. |
| 8325 | } else { |
| 8326 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 8327 | mAlreadyLoggedViolatedStacks.clear(); |
| 8328 | } |
| 8329 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 8330 | } |
| 8331 | } |
| 8332 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8333 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8334 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8335 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8336 | |
| 8337 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 8338 | AppErrorResult result = new AppErrorResult(); |
| 8339 | synchronized (this) { |
| 8340 | final long origId = Binder.clearCallingIdentity(); |
| 8341 | |
| 8342 | Message msg = Message.obtain(); |
| 8343 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 8344 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 8345 | data.put("result", result); |
| 8346 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8347 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8348 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8349 | msg.obj = data; |
| 8350 | mHandler.sendMessage(msg); |
| 8351 | |
| 8352 | Binder.restoreCallingIdentity(origId); |
| 8353 | } |
| 8354 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 8355 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8356 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8357 | } |
| 8358 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8359 | // Depending on the policy in effect, there could be a bunch of |
| 8360 | // these in quick succession so we try to batch these together to |
| 8361 | // minimize disk writes, number of dropbox entries, and maximize |
| 8362 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8363 | private void logStrictModeViolationToDropBox( |
| 8364 | ProcessRecord process, |
| 8365 | StrictMode.ViolationInfo info) { |
| 8366 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8367 | return; |
| 8368 | } |
| 8369 | final boolean isSystemApp = process == null || |
| 8370 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 8371 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8372 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8373 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 8374 | final DropBoxManager dbox = (DropBoxManager) |
| 8375 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8376 | |
| 8377 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8378 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8379 | |
| 8380 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8381 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8382 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 8383 | synchronized (sb) { |
| 8384 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8385 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8386 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 8387 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8388 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 8389 | if (info.violationNumThisLoop != 0) { |
| 8390 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 8391 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 8392 | if (info.numAnimationsRunning != 0) { |
| 8393 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 8394 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 8395 | if (info.broadcastIntentAction != null) { |
| 8396 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 8397 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8398 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8399 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8400 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 8401 | if (info.numInstances != -1) { |
| 8402 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 8403 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8404 | if (info.tags != null) { |
| 8405 | for (String tag : info.tags) { |
| 8406 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 8407 | } |
| 8408 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8409 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8410 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 8411 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8412 | } |
| 8413 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8414 | |
| 8415 | // Only buffer up to ~64k. Various logging bits truncate |
| 8416 | // things at 128k. |
| 8417 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8418 | } |
| 8419 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8420 | // Flush immediately if the buffer's grown too large, or this |
| 8421 | // is a non-system app. Non-system apps are isolated with a |
| 8422 | // different tag & policy and not batched. |
| 8423 | // |
| 8424 | // Batching is useful during internal testing with |
| 8425 | // StrictMode settings turned up high. Without batching, |
| 8426 | // thousands of separate files could be created on boot. |
| 8427 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8428 | new Thread("Error dump: " + dropboxTag) { |
| 8429 | @Override |
| 8430 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8431 | String report; |
| 8432 | synchronized (sb) { |
| 8433 | report = sb.toString(); |
| 8434 | sb.delete(0, sb.length()); |
| 8435 | sb.trimToSize(); |
| 8436 | } |
| 8437 | if (report.length() != 0) { |
| 8438 | dbox.addText(dropboxTag, report); |
| 8439 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8440 | } |
| 8441 | }.start(); |
| 8442 | return; |
| 8443 | } |
| 8444 | |
| 8445 | // System app batching: |
| 8446 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8447 | // An existing dropbox-writing thread is outstanding, so |
| 8448 | // we don't need to start it up. The existing thread will |
| 8449 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8450 | return; |
| 8451 | } |
| 8452 | |
| 8453 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 8454 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8455 | new Thread("Error dump: " + dropboxTag) { |
| 8456 | @Override |
| 8457 | public void run() { |
| 8458 | // 5 second sleep to let stacks arrive and be batched together |
| 8459 | try { |
| 8460 | Thread.sleep(5000); // 5 seconds |
| 8461 | } catch (InterruptedException e) {} |
| 8462 | |
| 8463 | String errorReport; |
| 8464 | synchronized (mStrictModeBuffer) { |
| 8465 | errorReport = mStrictModeBuffer.toString(); |
| 8466 | if (errorReport.length() == 0) { |
| 8467 | return; |
| 8468 | } |
| 8469 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 8470 | mStrictModeBuffer.trimToSize(); |
| 8471 | } |
| 8472 | dbox.addText(dropboxTag, errorReport); |
| 8473 | } |
| 8474 | }.start(); |
| 8475 | } |
| 8476 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8477 | /** |
| 8478 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 8479 | * @param app object of the crashing app, null for the system server |
| 8480 | * @param tag reported by the caller |
| 8481 | * @param crashInfo describing the context of the error |
| 8482 | * @return true if the process should exit immediately (WTF is fatal) |
| 8483 | */ |
| 8484 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8485 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8486 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8487 | final String processName = app == null ? "system_server" |
| 8488 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8489 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8490 | EventLog.writeEvent(EventLogTags.AM_WTF, |
| 8491 | UserHandle.getUserId(Binder.getCallingUid()), Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8492 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8493 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8494 | tag, crashInfo.exceptionMessage); |
| 8495 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8496 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8497 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 8498 | if (r != null && r.pid != Process.myPid() && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 8499 | Settings.Global.getInt(mContext.getContentResolver(), |
| 8500 | Settings.Global.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8501 | crashApplication(r, crashInfo); |
| 8502 | return true; |
| 8503 | } else { |
| 8504 | return false; |
| 8505 | } |
| 8506 | } |
| 8507 | |
| 8508 | /** |
| 8509 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 8510 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 8511 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8512 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8513 | if (app == null) { |
| 8514 | return null; |
| 8515 | } |
| 8516 | |
| 8517 | synchronized (this) { |
| 8518 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 8519 | final int NA = apps.size(); |
| 8520 | for (int ia=0; ia<NA; ia++) { |
| 8521 | ProcessRecord p = apps.valueAt(ia); |
| 8522 | if (p.thread != null && p.thread.asBinder() == app) { |
| 8523 | return p; |
| 8524 | } |
| 8525 | } |
| 8526 | } |
| 8527 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8528 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 8529 | + " from pid=" + Binder.getCallingPid() |
| 8530 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8531 | return null; |
| 8532 | } |
| 8533 | } |
| 8534 | |
| 8535 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8536 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 8537 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8538 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8539 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 8540 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8541 | // Watchdog thread ends up invoking this function (with |
| 8542 | // a null ProcessRecord) to add the stack file to dropbox. |
| 8543 | // Do not acquire a lock on this (am) in such cases, as it |
| 8544 | // could cause a potential deadlock, if and when watchdog |
| 8545 | // is invoked due to unavailability of lock on am and it |
| 8546 | // would prevent watchdog from killing system_server. |
| 8547 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8548 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8549 | return; |
| 8550 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 8551 | // Note: ProcessRecord 'process' is guarded by the service |
| 8552 | // instance. (notably process.pkgList, which could otherwise change |
| 8553 | // concurrently during execution of this method) |
| 8554 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8555 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8556 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8557 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8558 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 8559 | for (String pkg : process.pkgList) { |
| 8560 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8561 | try { |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 8562 | PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId()); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8563 | if (pi != null) { |
| 8564 | sb.append(" v").append(pi.versionCode); |
| 8565 | if (pi.versionName != null) { |
| 8566 | sb.append(" (").append(pi.versionName).append(")"); |
| 8567 | } |
| 8568 | } |
| 8569 | } catch (RemoteException e) { |
| 8570 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8571 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8572 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8573 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8574 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8575 | } |
| 8576 | |
| 8577 | private static String processClass(ProcessRecord process) { |
| 8578 | if (process == null || process.pid == MY_PID) { |
| 8579 | return "system_server"; |
| 8580 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 8581 | return "system_app"; |
| 8582 | } else { |
| 8583 | return "data_app"; |
| 8584 | } |
| 8585 | } |
| 8586 | |
| 8587 | /** |
| 8588 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 8589 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 8590 | * @param process which caused the error, null means the system server |
| 8591 | * @param activity which triggered the error, null if unknown |
| 8592 | * @param parent activity related to the error, null if unknown |
| 8593 | * @param subject line related to the error, null if absent |
| 8594 | * @param report in long form describing the error, null if absent |
| 8595 | * @param logFile to include in the report, null if none |
| 8596 | * @param crashInfo giving an application stack trace, null if absent |
| 8597 | */ |
| 8598 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8599 | ProcessRecord process, String processName, ActivityRecord activity, |
| 8600 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8601 | final String report, final File logFile, |
| 8602 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 8603 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 8604 | // otherwise the watchdog may be prevented from resetting the system. |
| 8605 | |
| 8606 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 8607 | final DropBoxManager dbox = (DropBoxManager) |
| 8608 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8609 | |
| 8610 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8611 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8612 | |
| 8613 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8614 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8615 | if (activity != null) { |
| 8616 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 8617 | } |
| 8618 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 8619 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 8620 | } |
| 8621 | if (parent != null && parent != activity) { |
| 8622 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 8623 | } |
| 8624 | if (subject != null) { |
| 8625 | sb.append("Subject: ").append(subject).append("\n"); |
| 8626 | } |
| 8627 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 8628 | if (Debug.isDebuggerConnected()) { |
| 8629 | sb.append("Debugger: Connected\n"); |
| 8630 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8631 | sb.append("\n"); |
| 8632 | |
| 8633 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 8634 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8635 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 8636 | @Override |
| 8637 | public void run() { |
| 8638 | if (report != null) { |
| 8639 | sb.append(report); |
| 8640 | } |
| 8641 | if (logFile != null) { |
| 8642 | try { |
| 8643 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 8644 | } catch (IOException e) { |
| 8645 | Slog.e(TAG, "Error reading " + logFile, e); |
| 8646 | } |
| 8647 | } |
| 8648 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 8649 | sb.append(crashInfo.stackTrace); |
| 8650 | } |
| 8651 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 8652 | String setting = Settings.Global.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 8653 | int lines = Settings.Global.getInt(mContext.getContentResolver(), setting, 0); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8654 | if (lines > 0) { |
| 8655 | sb.append("\n"); |
| 8656 | |
| 8657 | // Merge several logcat streams, and take the last N lines |
| 8658 | InputStreamReader input = null; |
| 8659 | try { |
| 8660 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 8661 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 8662 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 8663 | |
| 8664 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 8665 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 8666 | input = new InputStreamReader(logcat.getInputStream()); |
| 8667 | |
| 8668 | int num; |
| 8669 | char[] buf = new char[8192]; |
| 8670 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 8671 | } catch (IOException e) { |
| 8672 | Slog.e(TAG, "Error running logcat", e); |
| 8673 | } finally { |
| 8674 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 8675 | } |
| 8676 | } |
| 8677 | |
| 8678 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8679 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8680 | }; |
| 8681 | |
Dianne Hackborn | 56385cc | 2012-04-30 15:07:47 -0700 | [diff] [blame] | 8682 | if (process == null) { |
| 8683 | // If process is null, we are being called from some internal code |
| 8684 | // and may be about to die -- run this synchronously. |
| 8685 | worker.run(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8686 | } else { |
| 8687 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8688 | } |
| 8689 | } |
| 8690 | |
| 8691 | /** |
| 8692 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 8693 | * Deal with edge cases (intercepts from instrumented applications, |
| 8694 | * ActivityController, error intent receivers, that sort of thing). |
| 8695 | * @param r the application crashing |
| 8696 | * @param crashInfo describing the failure |
| 8697 | */ |
| 8698 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8699 | long timeMillis = System.currentTimeMillis(); |
| 8700 | String shortMsg = crashInfo.exceptionClassName; |
| 8701 | String longMsg = crashInfo.exceptionMessage; |
| 8702 | String stackTrace = crashInfo.stackTrace; |
| 8703 | if (shortMsg != null && longMsg != null) { |
| 8704 | longMsg = shortMsg + ": " + longMsg; |
| 8705 | } else if (shortMsg != null) { |
| 8706 | longMsg = shortMsg; |
| 8707 | } |
| 8708 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8709 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8710 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8711 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8712 | try { |
| 8713 | String name = r != null ? r.processName : null; |
| 8714 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8715 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8716 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8717 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8718 | + " at watcher's request"); |
| 8719 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8720 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8721 | } |
| 8722 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8723 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8724 | } |
| 8725 | } |
| 8726 | |
| 8727 | final long origId = Binder.clearCallingIdentity(); |
| 8728 | |
| 8729 | // If this process is running instrumentation, finish it. |
| 8730 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8731 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8732 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8733 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 8734 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8735 | Bundle info = new Bundle(); |
| 8736 | info.putString("shortMsg", shortMsg); |
| 8737 | info.putString("longMsg", longMsg); |
| 8738 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 8739 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8740 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8741 | } |
| 8742 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8743 | // If we can't identify the process or it's already exceeded its crash quota, |
| 8744 | // quit right away without showing a crash dialog. |
| 8745 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8746 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8747 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8748 | } |
| 8749 | |
| 8750 | Message msg = Message.obtain(); |
| 8751 | msg.what = SHOW_ERROR_MSG; |
| 8752 | HashMap data = new HashMap(); |
| 8753 | data.put("result", result); |
| 8754 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8755 | msg.obj = data; |
| 8756 | mHandler.sendMessage(msg); |
| 8757 | |
| 8758 | Binder.restoreCallingIdentity(origId); |
| 8759 | } |
| 8760 | |
| 8761 | int res = result.get(); |
| 8762 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8763 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8764 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8765 | if (r != null && !r.isolated) { |
| 8766 | // XXX Can't keep track of crash time for isolated processes, |
| 8767 | // since they don't have a persistent identity. |
| 8768 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8769 | SystemClock.uptimeMillis()); |
| 8770 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8771 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8772 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8773 | } |
| 8774 | } |
| 8775 | |
| 8776 | if (appErrorIntent != null) { |
| 8777 | try { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 8778 | mContext.startActivityAsUser(appErrorIntent, new UserHandle(r.userId)); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8779 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8780 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8781 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8782 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8783 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8784 | |
| 8785 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 8786 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 8787 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8788 | if (report == null) { |
| 8789 | return null; |
| 8790 | } |
| 8791 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 8792 | result.setComponent(r.errorReportReceiver); |
| 8793 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 8794 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 8795 | return result; |
| 8796 | } |
| 8797 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8798 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 8799 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8800 | if (r.errorReportReceiver == null) { |
| 8801 | return null; |
| 8802 | } |
| 8803 | |
| 8804 | if (!r.crashing && !r.notResponding) { |
| 8805 | return null; |
| 8806 | } |
| 8807 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8808 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 8809 | report.packageName = r.info.packageName; |
| 8810 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 8811 | report.processName = r.processName; |
| 8812 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 8813 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8814 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8815 | if (r.crashing) { |
| 8816 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 8817 | report.crashInfo = crashInfo; |
| 8818 | } else if (r.notResponding) { |
| 8819 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 8820 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8821 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8822 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 8823 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 8824 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8825 | } |
| 8826 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8827 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8828 | } |
| 8829 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8830 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8831 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8832 | // assume our apps are happy - lazy create the list |
| 8833 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 8834 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8835 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8836 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8837 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8838 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 8839 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8840 | synchronized (this) { |
| 8841 | |
| 8842 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8843 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8844 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8845 | if (!allUsers && app.userId != userId) { |
| 8846 | continue; |
| 8847 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8848 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 8849 | // This one's in trouble, so we'll generate a report for it |
| 8850 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 8851 | ActivityManager.ProcessErrorStateInfo report = null; |
| 8852 | if (app.crashing) { |
| 8853 | report = app.crashingReport; |
| 8854 | } else if (app.notResponding) { |
| 8855 | report = app.notRespondingReport; |
| 8856 | } |
| 8857 | |
| 8858 | if (report != null) { |
| 8859 | if (errList == null) { |
| 8860 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 8861 | } |
| 8862 | errList.add(report); |
| 8863 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8864 | 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] | 8865 | " crashing = " + app.crashing + |
| 8866 | " notResponding = " + app.notResponding); |
| 8867 | } |
| 8868 | } |
| 8869 | } |
| 8870 | } |
| 8871 | |
| 8872 | return errList; |
| 8873 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8874 | |
| 8875 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8876 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8877 | if (currApp != null) { |
| 8878 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 8879 | } |
| 8880 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8881 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 8882 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8883 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 8884 | if (currApp != null) { |
| 8885 | currApp.lru = 0; |
| 8886 | } |
| 8887 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8888 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8889 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 8890 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8891 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 8892 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8893 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 8894 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8895 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 8896 | } else { |
| 8897 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 8898 | } |
| 8899 | } |
| 8900 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8901 | private void fillInProcMemInfo(ProcessRecord app, |
| 8902 | ActivityManager.RunningAppProcessInfo outInfo) { |
| 8903 | outInfo.pid = app.pid; |
| 8904 | outInfo.uid = app.info.uid; |
| 8905 | if (mHeavyWeightProcess == app) { |
| 8906 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
| 8907 | } |
| 8908 | if (app.persistent) { |
| 8909 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 8910 | } |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8911 | if (app.hasActivities) { |
| 8912 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES; |
| 8913 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8914 | outInfo.lastTrimLevel = app.trimMemoryLevel; |
| 8915 | int adj = app.curAdj; |
| 8916 | outInfo.importance = oomAdjToImportance(adj, outInfo); |
| 8917 | outInfo.importanceReasonCode = app.adjTypeCode; |
| 8918 | } |
| 8919 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8920 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8921 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8922 | // Lazy instantiation of list |
| 8923 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8924 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8925 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8926 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8927 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8928 | synchronized (this) { |
| 8929 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8930 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8931 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8932 | if (!allUsers && app.userId != userId) { |
| 8933 | continue; |
| 8934 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8935 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 8936 | // Generate process state info for running application |
| 8937 | ActivityManager.RunningAppProcessInfo currApp = |
| 8938 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 8939 | app.pid, app.getPackageList()); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8940 | fillInProcMemInfo(app, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8941 | if (app.adjSource instanceof ProcessRecord) { |
| 8942 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8943 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 8944 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8945 | } else if (app.adjSource instanceof ActivityRecord) { |
| 8946 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8947 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 8948 | } |
| 8949 | if (app.adjTarget instanceof ComponentName) { |
| 8950 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 8951 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8952 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8953 | // + " lru=" + currApp.lru); |
| 8954 | if (runList == null) { |
| 8955 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 8956 | } |
| 8957 | runList.add(currApp); |
| 8958 | } |
| 8959 | } |
| 8960 | } |
| 8961 | return runList; |
| 8962 | } |
| 8963 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8964 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8965 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8966 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8967 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8968 | if (runningApps != null && runningApps.size() > 0) { |
| 8969 | Set<String> extList = new HashSet<String>(); |
| 8970 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8971 | if (app.pkgList != null) { |
| 8972 | for (String pkg : app.pkgList) { |
| 8973 | extList.add(pkg); |
| 8974 | } |
| 8975 | } |
| 8976 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8977 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8978 | for (String pkg : extList) { |
| 8979 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 8980 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId()); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8981 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8982 | retList.add(info); |
| 8983 | } |
| 8984 | } catch (RemoteException e) { |
| 8985 | } |
| 8986 | } |
| 8987 | } |
| 8988 | return retList; |
| 8989 | } |
| 8990 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8991 | @Override |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8992 | public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) { |
| 8993 | enforceNotIsolatedCaller("getMyMemoryState"); |
| 8994 | synchronized (this) { |
| 8995 | ProcessRecord proc; |
| 8996 | synchronized (mPidsSelfLocked) { |
| 8997 | proc = mPidsSelfLocked.get(Binder.getCallingPid()); |
| 8998 | } |
| 8999 | fillInProcMemInfo(proc, outInfo); |
| 9000 | } |
| 9001 | } |
| 9002 | |
| 9003 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9004 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9005 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 9006 | != PackageManager.PERMISSION_GRANTED) { |
| 9007 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 9008 | + Binder.getCallingPid() |
| 9009 | + ", uid=" + Binder.getCallingUid() |
| 9010 | + " without permission " |
| 9011 | + android.Manifest.permission.DUMP); |
| 9012 | return; |
| 9013 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9014 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9015 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9016 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9017 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9018 | |
| 9019 | int opti = 0; |
| 9020 | while (opti < args.length) { |
| 9021 | String opt = args[opti]; |
| 9022 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 9023 | break; |
| 9024 | } |
| 9025 | opti++; |
| 9026 | if ("-a".equals(opt)) { |
| 9027 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9028 | } else if ("-c".equals(opt)) { |
| 9029 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9030 | } else if ("-h".equals(opt)) { |
| 9031 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9032 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9033 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9034 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 9035 | pw.println(" b[roadcasts] [PACKAGE_NAME] [history [-s]]: broadcast state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9036 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 9037 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9038 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9039 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9040 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9041 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9042 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9043 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9044 | pw.println(" all: dump all activities"); |
| 9045 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9046 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9047 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 9048 | pw.println(" a partial substring in a component name, a"); |
| 9049 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9050 | pw.println(" -a: include all available server state."); |
| 9051 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9052 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9053 | } else { |
| 9054 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9055 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9056 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9057 | |
| 9058 | long origId = Binder.clearCallingIdentity(); |
| 9059 | boolean more = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9060 | // Is the caller requesting to dump a particular piece of data? |
| 9061 | if (opti < args.length) { |
| 9062 | String cmd = args[opti]; |
| 9063 | opti++; |
| 9064 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 9065 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9066 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9067 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9068 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9069 | String[] newArgs; |
| 9070 | String name; |
| 9071 | if (opti >= args.length) { |
| 9072 | name = null; |
| 9073 | newArgs = EMPTY_STRING_ARRAY; |
| 9074 | } else { |
| 9075 | name = args[opti]; |
| 9076 | opti++; |
| 9077 | newArgs = new String[args.length - opti]; |
| 9078 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9079 | args.length - opti); |
| 9080 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9081 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9082 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9083 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9084 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9085 | String[] newArgs; |
| 9086 | String name; |
| 9087 | if (opti >= args.length) { |
| 9088 | name = null; |
| 9089 | newArgs = EMPTY_STRING_ARRAY; |
| 9090 | } else { |
| 9091 | name = args[opti]; |
| 9092 | opti++; |
| 9093 | newArgs = new String[args.length - opti]; |
| 9094 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9095 | args.length - opti); |
| 9096 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9097 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9098 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9099 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9100 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9101 | String[] newArgs; |
| 9102 | String name; |
| 9103 | if (opti >= args.length) { |
| 9104 | name = null; |
| 9105 | newArgs = EMPTY_STRING_ARRAY; |
| 9106 | } else { |
| 9107 | name = args[opti]; |
| 9108 | opti++; |
| 9109 | newArgs = new String[args.length - opti]; |
| 9110 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9111 | args.length - opti); |
| 9112 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9113 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9114 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9115 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9116 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 9117 | synchronized (this) { |
| 9118 | dumpOomLocked(fd, pw, args, opti, true); |
| 9119 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9120 | } else if ("provider".equals(cmd)) { |
| 9121 | String[] newArgs; |
| 9122 | String name; |
| 9123 | if (opti >= args.length) { |
| 9124 | name = null; |
| 9125 | newArgs = EMPTY_STRING_ARRAY; |
| 9126 | } else { |
| 9127 | name = args[opti]; |
| 9128 | opti++; |
| 9129 | newArgs = new String[args.length - opti]; |
| 9130 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9131 | } |
| 9132 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 9133 | pw.println("No providers match: " + name); |
| 9134 | pw.println("Use -h for help."); |
| 9135 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9136 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 9137 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9138 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9139 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9140 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9141 | String[] newArgs; |
| 9142 | String name; |
| 9143 | if (opti >= args.length) { |
| 9144 | name = null; |
| 9145 | newArgs = EMPTY_STRING_ARRAY; |
| 9146 | } else { |
| 9147 | name = args[opti]; |
| 9148 | opti++; |
| 9149 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9150 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9151 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9152 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9153 | if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9154 | pw.println("No services match: " + name); |
| 9155 | pw.println("Use -h for help."); |
| 9156 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9157 | } else if ("package".equals(cmd)) { |
| 9158 | String[] newArgs; |
| 9159 | if (opti >= args.length) { |
| 9160 | pw.println("package: no package name specified"); |
| 9161 | pw.println("Use -h for help."); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9162 | } else { |
| 9163 | dumpPackage = args[opti]; |
| 9164 | opti++; |
| 9165 | newArgs = new String[args.length - opti]; |
| 9166 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9167 | args.length - opti); |
| 9168 | args = newArgs; |
| 9169 | opti = 0; |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9170 | more = true; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9171 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9172 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 9173 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9174 | mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9175 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9176 | } else { |
| 9177 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9178 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 9179 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 9180 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9181 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9182 | } |
| 9183 | if (!more) { |
| 9184 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9185 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9186 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9187 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9188 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9189 | // No piece of data specified, dump everything. |
| 9190 | synchronized (this) { |
| 9191 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9192 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9193 | if (needSep) { |
| 9194 | pw.println(" "); |
| 9195 | } |
| 9196 | if (dumpAll) { |
| 9197 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9198 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9199 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9200 | if (needSep) { |
| 9201 | pw.println(" "); |
| 9202 | } |
| 9203 | if (dumpAll) { |
| 9204 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9205 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9206 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9207 | if (needSep) { |
| 9208 | pw.println(" "); |
| 9209 | } |
| 9210 | if (dumpAll) { |
| 9211 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9212 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9213 | needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9214 | if (needSep) { |
| 9215 | pw.println(" "); |
| 9216 | } |
| 9217 | if (dumpAll) { |
| 9218 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9219 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9220 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9221 | if (needSep) { |
| 9222 | pw.println(" "); |
| 9223 | } |
| 9224 | if (dumpAll) { |
| 9225 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9226 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9227 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9228 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9229 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9230 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9231 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9232 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9233 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9234 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 9235 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9236 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 9237 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9238 | pw.println(" "); |
| 9239 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9240 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 9241 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9242 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9243 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9244 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9245 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9246 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9247 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9248 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9249 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9250 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9251 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9252 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9253 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9254 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 9255 | pw.println(" "); |
| 9256 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9257 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9258 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9259 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9260 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9261 | pw.println(" "); |
| 9262 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9263 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9264 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9265 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9266 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9267 | pw.println(" "); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 9268 | if (mMainStack.mPausingActivity != null) { |
| 9269 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9270 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9271 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9272 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9273 | if (dumpAll) { |
| 9274 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 9275 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9276 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 9277 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9279 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9280 | if (mRecentTasks.size() > 0) { |
| 9281 | pw.println(); |
| 9282 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9283 | |
| 9284 | final int N = mRecentTasks.size(); |
| 9285 | for (int i=0; i<N; i++) { |
| 9286 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9287 | if (dumpPackage != null) { |
| 9288 | if (tr.realActivity == null || |
| 9289 | !dumpPackage.equals(tr.realActivity)) { |
| 9290 | continue; |
| 9291 | } |
| 9292 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9293 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 9294 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9295 | if (dumpAll) { |
| 9296 | mRecentTasks.get(i).dump(pw, " "); |
| 9297 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9298 | } |
| 9299 | } |
| 9300 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9301 | if (dumpAll) { |
| 9302 | pw.println(" "); |
| 9303 | pw.println(" mCurTask: " + mCurTask); |
| 9304 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9305 | |
| 9306 | return true; |
| 9307 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9308 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9309 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9310 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9311 | boolean needSep = false; |
| 9312 | int numPers = 0; |
| 9313 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9314 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 9315 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9316 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9317 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 9318 | final int NA = procs.size(); |
| 9319 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9320 | ProcessRecord r = procs.valueAt(ia); |
| 9321 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9322 | continue; |
| 9323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9324 | if (!needSep) { |
| 9325 | pw.println(" All known processes:"); |
| 9326 | needSep = true; |
| 9327 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9328 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 9329 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 9330 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9331 | r.dump(pw, " "); |
| 9332 | if (r.persistent) { |
| 9333 | numPers++; |
| 9334 | } |
| 9335 | } |
| 9336 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9337 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 9338 | |
| 9339 | if (mIsolatedProcesses.size() > 0) { |
| 9340 | if (needSep) pw.println(" "); |
| 9341 | needSep = true; |
| 9342 | pw.println(" Isolated process list (sorted by uid):"); |
| 9343 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 9344 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 9345 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9346 | continue; |
| 9347 | } |
| 9348 | pw.println(String.format("%sIsolated #%2d: %s", |
| 9349 | " ", i, r.toString())); |
| 9350 | } |
| 9351 | } |
| 9352 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9353 | if (mLruProcesses.size() > 0) { |
| 9354 | if (needSep) pw.println(" "); |
| 9355 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9356 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9357 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9358 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9359 | needSep = true; |
| 9360 | } |
| 9361 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9362 | if (dumpAll) { |
| 9363 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9364 | boolean printed = false; |
| 9365 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 9366 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 9367 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9368 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9369 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9370 | if (!printed) { |
| 9371 | if (needSep) pw.println(" "); |
| 9372 | needSep = true; |
| 9373 | pw.println(" PID mappings:"); |
| 9374 | printed = true; |
| 9375 | } |
| 9376 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 9377 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9378 | } |
| 9379 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9380 | } |
| 9381 | |
| 9382 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9383 | synchronized (mPidsSelfLocked) { |
| 9384 | boolean printed = false; |
| 9385 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 9386 | ProcessRecord r = mPidsSelfLocked.get( |
| 9387 | mForegroundProcesses.valueAt(i).pid); |
| 9388 | if (dumpPackage != null && (r == null |
| 9389 | || !dumpPackage.equals(r.info.packageName))) { |
| 9390 | continue; |
| 9391 | } |
| 9392 | if (!printed) { |
| 9393 | if (needSep) pw.println(" "); |
| 9394 | needSep = true; |
| 9395 | pw.println(" Foreground Processes:"); |
| 9396 | printed = true; |
| 9397 | } |
| 9398 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 9399 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 9400 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9401 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9402 | } |
| 9403 | |
| 9404 | if (mPersistentStartingProcesses.size() > 0) { |
| 9405 | if (needSep) pw.println(" "); |
| 9406 | needSep = true; |
| 9407 | pw.println(" Persisent processes that are starting:"); |
| 9408 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9409 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9410 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9411 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9412 | if (mRemovedProcesses.size() > 0) { |
| 9413 | if (needSep) pw.println(" "); |
| 9414 | needSep = true; |
| 9415 | pw.println(" Processes that are being removed:"); |
| 9416 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9417 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9418 | } |
| 9419 | |
| 9420 | if (mProcessesOnHold.size() > 0) { |
| 9421 | if (needSep) pw.println(" "); |
| 9422 | needSep = true; |
| 9423 | pw.println(" Processes that are on old until the system is ready:"); |
| 9424 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9425 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9426 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9427 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9428 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9429 | |
| 9430 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9431 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9432 | long now = SystemClock.uptimeMillis(); |
| 9433 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9434 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9435 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9436 | SparseArray<Long> uids = procs.getValue(); |
| 9437 | final int N = uids.size(); |
| 9438 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9439 | int puid = uids.keyAt(i); |
| 9440 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9441 | if (dumpPackage != null && (r == null |
| 9442 | || !dumpPackage.equals(r.info.packageName))) { |
| 9443 | continue; |
| 9444 | } |
| 9445 | if (!printed) { |
| 9446 | if (needSep) pw.println(" "); |
| 9447 | needSep = true; |
| 9448 | pw.println(" Time since processes crashed:"); |
| 9449 | printed = true; |
| 9450 | } |
| 9451 | pw.print(" Process "); pw.print(pname); |
| 9452 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9453 | pw.print(": last crashed "); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 9454 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 9455 | pw.println(" ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9456 | } |
| 9457 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9458 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9459 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9460 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9461 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9462 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9463 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9464 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9465 | SparseArray<Long> uids = procs.getValue(); |
| 9466 | final int N = uids.size(); |
| 9467 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9468 | int puid = uids.keyAt(i); |
| 9469 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9470 | if (dumpPackage != null && (r == null |
| 9471 | || !dumpPackage.equals(r.info.packageName))) { |
| 9472 | continue; |
| 9473 | } |
| 9474 | if (!printed) { |
| 9475 | if (needSep) pw.println(" "); |
| 9476 | needSep = true; |
| 9477 | pw.println(" Bad processes:"); |
| 9478 | } |
| 9479 | pw.print(" Bad process "); pw.print(pname); |
| 9480 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9481 | pw.print(": crashed at time "); |
| 9482 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9483 | } |
| 9484 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9485 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9486 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9487 | pw.println(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9488 | pw.println(" mStartedUsers:"); |
| 9489 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 9490 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 9491 | pw.print(" User #"); pw.print(uss.mHandle.getIdentifier()); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9492 | pw.print(": "); uss.dump("", pw); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9493 | } |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 9494 | pw.print(" mStartedUserArray: ["); |
| 9495 | for (int i=0; i<mStartedUserArray.length; i++) { |
| 9496 | if (i > 0) pw.print(", "); |
| 9497 | pw.print(mStartedUserArray[i]); |
| 9498 | } |
| 9499 | pw.println("]"); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9500 | pw.print(" mUserLru: ["); |
| 9501 | for (int i=0; i<mUserLru.size(); i++) { |
| 9502 | if (i > 0) pw.print(", "); |
| 9503 | pw.print(mUserLru.get(i)); |
| 9504 | } |
| 9505 | pw.println("]"); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 9506 | if (dumpAll) { |
| 9507 | pw.print(" mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray)); |
| 9508 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9509 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9510 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 9511 | if (dumpAll) { |
| 9512 | StringBuilder sb = new StringBuilder(128); |
| 9513 | sb.append(" mPreviousProcessVisibleTime: "); |
| 9514 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 9515 | pw.println(sb); |
| 9516 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9517 | if (mHeavyWeightProcess != null) { |
| 9518 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9519 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9520 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9521 | if (dumpAll) { |
| 9522 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 9523 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9524 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9525 | for (Map.Entry<String, Integer> entry |
| 9526 | : mCompatModePackages.getPackages().entrySet()) { |
| 9527 | String pkg = entry.getKey(); |
| 9528 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9529 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 9530 | continue; |
| 9531 | } |
| 9532 | if (!printed) { |
| 9533 | pw.println(" mScreenCompatPackages:"); |
| 9534 | printed = true; |
| 9535 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9536 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 9537 | pw.print(mode); pw.println(); |
| 9538 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 9539 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9540 | } |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 9541 | if (mSleeping || mWentToSleep || mLockScreenShown) { |
| 9542 | pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep |
| 9543 | + " mLockScreenShown " + mLockScreenShown); |
| 9544 | } |
| 9545 | if (mShuttingDown) { |
| 9546 | pw.println(" mShuttingDown=" + mShuttingDown); |
| 9547 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9548 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 9549 | || mOrigWaitForDebugger) { |
| 9550 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 9551 | + " mDebugTransient=" + mDebugTransient |
| 9552 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 9553 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 9554 | if (mOpenGlTraceApp != null) { |
| 9555 | pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp); |
| 9556 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 9557 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 9558 | || mProfileFd != null) { |
| 9559 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 9560 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 9561 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 9562 | + mAutoStopProfiler); |
| 9563 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9564 | if (mAlwaysFinishActivities || mController != null) { |
| 9565 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 9566 | + " mController=" + mController); |
| 9567 | } |
| 9568 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9569 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9570 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9571 | + " mProcessesReady=" + mProcessesReady |
| 9572 | + " mSystemReady=" + mSystemReady); |
| 9573 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9574 | + " mBooted=" + mBooted |
| 9575 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9576 | pw.print(" mLastPowerCheckRealtime="); |
| 9577 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 9578 | pw.println(""); |
| 9579 | pw.print(" mLastPowerCheckUptime="); |
| 9580 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 9581 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9582 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 9583 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 9584 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 9585 | pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs |
| 9586 | + " mNumHiddenProcs=" + mNumHiddenProcs |
| 9587 | + " mNumServiceProcs=" + mNumServiceProcs |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9588 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9589 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9590 | |
| 9591 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9592 | } |
| 9593 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9594 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9595 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9596 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9597 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9598 | long now = SystemClock.uptimeMillis(); |
| 9599 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 9600 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9601 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 9602 | continue; |
| 9603 | } |
| 9604 | if (!printed) { |
| 9605 | if (needSep) pw.println(" "); |
| 9606 | needSep = true; |
| 9607 | pw.println(" Processes that are waiting to GC:"); |
| 9608 | printed = true; |
| 9609 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9610 | pw.print(" Process "); pw.println(proc); |
| 9611 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 9612 | pw.print(", last gced="); |
| 9613 | pw.print(now-proc.lastRequestedGc); |
| 9614 | pw.print(" ms ago, last lowMem="); |
| 9615 | pw.print(now-proc.lastLowMemory); |
| 9616 | pw.println(" ms ago"); |
| 9617 | |
| 9618 | } |
| 9619 | } |
| 9620 | return needSep; |
| 9621 | } |
| 9622 | |
| 9623 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 9624 | int opti, boolean dumpAll) { |
| 9625 | boolean needSep = false; |
| 9626 | |
| 9627 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9628 | if (needSep) pw.println(" "); |
| 9629 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9630 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9631 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9632 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9633 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 9634 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 9635 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9636 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9637 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9638 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9639 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9640 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9641 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9642 | 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] | 9643 | 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] | 9644 | |
| 9645 | if (needSep) pw.println(" "); |
| 9646 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9647 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9648 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9649 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9650 | needSep = true; |
| 9651 | } |
| 9652 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9653 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9654 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9655 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9656 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9657 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9658 | if (mHeavyWeightProcess != null) { |
| 9659 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9660 | } |
| 9661 | |
| 9662 | return true; |
| 9663 | } |
| 9664 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9665 | /** |
| 9666 | * There are three ways to call this: |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9667 | * - no provider specified: dump all the providers |
| 9668 | * - a flattened component name that matched an existing provider was specified as the |
| 9669 | * first arg: dump that one provider |
| 9670 | * - the first arg isn't the flattened component name of an existing provider: |
| 9671 | * dump all providers whose component contains the first arg as a substring |
| 9672 | */ |
| 9673 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9674 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 9675 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9676 | } |
| 9677 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9678 | static class ItemMatcher { |
| 9679 | ArrayList<ComponentName> components; |
| 9680 | ArrayList<String> strings; |
| 9681 | ArrayList<Integer> objects; |
| 9682 | boolean all; |
| 9683 | |
| 9684 | ItemMatcher() { |
| 9685 | all = true; |
| 9686 | } |
| 9687 | |
| 9688 | void build(String name) { |
| 9689 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 9690 | if (componentName != null) { |
| 9691 | if (components == null) { |
| 9692 | components = new ArrayList<ComponentName>(); |
| 9693 | } |
| 9694 | components.add(componentName); |
| 9695 | all = false; |
| 9696 | } else { |
| 9697 | int objectId = 0; |
| 9698 | // Not a '/' separated full component name; maybe an object ID? |
| 9699 | try { |
| 9700 | objectId = Integer.parseInt(name, 16); |
| 9701 | if (objects == null) { |
| 9702 | objects = new ArrayList<Integer>(); |
| 9703 | } |
| 9704 | objects.add(objectId); |
| 9705 | all = false; |
| 9706 | } catch (RuntimeException e) { |
| 9707 | // Not an integer; just do string match. |
| 9708 | if (strings == null) { |
| 9709 | strings = new ArrayList<String>(); |
| 9710 | } |
| 9711 | strings.add(name); |
| 9712 | all = false; |
| 9713 | } |
| 9714 | } |
| 9715 | } |
| 9716 | |
| 9717 | int build(String[] args, int opti) { |
| 9718 | for (; opti<args.length; opti++) { |
| 9719 | String name = args[opti]; |
| 9720 | if ("--".equals(name)) { |
| 9721 | return opti+1; |
| 9722 | } |
| 9723 | build(name); |
| 9724 | } |
| 9725 | return opti; |
| 9726 | } |
| 9727 | |
| 9728 | boolean match(Object object, ComponentName comp) { |
| 9729 | if (all) { |
| 9730 | return true; |
| 9731 | } |
| 9732 | if (components != null) { |
| 9733 | for (int i=0; i<components.size(); i++) { |
| 9734 | if (components.get(i).equals(comp)) { |
| 9735 | return true; |
| 9736 | } |
| 9737 | } |
| 9738 | } |
| 9739 | if (objects != null) { |
| 9740 | for (int i=0; i<objects.size(); i++) { |
| 9741 | if (System.identityHashCode(object) == objects.get(i)) { |
| 9742 | return true; |
| 9743 | } |
| 9744 | } |
| 9745 | } |
| 9746 | if (strings != null) { |
| 9747 | String flat = comp.flattenToString(); |
| 9748 | for (int i=0; i<strings.size(); i++) { |
| 9749 | if (flat.contains(strings.get(i))) { |
| 9750 | return true; |
| 9751 | } |
| 9752 | } |
| 9753 | } |
| 9754 | return false; |
| 9755 | } |
| 9756 | } |
| 9757 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9758 | /** |
| 9759 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9760 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9761 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 9762 | * dump all activity whose component contains the cmd as a substring |
| 9763 | * - A hex number of the ActivityRecord object instance. |
| 9764 | */ |
| 9765 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9766 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9767 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9768 | |
| 9769 | if ("all".equals(name)) { |
| 9770 | synchronized (this) { |
| 9771 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9772 | activities.add(r1); |
| 9773 | } |
| 9774 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 9775 | } else if ("top".equals(name)) { |
| 9776 | synchronized (this) { |
| 9777 | final int N = mMainStack.mHistory.size(); |
| 9778 | if (N > 0) { |
| 9779 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 9780 | } |
| 9781 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9782 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9783 | ItemMatcher matcher = new ItemMatcher(); |
| 9784 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9785 | |
| 9786 | synchronized (this) { |
| 9787 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9788 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9789 | activities.add(r1); |
| 9790 | } |
| 9791 | } |
| 9792 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9793 | } |
| 9794 | |
| 9795 | if (activities.size() <= 0) { |
| 9796 | return false; |
| 9797 | } |
| 9798 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9799 | String[] newArgs = new String[args.length - opti]; |
| 9800 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9801 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9802 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9803 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9804 | for (int i=activities.size()-1; i>=0; i--) { |
| 9805 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9806 | if (needSep) { |
| 9807 | pw.println(); |
| 9808 | } |
| 9809 | needSep = true; |
| 9810 | synchronized (this) { |
| 9811 | if (lastTask != r.task) { |
| 9812 | lastTask = r.task; |
| 9813 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 9814 | pw.print(" id="); pw.println(lastTask.taskId); |
| 9815 | if (dumpAll) { |
| 9816 | lastTask.dump(pw, " "); |
| 9817 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9818 | } |
| 9819 | } |
| 9820 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9821 | } |
| 9822 | return true; |
| 9823 | } |
| 9824 | |
| 9825 | /** |
| 9826 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 9827 | * there is a thread associated with the activity. |
| 9828 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9829 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9830 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 9831 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9832 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9833 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 9834 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9835 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9836 | if (r.app != null) pw.println(r.app.pid); |
| 9837 | else pw.println("(not running)"); |
| 9838 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9839 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9840 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9841 | } |
| 9842 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9843 | // flush anything that is already in the PrintWriter since the thread is going |
| 9844 | // to write to the file descriptor directly |
| 9845 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9846 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9847 | TransferPipe tp = new TransferPipe(); |
| 9848 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9849 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9850 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9851 | tp.go(fd); |
| 9852 | } finally { |
| 9853 | tp.kill(); |
| 9854 | } |
| 9855 | } catch (IOException e) { |
| 9856 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9857 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9858 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9859 | } |
| 9860 | } |
| 9861 | } |
| 9862 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9863 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9864 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9865 | boolean needSep = false; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9866 | boolean onlyHistory = false; |
| 9867 | |
| 9868 | if ("history".equals(dumpPackage)) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 9869 | if (opti < args.length && "-s".equals(args[opti])) { |
| 9870 | dumpAll = false; |
| 9871 | } |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9872 | onlyHistory = true; |
| 9873 | dumpPackage = null; |
| 9874 | } |
| 9875 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9876 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9877 | if (!onlyHistory && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9878 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9879 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9880 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 9881 | while (it.hasNext()) { |
| 9882 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9883 | if (dumpPackage != null && (r.app == null || |
| 9884 | !dumpPackage.equals(r.app.info.packageName))) { |
| 9885 | continue; |
| 9886 | } |
| 9887 | if (!printed) { |
| 9888 | pw.println(" Registered Receivers:"); |
| 9889 | needSep = true; |
| 9890 | printed = true; |
| 9891 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9892 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9893 | r.dump(pw, " "); |
| 9894 | } |
| 9895 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9896 | |
| 9897 | if (mReceiverResolver.dump(pw, needSep ? |
| 9898 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9899 | " ", dumpPackage, false)) { |
| 9900 | needSep = true; |
| 9901 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9902 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9903 | |
| 9904 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9905 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9906 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9907 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9908 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9909 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9910 | if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9911 | for (int user=0; user<mStickyBroadcasts.size(); user++) { |
| 9912 | if (needSep) { |
| 9913 | pw.println(); |
| 9914 | } |
| 9915 | needSep = true; |
| 9916 | pw.print(" Sticky broadcasts for user "); |
| 9917 | pw.print(mStickyBroadcasts.keyAt(user)); pw.println(":"); |
| 9918 | StringBuilder sb = new StringBuilder(128); |
| 9919 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9920 | : mStickyBroadcasts.valueAt(user).entrySet()) { |
| 9921 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
| 9922 | if (dumpAll) { |
| 9923 | pw.println(":"); |
| 9924 | ArrayList<Intent> intents = ent.getValue(); |
| 9925 | final int N = intents.size(); |
| 9926 | for (int i=0; i<N; i++) { |
| 9927 | sb.setLength(0); |
| 9928 | sb.append(" Intent: "); |
| 9929 | intents.get(i).toShortString(sb, false, true, false, false); |
| 9930 | pw.println(sb.toString()); |
| 9931 | Bundle bundle = intents.get(i).getExtras(); |
| 9932 | if (bundle != null) { |
| 9933 | pw.print(" "); |
| 9934 | pw.println(bundle.toString()); |
| 9935 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9936 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9937 | } else { |
| 9938 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9939 | } |
| 9940 | } |
| 9941 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9942 | } |
| 9943 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9944 | if (!onlyHistory && dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9945 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9946 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9947 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9948 | + queue.mBroadcastsScheduled); |
| 9949 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9950 | pw.println(" mHandler:"); |
| 9951 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9952 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9953 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9954 | |
| 9955 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9956 | } |
| 9957 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9958 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9959 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 9960 | boolean needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9961 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9962 | ItemMatcher matcher = new ItemMatcher(); |
| 9963 | matcher.build(args, opti); |
| 9964 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9965 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9966 | |
| 9967 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9968 | |
| 9969 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9970 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9971 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9972 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9973 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9974 | continue; |
| 9975 | } |
| 9976 | if (!printed) { |
| 9977 | if (needSep) pw.println(" "); |
| 9978 | needSep = true; |
| 9979 | pw.println(" Launching content providers:"); |
| 9980 | printed = true; |
| 9981 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9982 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9983 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9984 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9985 | } |
| 9986 | |
| 9987 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9988 | if (needSep) pw.println(); |
| 9989 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9990 | pw.println("Granted Uri Permissions:"); |
| 9991 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9992 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9993 | HashMap<Uri, UriPermission> perms |
| 9994 | = mGrantedUriPermissions.valueAt(i); |
| 9995 | pw.print(" * UID "); pw.print(uid); |
| 9996 | pw.println(" holds:"); |
| 9997 | for (UriPermission perm : perms.values()) { |
| 9998 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9999 | if (dumpAll) { |
| 10000 | perm.dump(pw, " "); |
| 10001 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10002 | } |
| 10003 | } |
| 10004 | needSep = true; |
| 10005 | } |
| 10006 | |
| 10007 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10008 | } |
| 10009 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10010 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10011 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10012 | boolean needSep = false; |
| 10013 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10014 | if (mIntentSenderRecords.size() > 0) { |
| 10015 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10016 | Iterator<WeakReference<PendingIntentRecord>> it |
| 10017 | = mIntentSenderRecords.values().iterator(); |
| 10018 | while (it.hasNext()) { |
| 10019 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 10020 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10021 | if (dumpPackage != null && (rec == null |
| 10022 | || !dumpPackage.equals(rec.key.packageName))) { |
| 10023 | continue; |
| 10024 | } |
| 10025 | if (!printed) { |
| 10026 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 10027 | printed = true; |
| 10028 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10029 | needSep = true; |
| 10030 | if (rec != null) { |
| 10031 | pw.print(" * "); pw.println(rec); |
| 10032 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10033 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10034 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10035 | } else { |
| 10036 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10037 | } |
| 10038 | } |
| 10039 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10040 | |
| 10041 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10042 | } |
| 10043 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10044 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10045 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 10046 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10047 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10048 | boolean needNL = false; |
| 10049 | final String innerPrefix = prefix + " "; |
| 10050 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10051 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10052 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10053 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 10054 | continue; |
| 10055 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 10056 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10057 | if (needNL) { |
| 10058 | pw.println(" "); |
| 10059 | needNL = false; |
| 10060 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10061 | if (lastTask != r.task) { |
| 10062 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 10063 | pw.print(prefix); |
| 10064 | pw.print(full ? "* " : " "); |
| 10065 | pw.println(lastTask); |
| 10066 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10067 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10068 | } else if (complete) { |
| 10069 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 10070 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 10071 | pw.print(prefix); pw.print(" "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 10072 | pw.println(lastTask.intent.toInsecureStringWithClip()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10073 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10075 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 10076 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 10077 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 10078 | pw.println(r); |
| 10079 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10080 | r.dump(pw, innerPrefix); |
| 10081 | } else if (complete) { |
| 10082 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 10083 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10084 | if (r.app != null) { |
| 10085 | pw.print(innerPrefix); pw.println(r.app); |
| 10086 | } |
| 10087 | } |
| 10088 | if (client && r.app != null && r.app.thread != null) { |
| 10089 | // flush anything that is already in the PrintWriter since the thread is going |
| 10090 | // to write to the file descriptor directly |
| 10091 | pw.flush(); |
| 10092 | try { |
| 10093 | TransferPipe tp = new TransferPipe(); |
| 10094 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 10095 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 10096 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10097 | // Short timeout, since blocking here can |
| 10098 | // deadlock with the application. |
| 10099 | tp.go(fd, 2000); |
| 10100 | } finally { |
| 10101 | tp.kill(); |
| 10102 | } |
| 10103 | } catch (IOException e) { |
| 10104 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 10105 | } catch (RemoteException e) { |
| 10106 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 10107 | } |
| 10108 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10110 | } |
| 10111 | } |
| 10112 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 10113 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 10114 | if (val == base) { |
| 10115 | if (space == null) return prefix; |
| 10116 | return prefix + " "; |
| 10117 | } |
| 10118 | return prefix + "+" + Integer.toString(val-base); |
| 10119 | } |
| 10120 | |
| 10121 | private static final int dumpProcessList(PrintWriter pw, |
| 10122 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10123 | String prefix, String normalLabel, String persistentLabel, |
| 10124 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10125 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 10126 | final int N = list.size()-1; |
| 10127 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10128 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10129 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 10130 | continue; |
| 10131 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10132 | pw.println(String.format("%s%s #%2d: %s", |
| 10133 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 10134 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10135 | if (r.persistent) { |
| 10136 | numPers++; |
| 10137 | } |
| 10138 | } |
| 10139 | return numPers; |
| 10140 | } |
| 10141 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10142 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10143 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10144 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10145 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10146 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10147 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 10148 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 10149 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10150 | ProcessRecord r = origList.get(i); |
| 10151 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 10152 | continue; |
| 10153 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10154 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 10155 | } |
| 10156 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10157 | if (list.size() <= 0) { |
| 10158 | return false; |
| 10159 | } |
| 10160 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10161 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 10162 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 10163 | @Override |
| 10164 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 10165 | Pair<ProcessRecord, Integer> object2) { |
| 10166 | if (object1.first.setAdj != object2.first.setAdj) { |
| 10167 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 10168 | } |
| 10169 | if (object1.second.intValue() != object2.second.intValue()) { |
| 10170 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 10171 | } |
| 10172 | return 0; |
| 10173 | } |
| 10174 | }; |
| 10175 | |
| 10176 | Collections.sort(list, comparator); |
| 10177 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10178 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 10179 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 10180 | final long curUptime = SystemClock.uptimeMillis(); |
| 10181 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 10182 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10183 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10184 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10185 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10186 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10187 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10188 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 10189 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10190 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 10191 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10192 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 10193 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10194 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 10195 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10196 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10197 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10198 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 10199 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 10200 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 10201 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 10202 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 10203 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 10204 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10205 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10206 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 10207 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10208 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 10209 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10210 | } else { |
| 10211 | oomAdj = Integer.toString(r.setAdj); |
| 10212 | } |
| 10213 | String schedGroup; |
| 10214 | switch (r.setSchedGroup) { |
| 10215 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 10216 | schedGroup = "B"; |
| 10217 | break; |
| 10218 | case Process.THREAD_GROUP_DEFAULT: |
| 10219 | schedGroup = "F"; |
| 10220 | break; |
| 10221 | default: |
| 10222 | schedGroup = Integer.toString(r.setSchedGroup); |
| 10223 | break; |
| 10224 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10225 | String foreground; |
| 10226 | if (r.foregroundActivities) { |
| 10227 | foreground = "A"; |
| 10228 | } else if (r.foregroundServices) { |
| 10229 | foreground = "S"; |
| 10230 | } else { |
| 10231 | foreground = " "; |
| 10232 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 10233 | 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] | 10234 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10235 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 10236 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10237 | if (r.adjSource != null || r.adjTarget != null) { |
| 10238 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10239 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10240 | if (r.adjTarget instanceof ComponentName) { |
| 10241 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 10242 | } else if (r.adjTarget != null) { |
| 10243 | pw.print(r.adjTarget.toString()); |
| 10244 | } else { |
| 10245 | pw.print("{null}"); |
| 10246 | } |
| 10247 | pw.print("<="); |
| 10248 | if (r.adjSource instanceof ProcessRecord) { |
| 10249 | pw.print("Proc{"); |
| 10250 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 10251 | pw.println("}"); |
| 10252 | } else if (r.adjSource != null) { |
| 10253 | pw.println(r.adjSource.toString()); |
| 10254 | } else { |
| 10255 | pw.println("{null}"); |
| 10256 | } |
| 10257 | } |
| 10258 | if (inclDetails) { |
| 10259 | pw.print(prefix); |
| 10260 | pw.print(" "); |
| 10261 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 10262 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 10263 | pw.print(" client="); pw.print(r.clientHiddenAdj); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 10264 | pw.print(" empty="); pw.print(r.emptyAdj); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10265 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 10266 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 10267 | pw.print(" cur="); pw.print(r.curAdj); |
| 10268 | pw.print(" set="); pw.println(r.setAdj); |
| 10269 | pw.print(prefix); |
| 10270 | pw.print(" "); |
| 10271 | pw.print("keeping="); pw.print(r.keeping); |
| 10272 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10273 | pw.print(" empty="); pw.print(r.empty); |
| 10274 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10275 | |
| 10276 | if (!r.keeping) { |
| 10277 | if (r.lastWakeTime != 0) { |
| 10278 | long wtime; |
| 10279 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 10280 | synchronized (stats) { |
| 10281 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 10282 | r.pid, curRealtime); |
| 10283 | } |
| 10284 | long timeUsed = wtime - r.lastWakeTime; |
| 10285 | pw.print(prefix); |
| 10286 | pw.print(" "); |
| 10287 | pw.print("keep awake over "); |
| 10288 | TimeUtils.formatDuration(realtimeSince, pw); |
| 10289 | pw.print(" used "); |
| 10290 | TimeUtils.formatDuration(timeUsed, pw); |
| 10291 | pw.print(" ("); |
| 10292 | pw.print((timeUsed*100)/realtimeSince); |
| 10293 | pw.println("%)"); |
| 10294 | } |
| 10295 | if (r.lastCpuTime != 0) { |
| 10296 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 10297 | pw.print(prefix); |
| 10298 | pw.print(" "); |
| 10299 | pw.print("run cpu over "); |
| 10300 | TimeUtils.formatDuration(uptimeSince, pw); |
| 10301 | pw.print(" used "); |
| 10302 | TimeUtils.formatDuration(timeUsed, pw); |
| 10303 | pw.print(" ("); |
| 10304 | pw.print((timeUsed*100)/uptimeSince); |
| 10305 | pw.println("%)"); |
| 10306 | } |
| 10307 | } |
| 10308 | } |
| 10309 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10310 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10311 | } |
| 10312 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10313 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10314 | ArrayList<ProcessRecord> procs; |
| 10315 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10316 | if (args != null && args.length > start |
| 10317 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10318 | procs = new ArrayList<ProcessRecord>(); |
| 10319 | int pid = -1; |
| 10320 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10321 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10322 | } catch (NumberFormatException e) { |
| 10323 | |
| 10324 | } |
| 10325 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 10326 | ProcessRecord proc = mLruProcesses.get(i); |
| 10327 | if (proc.pid == pid) { |
| 10328 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10329 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10330 | procs.add(proc); |
| 10331 | } |
| 10332 | } |
| 10333 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10334 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10335 | return null; |
| 10336 | } |
| 10337 | } else { |
| 10338 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 10339 | } |
| 10340 | } |
| 10341 | return procs; |
| 10342 | } |
| 10343 | |
| 10344 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 10345 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10346 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10347 | if (procs == null) { |
| 10348 | return; |
| 10349 | } |
| 10350 | |
| 10351 | long uptime = SystemClock.uptimeMillis(); |
| 10352 | long realtime = SystemClock.elapsedRealtime(); |
| 10353 | pw.println("Applications Graphics Acceleration Info:"); |
| 10354 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10355 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10356 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10357 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10358 | if (r.thread != null) { |
| 10359 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10360 | pw.flush(); |
| 10361 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10362 | TransferPipe tp = new TransferPipe(); |
| 10363 | try { |
| 10364 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10365 | tp.go(fd); |
| 10366 | } finally { |
| 10367 | tp.kill(); |
| 10368 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10369 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10370 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10371 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10372 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10373 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10374 | pw.flush(); |
| 10375 | } |
| 10376 | } |
| 10377 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10378 | } |
| 10379 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 10380 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 10381 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 10382 | if (procs == null) { |
| 10383 | return; |
| 10384 | } |
| 10385 | |
| 10386 | pw.println("Applications Database Info:"); |
| 10387 | |
| 10388 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10389 | ProcessRecord r = procs.get(i); |
| 10390 | if (r.thread != null) { |
| 10391 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10392 | pw.flush(); |
| 10393 | try { |
| 10394 | TransferPipe tp = new TransferPipe(); |
| 10395 | try { |
| 10396 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10397 | tp.go(fd); |
| 10398 | } finally { |
| 10399 | tp.kill(); |
| 10400 | } |
| 10401 | } catch (IOException e) { |
| 10402 | pw.println("Failure while dumping the app: " + r); |
| 10403 | pw.flush(); |
| 10404 | } catch (RemoteException e) { |
| 10405 | pw.println("Got a RemoteException while dumping the app " + r); |
| 10406 | pw.flush(); |
| 10407 | } |
| 10408 | } |
| 10409 | } |
| 10410 | } |
| 10411 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10412 | final static class MemItem { |
| 10413 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10414 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10415 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10416 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10417 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10418 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10419 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10420 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10421 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10422 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10423 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10424 | } |
| 10425 | } |
| 10426 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10427 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10428 | boolean sort) { |
| 10429 | if (sort) { |
| 10430 | Collections.sort(items, new Comparator<MemItem>() { |
| 10431 | @Override |
| 10432 | public int compare(MemItem lhs, MemItem rhs) { |
| 10433 | if (lhs.pss < rhs.pss) { |
| 10434 | return 1; |
| 10435 | } else if (lhs.pss > rhs.pss) { |
| 10436 | return -1; |
| 10437 | } |
| 10438 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10439 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10440 | }); |
| 10441 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10442 | |
| 10443 | for (int i=0; i<items.size(); i++) { |
| 10444 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10445 | 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] | 10446 | if (mi.subitems != null) { |
| 10447 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 10448 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10449 | } |
| 10450 | } |
| 10451 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10452 | // These are in KB. |
| 10453 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 10454 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 10455 | 120*1024, 160*1024, 200*1024, |
| 10456 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 10457 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 10458 | }; |
| 10459 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10460 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 10461 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10462 | int start = label.lastIndexOf('.'); |
| 10463 | if (start >= 0) start++; |
| 10464 | else start = 0; |
| 10465 | int end = label.length(); |
| 10466 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 10467 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 10468 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 10469 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10470 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10471 | out.append(label, start, end); |
| 10472 | return; |
| 10473 | } |
| 10474 | } |
| 10475 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10476 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10477 | out.append(label, start, end); |
| 10478 | } |
| 10479 | |
| 10480 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 10481 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 10482 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 10483 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 10484 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 10485 | }; |
| 10486 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 10487 | "System", "Persistent", "Foreground", |
| 10488 | "Visible", "Perceptible", "Heavy Weight", |
| 10489 | "Backup", "A Services", "Home", "Previous", |
| 10490 | "B Services", "Background" |
| 10491 | }; |
| 10492 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10493 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10494 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10495 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10496 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10497 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10498 | |
| 10499 | int opti = 0; |
| 10500 | while (opti < args.length) { |
| 10501 | String opt = args[opti]; |
| 10502 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 10503 | break; |
| 10504 | } |
| 10505 | opti++; |
| 10506 | if ("-a".equals(opt)) { |
| 10507 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10508 | } else if ("--oom".equals(opt)) { |
| 10509 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10510 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10511 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10512 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10513 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10514 | pw.println("If [process] is specified it can be the name or "); |
| 10515 | pw.println("pid of a specific process to dump."); |
| 10516 | return; |
| 10517 | } else { |
| 10518 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 10519 | } |
| 10520 | } |
| 10521 | |
| 10522 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10523 | if (procs == null) { |
| 10524 | return; |
| 10525 | } |
| 10526 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 10527 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10528 | long uptime = SystemClock.uptimeMillis(); |
| 10529 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10530 | |
| 10531 | if (procs.size() == 1 || isCheckinRequest) { |
| 10532 | dumpAll = true; |
| 10533 | } |
| 10534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10535 | if (isCheckinRequest) { |
| 10536 | // short checkin version |
| 10537 | pw.println(uptime + "," + realtime); |
| 10538 | pw.flush(); |
| 10539 | } else { |
| 10540 | pw.println("Applications Memory Usage (kB):"); |
| 10541 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10542 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10543 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10544 | String[] innerArgs = new String[args.length-opti]; |
| 10545 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 10546 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10547 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 10548 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 10549 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 10550 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10551 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 10552 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 10553 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10554 | |
| 10555 | long totalPss = 0; |
| 10556 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10557 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10558 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10559 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10560 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10561 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 10562 | pw.flush(); |
| 10563 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10564 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10565 | if (dumpAll) { |
| 10566 | try { |
| 10567 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 10568 | } catch (RemoteException e) { |
| 10569 | if (!isCheckinRequest) { |
| 10570 | pw.println("Got RemoteException!"); |
| 10571 | pw.flush(); |
| 10572 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10573 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10574 | } else { |
| 10575 | mi = new Debug.MemoryInfo(); |
| 10576 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10577 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10578 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10579 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10580 | long myTotalPss = mi.getTotalPss(); |
| 10581 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10582 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10583 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10584 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10585 | |
| 10586 | nativePss += mi.nativePss; |
| 10587 | dalvikPss += mi.dalvikPss; |
| 10588 | otherPss += mi.otherPss; |
| 10589 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 10590 | long mem = mi.getOtherPss(j); |
| 10591 | miscPss[j] += mem; |
| 10592 | otherPss -= mem; |
| 10593 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10594 | |
| 10595 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10596 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 10597 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10598 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10599 | if (oomProcs[oomIndex] == null) { |
| 10600 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 10601 | } |
| 10602 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10603 | break; |
| 10604 | } |
| 10605 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10607 | } |
| 10608 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10609 | |
| 10610 | if (!isCheckinRequest && procs.size() > 1) { |
| 10611 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 10612 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10613 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10614 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10615 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10616 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10617 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10618 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10619 | } |
| 10620 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10621 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10622 | for (int j=0; j<oomPss.length; j++) { |
| 10623 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10624 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10625 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10626 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10627 | item.subitems = oomProcs[j]; |
| 10628 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10629 | } |
| 10630 | } |
| 10631 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10632 | if (outTag != null || outStack != null) { |
| 10633 | if (outTag != null) { |
| 10634 | appendMemBucket(outTag, totalPss, "total", false); |
| 10635 | } |
| 10636 | if (outStack != null) { |
| 10637 | appendMemBucket(outStack, totalPss, "total", true); |
| 10638 | } |
| 10639 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10640 | for (int i=0; i<oomMems.size(); i++) { |
| 10641 | MemItem miCat = oomMems.get(i); |
| 10642 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10643 | continue; |
| 10644 | } |
| 10645 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10646 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10647 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10648 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10649 | outTag.append(" / "); |
| 10650 | } |
| 10651 | if (outStack != null) { |
| 10652 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10653 | if (firstLine) { |
| 10654 | outStack.append(":"); |
| 10655 | firstLine = false; |
| 10656 | } |
| 10657 | outStack.append("\n\t at "); |
| 10658 | } else { |
| 10659 | outStack.append("$"); |
| 10660 | } |
| 10661 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10662 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10663 | MemItem mi = miCat.subitems.get(j); |
| 10664 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10665 | if (outTag != null) { |
| 10666 | outTag.append(" "); |
| 10667 | } |
| 10668 | if (outStack != null) { |
| 10669 | outStack.append("$"); |
| 10670 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10671 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10672 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10673 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10674 | } |
| 10675 | if (outStack != null) { |
| 10676 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10677 | } |
| 10678 | } |
| 10679 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10680 | outStack.append("("); |
| 10681 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10682 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10683 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10684 | outStack.append(":"); |
| 10685 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10686 | } |
| 10687 | } |
| 10688 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10689 | } |
| 10690 | } |
| 10691 | } |
| 10692 | } |
| 10693 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10694 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10695 | pw.println(); |
| 10696 | pw.println("Total PSS by process:"); |
| 10697 | dumpMemItems(pw, " ", procMems, true); |
| 10698 | pw.println(); |
| 10699 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10700 | pw.println("Total PSS by OOM adjustment:"); |
| 10701 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10702 | if (!oomOnly) { |
| 10703 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10704 | out.println(); |
| 10705 | out.println("Total PSS by category:"); |
| 10706 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10707 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10708 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10709 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | d3e677b | 2012-04-05 14:58:18 -0700 | [diff] [blame] | 10710 | final int[] SINGLE_LONG_FORMAT = new int[] { |
| 10711 | Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG |
| 10712 | }; |
| 10713 | long[] longOut = new long[1]; |
| 10714 | Process.readProcFile("/sys/kernel/mm/ksm/pages_shared", |
| 10715 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10716 | long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10717 | longOut[0] = 0; |
| 10718 | Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing", |
| 10719 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10720 | long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10721 | longOut[0] = 0; |
| 10722 | Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared", |
| 10723 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10724 | long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10725 | longOut[0] = 0; |
| 10726 | Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile", |
| 10727 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10728 | long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10729 | pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared "); |
| 10730 | pw.print(shared); pw.println(" kB"); |
| 10731 | pw.print(" "); pw.print(unshared); pw.print(" kB unshared; "); |
| 10732 | pw.print(voltile); pw.println(" kB volatile"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10733 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10734 | } |
| 10735 | |
| 10736 | /** |
| 10737 | * Searches array of arguments for the specified string |
| 10738 | * @param args array of argument strings |
| 10739 | * @param value value to search for |
| 10740 | * @return true if the value is contained in the array |
| 10741 | */ |
| 10742 | private static boolean scanArgs(String[] args, String value) { |
| 10743 | if (args != null) { |
| 10744 | for (String arg : args) { |
| 10745 | if (value.equals(arg)) { |
| 10746 | return true; |
| 10747 | } |
| 10748 | } |
| 10749 | } |
| 10750 | return false; |
| 10751 | } |
| 10752 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10753 | private final boolean removeDyingProviderLocked(ProcessRecord proc, |
| 10754 | ContentProviderRecord cpr, boolean always) { |
| 10755 | final boolean inLaunching = mLaunchingProviders.contains(cpr); |
| 10756 | |
| 10757 | if (!inLaunching || always) { |
| 10758 | synchronized (cpr) { |
| 10759 | cpr.launchingApp = null; |
| 10760 | cpr.notifyAll(); |
| 10761 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10762 | mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid)); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10763 | String names[] = cpr.info.authority.split(";"); |
| 10764 | for (int j = 0; j < names.length; j++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10765 | mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10766 | } |
| 10767 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10768 | |
| 10769 | for (int i=0; i<cpr.connections.size(); i++) { |
| 10770 | ContentProviderConnection conn = cpr.connections.get(i); |
| 10771 | if (conn.waiting) { |
| 10772 | // If this connection is waiting for the provider, then we don't |
| 10773 | // need to mess with its process unless we are always removing |
| 10774 | // or for some reason the provider is not currently launching. |
| 10775 | if (inLaunching && !always) { |
| 10776 | continue; |
| 10777 | } |
| 10778 | } |
| 10779 | ProcessRecord capp = conn.client; |
| 10780 | conn.dead = true; |
| 10781 | if (conn.stableCount > 0) { |
| 10782 | if (!capp.persistent && capp.thread != null |
| 10783 | && capp.pid != 0 |
| 10784 | && capp.pid != MY_PID) { |
| 10785 | Slog.i(TAG, "Kill " + capp.processName |
| 10786 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 10787 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 10788 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.userId, capp.pid, |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10789 | capp.processName, capp.setAdj, "dying provider " |
| 10790 | + cpr.name.toShortString()); |
| 10791 | Process.killProcessQuiet(capp.pid); |
| 10792 | } |
| 10793 | } else if (capp.thread != null && conn.provider.provider != null) { |
| 10794 | try { |
| 10795 | capp.thread.unstableProviderDied(conn.provider.provider.asBinder()); |
| 10796 | } catch (RemoteException e) { |
| 10797 | } |
| 10798 | // In the protocol here, we don't expect the client to correctly |
| 10799 | // clean up this connection, we'll just remove it. |
| 10800 | cpr.connections.remove(i); |
| 10801 | conn.client.conProviders.remove(conn); |
| 10802 | } |
| 10803 | } |
| 10804 | |
| 10805 | if (inLaunching && always) { |
| 10806 | mLaunchingProviders.remove(cpr); |
| 10807 | } |
| 10808 | return inLaunching; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10809 | } |
| 10810 | |
| 10811 | /** |
| 10812 | * Main code for cleaning up a process when it has gone away. This is |
| 10813 | * called both as a result of the process dying, or directly when stopping |
| 10814 | * a process when running in single process mode. |
| 10815 | */ |
| 10816 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10817 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10818 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10819 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10820 | } |
| 10821 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10822 | mProcessesToGc.remove(app); |
| 10823 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10824 | // Dismiss any open dialogs. |
| 10825 | if (app.crashDialog != null) { |
| 10826 | app.crashDialog.dismiss(); |
| 10827 | app.crashDialog = null; |
| 10828 | } |
| 10829 | if (app.anrDialog != null) { |
| 10830 | app.anrDialog.dismiss(); |
| 10831 | app.anrDialog = null; |
| 10832 | } |
| 10833 | if (app.waitDialog != null) { |
| 10834 | app.waitDialog.dismiss(); |
| 10835 | app.waitDialog = null; |
| 10836 | } |
| 10837 | |
| 10838 | app.crashing = false; |
| 10839 | app.notResponding = false; |
| 10840 | |
| 10841 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10842 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10843 | app.thread = null; |
| 10844 | app.forcingToForeground = null; |
| 10845 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10846 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10847 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10848 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10849 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10850 | mServices.killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10851 | |
| 10852 | boolean restart = false; |
| 10853 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10854 | // Remove published content providers. |
| 10855 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10856 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10857 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10858 | ContentProviderRecord cpr = it.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10859 | |
| 10860 | final boolean always = app.bad || !allowRestart; |
| 10861 | if (removeDyingProviderLocked(app, cpr, always) || always) { |
| 10862 | // We left the provider in the launching list, need to |
| 10863 | // restart it. |
| 10864 | restart = true; |
| 10865 | } |
| 10866 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10867 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10868 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10869 | } |
| 10870 | app.pubProviders.clear(); |
| 10871 | } |
| 10872 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10873 | // Take care of any launching providers waiting for this process. |
| 10874 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10875 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10876 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10877 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10878 | // Unregister from connected content providers. |
| 10879 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10880 | for (int i=0; i<app.conProviders.size(); i++) { |
| 10881 | ContentProviderConnection conn = app.conProviders.get(i); |
| 10882 | conn.provider.connections.remove(conn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10883 | } |
| 10884 | app.conProviders.clear(); |
| 10885 | } |
| 10886 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10887 | // At this point there may be remaining entries in mLaunchingProviders |
| 10888 | // where we were the only one waiting, so they are no longer of use. |
| 10889 | // Look for these and clean up if found. |
| 10890 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10891 | // the actual situation to identify what is actually going on. |
| 10892 | if (false) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10893 | for (int i=0; i<mLaunchingProviders.size(); i++) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10894 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10895 | mLaunchingProviders.get(i); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10896 | if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10897 | synchronized (cpr) { |
| 10898 | cpr.launchingApp = null; |
| 10899 | cpr.notifyAll(); |
| 10900 | } |
| 10901 | } |
| 10902 | } |
| 10903 | } |
| 10904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10905 | skipCurrentReceiverLocked(app); |
| 10906 | |
| 10907 | // Unregister any receivers. |
| 10908 | if (app.receivers.size() > 0) { |
| 10909 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10910 | while (it.hasNext()) { |
| 10911 | removeReceiverLocked(it.next()); |
| 10912 | } |
| 10913 | app.receivers.clear(); |
| 10914 | } |
| 10915 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10916 | // If the app is undergoing backup, tell the backup manager about it |
| 10917 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10918 | if (DEBUG_BACKUP || DEBUG_CLEANUP) Slog.d(TAG, "App " |
| 10919 | + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10920 | try { |
| 10921 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10922 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10923 | bm.agentDisconnected(app.info.packageName); |
| 10924 | } catch (RemoteException e) { |
| 10925 | // can't happen; backup manager is local |
| 10926 | } |
| 10927 | } |
| 10928 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 10929 | for (int i = mPendingProcessChanges.size()-1; i>=0; i--) { |
| 10930 | ProcessChangeItem item = mPendingProcessChanges.get(i); |
| 10931 | if (item.pid == app.pid) { |
| 10932 | mPendingProcessChanges.remove(i); |
| 10933 | mAvailProcessChanges.add(item); |
| 10934 | } |
| 10935 | } |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10936 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10937 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10938 | // If the caller is restarting this app, then leave it in its |
| 10939 | // current lists and let the caller take care of it. |
| 10940 | if (restarting) { |
| 10941 | return; |
| 10942 | } |
| 10943 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10944 | if (!app.persistent || app.isolated) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10945 | if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10946 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10947 | mProcessNames.remove(app.processName, app.uid); |
| 10948 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10949 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10950 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 10951 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10952 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10953 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10954 | } else if (!app.removed) { |
| 10955 | // This app is persistent, so we need to keep its record around. |
| 10956 | // If it is not already on the pending app list, add it there |
| 10957 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10958 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10959 | mPersistentStartingProcesses.add(app); |
| 10960 | restart = true; |
| 10961 | } |
| 10962 | } |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10963 | if ((DEBUG_PROCESSES || DEBUG_CLEANUP) && mProcessesOnHold.contains(app)) Slog.v(TAG, |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10964 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10965 | mProcessesOnHold.remove(app); |
| 10966 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10967 | if (app == mHomeProcess) { |
| 10968 | mHomeProcess = null; |
| 10969 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10970 | if (app == mPreviousProcess) { |
| 10971 | mPreviousProcess = null; |
| 10972 | } |
| 10973 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10974 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10975 | // We have components that still need to be running in the |
| 10976 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10977 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10978 | startProcessLocked(app, "restart", app.processName); |
| 10979 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10980 | // Goodbye! |
| 10981 | synchronized (mPidsSelfLocked) { |
| 10982 | mPidsSelfLocked.remove(app.pid); |
| 10983 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10984 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10985 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10986 | } |
| 10987 | } |
| 10988 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10989 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10990 | // Look through the content providers we are waiting to have launched, |
| 10991 | // and if any run in this process then either schedule a restart of |
| 10992 | // the process or kill the client waiting for it if this process has |
| 10993 | // gone bad. |
| 10994 | int NL = mLaunchingProviders.size(); |
| 10995 | boolean restart = false; |
| 10996 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10997 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10998 | if (cpr.launchingApp == app) { |
| 10999 | if (!alwaysBad && !app.bad) { |
| 11000 | restart = true; |
| 11001 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 11002 | removeDyingProviderLocked(app, cpr, true); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 11003 | // cpr should have been removed from mLaunchingProviders |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 11004 | NL = mLaunchingProviders.size(); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 11005 | i--; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 11006 | } |
| 11007 | } |
| 11008 | } |
| 11009 | return restart; |
| 11010 | } |
| 11011 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11012 | // ========================================================= |
| 11013 | // SERVICES |
| 11014 | // ========================================================= |
| 11015 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11016 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 11017 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11018 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11019 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11020 | return mServices.getRunningServiceInfoLocked(maxNum, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11021 | } |
| 11022 | } |
| 11023 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11024 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11025 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11026 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11027 | return mServices.getRunningServiceControlPanelLocked(name); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11028 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11029 | } |
| 11030 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11031 | public ComponentName startService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11032 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11033 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11034 | // Refuse possible leaked file descriptors |
| 11035 | if (service != null && service.hasFileDescriptors() == true) { |
| 11036 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11037 | } |
| 11038 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11039 | if (DEBUG_SERVICE) |
| 11040 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11041 | synchronized(this) { |
| 11042 | final int callingPid = Binder.getCallingPid(); |
| 11043 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11044 | checkValidCaller(callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11045 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11046 | ComponentName res = mServices.startServiceLocked(caller, service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11047 | resolvedType, callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11048 | Binder.restoreCallingIdentity(origId); |
| 11049 | return res; |
| 11050 | } |
| 11051 | } |
| 11052 | |
| 11053 | ComponentName startServiceInPackage(int uid, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11054 | Intent service, String resolvedType, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11055 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11056 | if (DEBUG_SERVICE) |
| 11057 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11058 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11059 | ComponentName res = mServices.startServiceLocked(null, service, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11060 | resolvedType, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11061 | Binder.restoreCallingIdentity(origId); |
| 11062 | return res; |
| 11063 | } |
| 11064 | } |
| 11065 | |
| 11066 | public int stopService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11067 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11068 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11069 | // Refuse possible leaked file descriptors |
| 11070 | if (service != null && service.hasFileDescriptors() == true) { |
| 11071 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11072 | } |
| 11073 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11074 | checkValidCaller(Binder.getCallingUid(), userId); |
| 11075 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11076 | synchronized(this) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11077 | return mServices.stopServiceLocked(caller, service, resolvedType, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11078 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11079 | } |
| 11080 | |
| 11081 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11082 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11083 | // Refuse possible leaked file descriptors |
| 11084 | if (service != null && service.hasFileDescriptors() == true) { |
| 11085 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11086 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11087 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11088 | return mServices.peekServiceLocked(service, resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11090 | } |
| 11091 | |
| 11092 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 11093 | int startId) { |
| 11094 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11095 | return mServices.stopServiceTokenLocked(className, token, startId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11096 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11097 | } |
| 11098 | |
| 11099 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11100 | int id, Notification notification, boolean removeNotification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11101 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11102 | mServices.setServiceForegroundLocked(className, token, id, notification, |
| 11103 | removeNotification); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11104 | } |
| 11105 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11106 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11107 | public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 11108 | boolean requireFull, String name, String callerPackage) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11109 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 11110 | if (callingUserId != userId) { |
| 11111 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 11112 | if ((requireFull || checkComponentPermission( |
| 11113 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 11114 | callingPid, callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) |
| 11115 | && checkComponentPermission( |
| 11116 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 11117 | callingPid, callingUid, -1, true) |
| 11118 | != PackageManager.PERMISSION_GRANTED) { |
| 11119 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 11120 | // In this case, they would like to just execute as their |
| 11121 | // owner user instead of failing. |
| 11122 | userId = callingUserId; |
| 11123 | } else { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11124 | StringBuilder builder = new StringBuilder(128); |
| 11125 | builder.append("Permission Denial: "); |
| 11126 | builder.append(name); |
| 11127 | if (callerPackage != null) { |
| 11128 | builder.append(" from "); |
| 11129 | builder.append(callerPackage); |
| 11130 | } |
| 11131 | builder.append(" asks to run as user "); |
| 11132 | builder.append(userId); |
| 11133 | builder.append(" but is calling from user "); |
| 11134 | builder.append(UserHandle.getUserId(callingUid)); |
| 11135 | builder.append("; this requires "); |
| 11136 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 11137 | if (!requireFull) { |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 11138 | builder.append(" or "); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11139 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 11140 | } |
| 11141 | String msg = builder.toString(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11142 | Slog.w(TAG, msg); |
| 11143 | throw new SecurityException(msg); |
| 11144 | } |
| 11145 | } |
| 11146 | } |
| 11147 | if (userId == UserHandle.USER_CURRENT |
| 11148 | || userId == UserHandle.USER_CURRENT_OR_SELF) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11149 | // Note that we may be accessing this outside of a lock... |
| 11150 | // shouldn't be a big deal, if this is being called outside |
| 11151 | // of a locked context there is intrinsically a race with |
| 11152 | // the value the caller will receive and someone else changing it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11153 | userId = mCurrentUserId; |
| 11154 | } |
| 11155 | if (!allowAll && userId < 0) { |
| 11156 | throw new IllegalArgumentException( |
| 11157 | "Call does not support special user #" + userId); |
| 11158 | } |
| 11159 | } |
| 11160 | return userId; |
| 11161 | } |
| 11162 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11163 | boolean isSingleton(String componentProcessName, ApplicationInfo aInfo, |
| 11164 | String className, int flags) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11165 | boolean result = false; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11166 | if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11167 | if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) { |
| 11168 | if (ActivityManager.checkUidPermission( |
| 11169 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 11170 | aInfo.uid) != PackageManager.PERMISSION_GRANTED) { |
| 11171 | ComponentName comp = new ComponentName(aInfo.packageName, className); |
| 11172 | String msg = "Permission Denial: Component " + comp.flattenToShortString() |
| 11173 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 11174 | + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 11175 | Slog.w(TAG, msg); |
| 11176 | throw new SecurityException(msg); |
| 11177 | } |
| 11178 | result = true; |
| 11179 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11180 | } else if (componentProcessName == aInfo.packageName) { |
| 11181 | result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 11182 | } else if ("system".equals(componentProcessName)) { |
| 11183 | result = true; |
| 11184 | } |
| 11185 | if (DEBUG_MU) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11186 | Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo |
| 11187 | + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11188 | } |
| 11189 | return result; |
| 11190 | } |
| 11191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11192 | public int bindService(IApplicationThread caller, IBinder token, |
| 11193 | Intent service, String resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 11194 | IServiceConnection connection, int flags, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11195 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11196 | // Refuse possible leaked file descriptors |
| 11197 | if (service != null && service.hasFileDescriptors() == true) { |
| 11198 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11199 | } |
| 11200 | |
| 11201 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11202 | return mServices.bindServiceLocked(caller, token, service, resolvedType, |
| 11203 | connection, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11204 | } |
| 11205 | } |
| 11206 | |
| 11207 | public boolean unbindService(IServiceConnection connection) { |
| 11208 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11209 | return mServices.unbindServiceLocked(connection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11211 | } |
| 11212 | |
| 11213 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 11214 | // Refuse possible leaked file descriptors |
| 11215 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11216 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11217 | } |
| 11218 | |
| 11219 | synchronized(this) { |
| 11220 | if (!(token instanceof ServiceRecord)) { |
| 11221 | throw new IllegalArgumentException("Invalid service token"); |
| 11222 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11223 | mServices.publishServiceLocked((ServiceRecord)token, intent, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11224 | } |
| 11225 | } |
| 11226 | |
| 11227 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 11228 | // Refuse possible leaked file descriptors |
| 11229 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11230 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11231 | } |
| 11232 | |
| 11233 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11234 | mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11235 | } |
| 11236 | } |
| 11237 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11238 | 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] | 11239 | synchronized(this) { |
| 11240 | if (!(token instanceof ServiceRecord)) { |
| 11241 | throw new IllegalArgumentException("Invalid service token"); |
| 11242 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11243 | mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11244 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11245 | } |
| 11246 | |
| 11247 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11248 | // BACKUP AND RESTORE |
| 11249 | // ========================================================= |
| 11250 | |
| 11251 | // Cause the target app to be launched if necessary and its backup agent |
| 11252 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11253 | // activity manager to announce its creation. |
| 11254 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11255 | if (DEBUG_BACKUP) Slog.v(TAG, "bindBackupAgent: app=" + app + " mode=" + backupMode); |
| 11256 | enforceCallingPermission("android.permission.BACKUP", "bindBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11257 | |
| 11258 | synchronized(this) { |
| 11259 | // !!! TODO: currently no check here that we're already bound |
| 11260 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11261 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11262 | synchronized (stats) { |
| 11263 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11264 | } |
| 11265 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11266 | // Backup agent is now in use, its package can't be stopped. |
| 11267 | try { |
| 11268 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11269 | app.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11270 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11271 | } catch (IllegalArgumentException e) { |
| 11272 | Slog.w(TAG, "Failed trying to unstop package " |
| 11273 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11274 | } |
| 11275 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11276 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11277 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11278 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11279 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11280 | // startProcessLocked() returns existing proc's record if it's already running |
| 11281 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11282 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11283 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11284 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11285 | return false; |
| 11286 | } |
| 11287 | |
| 11288 | r.app = proc; |
| 11289 | mBackupTarget = r; |
| 11290 | mBackupAppName = app.packageName; |
| 11291 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11292 | // Try not to kill the process during backup |
| 11293 | updateOomAdjLocked(proc); |
| 11294 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11295 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11296 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11297 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11298 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11299 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11300 | proc.thread.scheduleCreateBackupAgent(app, |
| 11301 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11302 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11303 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11304 | } |
| 11305 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11306 | 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] | 11307 | } |
| 11308 | // Invariants: at this point, the target app process exists and the application |
| 11309 | // is either already running or in the process of coming up. mBackupTarget and |
| 11310 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11311 | // know that it's scheduled for a backup-agent operation. |
| 11312 | } |
| 11313 | |
| 11314 | return true; |
| 11315 | } |
| 11316 | |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11317 | @Override |
| 11318 | public void clearPendingBackup() { |
| 11319 | if (DEBUG_BACKUP) Slog.v(TAG, "clearPendingBackup"); |
| 11320 | enforceCallingPermission("android.permission.BACKUP", "clearPendingBackup"); |
| 11321 | |
| 11322 | synchronized (this) { |
| 11323 | mBackupTarget = null; |
| 11324 | mBackupAppName = null; |
| 11325 | } |
| 11326 | } |
| 11327 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11328 | // A backup agent has just come up |
| 11329 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11330 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11331 | + " = " + agent); |
| 11332 | |
| 11333 | synchronized(this) { |
| 11334 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11335 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11336 | return; |
| 11337 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11338 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11339 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11340 | long oldIdent = Binder.clearCallingIdentity(); |
| 11341 | try { |
| 11342 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 11343 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 11344 | bm.agentConnected(agentPackageName, agent); |
| 11345 | } catch (RemoteException e) { |
| 11346 | // can't happen; the backup manager service is local |
| 11347 | } catch (Exception e) { |
| 11348 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 11349 | e.printStackTrace(); |
| 11350 | } finally { |
| 11351 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11352 | } |
| 11353 | } |
| 11354 | |
| 11355 | // done with this agent |
| 11356 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11357 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11358 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11359 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11360 | return; |
| 11361 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11362 | |
| 11363 | synchronized(this) { |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11364 | try { |
| 11365 | if (mBackupAppName == null) { |
| 11366 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
| 11367 | return; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11368 | } |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11369 | |
| 11370 | if (!mBackupAppName.equals(appInfo.packageName)) { |
| 11371 | Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target"); |
| 11372 | return; |
| 11373 | } |
| 11374 | |
| 11375 | // Not backing this app up any more; reset its OOM adjustment |
| 11376 | final ProcessRecord proc = mBackupTarget.app; |
| 11377 | updateOomAdjLocked(proc); |
| 11378 | |
| 11379 | // If the app crashed during backup, 'thread' will be null here |
| 11380 | if (proc.thread != null) { |
| 11381 | try { |
| 11382 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 11383 | compatibilityInfoForPackageLocked(appInfo)); |
| 11384 | } catch (Exception e) { |
| 11385 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
| 11386 | e.printStackTrace(); |
| 11387 | } |
| 11388 | } |
| 11389 | } finally { |
| 11390 | mBackupTarget = null; |
| 11391 | mBackupAppName = null; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11392 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11393 | } |
| 11394 | } |
| 11395 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11396 | // BROADCASTS |
| 11397 | // ========================================================= |
| 11398 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11399 | private final List getStickiesLocked(String action, IntentFilter filter, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11400 | List cur, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11401 | final ContentResolver resolver = mContext.getContentResolver(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11402 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11403 | if (stickies == null) { |
| 11404 | return cur; |
| 11405 | } |
| 11406 | final ArrayList<Intent> list = stickies.get(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11407 | if (list == null) { |
| 11408 | return cur; |
| 11409 | } |
| 11410 | int N = list.size(); |
| 11411 | for (int i=0; i<N; i++) { |
| 11412 | Intent intent = list.get(i); |
| 11413 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 11414 | if (cur == null) { |
| 11415 | cur = new ArrayList<Intent>(); |
| 11416 | } |
| 11417 | cur.add(intent); |
| 11418 | } |
| 11419 | } |
| 11420 | return cur; |
| 11421 | } |
| 11422 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11423 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 11424 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 11425 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 11426 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11427 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11428 | void skipPendingBroadcastLocked(int pid) { |
| 11429 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 11430 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11431 | queue.skipPendingBroadcastLocked(pid); |
| 11432 | } |
| 11433 | } |
| 11434 | |
| 11435 | // The app just attached; send any pending broadcasts that it should receive |
| 11436 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 11437 | boolean didSomething = false; |
| 11438 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11439 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11440 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11441 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11442 | } |
| 11443 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11444 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11445 | IIntentReceiver receiver, IntentFilter filter, String permission, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11446 | enforceNotIsolatedCaller("registerReceiver"); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11447 | int callingUid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11448 | int callingPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11449 | synchronized(this) { |
| 11450 | ProcessRecord callerApp = null; |
| 11451 | if (caller != null) { |
| 11452 | callerApp = getRecordForAppLocked(caller); |
| 11453 | if (callerApp == null) { |
| 11454 | throw new SecurityException( |
| 11455 | "Unable to find app for caller " + caller |
| 11456 | + " (pid=" + Binder.getCallingPid() |
| 11457 | + ") when registering receiver " + receiver); |
| 11458 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11459 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 11460 | !callerApp.pkgList.contains(callerPackage)) { |
| 11461 | throw new SecurityException("Given caller package " + callerPackage |
| 11462 | + " is not running in process " + callerApp); |
| 11463 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11464 | callingUid = callerApp.info.uid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11465 | callingPid = callerApp.pid; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11466 | } else { |
| 11467 | callerPackage = null; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11468 | callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11469 | callingPid = Binder.getCallingPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11470 | } |
| 11471 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11472 | userId = this.handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11473 | true, true, "registerReceiver", callerPackage); |
| 11474 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11475 | List allSticky = null; |
| 11476 | |
| 11477 | // Look for any matching sticky broadcasts... |
| 11478 | Iterator actions = filter.actionsIterator(); |
| 11479 | if (actions != null) { |
| 11480 | while (actions.hasNext()) { |
| 11481 | String action = (String)actions.next(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11482 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11483 | UserHandle.USER_ALL); |
| 11484 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11485 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11486 | } |
| 11487 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11488 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11489 | UserHandle.USER_ALL); |
| 11490 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11491 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11492 | } |
| 11493 | |
| 11494 | // The first sticky in the list is returned directly back to |
| 11495 | // the client. |
| 11496 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 11497 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11498 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11499 | + ": " + sticky); |
| 11500 | |
| 11501 | if (receiver == null) { |
| 11502 | return sticky; |
| 11503 | } |
| 11504 | |
| 11505 | ReceiverList rl |
| 11506 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11507 | if (rl == null) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11508 | rl = new ReceiverList(this, callerApp, callingPid, callingUid, |
| 11509 | userId, receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11510 | if (rl.app != null) { |
| 11511 | rl.app.receivers.add(rl); |
| 11512 | } else { |
| 11513 | try { |
| 11514 | receiver.asBinder().linkToDeath(rl, 0); |
| 11515 | } catch (RemoteException e) { |
| 11516 | return sticky; |
| 11517 | } |
| 11518 | rl.linkedToDeath = true; |
| 11519 | } |
| 11520 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11521 | } else if (rl.uid != callingUid) { |
| 11522 | throw new IllegalArgumentException( |
| 11523 | "Receiver requested to register for uid " + callingUid |
| 11524 | + " was previously registered for uid " + rl.uid); |
| 11525 | } else if (rl.pid != callingPid) { |
| 11526 | throw new IllegalArgumentException( |
| 11527 | "Receiver requested to register for pid " + callingPid |
| 11528 | + " was previously registered for pid " + rl.pid); |
| 11529 | } else if (rl.userId != userId) { |
| 11530 | throw new IllegalArgumentException( |
| 11531 | "Receiver requested to register for user " + userId |
| 11532 | + " was previously registered for user " + rl.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11533 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11534 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11535 | permission, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11536 | rl.add(bf); |
| 11537 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11538 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11539 | } |
| 11540 | mReceiverResolver.addFilter(bf); |
| 11541 | |
| 11542 | // Enqueue broadcasts for all existing stickies that match |
| 11543 | // this filter. |
| 11544 | if (allSticky != null) { |
| 11545 | ArrayList receivers = new ArrayList(); |
| 11546 | receivers.add(bf); |
| 11547 | |
| 11548 | int N = allSticky.size(); |
| 11549 | for (int i=0; i<N; i++) { |
| 11550 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11551 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11552 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11553 | null, -1, -1, null, receivers, null, 0, null, null, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11554 | false, true, true, -1); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11555 | queue.enqueueParallelBroadcastLocked(r); |
| 11556 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11557 | } |
| 11558 | } |
| 11559 | |
| 11560 | return sticky; |
| 11561 | } |
| 11562 | } |
| 11563 | |
| 11564 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11565 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11566 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11567 | final long origId = Binder.clearCallingIdentity(); |
| 11568 | try { |
| 11569 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11570 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11571 | synchronized(this) { |
| 11572 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11573 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11574 | if (rl != null) { |
| 11575 | if (rl.curBroadcast != null) { |
| 11576 | BroadcastRecord r = rl.curBroadcast; |
| 11577 | final boolean doNext = finishReceiverLocked( |
| 11578 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11579 | r.resultExtras, r.resultAbort, true); |
| 11580 | if (doNext) { |
| 11581 | doTrim = true; |
| 11582 | r.queue.processNextBroadcast(false); |
| 11583 | } |
| 11584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11585 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11586 | if (rl.app != null) { |
| 11587 | rl.app.receivers.remove(rl); |
| 11588 | } |
| 11589 | removeReceiverLocked(rl); |
| 11590 | if (rl.linkedToDeath) { |
| 11591 | rl.linkedToDeath = false; |
| 11592 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 11593 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11594 | } |
| 11595 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11596 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11597 | // If we actually concluded any broadcasts, we might now be able |
| 11598 | // to trim the recipients' apps from our working set |
| 11599 | if (doTrim) { |
| 11600 | trimApplications(); |
| 11601 | return; |
| 11602 | } |
| 11603 | |
| 11604 | } finally { |
| 11605 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11607 | } |
| 11608 | |
| 11609 | void removeReceiverLocked(ReceiverList rl) { |
| 11610 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 11611 | int N = rl.size(); |
| 11612 | for (int i=0; i<N; i++) { |
| 11613 | mReceiverResolver.removeFilter(rl.get(i)); |
| 11614 | } |
| 11615 | } |
| 11616 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11617 | private final void sendPackageBroadcastLocked(int cmd, String[] packages, int userId) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11618 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 11619 | ProcessRecord r = mLruProcesses.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11620 | if (r.thread != null && (userId == UserHandle.USER_ALL || r.userId == userId)) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11621 | try { |
| 11622 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 11623 | } catch (RemoteException ex) { |
| 11624 | } |
| 11625 | } |
| 11626 | } |
| 11627 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11628 | |
| 11629 | private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType, |
| 11630 | int[] users) { |
| 11631 | List<ResolveInfo> receivers = null; |
| 11632 | try { |
| 11633 | HashSet<ComponentName> singleUserReceivers = null; |
| 11634 | boolean scannedFirstReceivers = false; |
| 11635 | for (int user : users) { |
| 11636 | List<ResolveInfo> newReceivers = AppGlobals.getPackageManager() |
| 11637 | .queryIntentReceivers(intent, resolvedType, STOCK_PM_FLAGS, user); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 11638 | if (user != 0 && newReceivers != null) { |
| 11639 | // If this is not the primary user, we need to check for |
| 11640 | // any receivers that should be filtered out. |
| 11641 | for (int i=0; i<newReceivers.size(); i++) { |
| 11642 | ResolveInfo ri = newReceivers.get(i); |
| 11643 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_PRIMARY_USER_ONLY) != 0) { |
| 11644 | newReceivers.remove(i); |
| 11645 | i--; |
| 11646 | } |
| 11647 | } |
| 11648 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11649 | if (newReceivers != null && newReceivers.size() == 0) { |
| 11650 | newReceivers = null; |
| 11651 | } |
| 11652 | if (receivers == null) { |
| 11653 | receivers = newReceivers; |
| 11654 | } else if (newReceivers != null) { |
| 11655 | // We need to concatenate the additional receivers |
| 11656 | // found with what we have do far. This would be easy, |
| 11657 | // but we also need to de-dup any receivers that are |
| 11658 | // singleUser. |
| 11659 | if (!scannedFirstReceivers) { |
| 11660 | // Collect any single user receivers we had already retrieved. |
| 11661 | scannedFirstReceivers = true; |
| 11662 | for (int i=0; i<receivers.size(); i++) { |
| 11663 | ResolveInfo ri = receivers.get(i); |
| 11664 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11665 | ComponentName cn = new ComponentName( |
| 11666 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11667 | if (singleUserReceivers == null) { |
| 11668 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11669 | } |
| 11670 | singleUserReceivers.add(cn); |
| 11671 | } |
| 11672 | } |
| 11673 | } |
| 11674 | // Add the new results to the existing results, tracking |
| 11675 | // and de-dupping single user receivers. |
| 11676 | for (int i=0; i<newReceivers.size(); i++) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 11677 | ResolveInfo ri = newReceivers.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11678 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11679 | ComponentName cn = new ComponentName( |
| 11680 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11681 | if (singleUserReceivers == null) { |
| 11682 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11683 | } |
| 11684 | if (!singleUserReceivers.contains(cn)) { |
| 11685 | singleUserReceivers.add(cn); |
| 11686 | receivers.add(ri); |
| 11687 | } |
| 11688 | } else { |
| 11689 | receivers.add(ri); |
| 11690 | } |
| 11691 | } |
| 11692 | } |
| 11693 | } |
| 11694 | } catch (RemoteException ex) { |
| 11695 | // pm is in same process, this will never happen. |
| 11696 | } |
| 11697 | return receivers; |
| 11698 | } |
| 11699 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11700 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 11701 | String callerPackage, Intent intent, String resolvedType, |
| 11702 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 11703 | Bundle map, String requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11704 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 11705 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11706 | intent = new Intent(intent); |
| 11707 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11708 | // By default broadcasts do not go to stopped apps. |
| 11709 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 11710 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11711 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11712 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 11713 | + " ordered=" + ordered + " userid=" + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11714 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11715 | 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] | 11716 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11717 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11718 | userId = handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11719 | true, false, "broadcast", callerPackage); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11720 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11721 | // Make sure that the user who is receiving this broadcast is started. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11722 | // If not, we will just skip it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11723 | if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 11724 | if (callingUid != Process.SYSTEM_UID || (intent.getFlags() |
| 11725 | & Intent.FLAG_RECEIVER_BOOT_UPGRADE) == 0) { |
| 11726 | Slog.w(TAG, "Skipping broadcast of " + intent |
| 11727 | + ": user " + userId + " is stopped"); |
| 11728 | return ActivityManager.BROADCAST_SUCCESS; |
| 11729 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11730 | } |
| 11731 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11732 | /* |
| 11733 | * Prevent non-system code (defined here to be non-persistent |
| 11734 | * processes) from sending protected broadcasts. |
| 11735 | */ |
Dianne Hackborn | c7ba771 | 2012-09-26 23:22:59 -0700 | [diff] [blame] | 11736 | int callingAppId = UserHandle.getAppId(callingUid); |
| 11737 | if (callingAppId == Process.SYSTEM_UID || callingAppId == Process.PHONE_UID |
| 11738 | || callingAppId == Process.SHELL_UID || callingAppId == Process.BLUETOOTH_UID || |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11739 | callingUid == 0) { |
| 11740 | // Always okay. |
| 11741 | } else if (callerApp == null || !callerApp.persistent) { |
| 11742 | try { |
| 11743 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
| 11744 | intent.getAction())) { |
| 11745 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11746 | + intent.getAction() + " from pid=" |
| 11747 | + callingPid + ", uid=" + callingUid; |
| 11748 | Slog.w(TAG, msg); |
| 11749 | throw new SecurityException(msg); |
| 11750 | } |
| 11751 | } catch (RemoteException e) { |
| 11752 | Slog.w(TAG, "Remote exception", e); |
| 11753 | return ActivityManager.BROADCAST_SUCCESS; |
| 11754 | } |
| 11755 | } |
| 11756 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11757 | // Handle special intents: if this broadcast is from the package |
| 11758 | // manager about a package being removed, we need to remove all of |
| 11759 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11760 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11761 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11762 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11763 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11764 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11765 | || uidRemoved) { |
| 11766 | if (checkComponentPermission( |
| 11767 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11768 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11769 | == PackageManager.PERMISSION_GRANTED) { |
| 11770 | if (uidRemoved) { |
| 11771 | final Bundle intentExtras = intent.getExtras(); |
| 11772 | final int uid = intentExtras != null |
| 11773 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11774 | if (uid >= 0) { |
| 11775 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11776 | synchronized (bs) { |
| 11777 | bs.removeUidStatsLocked(uid); |
| 11778 | } |
| 11779 | } |
| 11780 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11781 | // If resources are unavailable just force stop all |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11782 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11783 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11784 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11785 | if (list != null && (list.length > 0)) { |
| 11786 | for (String pkg : list) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11787 | forceStopPackageLocked(pkg, -1, false, true, true, false, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11788 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11789 | sendPackageBroadcastLocked( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11790 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11791 | } |
| 11792 | } else { |
| 11793 | Uri data = intent.getData(); |
| 11794 | String ssp; |
| 11795 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11796 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11797 | forceStopPackageLocked(ssp, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11798 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, |
| 11799 | false, userId); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11800 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11801 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11802 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11803 | new String[] {ssp}, userId); |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11804 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11805 | } |
| 11806 | } |
| 11807 | } |
| 11808 | } else { |
| 11809 | String msg = "Permission Denial: " + intent.getAction() |
| 11810 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11811 | + ", uid=" + callingUid + ")" |
| 11812 | + " requires " |
| 11813 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11814 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11815 | throw new SecurityException(msg); |
| 11816 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11817 | |
| 11818 | // Special case for adding a package: by default turn on compatibility |
| 11819 | // mode. |
| 11820 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 11821 | Uri data = intent.getData(); |
| 11822 | String ssp; |
| 11823 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11824 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 11825 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11826 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11827 | } |
| 11828 | |
| 11829 | /* |
| 11830 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11831 | * of all currently running processes. This message will get queued up before the broadcast |
| 11832 | * happens. |
| 11833 | */ |
| 11834 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11835 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11836 | } |
| 11837 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11838 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11839 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11840 | } |
| 11841 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11842 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11843 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11844 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11845 | } |
| 11846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11847 | // Add to the sticky list if requested. |
| 11848 | if (sticky) { |
| 11849 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11850 | callingPid, callingUid) |
| 11851 | != PackageManager.PERMISSION_GRANTED) { |
| 11852 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11853 | + callingPid + ", uid=" + callingUid |
| 11854 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11855 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11856 | throw new SecurityException(msg); |
| 11857 | } |
| 11858 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11859 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11860 | + " and enforce permission " + requiredPermission); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11861 | return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11862 | } |
| 11863 | if (intent.getComponent() != null) { |
| 11864 | throw new SecurityException( |
| 11865 | "Sticky broadcasts can't target a specific component"); |
| 11866 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11867 | // We use userId directly here, since the "all" target is maintained |
| 11868 | // as a separate set of sticky broadcasts. |
| 11869 | if (userId != UserHandle.USER_ALL) { |
| 11870 | // But first, if this is not a broadcast to all users, then |
| 11871 | // make sure it doesn't conflict with an existing broadcast to |
| 11872 | // all users. |
| 11873 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get( |
| 11874 | UserHandle.USER_ALL); |
| 11875 | if (stickies != null) { |
| 11876 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11877 | if (list != null) { |
| 11878 | int N = list.size(); |
| 11879 | int i; |
| 11880 | for (i=0; i<N; i++) { |
| 11881 | if (intent.filterEquals(list.get(i))) { |
| 11882 | throw new IllegalArgumentException( |
| 11883 | "Sticky broadcast " + intent + " for user " |
| 11884 | + userId + " conflicts with existing global broadcast"); |
| 11885 | } |
| 11886 | } |
| 11887 | } |
| 11888 | } |
| 11889 | } |
| 11890 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11891 | if (stickies == null) { |
| 11892 | stickies = new HashMap<String, ArrayList<Intent>>(); |
| 11893 | mStickyBroadcasts.put(userId, stickies); |
| 11894 | } |
| 11895 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11896 | if (list == null) { |
| 11897 | list = new ArrayList<Intent>(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11898 | stickies.put(intent.getAction(), list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11899 | } |
| 11900 | int N = list.size(); |
| 11901 | int i; |
| 11902 | for (i=0; i<N; i++) { |
| 11903 | if (intent.filterEquals(list.get(i))) { |
| 11904 | // This sticky already exists, replace it. |
| 11905 | list.set(i, new Intent(intent)); |
| 11906 | break; |
| 11907 | } |
| 11908 | } |
| 11909 | if (i >= N) { |
| 11910 | list.add(new Intent(intent)); |
| 11911 | } |
| 11912 | } |
| 11913 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11914 | int[] users; |
| 11915 | if (userId == UserHandle.USER_ALL) { |
| 11916 | // Caller wants broadcast to go to all started users. |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11917 | users = mStartedUserArray; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11918 | } else { |
| 11919 | // Caller wants broadcast to go to one specific user. |
Amith Yamasani | e98bde0 | 2012-10-01 11:30:47 -0700 | [diff] [blame] | 11920 | users = new int[] {userId}; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11921 | } |
| 11922 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11923 | // Figure out who all will receive this broadcast. |
| 11924 | List receivers = null; |
| 11925 | List<BroadcastFilter> registeredReceivers = null; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11926 | // Need to resolve the intent to interested receivers... |
| 11927 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11928 | == 0) { |
| 11929 | receivers = collectReceiverComponents(intent, resolvedType, users); |
| 11930 | } |
| 11931 | if (intent.getComponent() == null) { |
| 11932 | registeredReceivers = mReceiverResolver.queryIntent(intent, |
| 11933 | resolvedType, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11934 | } |
| 11935 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11936 | final boolean replacePending = |
| 11937 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11938 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11939 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11940 | + " replacePending=" + replacePending); |
| 11941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11942 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11943 | if (!ordered && NR > 0) { |
| 11944 | // If we are not serializing this broadcast, then send the |
| 11945 | // registered receivers separately so they don't wait for the |
| 11946 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11947 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11948 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11949 | callerPackage, callingPid, callingUid, requiredPermission, |
| 11950 | registeredReceivers, resultTo, resultCode, resultData, map, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11951 | ordered, sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11952 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11953 | TAG, "Enqueueing parallel broadcast " + r); |
| 11954 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11955 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11956 | queue.enqueueParallelBroadcastLocked(r); |
| 11957 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11958 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11959 | registeredReceivers = null; |
| 11960 | NR = 0; |
| 11961 | } |
| 11962 | |
| 11963 | // Merge into one list. |
| 11964 | int ir = 0; |
| 11965 | if (receivers != null) { |
| 11966 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11967 | // being added to see this broadcast. This prevents them from |
| 11968 | // using this as a back door to get run as soon as they are |
| 11969 | // installed. Maybe in the future we want to have a special install |
| 11970 | // broadcast or such for apps, but we'd like to deliberately make |
| 11971 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11972 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11973 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11974 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11975 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11976 | Uri data = intent.getData(); |
| 11977 | if (data != null) { |
| 11978 | String pkgName = data.getSchemeSpecificPart(); |
| 11979 | if (pkgName != null) { |
| 11980 | skipPackages = new String[] { pkgName }; |
| 11981 | } |
| 11982 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11983 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11984 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11985 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11986 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 11987 | for (String skipPackage : skipPackages) { |
| 11988 | if (skipPackage != null) { |
| 11989 | int NT = receivers.size(); |
| 11990 | for (int it=0; it<NT; it++) { |
| 11991 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 11992 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 11993 | receivers.remove(it); |
| 11994 | it--; |
| 11995 | NT--; |
| 11996 | } |
| 11997 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11998 | } |
| 11999 | } |
| 12000 | } |
| 12001 | |
| 12002 | int NT = receivers != null ? receivers.size() : 0; |
| 12003 | int it = 0; |
| 12004 | ResolveInfo curt = null; |
| 12005 | BroadcastFilter curr = null; |
| 12006 | while (it < NT && ir < NR) { |
| 12007 | if (curt == null) { |
| 12008 | curt = (ResolveInfo)receivers.get(it); |
| 12009 | } |
| 12010 | if (curr == null) { |
| 12011 | curr = registeredReceivers.get(ir); |
| 12012 | } |
| 12013 | if (curr.getPriority() >= curt.priority) { |
| 12014 | // Insert this broadcast record into the final list. |
| 12015 | receivers.add(it, curr); |
| 12016 | ir++; |
| 12017 | curr = null; |
| 12018 | it++; |
| 12019 | NT++; |
| 12020 | } else { |
| 12021 | // Skip to the next ResolveInfo in the final list. |
| 12022 | it++; |
| 12023 | curt = null; |
| 12024 | } |
| 12025 | } |
| 12026 | } |
| 12027 | while (ir < NR) { |
| 12028 | if (receivers == null) { |
| 12029 | receivers = new ArrayList(); |
| 12030 | } |
| 12031 | receivers.add(registeredReceivers.get(ir)); |
| 12032 | ir++; |
| 12033 | } |
| 12034 | |
| 12035 | if ((receivers != null && receivers.size() > 0) |
| 12036 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12037 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 12038 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12039 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12040 | receivers, resultTo, resultCode, resultData, map, ordered, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 12041 | sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12042 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12043 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12044 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12045 | if (DEBUG_BROADCAST) { |
| 12046 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12047 | 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] | 12048 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12049 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12050 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12051 | queue.enqueueOrderedBroadcastLocked(r); |
| 12052 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12053 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12054 | } |
| 12055 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 12056 | return ActivityManager.BROADCAST_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12057 | } |
| 12058 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12059 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12060 | // Refuse possible leaked file descriptors |
| 12061 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12062 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12063 | } |
| 12064 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12065 | int flags = intent.getFlags(); |
| 12066 | |
| 12067 | if (!mProcessesReady) { |
| 12068 | // if the caller really truly claims to know what they're doing, go |
| 12069 | // ahead and allow the broadcast without launching any receivers |
| 12070 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 12071 | intent = new Intent(intent); |
| 12072 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 12073 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 12074 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 12075 | + " before boot completion"); |
| 12076 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 12077 | } |
| 12078 | } |
| 12079 | |
| 12080 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 12081 | throw new IllegalArgumentException( |
| 12082 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 12083 | } |
| 12084 | |
| 12085 | return intent; |
| 12086 | } |
| 12087 | |
| 12088 | public final int broadcastIntent(IApplicationThread caller, |
| 12089 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12090 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12091 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12092 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12093 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12094 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12095 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12096 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 12097 | final int callingPid = Binder.getCallingPid(); |
| 12098 | final int callingUid = Binder.getCallingUid(); |
| 12099 | final long origId = Binder.clearCallingIdentity(); |
| 12100 | int res = broadcastIntentLocked(callerApp, |
| 12101 | callerApp != null ? callerApp.info.packageName : null, |
| 12102 | intent, resolvedType, resultTo, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12103 | resultCode, resultData, map, requiredPermission, serialized, sticky, |
| 12104 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12105 | Binder.restoreCallingIdentity(origId); |
| 12106 | return res; |
| 12107 | } |
| 12108 | } |
| 12109 | |
| 12110 | int broadcastIntentInPackage(String packageName, int uid, |
| 12111 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12112 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12113 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12114 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12115 | intent = verifyBroadcastLocked(intent); |
| 12116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12117 | final long origId = Binder.clearCallingIdentity(); |
| 12118 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 12119 | resultTo, resultCode, resultData, map, requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12120 | serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12121 | Binder.restoreCallingIdentity(origId); |
| 12122 | return res; |
| 12123 | } |
| 12124 | } |
| 12125 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12126 | 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] | 12127 | // Refuse possible leaked file descriptors |
| 12128 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12129 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12130 | } |
| 12131 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 12132 | userId = handleIncomingUser(Binder.getCallingPid(), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12133 | Binder.getCallingUid(), userId, true, false, "removeStickyBroadcast", null); |
| 12134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12135 | synchronized(this) { |
| 12136 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 12137 | != PackageManager.PERMISSION_GRANTED) { |
| 12138 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 12139 | + Binder.getCallingPid() |
| 12140 | + ", uid=" + Binder.getCallingUid() |
| 12141 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12142 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12143 | throw new SecurityException(msg); |
| 12144 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12145 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 12146 | if (stickies != null) { |
| 12147 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 12148 | if (list != null) { |
| 12149 | int N = list.size(); |
| 12150 | int i; |
| 12151 | for (i=0; i<N; i++) { |
| 12152 | if (intent.filterEquals(list.get(i))) { |
| 12153 | list.remove(i); |
| 12154 | break; |
| 12155 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12156 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12157 | if (list.size() <= 0) { |
| 12158 | stickies.remove(intent.getAction()); |
| 12159 | } |
| 12160 | } |
| 12161 | if (stickies.size() <= 0) { |
| 12162 | mStickyBroadcasts.remove(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12163 | } |
| 12164 | } |
| 12165 | } |
| 12166 | } |
| 12167 | |
| 12168 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 12169 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 12170 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12171 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 12172 | if (r == null) { |
| 12173 | 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] | 12174 | return false; |
| 12175 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12176 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12177 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 12178 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12179 | } |
| 12180 | |
| 12181 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 12182 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12183 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12184 | |
| 12185 | // Refuse possible leaked file descriptors |
| 12186 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 12187 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12188 | } |
| 12189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12190 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12191 | try { |
| 12192 | boolean doNext = false; |
| 12193 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12194 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12195 | synchronized(this) { |
| 12196 | r = broadcastRecordForReceiverLocked(who); |
| 12197 | if (r != null) { |
| 12198 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 12199 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12200 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12201 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12202 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12203 | if (doNext) { |
| 12204 | r.queue.processNextBroadcast(false); |
| 12205 | } |
| 12206 | trimApplications(); |
| 12207 | } finally { |
| 12208 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12209 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12212 | // ========================================================= |
| 12213 | // INSTRUMENTATION |
| 12214 | // ========================================================= |
| 12215 | |
| 12216 | public boolean startInstrumentation(ComponentName className, |
| 12217 | String profileFile, int flags, Bundle arguments, |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 12218 | IInstrumentationWatcher watcher, IUiAutomationConnection uiAutomationConnection, |
| 12219 | int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12220 | enforceNotIsolatedCaller("startInstrumentation"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 12221 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12222 | userId, false, true, "startInstrumentation", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12223 | // Refuse possible leaked file descriptors |
| 12224 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 12225 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12226 | } |
| 12227 | |
| 12228 | synchronized(this) { |
| 12229 | InstrumentationInfo ii = null; |
| 12230 | ApplicationInfo ai = null; |
| 12231 | try { |
| 12232 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12233 | className, STOCK_PM_FLAGS); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12234 | ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 12235 | ii.targetPackage, STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12236 | } catch (PackageManager.NameNotFoundException e) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12237 | } catch (RemoteException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12238 | } |
| 12239 | if (ii == null) { |
| 12240 | reportStartInstrumentationFailure(watcher, className, |
| 12241 | "Unable to find instrumentation info for: " + className); |
| 12242 | return false; |
| 12243 | } |
| 12244 | if (ai == null) { |
| 12245 | reportStartInstrumentationFailure(watcher, className, |
| 12246 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12247 | return false; |
| 12248 | } |
| 12249 | |
| 12250 | int match = mContext.getPackageManager().checkSignatures( |
| 12251 | ii.targetPackage, ii.packageName); |
| 12252 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12253 | String msg = "Permission Denial: starting instrumentation " |
| 12254 | + className + " from pid=" |
| 12255 | + Binder.getCallingPid() |
| 12256 | + ", uid=" + Binder.getCallingPid() |
| 12257 | + " not allowed because package " + ii.packageName |
| 12258 | + " does not have a signature matching the target " |
| 12259 | + ii.targetPackage; |
| 12260 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12261 | throw new SecurityException(msg); |
| 12262 | } |
| 12263 | |
| 12264 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12265 | // Instrumentation can kill and relaunch even persistent processes |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12266 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 12267 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12268 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12269 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12270 | app.instrumentationProfileFile = profileFile; |
| 12271 | app.instrumentationArguments = arguments; |
| 12272 | app.instrumentationWatcher = watcher; |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 12273 | app.instrumentationUiAutomationConnection = uiAutomationConnection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12274 | app.instrumentationResultClass = className; |
| 12275 | Binder.restoreCallingIdentity(origId); |
| 12276 | } |
| 12277 | |
| 12278 | return true; |
| 12279 | } |
| 12280 | |
| 12281 | /** |
| 12282 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 12283 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 12284 | * the "am" command to report errors with more information. |
| 12285 | * |
| 12286 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 12287 | * @param cn The component name of the instrumentation. |
| 12288 | * @param report The error report. |
| 12289 | */ |
| 12290 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 12291 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12292 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12293 | try { |
| 12294 | if (watcher != null) { |
| 12295 | Bundle results = new Bundle(); |
| 12296 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 12297 | results.putString("Error", report); |
| 12298 | watcher.instrumentationStatus(cn, -1, results); |
| 12299 | } |
| 12300 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12301 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12302 | } |
| 12303 | } |
| 12304 | |
| 12305 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 12306 | if (app.instrumentationWatcher != null) { |
| 12307 | try { |
| 12308 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 12309 | app.instrumentationWatcher.instrumentationFinished( |
| 12310 | app.instrumentationClass, |
| 12311 | resultCode, |
| 12312 | results); |
| 12313 | } catch (RemoteException e) { |
| 12314 | } |
| 12315 | } |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 12316 | if (app.instrumentationUiAutomationConnection != null) { |
| 12317 | try { |
| 12318 | app.instrumentationUiAutomationConnection.shutdown(); |
| 12319 | } catch (RemoteException re) { |
| 12320 | /* ignore */ |
| 12321 | } |
| 12322 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12323 | app.instrumentationWatcher = null; |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 12324 | app.instrumentationUiAutomationConnection = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12325 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12326 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12327 | app.instrumentationProfileFile = null; |
| 12328 | app.instrumentationArguments = null; |
| 12329 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 12330 | 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] | 12331 | } |
| 12332 | |
| 12333 | public void finishInstrumentation(IApplicationThread target, |
| 12334 | int resultCode, Bundle results) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 12335 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12336 | // Refuse possible leaked file descriptors |
| 12337 | if (results != null && results.hasFileDescriptors()) { |
| 12338 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12339 | } |
| 12340 | |
| 12341 | synchronized(this) { |
| 12342 | ProcessRecord app = getRecordForAppLocked(target); |
| 12343 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12344 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12345 | return; |
| 12346 | } |
| 12347 | final long origId = Binder.clearCallingIdentity(); |
| 12348 | finishInstrumentationLocked(app, resultCode, results); |
| 12349 | Binder.restoreCallingIdentity(origId); |
| 12350 | } |
| 12351 | } |
| 12352 | |
| 12353 | // ========================================================= |
| 12354 | // CONFIGURATION |
| 12355 | // ========================================================= |
| 12356 | |
| 12357 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 12358 | ConfigurationInfo config = new ConfigurationInfo(); |
| 12359 | synchronized (this) { |
| 12360 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 12361 | config.reqKeyboardType = mConfiguration.keyboard; |
| 12362 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12363 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 12364 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12365 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 12366 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12367 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 12368 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12369 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 12370 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 12371 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12372 | } |
| 12373 | return config; |
| 12374 | } |
| 12375 | |
| 12376 | public Configuration getConfiguration() { |
| 12377 | Configuration ci; |
| 12378 | synchronized(this) { |
| 12379 | ci = new Configuration(mConfiguration); |
| 12380 | } |
| 12381 | return ci; |
| 12382 | } |
| 12383 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12384 | public void updatePersistentConfiguration(Configuration values) { |
| 12385 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12386 | "updateConfiguration()"); |
| 12387 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 12388 | "updateConfiguration()"); |
| 12389 | if (values == null) { |
| 12390 | throw new NullPointerException("Configuration must not be null"); |
| 12391 | } |
| 12392 | |
| 12393 | synchronized(this) { |
| 12394 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12395 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12396 | Binder.restoreCallingIdentity(origId); |
| 12397 | } |
| 12398 | } |
| 12399 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12400 | public void updateConfiguration(Configuration values) { |
| 12401 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12402 | "updateConfiguration()"); |
| 12403 | |
| 12404 | synchronized(this) { |
| 12405 | if (values == null && mWindowManager != null) { |
| 12406 | // sentinel: fetch the current configuration from the window manager |
| 12407 | values = mWindowManager.computeNewConfiguration(); |
| 12408 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12409 | |
| 12410 | if (mWindowManager != null) { |
| 12411 | mProcessList.applyDisplaySize(mWindowManager); |
| 12412 | } |
| 12413 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12414 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12415 | if (values != null) { |
| 12416 | Settings.System.clearConfiguration(values); |
| 12417 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12418 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12419 | Binder.restoreCallingIdentity(origId); |
| 12420 | } |
| 12421 | } |
| 12422 | |
| 12423 | /** |
| 12424 | * Do either or both things: (1) change the current configuration, and (2) |
| 12425 | * make sure the given activity is running with the (now) current |
| 12426 | * configuration. Returns true if the activity has been left running, or |
| 12427 | * false if <var>starting</var> is being destroyed to match the new |
| 12428 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12429 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12430 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12431 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12432 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 12433 | // do nothing if we are headless |
| 12434 | if (mHeadless) return true; |
| 12435 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12436 | int changes = 0; |
| 12437 | |
| 12438 | boolean kept = true; |
| 12439 | |
| 12440 | if (values != null) { |
| 12441 | Configuration newConfig = new Configuration(mConfiguration); |
| 12442 | changes = newConfig.updateFrom(values); |
| 12443 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12444 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12445 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12446 | } |
| 12447 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12448 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12449 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12450 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12451 | saveLocaleLocked(values.locale, |
| 12452 | !values.locale.equals(mConfiguration.locale), |
| 12453 | values.userSetLocale); |
| 12454 | } |
| 12455 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12456 | mConfigurationSeq++; |
| 12457 | if (mConfigurationSeq <= 0) { |
| 12458 | mConfigurationSeq = 1; |
| 12459 | } |
| 12460 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12461 | mConfiguration = newConfig; |
Craig Mautner | ae44659 | 2012-12-06 19:05:05 -0800 | [diff] [blame] | 12462 | Slog.i(TAG, "Config changes=" + Integer.toHexString(changes) + " " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12463 | |
| 12464 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12465 | |
| 12466 | // TODO: If our config changes, should we auto dismiss any currently |
| 12467 | // showing dialogs? |
| 12468 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12469 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12470 | AttributeCache ac = AttributeCache.instance(); |
| 12471 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12472 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12474 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12475 | // Make sure all resources in our process are updated |
| 12476 | // right now, so that anyone who is going to retrieve |
| 12477 | // resource values after we return will be sure to get |
| 12478 | // the new ones. This is especially important during |
| 12479 | // boot, where the first config change needs to guarantee |
| 12480 | // all resources have that config before following boot |
| 12481 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12482 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12483 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12484 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12485 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12486 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12487 | mHandler.sendMessage(msg); |
| 12488 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12489 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12490 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12491 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12492 | try { |
| 12493 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12494 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12495 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12496 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12497 | } |
| 12498 | } catch (Exception e) { |
| 12499 | } |
| 12500 | } |
| 12501 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12502 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 12503 | | Intent.FLAG_RECEIVER_REPLACE_PENDING |
| 12504 | | Intent.FLAG_RECEIVER_FOREGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12505 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12506 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12507 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 12508 | intent = new Intent(Intent.ACTION_LOCALE_CHANGED); |
| 12509 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 12510 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12511 | null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12512 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12513 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12514 | } |
| 12515 | } |
| 12516 | |
| 12517 | if (changes != 0 && starting == null) { |
| 12518 | // If the configuration changed, and the caller is not already |
| 12519 | // in the process of starting an activity, then find the top |
| 12520 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12521 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12522 | } |
| 12523 | |
| 12524 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12525 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12526 | // And we need to make sure at this point that all other activities |
| 12527 | // are made visible with the correct configuration. |
| 12528 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12529 | } |
| 12530 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12531 | if (values != null && mWindowManager != null) { |
| 12532 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12533 | } |
| 12534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12535 | return kept; |
| 12536 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12537 | |
| 12538 | /** |
| 12539 | * Decide based on the configuration whether we should shouw the ANR, |
| 12540 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 12541 | * press the on-screen buttons, we shouldn't show the dialog. |
| 12542 | * |
| 12543 | * A thought: SystemUI might also want to get told about this, the Power |
| 12544 | * dialog / global actions also might want different behaviors. |
| 12545 | */ |
| 12546 | private static final boolean shouldShowDialogs(Configuration config) { |
| 12547 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 12548 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 12549 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12550 | |
| 12551 | /** |
| 12552 | * Save the locale. You must be inside a synchronized (this) block. |
| 12553 | */ |
| 12554 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 12555 | if(isDiff) { |
| 12556 | SystemProperties.set("user.language", l.getLanguage()); |
| 12557 | SystemProperties.set("user.region", l.getCountry()); |
| 12558 | } |
| 12559 | |
| 12560 | if(isPersist) { |
| 12561 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 12562 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 12563 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 12564 | } |
| 12565 | } |
| 12566 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12567 | @Override |
| 12568 | public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) { |
| 12569 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12570 | return srec != null && srec.task.affinity != null && |
| 12571 | srec.task.affinity.equals(destAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12572 | } |
| 12573 | |
| 12574 | public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode, |
| 12575 | Intent resultData) { |
| 12576 | ComponentName dest = destIntent.getComponent(); |
| 12577 | |
| 12578 | synchronized (this) { |
| 12579 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12580 | if (srec == null) { |
| 12581 | return false; |
| 12582 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12583 | ArrayList<ActivityRecord> history = srec.stack.mHistory; |
| 12584 | final int start = history.indexOf(srec); |
| 12585 | if (start < 0) { |
| 12586 | // Current activity is not in history stack; do nothing. |
| 12587 | return false; |
| 12588 | } |
| 12589 | int finishTo = start - 1; |
| 12590 | ActivityRecord parent = null; |
| 12591 | boolean foundParentInTask = false; |
| 12592 | if (dest != null) { |
| 12593 | TaskRecord tr = srec.task; |
| 12594 | for (int i = start - 1; i >= 0; i--) { |
| 12595 | ActivityRecord r = history.get(i); |
| 12596 | if (tr != r.task) { |
| 12597 | // Couldn't find parent in the same task; stop at the one above this. |
| 12598 | // (Root of current task; in-app "home" behavior) |
| 12599 | // Always at least finish the current activity. |
| 12600 | finishTo = Math.min(start - 1, i + 1); |
| 12601 | parent = history.get(finishTo); |
| 12602 | break; |
| 12603 | } else if (r.info.packageName.equals(dest.getPackageName()) && |
| 12604 | r.info.name.equals(dest.getClassName())) { |
| 12605 | finishTo = i; |
| 12606 | parent = r; |
| 12607 | foundParentInTask = true; |
| 12608 | break; |
| 12609 | } |
| 12610 | } |
| 12611 | } |
| 12612 | |
| 12613 | if (mController != null) { |
| 12614 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
| 12615 | if (next != null) { |
| 12616 | // ask watcher if this is allowed |
| 12617 | boolean resumeOK = true; |
| 12618 | try { |
| 12619 | resumeOK = mController.activityResuming(next.packageName); |
| 12620 | } catch (RemoteException e) { |
| 12621 | mController = null; |
| 12622 | } |
| 12623 | |
| 12624 | if (!resumeOK) { |
| 12625 | return false; |
| 12626 | } |
| 12627 | } |
| 12628 | } |
| 12629 | final long origId = Binder.clearCallingIdentity(); |
| 12630 | for (int i = start; i > finishTo; i--) { |
| 12631 | ActivityRecord r = history.get(i); |
| 12632 | mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 12633 | "navigate-up", true); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12634 | // Only return the supplied result for the first activity finished |
| 12635 | resultCode = Activity.RESULT_CANCELED; |
| 12636 | resultData = null; |
| 12637 | } |
| 12638 | |
| 12639 | if (parent != null && foundParentInTask) { |
| 12640 | final int parentLaunchMode = parent.info.launchMode; |
| 12641 | final int destIntentFlags = destIntent.getFlags(); |
| 12642 | if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || |
| 12643 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK || |
| 12644 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP || |
| 12645 | (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
Adam Powell | 69de7e1 | 2012-05-07 18:42:24 -0700 | [diff] [blame] | 12646 | parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12647 | } else { |
| 12648 | try { |
| 12649 | ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo( |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 12650 | destIntent.getComponent(), 0, srec.userId); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12651 | int res = mMainStack.startActivityLocked(srec.app.thread, destIntent, |
| 12652 | null, aInfo, parent.appToken, null, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 12653 | 0, -1, parent.launchedFromUid, parent.launchedFromPackage, |
| 12654 | 0, null, true, null); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12655 | foundParentInTask = res == ActivityManager.START_SUCCESS; |
| 12656 | } catch (RemoteException e) { |
| 12657 | foundParentInTask = false; |
| 12658 | } |
| 12659 | mMainStack.requestFinishActivityLocked(parent.appToken, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 12660 | resultData, "navigate-up", true); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12661 | } |
| 12662 | } |
| 12663 | Binder.restoreCallingIdentity(origId); |
| 12664 | return foundParentInTask; |
| 12665 | } |
| 12666 | } |
| 12667 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 12668 | public int getLaunchedFromUid(IBinder activityToken) { |
| 12669 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 12670 | if (srec == null) { |
| 12671 | return -1; |
| 12672 | } |
| 12673 | return srec.launchedFromUid; |
| 12674 | } |
| 12675 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 12676 | public String getLaunchedFromPackage(IBinder activityToken) { |
| 12677 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 12678 | if (srec == null) { |
| 12679 | return null; |
| 12680 | } |
| 12681 | return srec.launchedFromPackage; |
| 12682 | } |
| 12683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12684 | // ========================================================= |
| 12685 | // LIFETIME MANAGEMENT |
| 12686 | // ========================================================= |
| 12687 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12688 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 12689 | // on, or 'null' if the app is not an active broadcast recipient. |
| 12690 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 12691 | BroadcastRecord r = app.curReceiver; |
| 12692 | if (r != null) { |
| 12693 | return r.queue; |
| 12694 | } |
| 12695 | |
| 12696 | // It's not the current receiver, but it might be starting up to become one |
| 12697 | synchronized (this) { |
| 12698 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12699 | r = queue.mPendingBroadcast; |
| 12700 | if (r != null && r.curApp == app) { |
| 12701 | // found it; report which queue it's in |
| 12702 | return queue; |
| 12703 | } |
| 12704 | } |
| 12705 | } |
| 12706 | |
| 12707 | return null; |
| 12708 | } |
| 12709 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12710 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, int clientHiddenAdj, |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12711 | int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12712 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12713 | // This adjustment has already been computed. If we are calling |
| 12714 | // from the top, we may have already computed our adjustment with |
| 12715 | // an earlier hidden adjustment that isn't really for us... if |
| 12716 | // so, use the new hidden adjustment. |
| 12717 | if (!recursed && app.hidden) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12718 | if (app.hasActivities) { |
| 12719 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = hiddenAdj; |
| 12720 | } else if (app.hasClientActivities) { |
| 12721 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = clientHiddenAdj; |
| 12722 | } else { |
| 12723 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = emptyAdj; |
| 12724 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12725 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12726 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12727 | } |
| 12728 | |
| 12729 | if (app.thread == null) { |
| 12730 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12731 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12732 | 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] | 12733 | } |
| 12734 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12735 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 12736 | app.adjSource = null; |
| 12737 | app.adjTarget = null; |
| 12738 | app.empty = false; |
| 12739 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12740 | app.hasClientActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12741 | |
| 12742 | final int activitiesSize = app.activities.size(); |
| 12743 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12744 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12745 | // The max adjustment doesn't allow this app to be anything |
| 12746 | // below foreground, so it is not worth doing work for it. |
| 12747 | app.adjType = "fixed"; |
| 12748 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12749 | app.curRawAdj = app.nonStoppingAdj = app.maxAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12750 | app.hasActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12751 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12752 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12753 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12754 | // System process can do UI, and when they do we want to have |
| 12755 | // them trim their memory after the user leaves the UI. To |
| 12756 | // facilitate this, here we need to determine whether or not it |
| 12757 | // is currently showing UI. |
| 12758 | app.systemNoUi = true; |
| 12759 | if (app == TOP_APP) { |
| 12760 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12761 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12762 | } else if (activitiesSize > 0) { |
| 12763 | for (int j = 0; j < activitiesSize; j++) { |
| 12764 | final ActivityRecord r = app.activities.get(j); |
| 12765 | if (r.visible) { |
| 12766 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12767 | } |
| 12768 | if (r.app == app) { |
| 12769 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12770 | } |
| 12771 | } |
| 12772 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12773 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12774 | } |
| 12775 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12776 | app.keeping = false; |
| 12777 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12778 | app.hasActivities = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12779 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12780 | // Determine the importance of the process, starting with most |
| 12781 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12782 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12783 | int schedGroup; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12784 | boolean foregroundActivities = false; |
| 12785 | boolean interesting = false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12786 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12787 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12788 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12789 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12790 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12791 | app.adjType = "top-activity"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12792 | foregroundActivities = true; |
| 12793 | interesting = true; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12794 | app.hasActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12795 | } else if (app.instrumentationClass != null) { |
| 12796 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12797 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12798 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12799 | app.adjType = "instrumentation"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12800 | interesting = true; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12801 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12802 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12803 | // counts as being in the foreground for OOM killer purposes. |
| 12804 | // It's placed in a sched group based on the nature of the |
| 12805 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12806 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12807 | schedGroup = (queue == mFgBroadcastQueue) |
| 12808 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12809 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12810 | } else if (app.executingServices.size() > 0) { |
| 12811 | // An app that is currently executing a service callback also |
| 12812 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12813 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12814 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12815 | app.adjType = "exec-service"; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12816 | } else { |
| 12817 | // Assume process is hidden (has activities); we will correct |
| 12818 | // later if this is not the case. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12819 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12820 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12821 | app.hidden = true; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12822 | app.adjType = "bg-act"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12823 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12824 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12825 | boolean hasStoppingActivities = false; |
| 12826 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12827 | // Examine all activities if not already foreground. |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12828 | if (!foregroundActivities && activitiesSize > 0) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12829 | for (int j = 0; j < activitiesSize; j++) { |
| 12830 | final ActivityRecord r = app.activities.get(j); |
| 12831 | if (r.visible) { |
| 12832 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12833 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12834 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12835 | app.adjType = "visible"; |
| 12836 | } |
| 12837 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12838 | app.hidden = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12839 | app.hasActivities = true; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12840 | foregroundActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12841 | break; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12842 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12843 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12844 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12845 | app.adjType = "pausing"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12846 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 12847 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12848 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12849 | } else if (r.state == ActivityState.STOPPING) { |
| 12850 | // We will apply the actual adjustment later, because |
| 12851 | // we want to allow this process to immediately go through |
| 12852 | // any memory trimming that is in effect. |
| 12853 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12854 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12855 | hasStoppingActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12856 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12857 | if (r.app == app) { |
| 12858 | app.hasActivities = true; |
| 12859 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12860 | } |
| 12861 | } |
| 12862 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12863 | if (adj == hiddenAdj && !app.hasActivities) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12864 | if (app.hasClientActivities) { |
| 12865 | adj = clientHiddenAdj; |
| 12866 | app.adjType = "bg-client-act"; |
| 12867 | } else { |
| 12868 | // Whoops, this process is completely empty as far as we know |
| 12869 | // at this point. |
| 12870 | adj = emptyAdj; |
| 12871 | app.empty = true; |
| 12872 | app.adjType = "bg-empty"; |
| 12873 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12874 | } |
| 12875 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12876 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12877 | if (app.foregroundServices) { |
| 12878 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12879 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12880 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12881 | app.adjType = "fg-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12882 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12883 | } else if (app.forcingToForeground != null) { |
| 12884 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12885 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12886 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12887 | app.adjType = "force-fg"; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12888 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12889 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12890 | } |
| 12891 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12892 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12893 | if (app.foregroundServices) { |
| 12894 | interesting = true; |
| 12895 | } |
| 12896 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12897 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12898 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12899 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12900 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12901 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12902 | app.adjType = "heavy"; |
| 12903 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12904 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12905 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12906 | // This process is hosting what we currently consider to be the |
| 12907 | // 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] | 12908 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12909 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12910 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12911 | app.adjType = "home"; |
| 12912 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12913 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 12914 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 12915 | && app.activities.size() > 0) { |
| 12916 | // This was the previous process that showed UI to the user. |
| 12917 | // We want to try to keep it around more aggressively, to give |
| 12918 | // a good experience around switching between two apps. |
| 12919 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 12920 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12921 | app.hidden = false; |
| 12922 | app.adjType = "previous"; |
| 12923 | } |
| 12924 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12925 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 12926 | + " reason=" + app.adjType); |
| 12927 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12928 | // 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] | 12929 | // there are applications dependent on our services or providers, but |
| 12930 | // this gives us a baseline and makes sure we don't get into an |
| 12931 | // infinite recursion. |
| 12932 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12933 | app.curRawAdj = app.nonStoppingAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12934 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12935 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 12936 | // 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] | 12937 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12938 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12939 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12940 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12941 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12942 | } |
| 12943 | } |
| 12944 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12945 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12946 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12947 | final long now = SystemClock.uptimeMillis(); |
| 12948 | // This process is more important if the top activity is |
| 12949 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12950 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12951 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12952 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12953 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12954 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12955 | // If this process has shown some UI, let it immediately |
| 12956 | // go to the LRU list because it may be pretty heavy with |
| 12957 | // UI stuff. We'll tag it with a label just to help |
| 12958 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12959 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12960 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12961 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12962 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12963 | if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12964 | // This service has seen some activity within |
| 12965 | // recent memory, so we will keep its process ahead |
| 12966 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12967 | if (adj > ProcessList.SERVICE_ADJ) { |
| 12968 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12969 | app.adjType = "started-services"; |
| 12970 | app.hidden = false; |
| 12971 | } |
| 12972 | } |
| 12973 | // If we have let the service slide into the background |
| 12974 | // state, still have some text describing what it is doing |
| 12975 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12976 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12977 | app.adjType = "started-bg-services"; |
| 12978 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12979 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12980 | // Don't kill this process because it is doing work; it |
| 12981 | // has said it is doing work. |
| 12982 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12983 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12984 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12985 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12986 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12987 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12988 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12989 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12990 | 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] | 12991 | // XXX should compute this based on the max of |
| 12992 | // all connected clients. |
| 12993 | ConnectionRecord cr = clist.get(i); |
| 12994 | if (cr.binding.client == app) { |
| 12995 | // Binding to ourself is not interesting. |
| 12996 | continue; |
| 12997 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12998 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12999 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13000 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13001 | int myHiddenAdj = hiddenAdj; |
| 13002 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13003 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13004 | myHiddenAdj = client.hiddenAdj; |
| 13005 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13006 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13007 | } |
| 13008 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13009 | int myClientHiddenAdj = clientHiddenAdj; |
| 13010 | if (myClientHiddenAdj > client.clientHiddenAdj) { |
| 13011 | if (client.clientHiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13012 | myClientHiddenAdj = client.clientHiddenAdj; |
| 13013 | } else { |
| 13014 | myClientHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
| 13015 | } |
| 13016 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13017 | int myEmptyAdj = emptyAdj; |
| 13018 | if (myEmptyAdj > client.emptyAdj) { |
| 13019 | if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13020 | myEmptyAdj = client.emptyAdj; |
| 13021 | } else { |
| 13022 | myEmptyAdj = ProcessList.VISIBLE_APP_ADJ; |
| 13023 | } |
| 13024 | } |
| 13025 | clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13026 | myClientHiddenAdj, myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13027 | String adjType = null; |
| 13028 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 13029 | // Not doing bind OOM management, so treat |
| 13030 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13031 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13032 | // If this process has shown some UI, let it immediately |
| 13033 | // go to the LRU list because it may be pretty heavy with |
| 13034 | // UI stuff. We'll tag it with a label just to help |
| 13035 | // debug and understand what is going on. |
| 13036 | if (adj > clientAdj) { |
| 13037 | adjType = "bound-bg-ui-services"; |
| 13038 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13039 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13040 | clientAdj = adj; |
| 13041 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13042 | if (now >= (s.lastActivity |
| 13043 | + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13044 | // This service has not seen activity within |
| 13045 | // recent memory, so allow it to drop to the |
| 13046 | // LRU list if there is no other reason to keep |
| 13047 | // it around. We'll also tag it with a label just |
| 13048 | // to help debug and undertand what is going on. |
| 13049 | if (adj > clientAdj) { |
| 13050 | adjType = "bound-bg-services"; |
| 13051 | } |
| 13052 | clientAdj = adj; |
| 13053 | } |
| 13054 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13055 | } else if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 13056 | if ((cr.flags&Context.BIND_NOT_VISIBLE) == 0) { |
| 13057 | // If this connection is keeping the service |
| 13058 | // created, then we want to try to better follow |
| 13059 | // its memory management semantics for activities. |
| 13060 | // That is, if it is sitting in the background |
| 13061 | // LRU list as a hidden process (with activities), |
| 13062 | // we don't want the service it is connected to |
| 13063 | // to go into the empty LRU and quickly get killed, |
| 13064 | // because I'll we'll do is just end up restarting |
| 13065 | // the service. |
| 13066 | app.hasClientActivities |= client.hasActivities; |
| 13067 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13068 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13069 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13070 | // If this process has recently shown UI, and |
| 13071 | // the process that is binding to it is less |
| 13072 | // important than being visible, then we don't |
| 13073 | // care about the binding as much as we care |
| 13074 | // about letting this process get into the LRU |
| 13075 | // list to be killed and restarted if needed for |
| 13076 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13077 | if (app.hasShownUi && app != mHomeProcess |
| 13078 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13079 | adjType = "bound-bg-ui-services"; |
| 13080 | } else { |
| 13081 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 13082 | |Context.BIND_IMPORTANT)) != 0) { |
| 13083 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13084 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 13085 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 13086 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13087 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13088 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13089 | adj = clientAdj; |
| 13090 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13091 | app.pendingUiClean = true; |
| 13092 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13093 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13094 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13095 | } |
| 13096 | if (!client.hidden) { |
| 13097 | app.hidden = false; |
| 13098 | } |
| 13099 | if (client.keeping) { |
| 13100 | app.keeping = true; |
| 13101 | } |
| 13102 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13103 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13104 | } |
| 13105 | if (adjType != null) { |
| 13106 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13107 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13108 | .REASON_SERVICE_IN_USE; |
| 13109 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13110 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13111 | app.adjTarget = s.name; |
| 13112 | } |
| 13113 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13114 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13115 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13117 | } |
| 13118 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13119 | final ActivityRecord a = cr.activity; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13120 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13121 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13122 | (a.visible || a.state == ActivityState.RESUMED |
| 13123 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13124 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13125 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13126 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13127 | } |
| 13128 | app.hidden = false; |
| 13129 | app.adjType = "service"; |
| 13130 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13131 | .REASON_SERVICE_IN_USE; |
| 13132 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13133 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13134 | app.adjTarget = s.name; |
| 13135 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13136 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13137 | } |
| 13138 | } |
| 13139 | } |
| 13140 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13141 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13142 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13143 | // would like to avoid killing it unless it would prevent the current |
| 13144 | // application from running. By default we put the process in |
| 13145 | // with the rest of the background processes; as we scan through |
| 13146 | // its services we may bump it up from there. |
| 13147 | if (adj > hiddenAdj) { |
| 13148 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13149 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13150 | app.adjType = "bg-services"; |
| 13151 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13152 | } |
| 13153 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13154 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13155 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13156 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13157 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13158 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13159 | ContentProviderRecord cpr = jt.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13160 | for (int i = cpr.connections.size()-1; |
| 13161 | i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
| 13162 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE); |
| 13163 | i--) { |
| 13164 | ContentProviderConnection conn = cpr.connections.get(i); |
| 13165 | ProcessRecord client = conn.client; |
| 13166 | if (client == app) { |
| 13167 | // Being our own client is not interesting. |
| 13168 | continue; |
| 13169 | } |
| 13170 | int myHiddenAdj = hiddenAdj; |
| 13171 | if (myHiddenAdj > client.hiddenAdj) { |
| 13172 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13173 | myHiddenAdj = client.hiddenAdj; |
| 13174 | } else { |
| 13175 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 13176 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13177 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13178 | int myClientHiddenAdj = clientHiddenAdj; |
| 13179 | if (myClientHiddenAdj > client.clientHiddenAdj) { |
| 13180 | if (client.clientHiddenAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 13181 | myClientHiddenAdj = client.clientHiddenAdj; |
| 13182 | } else { |
| 13183 | myClientHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 13184 | } |
| 13185 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13186 | int myEmptyAdj = emptyAdj; |
| 13187 | if (myEmptyAdj > client.emptyAdj) { |
| 13188 | if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13189 | myEmptyAdj = client.emptyAdj; |
| 13190 | } else { |
| 13191 | myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 13192 | } |
| 13193 | } |
| 13194 | int clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13195 | myClientHiddenAdj, myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13196 | if (adj > clientAdj) { |
| 13197 | if (app.hasShownUi && app != mHomeProcess |
| 13198 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13199 | app.adjType = "bg-ui-provider"; |
| 13200 | } else { |
| 13201 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13202 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
| 13203 | app.adjType = "provider"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13204 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13205 | if (!client.hidden) { |
| 13206 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13207 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13208 | if (client.keeping) { |
| 13209 | app.keeping = true; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13210 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13211 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13212 | .REASON_PROVIDER_IN_USE; |
| 13213 | app.adjSource = client; |
| 13214 | app.adjSourceOom = clientAdj; |
| 13215 | app.adjTarget = cpr.name; |
| 13216 | } |
| 13217 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13218 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13219 | } |
| 13220 | } |
| 13221 | // If the provider has external (non-framework) process |
| 13222 | // dependencies, ensure that its adjustment is at least |
| 13223 | // FOREGROUND_APP_ADJ. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 13224 | if (cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13225 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13226 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13227 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13228 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13229 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13230 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13231 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13232 | } |
| 13233 | } |
| 13234 | } |
| 13235 | } |
| 13236 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13237 | if (adj == ProcessList.SERVICE_ADJ) { |
| 13238 | if (doingAll) { |
| 13239 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 13240 | mNewNumServiceProcs++; |
| 13241 | } |
| 13242 | if (app.serviceb) { |
| 13243 | adj = ProcessList.SERVICE_B_ADJ; |
| 13244 | } |
| 13245 | } else { |
| 13246 | app.serviceb = false; |
| 13247 | } |
| 13248 | |
| 13249 | app.nonStoppingAdj = adj; |
| 13250 | |
| 13251 | if (hasStoppingActivities) { |
| 13252 | // Only upgrade adjustment. |
| 13253 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13254 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13255 | app.adjType = "stopping"; |
| 13256 | } |
| 13257 | } |
| 13258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13259 | app.curRawAdj = adj; |
| 13260 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13261 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13262 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 13263 | if (adj > app.maxAdj) { |
| 13264 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13265 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13266 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13267 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13268 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13269 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13270 | app.keeping = true; |
| 13271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13272 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13273 | if (app.hasAboveClient) { |
| 13274 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 13275 | // then we need to drop its adjustment to be lower than the service's |
| 13276 | // in order to honor the request. We want to drop it by one adjustment |
| 13277 | // level... but there is special meaning applied to various levels so |
| 13278 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13279 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13280 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13281 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 13282 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13283 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13284 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13285 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13286 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13287 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13288 | adj++; |
| 13289 | } |
| 13290 | } |
| 13291 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13292 | int importance = app.memImportance; |
| 13293 | if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) { |
| 13294 | app.curAdj = adj; |
| 13295 | app.curSchedGroup = schedGroup; |
| 13296 | if (!interesting) { |
| 13297 | // For this reporting, if there is not something explicitly |
| 13298 | // interesting in this process then we will push it to the |
| 13299 | // background importance. |
| 13300 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13301 | } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13302 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13303 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 13304 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 13305 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 13306 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13307 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
| 13308 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 13309 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 13310 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 13311 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13312 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 13313 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13314 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 13315 | } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 13316 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 13317 | } else { |
| 13318 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT; |
| 13319 | } |
| 13320 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13321 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13322 | int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0; |
| 13323 | if (foregroundActivities != app.foregroundActivities) { |
| 13324 | changes |= ProcessChangeItem.CHANGE_ACTIVITIES; |
| 13325 | } |
| 13326 | if (changes != 0) { |
| 13327 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes); |
| 13328 | app.memImportance = importance; |
| 13329 | app.foregroundActivities = foregroundActivities; |
| 13330 | int i = mPendingProcessChanges.size()-1; |
| 13331 | ProcessChangeItem item = null; |
| 13332 | while (i >= 0) { |
| 13333 | item = mPendingProcessChanges.get(i); |
| 13334 | if (item.pid == app.pid) { |
| 13335 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item); |
| 13336 | break; |
| 13337 | } |
| 13338 | i--; |
| 13339 | } |
| 13340 | if (i < 0) { |
| 13341 | // No existing item in pending changes; need a new one. |
| 13342 | final int NA = mAvailProcessChanges.size(); |
| 13343 | if (NA > 0) { |
| 13344 | item = mAvailProcessChanges.remove(NA-1); |
| 13345 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item); |
| 13346 | } else { |
| 13347 | item = new ProcessChangeItem(); |
| 13348 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item); |
| 13349 | } |
| 13350 | item.changes = 0; |
| 13351 | item.pid = app.pid; |
| 13352 | item.uid = app.info.uid; |
| 13353 | if (mPendingProcessChanges.size() == 0) { |
| 13354 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, |
| 13355 | "*** Enqueueing dispatch processes changed!"); |
| 13356 | mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget(); |
| 13357 | } |
| 13358 | mPendingProcessChanges.add(item); |
| 13359 | } |
| 13360 | item.changes |= changes; |
| 13361 | item.importance = importance; |
| 13362 | item.foregroundActivities = foregroundActivities; |
| 13363 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item " |
| 13364 | + Integer.toHexString(System.identityHashCode(item)) |
| 13365 | + " " + app.toShortString() + ": changes=" + item.changes |
| 13366 | + " importance=" + item.importance |
| 13367 | + " foreground=" + item.foregroundActivities |
| 13368 | + " type=" + app.adjType + " source=" + app.adjSource |
| 13369 | + " target=" + app.adjTarget); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13370 | } |
| 13371 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13372 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13373 | } |
| 13374 | |
| 13375 | /** |
| 13376 | * Ask a given process to GC right now. |
| 13377 | */ |
| 13378 | final void performAppGcLocked(ProcessRecord app) { |
| 13379 | try { |
| 13380 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13381 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13382 | if (app.reportLowMemory) { |
| 13383 | app.reportLowMemory = false; |
| 13384 | app.thread.scheduleLowMemory(); |
| 13385 | } else { |
| 13386 | app.thread.processInBackground(); |
| 13387 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13388 | } |
| 13389 | } catch (Exception e) { |
| 13390 | // whatever. |
| 13391 | } |
| 13392 | } |
| 13393 | |
| 13394 | /** |
| 13395 | * Returns true if things are idle enough to perform GCs. |
| 13396 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13397 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13398 | boolean processingBroadcasts = false; |
| 13399 | for (BroadcastQueue q : mBroadcastQueues) { |
| 13400 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 13401 | processingBroadcasts = true; |
| 13402 | } |
| 13403 | } |
| 13404 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13405 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13406 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13407 | } |
| 13408 | |
| 13409 | /** |
| 13410 | * Perform GCs on all processes that are waiting for it, but only |
| 13411 | * if things are idle. |
| 13412 | */ |
| 13413 | final void performAppGcsLocked() { |
| 13414 | final int N = mProcessesToGc.size(); |
| 13415 | if (N <= 0) { |
| 13416 | return; |
| 13417 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13418 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13419 | while (mProcessesToGc.size() > 0) { |
| 13420 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13421 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13422 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 13423 | <= SystemClock.uptimeMillis()) { |
| 13424 | // To avoid spamming the system, we will GC processes one |
| 13425 | // at a time, waiting a few seconds between each. |
| 13426 | performAppGcLocked(proc); |
| 13427 | scheduleAppGcsLocked(); |
| 13428 | return; |
| 13429 | } else { |
| 13430 | // It hasn't been long enough since we last GCed this |
| 13431 | // process... put it in the list to wait for its time. |
| 13432 | addProcessToGcListLocked(proc); |
| 13433 | break; |
| 13434 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13435 | } |
| 13436 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13437 | |
| 13438 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13439 | } |
| 13440 | } |
| 13441 | |
| 13442 | /** |
| 13443 | * If all looks good, perform GCs on all processes waiting for them. |
| 13444 | */ |
| 13445 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13446 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13447 | performAppGcsLocked(); |
| 13448 | return; |
| 13449 | } |
| 13450 | // Still not idle, wait some more. |
| 13451 | scheduleAppGcsLocked(); |
| 13452 | } |
| 13453 | |
| 13454 | /** |
| 13455 | * Schedule the execution of all pending app GCs. |
| 13456 | */ |
| 13457 | final void scheduleAppGcsLocked() { |
| 13458 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13459 | |
| 13460 | if (mProcessesToGc.size() > 0) { |
| 13461 | // Schedule a GC for the time to the next process. |
| 13462 | ProcessRecord proc = mProcessesToGc.get(0); |
| 13463 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 13464 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13465 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13466 | long now = SystemClock.uptimeMillis(); |
| 13467 | if (when < (now+GC_TIMEOUT)) { |
| 13468 | when = now + GC_TIMEOUT; |
| 13469 | } |
| 13470 | mHandler.sendMessageAtTime(msg, when); |
| 13471 | } |
| 13472 | } |
| 13473 | |
| 13474 | /** |
| 13475 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 13476 | * list in sorted order by the last GC time. The process can't already be |
| 13477 | * on the list. |
| 13478 | */ |
| 13479 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 13480 | boolean added = false; |
| 13481 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 13482 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 13483 | proc.lastRequestedGc) { |
| 13484 | added = true; |
| 13485 | mProcessesToGc.add(i+1, proc); |
| 13486 | break; |
| 13487 | } |
| 13488 | } |
| 13489 | if (!added) { |
| 13490 | mProcessesToGc.add(0, proc); |
| 13491 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13492 | } |
| 13493 | |
| 13494 | /** |
| 13495 | * Set up to ask a process to GC itself. This will either do it |
| 13496 | * immediately, or put it on the list of processes to gc the next |
| 13497 | * time things are idle. |
| 13498 | */ |
| 13499 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 13500 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13501 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13502 | return; |
| 13503 | } |
| 13504 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13505 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13506 | scheduleAppGcsLocked(); |
| 13507 | } |
| 13508 | } |
| 13509 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13510 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 13511 | updateCpuStatsNow(); |
| 13512 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13513 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13514 | boolean doWakeKills = doKills; |
| 13515 | boolean doCpuKills = doKills; |
| 13516 | if (mLastPowerCheckRealtime == 0) { |
| 13517 | doWakeKills = false; |
| 13518 | } |
| 13519 | if (mLastPowerCheckUptime == 0) { |
| 13520 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13521 | } |
| 13522 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13523 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13524 | } |
| 13525 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13526 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 13527 | final long curUptime = SystemClock.uptimeMillis(); |
| 13528 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 13529 | mLastPowerCheckRealtime = curRealtime; |
| 13530 | mLastPowerCheckUptime = curUptime; |
| 13531 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 13532 | doWakeKills = false; |
| 13533 | } |
| 13534 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 13535 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13536 | } |
| 13537 | int i = mLruProcesses.size(); |
| 13538 | while (i > 0) { |
| 13539 | i--; |
| 13540 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13541 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13542 | long wtime; |
| 13543 | synchronized (stats) { |
| 13544 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 13545 | app.pid, curRealtime); |
| 13546 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13547 | long wtimeUsed = wtime - app.lastWakeTime; |
| 13548 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13549 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13550 | StringBuilder sb = new StringBuilder(128); |
| 13551 | sb.append("Wake for "); |
| 13552 | app.toShortString(sb); |
| 13553 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13554 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13555 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13556 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13557 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13558 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13559 | sb.append("%)"); |
| 13560 | Slog.i(TAG, sb.toString()); |
| 13561 | sb.setLength(0); |
| 13562 | sb.append("CPU for "); |
| 13563 | app.toShortString(sb); |
| 13564 | sb.append(": over "); |
| 13565 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13566 | sb.append(" used "); |
| 13567 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13568 | sb.append(" ("); |
| 13569 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13570 | sb.append("%)"); |
| 13571 | Slog.i(TAG, sb.toString()); |
| 13572 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13573 | // If a process has held a wake lock for more |
| 13574 | // than 50% of the time during this period, |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13575 | // that sounds bad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13576 | if (doWakeKills && realtimeSince > 0 |
| 13577 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13578 | synchronized (stats) { |
| 13579 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13580 | realtimeSince, wtimeUsed); |
| 13581 | } |
| 13582 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13583 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13584 | + " during " + realtimeSince); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13585 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13586 | app.processName, app.setAdj, "excessive wake lock"); |
| 13587 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13588 | } else if (doCpuKills && uptimeSince > 0 |
| 13589 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13590 | synchronized (stats) { |
| 13591 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13592 | uptimeSince, cputimeUsed); |
| 13593 | } |
| 13594 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13595 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13596 | + " during " + uptimeSince); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13597 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13598 | app.processName, app.setAdj, "excessive cpu"); |
| 13599 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13600 | } else { |
| 13601 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13602 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13603 | } |
| 13604 | } |
| 13605 | } |
| 13606 | } |
| 13607 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13608 | private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13609 | int clientHiddenAdj, int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13610 | app.hiddenAdj = hiddenAdj; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13611 | app.clientHiddenAdj = clientHiddenAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13612 | app.emptyAdj = emptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13613 | |
| 13614 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13615 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13616 | } |
| 13617 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13618 | final boolean wasKeeping = app.keeping; |
| 13619 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13620 | boolean success = true; |
| 13621 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13622 | computeOomAdjLocked(app, hiddenAdj, clientHiddenAdj, emptyAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13623 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13624 | if (app.curRawAdj != app.setRawAdj) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13625 | if (wasKeeping && !app.keeping) { |
| 13626 | // This app is no longer something we want to keep. Note |
| 13627 | // its current wake lock time to later know to kill it if |
| 13628 | // it is not behaving well. |
| 13629 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13630 | synchronized (stats) { |
| 13631 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13632 | app.pid, SystemClock.elapsedRealtime()); |
| 13633 | } |
| 13634 | app.lastCpuTime = app.curCpuTime; |
| 13635 | } |
| 13636 | |
| 13637 | app.setRawAdj = app.curRawAdj; |
| 13638 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13639 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13640 | if (app.curAdj != app.setAdj) { |
| 13641 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 13642 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13643 | TAG, "Set " + app.pid + " " + app.processName + |
| 13644 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13645 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13646 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13647 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13648 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13649 | } |
| 13650 | } |
| 13651 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13652 | app.setSchedGroup = app.curSchedGroup; |
| 13653 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 13654 | "Setting process group of " + app.processName |
| 13655 | + " to " + app.curSchedGroup); |
| 13656 | if (app.waitingToKill != null && |
| 13657 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13658 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13659 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13660 | app.processName, app.setAdj, app.waitingToKill); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 13661 | app.killedBackground = true; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13662 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13663 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13664 | } else { |
| 13665 | if (true) { |
| 13666 | long oldId = Binder.clearCallingIdentity(); |
| 13667 | try { |
| 13668 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13669 | } catch (Exception e) { |
| 13670 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13671 | + " to " + app.curSchedGroup); |
| 13672 | e.printStackTrace(); |
| 13673 | } finally { |
| 13674 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13675 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13676 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13677 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13678 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13679 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13680 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13681 | } |
| 13682 | } |
| 13683 | } |
| 13684 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13685 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13686 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13687 | } |
| 13688 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13689 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13690 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13691 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 13692 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13693 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13694 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13695 | } |
| 13696 | } |
| 13697 | return resumedActivity; |
| 13698 | } |
| 13699 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13700 | final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13701 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13702 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13703 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13704 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13705 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13706 | |
| 13707 | mAdjSeq++; |
| 13708 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13709 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.clientHiddenAdj, |
| 13710 | app.emptyAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13711 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13712 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13713 | if (nowHidden != wasHidden) { |
| 13714 | // Changed to/from hidden state, so apps after it in the LRU |
| 13715 | // list may also be changed. |
| 13716 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13717 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13718 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13719 | } |
| 13720 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13721 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13722 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13723 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13724 | final long oldTime = SystemClock.uptimeMillis() - ProcessList.MAX_EMPTY_TIME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13725 | |
| 13726 | if (false) { |
| 13727 | RuntimeException e = new RuntimeException(); |
| 13728 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13729 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13730 | } |
| 13731 | |
| 13732 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13733 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13734 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13735 | final int emptyProcessLimit; |
| 13736 | final int hiddenProcessLimit; |
| 13737 | if (mProcessLimit <= 0) { |
| 13738 | emptyProcessLimit = hiddenProcessLimit = 0; |
| 13739 | } else if (mProcessLimit == 1) { |
| 13740 | emptyProcessLimit = 1; |
| 13741 | hiddenProcessLimit = 0; |
| 13742 | } else { |
| 13743 | emptyProcessLimit = (mProcessLimit*2)/3; |
| 13744 | hiddenProcessLimit = mProcessLimit - emptyProcessLimit; |
| 13745 | } |
| 13746 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13747 | // Let's determine how many processes we have running vs. |
| 13748 | // how many slots we have for background processes; we may want |
| 13749 | // to put multiple processes in a slot of there are enough of |
| 13750 | // them. |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13751 | int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ |
| 13752 | - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13753 | int numEmptyProcs = mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs; |
| 13754 | if (numEmptyProcs > hiddenProcessLimit) { |
| 13755 | // If there are more empty processes than our limit on hidden |
| 13756 | // processes, then use the hidden process limit for the factor. |
| 13757 | // This ensures that the really old empty processes get pushed |
| 13758 | // down to the bottom, so if we are running low on memory we will |
| 13759 | // have a better chance at keeping around more hidden processes |
| 13760 | // instead of a gazillion empty processes. |
| 13761 | numEmptyProcs = hiddenProcessLimit; |
| 13762 | } |
| 13763 | int emptyFactor = numEmptyProcs/numSlots; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13764 | if (emptyFactor < 1) emptyFactor = 1; |
| 13765 | int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots; |
| 13766 | if (hiddenFactor < 1) hiddenFactor = 1; |
| 13767 | int stepHidden = 0; |
| 13768 | int stepEmpty = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13769 | int numHidden = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13770 | int numEmpty = 0; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13771 | int numTrimming = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13772 | |
| 13773 | mNumNonHiddenProcs = 0; |
| 13774 | mNumHiddenProcs = 0; |
| 13775 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13776 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13777 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13778 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13779 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13780 | int nextHiddenAdj = curHiddenAdj+1; |
| 13781 | int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
| 13782 | int nextEmptyAdj = curEmptyAdj+2; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13783 | int curClientHiddenAdj = curEmptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13784 | while (i > 0) { |
| 13785 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13786 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13787 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13788 | updateOomAdjLocked(app, curHiddenAdj, curClientHiddenAdj, curEmptyAdj, TOP_APP, true); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13789 | if (!app.killedBackground) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13790 | if (app.curRawAdj == curHiddenAdj && app.hasActivities) { |
| 13791 | // This process was assigned as a hidden process... step the |
| 13792 | // hidden level. |
| 13793 | mNumHiddenProcs++; |
| 13794 | if (curHiddenAdj != nextHiddenAdj) { |
| 13795 | stepHidden++; |
| 13796 | if (stepHidden >= hiddenFactor) { |
| 13797 | stepHidden = 0; |
| 13798 | curHiddenAdj = nextHiddenAdj; |
| 13799 | nextHiddenAdj += 2; |
| 13800 | if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13801 | nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13802 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13803 | if (curClientHiddenAdj <= curHiddenAdj) { |
| 13804 | curClientHiddenAdj = curHiddenAdj + 1; |
| 13805 | if (curClientHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13806 | curClientHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13807 | } |
| 13808 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13809 | } |
| 13810 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13811 | numHidden++; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13812 | if (numHidden > hiddenProcessLimit) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13813 | Slog.i(TAG, "No longer want " + app.processName |
| 13814 | + " (pid " + app.pid + "): hidden #" + numHidden); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13815 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13816 | app.processName, app.setAdj, "too many background"); |
| 13817 | app.killedBackground = true; |
| 13818 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13819 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13820 | } else if (app.curRawAdj == curHiddenAdj && app.hasClientActivities) { |
| 13821 | // This process has a client that has activities. We will have |
| 13822 | // given it the current hidden adj; here we will just leave it |
| 13823 | // without stepping the hidden adj. |
| 13824 | curClientHiddenAdj++; |
| 13825 | if (curClientHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13826 | curClientHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13827 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13828 | } else { |
| 13829 | if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) { |
| 13830 | // This process was assigned as an empty process... step the |
| 13831 | // empty level. |
| 13832 | if (curEmptyAdj != nextEmptyAdj) { |
| 13833 | stepEmpty++; |
| 13834 | if (stepEmpty >= emptyFactor) { |
| 13835 | stepEmpty = 0; |
| 13836 | curEmptyAdj = nextEmptyAdj; |
| 13837 | nextEmptyAdj += 2; |
| 13838 | if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13839 | nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13840 | } |
| 13841 | } |
| 13842 | } |
| 13843 | } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13844 | mNumNonHiddenProcs++; |
| 13845 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13846 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13847 | && !app.hasClientActivities) { |
| 13848 | if (numEmpty > ProcessList.TRIM_EMPTY_APPS |
| 13849 | && app.lastActivityTime < oldTime) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13850 | Slog.i(TAG, "No longer want " + app.processName |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13851 | + " (pid " + app.pid + "): empty for " |
| 13852 | + ((oldTime+ProcessList.MAX_EMPTY_TIME-app.lastActivityTime) |
| 13853 | / 1000) + "s"); |
| 13854 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
| 13855 | app.processName, app.setAdj, "old background process"); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13856 | app.killedBackground = true; |
| 13857 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13858 | } else { |
| 13859 | numEmpty++; |
| 13860 | if (numEmpty > emptyProcessLimit) { |
| 13861 | Slog.i(TAG, "No longer want " + app.processName |
| 13862 | + " (pid " + app.pid + "): empty #" + numEmpty); |
| 13863 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
| 13864 | app.processName, app.setAdj, "too many background"); |
| 13865 | app.killedBackground = true; |
| 13866 | Process.killProcessQuiet(app.pid); |
| 13867 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13868 | } |
| 13869 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13870 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13871 | if (app.isolated && app.services.size() <= 0) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13872 | // If this is an isolated process, and there are no |
| 13873 | // services running in it, then the process is no longer |
| 13874 | // needed. We agressively kill these because we can by |
| 13875 | // definition not re-use the same process again, and it is |
| 13876 | // good to avoid having whatever code was running in them |
| 13877 | // left sitting around after no longer needed. |
| 13878 | Slog.i(TAG, "Isolated process " + app.processName |
| 13879 | + " (pid " + app.pid + ") no longer needed"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13880 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13881 | app.processName, app.setAdj, "isolated not needed"); |
| 13882 | app.killedBackground = true; |
| 13883 | Process.killProcessQuiet(app.pid); |
| 13884 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13885 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13886 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
| 13887 | && !app.killedBackground) { |
| 13888 | numTrimming++; |
| 13889 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13890 | } |
| 13891 | } |
| 13892 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13893 | mNumServiceProcs = mNewNumServiceProcs; |
| 13894 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13895 | // Now determine the memory trimming level of background processes. |
| 13896 | // Unfortunately we need to start at the back of the list to do this |
| 13897 | // properly. We only do this if the number of background apps we |
| 13898 | // are managing to keep around is less than half the maximum we desire; |
| 13899 | // if we are keeping a good number around, we'll let them use whatever |
| 13900 | // memory they want. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13901 | if (numHidden <= ProcessList.TRIM_HIDDEN_APPS |
| 13902 | && numEmpty <= ProcessList.TRIM_EMPTY_APPS) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13903 | final int numHiddenAndEmpty = numHidden + numEmpty; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13904 | final int N = mLruProcesses.size(); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13905 | int factor = numTrimming/3; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13906 | int minFactor = 2; |
| 13907 | if (mHomeProcess != null) minFactor++; |
| 13908 | if (mPreviousProcess != null) minFactor++; |
| 13909 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13910 | int step = 0; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13911 | int fgTrimLevel; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13912 | if (numHiddenAndEmpty <= ProcessList.TRIM_CRITICAL_THRESHOLD) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13913 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13914 | } else if (numHiddenAndEmpty <= ProcessList.TRIM_LOW_THRESHOLD) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13915 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
| 13916 | } else { |
| 13917 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
| 13918 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13919 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13920 | for (i=0; i<N; i++) { |
| 13921 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13922 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13923 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13924 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13925 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 13926 | try { |
| 13927 | app.thread.scheduleTrimMemory(curLevel); |
| 13928 | } catch (RemoteException e) { |
| 13929 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13930 | if (false) { |
| 13931 | // For now we won't do this; our memory trimming seems |
| 13932 | // to be good enough at this point that destroying |
| 13933 | // activities causes more harm than good. |
| 13934 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 13935 | && app != mHomeProcess && app != mPreviousProcess) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13936 | // Need to do this on its own message because the stack may not |
| 13937 | // be in a consistent state at this point. |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13938 | // For these apps we will also finish their activities |
| 13939 | // to help them free memory. |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13940 | mMainStack.scheduleDestroyActivities(app, false, "trim"); |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13941 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13942 | } |
| 13943 | } |
| 13944 | app.trimMemoryLevel = curLevel; |
| 13945 | step++; |
| 13946 | if (step >= factor) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13947 | step = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13948 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13949 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 13950 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13951 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13952 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 13953 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13954 | break; |
| 13955 | } |
| 13956 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13957 | } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13958 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13959 | && app.thread != null) { |
| 13960 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13961 | app.thread.scheduleTrimMemory( |
| 13962 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13963 | } catch (RemoteException e) { |
| 13964 | } |
| 13965 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13966 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13967 | } else { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13968 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13969 | && app.pendingUiClean) { |
| 13970 | // If this application is now in the background and it |
| 13971 | // had done UI, then give it the special trim level to |
| 13972 | // have it free UI resources. |
| 13973 | final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13974 | if (app.trimMemoryLevel < level && app.thread != null) { |
| 13975 | try { |
| 13976 | app.thread.scheduleTrimMemory(level); |
| 13977 | } catch (RemoteException e) { |
| 13978 | } |
| 13979 | } |
| 13980 | app.pendingUiClean = false; |
| 13981 | } |
| 13982 | if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13983 | try { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13984 | app.thread.scheduleTrimMemory(fgTrimLevel); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13985 | } catch (RemoteException e) { |
| 13986 | } |
| 13987 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13988 | app.trimMemoryLevel = fgTrimLevel; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13989 | } |
| 13990 | } |
| 13991 | } else { |
| 13992 | final int N = mLruProcesses.size(); |
| 13993 | for (i=0; i<N; i++) { |
| 13994 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13995 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13996 | && app.pendingUiClean) { |
| 13997 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13998 | && app.thread != null) { |
| 13999 | try { |
| 14000 | app.thread.scheduleTrimMemory( |
| 14001 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 14002 | } catch (RemoteException e) { |
| 14003 | } |
| 14004 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14005 | app.pendingUiClean = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14006 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 14007 | app.trimMemoryLevel = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14008 | } |
| 14009 | } |
| 14010 | |
| 14011 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 14012 | // Need to do this on its own message because the stack may not |
| 14013 | // be in a consistent state at this point. |
| 14014 | mMainStack.scheduleDestroyActivities(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14015 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14016 | } |
| 14017 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14018 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14019 | synchronized (this) { |
| 14020 | int i; |
| 14021 | |
| 14022 | // First remove any unused application processes whose package |
| 14023 | // has been removed. |
| 14024 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 14025 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 14026 | if (app.activities.size() == 0 |
| 14027 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14028 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14029 | TAG, "Exiting empty application process " |
| 14030 | + app.processName + " (" |
| 14031 | + (app.thread != null ? app.thread.asBinder() : null) |
| 14032 | + ")\n"); |
| 14033 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 14034 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 14035 | app.processName, app.setAdj, "empty"); |
| 14036 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14037 | } else { |
| 14038 | try { |
| 14039 | app.thread.scheduleExit(); |
| 14040 | } catch (Exception e) { |
| 14041 | // Ignore exceptions. |
| 14042 | } |
| 14043 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 14044 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14045 | mRemovedProcesses.remove(i); |
| 14046 | |
| 14047 | if (app.persistent) { |
| 14048 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 14049 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14050 | } |
| 14051 | } |
| 14052 | } |
| 14053 | } |
| 14054 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14055 | // Now update the oom adj for all processes. |
| 14056 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14057 | } |
| 14058 | } |
| 14059 | |
| 14060 | /** This method sends the specified signal to each of the persistent apps */ |
| 14061 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 14062 | if (sig != Process.SIGNAL_USR1) { |
| 14063 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 14064 | } |
| 14065 | |
| 14066 | synchronized (this) { |
| 14067 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 14068 | != PackageManager.PERMISSION_GRANTED) { |
| 14069 | throw new SecurityException("Requires permission " |
| 14070 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 14071 | } |
| 14072 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14073 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 14074 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14075 | if (r.thread != null && r.persistent) { |
| 14076 | Process.sendSignal(r.pid, sig); |
| 14077 | } |
| 14078 | } |
| 14079 | } |
| 14080 | } |
| 14081 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14082 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 14083 | if (proc == null || proc == mProfileProc) { |
| 14084 | proc = mProfileProc; |
| 14085 | path = mProfileFile; |
| 14086 | profileType = mProfileType; |
| 14087 | clearProfilerLocked(); |
| 14088 | } |
| 14089 | if (proc == null) { |
| 14090 | return; |
| 14091 | } |
| 14092 | try { |
| 14093 | proc.thread.profilerControl(false, path, null, profileType); |
| 14094 | } catch (RemoteException e) { |
| 14095 | throw new IllegalStateException("Process disappeared"); |
| 14096 | } |
| 14097 | } |
| 14098 | |
| 14099 | private void clearProfilerLocked() { |
| 14100 | if (mProfileFd != null) { |
| 14101 | try { |
| 14102 | mProfileFd.close(); |
| 14103 | } catch (IOException e) { |
| 14104 | } |
| 14105 | } |
| 14106 | mProfileApp = null; |
| 14107 | mProfileProc = null; |
| 14108 | mProfileFile = null; |
| 14109 | mProfileType = 0; |
| 14110 | mAutoStopProfiler = false; |
| 14111 | } |
| 14112 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14113 | public boolean profileControl(String process, int userId, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 14114 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14115 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14116 | try { |
| 14117 | synchronized (this) { |
| 14118 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14119 | // its own permission. |
| 14120 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14121 | != PackageManager.PERMISSION_GRANTED) { |
| 14122 | throw new SecurityException("Requires permission " |
| 14123 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14124 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14125 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14126 | if (start && fd == null) { |
| 14127 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14128 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14129 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14130 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14131 | if (process != null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14132 | proc = findProcessLocked(process, userId, "profileControl"); |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14133 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14134 | |
| 14135 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14136 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14137 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14138 | |
| 14139 | if (start) { |
| 14140 | stopProfilerLocked(null, null, 0); |
| 14141 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 14142 | mProfileProc = proc; |
| 14143 | mProfileType = profileType; |
| 14144 | try { |
| 14145 | fd = fd.dup(); |
| 14146 | } catch (IOException e) { |
| 14147 | fd = null; |
| 14148 | } |
| 14149 | proc.thread.profilerControl(start, path, fd, profileType); |
| 14150 | fd = null; |
| 14151 | mProfileFd = null; |
| 14152 | } else { |
| 14153 | stopProfilerLocked(proc, path, profileType); |
| 14154 | if (fd != null) { |
| 14155 | try { |
| 14156 | fd.close(); |
| 14157 | } catch (IOException e) { |
| 14158 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14159 | } |
| 14160 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14161 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14162 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14163 | } |
| 14164 | } catch (RemoteException e) { |
| 14165 | throw new IllegalStateException("Process disappeared"); |
| 14166 | } finally { |
| 14167 | if (fd != null) { |
| 14168 | try { |
| 14169 | fd.close(); |
| 14170 | } catch (IOException e) { |
| 14171 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14172 | } |
| 14173 | } |
| 14174 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14175 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14176 | private ProcessRecord findProcessLocked(String process, int userId, String callName) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 14177 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14178 | userId, true, true, callName, null); |
| 14179 | ProcessRecord proc = null; |
| 14180 | try { |
| 14181 | int pid = Integer.parseInt(process); |
| 14182 | synchronized (mPidsSelfLocked) { |
| 14183 | proc = mPidsSelfLocked.get(pid); |
| 14184 | } |
| 14185 | } catch (NumberFormatException e) { |
| 14186 | } |
| 14187 | |
| 14188 | if (proc == null) { |
| 14189 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14190 | = mProcessNames.getMap(); |
| 14191 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14192 | if (procs != null && procs.size() > 0) { |
| 14193 | proc = procs.valueAt(0); |
| 14194 | if (userId != UserHandle.USER_ALL && proc.userId != userId) { |
| 14195 | for (int i=1; i<procs.size(); i++) { |
| 14196 | ProcessRecord thisProc = procs.valueAt(i); |
| 14197 | if (thisProc.userId == userId) { |
| 14198 | proc = thisProc; |
| 14199 | break; |
| 14200 | } |
| 14201 | } |
| 14202 | } |
| 14203 | } |
| 14204 | } |
| 14205 | |
| 14206 | return proc; |
| 14207 | } |
| 14208 | |
| 14209 | public boolean dumpHeap(String process, int userId, boolean managed, |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14210 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 14211 | |
| 14212 | try { |
| 14213 | synchronized (this) { |
| 14214 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14215 | // its own permission (same as profileControl). |
| 14216 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14217 | != PackageManager.PERMISSION_GRANTED) { |
| 14218 | throw new SecurityException("Requires permission " |
| 14219 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 14220 | } |
| 14221 | |
| 14222 | if (fd == null) { |
| 14223 | throw new IllegalArgumentException("null fd"); |
| 14224 | } |
| 14225 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14226 | ProcessRecord proc = findProcessLocked(process, userId, "dumpHeap"); |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14227 | if (proc == null || proc.thread == null) { |
| 14228 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14229 | } |
| 14230 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 14231 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 14232 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14233 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 14234 | throw new SecurityException("Process not debuggable: " + proc); |
| 14235 | } |
| 14236 | } |
| 14237 | |
| 14238 | proc.thread.dumpHeap(managed, path, fd); |
| 14239 | fd = null; |
| 14240 | return true; |
| 14241 | } |
| 14242 | } catch (RemoteException e) { |
| 14243 | throw new IllegalStateException("Process disappeared"); |
| 14244 | } finally { |
| 14245 | if (fd != null) { |
| 14246 | try { |
| 14247 | fd.close(); |
| 14248 | } catch (IOException e) { |
| 14249 | } |
| 14250 | } |
| 14251 | } |
| 14252 | } |
| 14253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14254 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 14255 | public void monitor() { |
| 14256 | synchronized (this) { } |
| 14257 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14258 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 14259 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14260 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 14261 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 14262 | try { |
| 14263 | if (processRecord.thread != null) { |
| 14264 | processRecord.thread.setCoreSettings(settings); |
| 14265 | } |
| 14266 | } catch (RemoteException re) { |
| 14267 | /* ignore */ |
| 14268 | } |
| 14269 | } |
| 14270 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14271 | |
| 14272 | // Multi-user methods |
| 14273 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14274 | @Override |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14275 | public boolean switchUser(final int userId) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14276 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14277 | != PackageManager.PERMISSION_GRANTED) { |
| 14278 | String msg = "Permission Denial: switchUser() from pid=" |
| 14279 | + Binder.getCallingPid() |
| 14280 | + ", uid=" + Binder.getCallingUid() |
| 14281 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14282 | Slog.w(TAG, msg); |
| 14283 | throw new SecurityException(msg); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14284 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14285 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14286 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14287 | try { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14288 | synchronized (this) { |
| 14289 | final int oldUserId = mCurrentUserId; |
| 14290 | if (oldUserId == userId) { |
| 14291 | return true; |
| 14292 | } |
| 14293 | |
| 14294 | final UserInfo userInfo = getUserManagerLocked().getUserInfo(userId); |
| 14295 | if (userInfo == null) { |
| 14296 | Slog.w(TAG, "No user info for user #" + userId); |
| 14297 | return false; |
| 14298 | } |
| 14299 | |
| 14300 | mWindowManager.startFreezingScreen(R.anim.screen_user_exit, |
| 14301 | R.anim.screen_user_enter); |
| 14302 | |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14303 | boolean needStart = false; |
| 14304 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14305 | // If the user we are switching to is not currently started, then |
| 14306 | // we need to start it now. |
| 14307 | if (mStartedUsers.get(userId) == null) { |
| 14308 | mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14309 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14310 | needStart = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14311 | } |
| 14312 | |
| 14313 | mCurrentUserId = userId; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14314 | mCurrentUserArray = new int[] { userId }; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14315 | final Integer userIdInt = Integer.valueOf(userId); |
| 14316 | mUserLru.remove(userIdInt); |
| 14317 | mUserLru.add(userIdInt); |
| 14318 | |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14319 | mWindowManager.setCurrentUser(userId); |
| 14320 | |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 14321 | // Once the internal notion of the active user has switched, we lock the device |
| 14322 | // with the option to show the user switcher on the keyguard. |
Winson Chung | f7614fc | 2012-11-26 14:43:24 -0800 | [diff] [blame] | 14323 | mWindowManager.lockNow(null); |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 14324 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14325 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14326 | |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14327 | // Make sure user is in the started state. If it is currently |
| 14328 | // stopping, we need to knock that off. |
| 14329 | if (uss.mState == UserStartedState.STATE_STOPPING) { |
| 14330 | // If we are stopping, we haven't sent ACTION_SHUTDOWN, |
| 14331 | // so we can just fairly silently bring the user back from |
| 14332 | // the almost-dead. |
| 14333 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14334 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14335 | needStart = true; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14336 | } else if (uss.mState == UserStartedState.STATE_SHUTDOWN) { |
| 14337 | // This means ACTION_SHUTDOWN has been sent, so we will |
| 14338 | // need to treat this as a new boot of the user. |
| 14339 | uss.mState = UserStartedState.STATE_BOOTING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14340 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14341 | needStart = true; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14342 | } |
| 14343 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14344 | mHandler.removeMessages(REPORT_USER_SWITCH_MSG); |
| 14345 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14346 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG, |
| 14347 | oldUserId, userId, uss)); |
| 14348 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG, |
| 14349 | oldUserId, userId, uss), USER_SWITCH_TIMEOUT); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14350 | if (needStart) { |
| 14351 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
| 14352 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14353 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 14354 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14355 | broadcastIntentLocked(null, null, intent, |
| 14356 | null, null, 0, null, null, null, |
| 14357 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14358 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14359 | |
| 14360 | if ((userInfo.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
| 14361 | if (userId != 0) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14362 | Intent intent = new Intent(Intent.ACTION_USER_INITIALIZE); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14363 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14364 | broadcastIntentLocked(null, null, intent, null, |
| 14365 | new IIntentReceiver.Stub() { |
| 14366 | public void performReceive(Intent intent, int resultCode, |
| 14367 | String data, Bundle extras, boolean ordered, |
| 14368 | boolean sticky, int sendingUser) { |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14369 | userInitialized(uss, userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14370 | } |
| 14371 | }, 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 14372 | userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14373 | uss.initializing = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14374 | } else { |
| 14375 | getUserManagerLocked().makeInitialized(userInfo.id); |
| 14376 | } |
| 14377 | } |
| 14378 | |
| 14379 | boolean haveActivities = mMainStack.switchUserLocked(userId, uss); |
| 14380 | if (!haveActivities) { |
| 14381 | startHomeActivityLocked(userId); |
| 14382 | } |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14383 | |
Jeff Sharkey | 86597df | 2012-11-09 15:00:31 -0800 | [diff] [blame] | 14384 | EventLogTags.writeAmSwitchUser(userId); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 14385 | getUserManagerLocked().userForeground(userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14386 | sendUserSwitchBroadcastsLocked(oldUserId, userId); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14387 | if (needStart) { |
| 14388 | Intent intent = new Intent(Intent.ACTION_USER_STARTING); |
| 14389 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14390 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14391 | broadcastIntentLocked(null, null, intent, |
| 14392 | null, new IIntentReceiver.Stub() { |
| 14393 | @Override |
| 14394 | public void performReceive(Intent intent, int resultCode, String data, |
| 14395 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) |
| 14396 | throws RemoteException { |
| 14397 | } |
| 14398 | }, 0, null, null, |
| 14399 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 14400 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14401 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14402 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14403 | } finally { |
| 14404 | Binder.restoreCallingIdentity(ident); |
| 14405 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14406 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14407 | return true; |
| 14408 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14409 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14410 | void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) { |
| 14411 | long ident = Binder.clearCallingIdentity(); |
| 14412 | try { |
| 14413 | Intent intent; |
| 14414 | if (oldUserId >= 0) { |
| 14415 | intent = new Intent(Intent.ACTION_USER_BACKGROUND); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14416 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14417 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14418 | intent.putExtra(Intent.EXTRA_USER_HANDLE, oldUserId); |
| 14419 | broadcastIntentLocked(null, null, intent, |
| 14420 | null, null, 0, null, null, null, |
| 14421 | false, false, MY_PID, Process.SYSTEM_UID, oldUserId); |
| 14422 | } |
| 14423 | if (newUserId >= 0) { |
| 14424 | intent = new Intent(Intent.ACTION_USER_FOREGROUND); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14425 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14426 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14427 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14428 | broadcastIntentLocked(null, null, intent, |
| 14429 | null, null, 0, null, null, null, |
| 14430 | false, false, MY_PID, Process.SYSTEM_UID, newUserId); |
| 14431 | intent = new Intent(Intent.ACTION_USER_SWITCHED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14432 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14433 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14434 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14435 | broadcastIntentLocked(null, null, intent, |
| 14436 | null, null, 0, null, null, |
| 14437 | android.Manifest.permission.MANAGE_USERS, |
| 14438 | false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
| 14439 | } |
| 14440 | } finally { |
| 14441 | Binder.restoreCallingIdentity(ident); |
| 14442 | } |
| 14443 | } |
| 14444 | |
| 14445 | void dispatchUserSwitch(final UserStartedState uss, final int oldUserId, |
| 14446 | final int newUserId) { |
| 14447 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14448 | if (N > 0) { |
| 14449 | final IRemoteCallback callback = new IRemoteCallback.Stub() { |
| 14450 | int mCount = 0; |
| 14451 | @Override |
| 14452 | public void sendResult(Bundle data) throws RemoteException { |
| 14453 | synchronized (ActivityManagerService.this) { |
| 14454 | if (mCurUserSwitchCallback == this) { |
| 14455 | mCount++; |
| 14456 | if (mCount == N) { |
| 14457 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14458 | } |
| 14459 | } |
| 14460 | } |
| 14461 | } |
| 14462 | }; |
| 14463 | synchronized (this) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14464 | uss.switching = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14465 | mCurUserSwitchCallback = callback; |
| 14466 | } |
| 14467 | for (int i=0; i<N; i++) { |
| 14468 | try { |
| 14469 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitching( |
| 14470 | newUserId, callback); |
| 14471 | } catch (RemoteException e) { |
| 14472 | } |
| 14473 | } |
| 14474 | } else { |
| 14475 | synchronized (this) { |
| 14476 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14477 | } |
| 14478 | } |
| 14479 | mUserSwitchObservers.finishBroadcast(); |
| 14480 | } |
| 14481 | |
| 14482 | void timeoutUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
| 14483 | synchronized (this) { |
| 14484 | Slog.w(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId); |
| 14485 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14486 | } |
| 14487 | } |
| 14488 | |
| 14489 | void sendContinueUserSwitchLocked(UserStartedState uss, int oldUserId, int newUserId) { |
| 14490 | mCurUserSwitchCallback = null; |
| 14491 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14492 | mHandler.sendMessage(mHandler.obtainMessage(CONTINUE_USER_SWITCH_MSG, |
| 14493 | oldUserId, newUserId, uss)); |
| 14494 | } |
| 14495 | |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14496 | void userInitialized(UserStartedState uss, int newUserId) { |
| 14497 | completeSwitchAndInitalize(uss, newUserId, true, false); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14498 | } |
| 14499 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14500 | void continueUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14501 | completeSwitchAndInitalize(uss, newUserId, false, true); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14502 | } |
| 14503 | |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14504 | void completeSwitchAndInitalize(UserStartedState uss, int newUserId, |
| 14505 | boolean clearInitializing, boolean clearSwitching) { |
| 14506 | boolean unfrozen = false; |
| 14507 | synchronized (this) { |
| 14508 | if (clearInitializing) { |
| 14509 | uss.initializing = false; |
| 14510 | getUserManagerLocked().makeInitialized(uss.mHandle.getIdentifier()); |
| 14511 | } |
| 14512 | if (clearSwitching) { |
| 14513 | uss.switching = false; |
| 14514 | } |
| 14515 | if (!uss.switching && !uss.initializing) { |
| 14516 | mWindowManager.stopFreezingScreen(); |
| 14517 | unfrozen = true; |
| 14518 | } |
| 14519 | } |
| 14520 | if (unfrozen) { |
| 14521 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14522 | for (int i=0; i<N; i++) { |
| 14523 | try { |
| 14524 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(newUserId); |
| 14525 | } catch (RemoteException e) { |
| 14526 | } |
| 14527 | } |
| 14528 | mUserSwitchObservers.finishBroadcast(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14529 | } |
| 14530 | } |
| 14531 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14532 | void finishUserSwitch(UserStartedState uss) { |
| 14533 | synchronized (this) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14534 | if (uss.mState == UserStartedState.STATE_BOOTING |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14535 | && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) { |
| 14536 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14537 | final int userId = uss.mHandle.getIdentifier(); |
| 14538 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 14539 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14540 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14541 | null, null, 0, null, null, |
| 14542 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14543 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14544 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14545 | int num = mUserLru.size(); |
| 14546 | int i = 0; |
| 14547 | while (num > MAX_RUNNING_USERS && i < mUserLru.size()) { |
| 14548 | Integer oldUserId = mUserLru.get(i); |
| 14549 | UserStartedState oldUss = mStartedUsers.get(oldUserId); |
| 14550 | if (oldUss == null) { |
| 14551 | // Shouldn't happen, but be sane if it does. |
| 14552 | mUserLru.remove(i); |
| 14553 | num--; |
| 14554 | continue; |
| 14555 | } |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14556 | if (oldUss.mState == UserStartedState.STATE_STOPPING |
| 14557 | || oldUss.mState == UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14558 | // This user is already stopping, doesn't count. |
| 14559 | num--; |
| 14560 | i++; |
| 14561 | continue; |
| 14562 | } |
| 14563 | if (oldUserId == UserHandle.USER_OWNER || oldUserId == mCurrentUserId) { |
| 14564 | // Owner and current can't be stopped, but count as running. |
| 14565 | i++; |
| 14566 | continue; |
| 14567 | } |
| 14568 | // This is a user to be stopped. |
| 14569 | stopUserLocked(oldUserId, null); |
| 14570 | num--; |
| 14571 | i++; |
| 14572 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14573 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14574 | } |
| 14575 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14576 | @Override |
| 14577 | public int stopUser(final int userId, final IStopUserCallback callback) { |
| 14578 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14579 | != PackageManager.PERMISSION_GRANTED) { |
| 14580 | String msg = "Permission Denial: switchUser() from pid=" |
| 14581 | + Binder.getCallingPid() |
| 14582 | + ", uid=" + Binder.getCallingUid() |
| 14583 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14584 | Slog.w(TAG, msg); |
| 14585 | throw new SecurityException(msg); |
| 14586 | } |
| 14587 | if (userId <= 0) { |
| 14588 | throw new IllegalArgumentException("Can't stop primary user " + userId); |
| 14589 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14590 | synchronized (this) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14591 | return stopUserLocked(userId, callback); |
| 14592 | } |
| 14593 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14594 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14595 | private int stopUserLocked(final int userId, final IStopUserCallback callback) { |
| 14596 | if (mCurrentUserId == userId) { |
| 14597 | return ActivityManager.USER_OP_IS_CURRENT; |
| 14598 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14599 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14600 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14601 | if (uss == null) { |
| 14602 | // User is not started, nothing to do... but we do need to |
| 14603 | // callback if requested. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14604 | if (callback != null) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14605 | mHandler.post(new Runnable() { |
| 14606 | @Override |
| 14607 | public void run() { |
| 14608 | try { |
| 14609 | callback.userStopped(userId); |
| 14610 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14611 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14612 | } |
| 14613 | }); |
| 14614 | } |
| 14615 | return ActivityManager.USER_OP_SUCCESS; |
| 14616 | } |
| 14617 | |
| 14618 | if (callback != null) { |
| 14619 | uss.mStopCallbacks.add(callback); |
| 14620 | } |
| 14621 | |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14622 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14623 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14624 | uss.mState = UserStartedState.STATE_STOPPING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14625 | updateStartedUserArrayLocked(); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14626 | |
| 14627 | long ident = Binder.clearCallingIdentity(); |
| 14628 | try { |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14629 | // We are going to broadcast ACTION_USER_STOPPING and then |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 14630 | // once that is done send a final ACTION_SHUTDOWN and then |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14631 | // stop the user. |
| 14632 | final Intent stoppingIntent = new Intent(Intent.ACTION_USER_STOPPING); |
| 14633 | stoppingIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14634 | stoppingIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14635 | final Intent shutdownIntent = new Intent(Intent.ACTION_SHUTDOWN); |
| 14636 | // This is the result receiver for the final shutdown broadcast. |
| 14637 | final IIntentReceiver shutdownReceiver = new IIntentReceiver.Stub() { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14638 | @Override |
| 14639 | public void performReceive(Intent intent, int resultCode, String data, |
| 14640 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 14641 | finishUserStop(uss); |
| 14642 | } |
| 14643 | }; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14644 | // This is the result receiver for the initial stopping broadcast. |
| 14645 | final IIntentReceiver stoppingReceiver = new IIntentReceiver.Stub() { |
| 14646 | @Override |
| 14647 | public void performReceive(Intent intent, int resultCode, String data, |
| 14648 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 14649 | // On to the next. |
| 14650 | synchronized (ActivityManagerService.this) { |
| 14651 | if (uss.mState != UserStartedState.STATE_STOPPING) { |
| 14652 | // Whoops, we are being started back up. Abort, abort! |
| 14653 | return; |
| 14654 | } |
| 14655 | uss.mState = UserStartedState.STATE_SHUTDOWN; |
| 14656 | } |
| 14657 | broadcastIntentLocked(null, null, shutdownIntent, |
| 14658 | null, shutdownReceiver, 0, null, null, null, |
| 14659 | true, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14660 | } |
| 14661 | }; |
| 14662 | // Kick things off. |
| 14663 | broadcastIntentLocked(null, null, stoppingIntent, |
| 14664 | null, stoppingReceiver, 0, null, null, |
| 14665 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 14666 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14667 | } finally { |
| 14668 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14669 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14670 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14671 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14672 | return ActivityManager.USER_OP_SUCCESS; |
| 14673 | } |
| 14674 | |
| 14675 | void finishUserStop(UserStartedState uss) { |
| 14676 | final int userId = uss.mHandle.getIdentifier(); |
| 14677 | boolean stopped; |
| 14678 | ArrayList<IStopUserCallback> callbacks; |
| 14679 | synchronized (this) { |
| 14680 | callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks); |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14681 | if (mStartedUsers.get(userId) != uss) { |
| 14682 | stopped = false; |
| 14683 | } else if (uss.mState != UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14684 | stopped = false; |
| 14685 | } else { |
| 14686 | stopped = true; |
| 14687 | // User can no longer run. |
| 14688 | mStartedUsers.remove(userId); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14689 | mUserLru.remove(Integer.valueOf(userId)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14690 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14691 | |
| 14692 | // Clean up all state and processes associated with the user. |
| 14693 | // Kill all the processes for the user. |
| 14694 | forceStopUserLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14695 | } |
| 14696 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14697 | |
| 14698 | for (int i=0; i<callbacks.size(); i++) { |
| 14699 | try { |
| 14700 | if (stopped) callbacks.get(i).userStopped(userId); |
| 14701 | else callbacks.get(i).userStopAborted(userId); |
| 14702 | } catch (RemoteException e) { |
| 14703 | } |
| 14704 | } |
| 14705 | } |
| 14706 | |
| 14707 | @Override |
| 14708 | public UserInfo getCurrentUser() { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14709 | if ((checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14710 | != PackageManager.PERMISSION_GRANTED) && ( |
| 14711 | checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14712 | != PackageManager.PERMISSION_GRANTED)) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14713 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 14714 | + Binder.getCallingPid() |
| 14715 | + ", uid=" + Binder.getCallingUid() |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14716 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14717 | Slog.w(TAG, msg); |
| 14718 | throw new SecurityException(msg); |
| 14719 | } |
| 14720 | synchronized (this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14721 | return getUserManagerLocked().getUserInfo(mCurrentUserId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14722 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14723 | } |
| 14724 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 14725 | int getCurrentUserIdLocked() { |
| 14726 | return mCurrentUserId; |
| 14727 | } |
| 14728 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14729 | @Override |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14730 | public boolean isUserRunning(int userId, boolean orStopped) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14731 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14732 | != PackageManager.PERMISSION_GRANTED) { |
| 14733 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14734 | + Binder.getCallingPid() |
| 14735 | + ", uid=" + Binder.getCallingUid() |
| 14736 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14737 | Slog.w(TAG, msg); |
| 14738 | throw new SecurityException(msg); |
| 14739 | } |
| 14740 | synchronized (this) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14741 | return isUserRunningLocked(userId, orStopped); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14742 | } |
| 14743 | } |
| 14744 | |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14745 | boolean isUserRunningLocked(int userId, boolean orStopped) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14746 | UserStartedState state = mStartedUsers.get(userId); |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14747 | if (state == null) { |
| 14748 | return false; |
| 14749 | } |
| 14750 | if (orStopped) { |
| 14751 | return true; |
| 14752 | } |
| 14753 | return state.mState != UserStartedState.STATE_STOPPING |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14754 | && state.mState != UserStartedState.STATE_SHUTDOWN; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14755 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14756 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14757 | @Override |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14758 | public int[] getRunningUserIds() { |
| 14759 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14760 | != PackageManager.PERMISSION_GRANTED) { |
| 14761 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14762 | + Binder.getCallingPid() |
| 14763 | + ", uid=" + Binder.getCallingUid() |
| 14764 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14765 | Slog.w(TAG, msg); |
| 14766 | throw new SecurityException(msg); |
| 14767 | } |
| 14768 | synchronized (this) { |
| 14769 | return mStartedUserArray; |
| 14770 | } |
| 14771 | } |
| 14772 | |
| 14773 | private void updateStartedUserArrayLocked() { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14774 | int num = 0; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14775 | for (int i=0; i<mStartedUsers.size(); i++) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14776 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 14777 | // This list does not include stopping users. |
| 14778 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14779 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
| 14780 | num++; |
| 14781 | } |
| 14782 | } |
| 14783 | mStartedUserArray = new int[num]; |
| 14784 | num = 0; |
| 14785 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 14786 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 14787 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14788 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
| 14789 | mStartedUserArray[num] = mStartedUsers.keyAt(i); |
| 14790 | num++; |
| 14791 | } |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14792 | } |
| 14793 | } |
| 14794 | |
| 14795 | @Override |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14796 | public void registerUserSwitchObserver(IUserSwitchObserver observer) { |
| 14797 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14798 | != PackageManager.PERMISSION_GRANTED) { |
| 14799 | String msg = "Permission Denial: registerUserSwitchObserver() from pid=" |
| 14800 | + Binder.getCallingPid() |
| 14801 | + ", uid=" + Binder.getCallingUid() |
| 14802 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14803 | Slog.w(TAG, msg); |
| 14804 | throw new SecurityException(msg); |
| 14805 | } |
| 14806 | |
| 14807 | mUserSwitchObservers.register(observer); |
| 14808 | } |
| 14809 | |
| 14810 | @Override |
| 14811 | public void unregisterUserSwitchObserver(IUserSwitchObserver observer) { |
| 14812 | mUserSwitchObservers.unregister(observer); |
| 14813 | } |
| 14814 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14815 | private boolean userExists(int userId) { |
| 14816 | if (userId == 0) { |
| 14817 | return true; |
| 14818 | } |
| 14819 | UserManagerService ums = getUserManagerLocked(); |
| 14820 | return ums != null ? (ums.getUserInfo(userId) != null) : false; |
| 14821 | } |
| 14822 | |
| 14823 | int[] getUsersLocked() { |
| 14824 | UserManagerService ums = getUserManagerLocked(); |
| 14825 | return ums != null ? ums.getUserIds() : new int[] { 0 }; |
| 14826 | } |
| 14827 | |
| 14828 | UserManagerService getUserManagerLocked() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14829 | if (mUserManager == null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14830 | IBinder b = ServiceManager.getService(Context.USER_SERVICE); |
| 14831 | mUserManager = (UserManagerService)IUserManager.Stub.asInterface(b); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14832 | } |
| 14833 | return mUserManager; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14834 | } |
| 14835 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14836 | private void checkValidCaller(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14837 | if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14838 | |
| 14839 | throw new SecurityException("Caller uid=" + uid |
| 14840 | + " is not privileged to communicate with user=" + userId); |
| 14841 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14842 | |
| 14843 | private int applyUserId(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14844 | return UserHandle.getUid(userId, uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14845 | } |
| 14846 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 14847 | ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14848 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14849 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 14850 | newInfo.uid = applyUserId(info.uid, userId); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14851 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 14852 | + info.packageName; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14853 | return newInfo; |
| 14854 | } |
| 14855 | |
| 14856 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14857 | if (aInfo == null |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14858 | || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14859 | return aInfo; |
| 14860 | } |
| 14861 | |
| 14862 | ActivityInfo info = new ActivityInfo(aInfo); |
| 14863 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 14864 | return info; |
| 14865 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14866 | } |