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 | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 21 | import android.app.AppOpsManager; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 22 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | 45ce864 | 2011-07-14 16:10:16 -0700 | [diff] [blame] | 24 | import com.android.internal.os.ProcessStats; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 25 | import com.android.server.AppOpsService; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 26 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import com.android.server.IntentResolver; |
| 28 | import com.android.server.ProcessMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import com.android.server.SystemServer; |
| 30 | import com.android.server.Watchdog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 31 | import com.android.server.am.ActivityStack.ActivityState; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 32 | import com.android.server.pm.UserManagerService; |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 33 | import com.android.server.wm.AppTransition; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 34 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 36 | import dalvik.system.Zygote; |
| 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.app.Activity; |
| 39 | import android.app.ActivityManager; |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 40 | import android.app.ActivityManager.RunningTaskInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.app.ActivityManagerNative; |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 42 | import android.app.ActivityOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.app.ActivityThread; |
| 44 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 45 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 46 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 48 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.app.IApplicationThread; |
| 50 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 51 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 52 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.app.IServiceConnection; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 54 | import android.app.IStopUserCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.app.IThumbnailReceiver; |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 56 | import android.app.IUiAutomationConnection; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 57 | import android.app.IUserSwitchObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 59 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 60 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.app.PendingIntent; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 62 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 63 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 64 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 65 | import android.content.ClipData; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 66 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.content.ComponentName; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 68 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import android.content.ContentResolver; |
| 70 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 71 | import android.content.DialogInterface; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 72 | import android.content.IContentProvider; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 73 | import android.content.IIntentReceiver; |
| 74 | import android.content.IIntentSender; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 75 | import android.content.Intent; |
| 76 | import android.content.IntentFilter; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 77 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import android.content.pm.ActivityInfo; |
| 79 | import android.content.pm.ApplicationInfo; |
| 80 | import android.content.pm.ConfigurationInfo; |
| 81 | import android.content.pm.IPackageDataObserver; |
| 82 | import android.content.pm.IPackageManager; |
| 83 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 84 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.content.pm.PackageManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 86 | import android.content.pm.UserInfo; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 87 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 88 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import android.content.pm.ProviderInfo; |
| 90 | import android.content.pm.ResolveInfo; |
| 91 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 92 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | import android.content.res.Configuration; |
| 94 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 95 | import android.net.Proxy; |
| 96 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.net.Uri; |
| 98 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 99 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 100 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 101 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 102 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 104 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.os.FileUtils; |
| 106 | import android.os.Handler; |
| 107 | import android.os.IBinder; |
| 108 | import android.os.IPermissionController; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 109 | import android.os.IRemoteCallback; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 110 | import android.os.IUserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.os.Looper; |
| 112 | import android.os.Message; |
| 113 | import android.os.Parcel; |
| 114 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 116 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import android.os.RemoteException; |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 118 | import android.os.SELinux; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 120 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | import android.os.SystemClock; |
| 122 | import android.os.SystemProperties; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 123 | import android.os.UpdateLock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 124 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import android.provider.Settings; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 126 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | import android.util.EventLog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 128 | import android.util.Log; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 129 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | import android.util.PrintWriterPrinter; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 131 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | import android.util.SparseArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 133 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | import android.view.Gravity; |
| 135 | import android.view.LayoutInflater; |
| 136 | import android.view.View; |
| 137 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 139 | import java.io.BufferedInputStream; |
| 140 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 141 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 142 | import java.io.DataInputStream; |
| 143 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | import java.io.File; |
| 145 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 146 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 148 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 149 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 150 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 152 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | import java.lang.ref.WeakReference; |
| 154 | import java.util.ArrayList; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 155 | import java.util.Arrays; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 156 | import java.util.Collections; |
| 157 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | import java.util.HashMap; |
| 159 | import java.util.HashSet; |
| 160 | import java.util.Iterator; |
| 161 | import java.util.List; |
| 162 | import java.util.Locale; |
| 163 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 164 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 165 | import java.util.concurrent.atomic.AtomicBoolean; |
| 166 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 168 | public final class ActivityManagerService extends ActivityManagerNative |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 169 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 170 | private static final String USER_DATA_DIR = "/data/user/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | static final String TAG = "ActivityManager"; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 172 | static final String TAG_MU = "ActivityManagerServiceMU"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 174 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 176 | static final boolean DEBUG_TASKS = localLOGV || false; |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 177 | static final boolean DEBUG_THUMBNAILS = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 179 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 180 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 181 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 182 | static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 183 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 185 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 187 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 188 | static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false; |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 189 | static final boolean DEBUG_CLEANUP = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 190 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 191 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 193 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 194 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 195 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 196 | static final boolean DEBUG_POWER = localLOGV || false; |
| 197 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 198 | static final boolean DEBUG_MU = localLOGV || false; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 199 | static final boolean DEBUG_IMMERSIVE = localLOGV || false; |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 200 | static final boolean VALIDATE_TOKENS = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 202 | |
| 203 | // Control over CPU and battery monitoring. |
| 204 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 205 | static final boolean MONITOR_CPU_USAGE = true; |
| 206 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 207 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 208 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 209 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 210 | // 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] | 211 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 212 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 213 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 215 | static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); |
| 216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | // Maximum number of recent tasks that we can remember. |
| 218 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 219 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 220 | // Amount of time after a call to stopAppSwitches() during which we will |
| 221 | // prevent further untrusted switches from happening. |
| 222 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | |
| 224 | // How long we wait for a launched process to attach to the activity manager |
| 225 | // before we decide it's never going to come up for real. |
| 226 | static final int PROC_START_TIMEOUT = 10*1000; |
| 227 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 228 | // How long we wait for a launched process to attach to the activity manager |
| 229 | // before we decide it's never going to come up for real, when the process was |
| 230 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 231 | // could take much longer than usual. |
| 232 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | // How long to wait after going idle before forcing apps to GC. |
| 235 | static final int GC_TIMEOUT = 5*1000; |
| 236 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 237 | // The minimum amount of time between successive GC requests for a process. |
| 238 | static final int GC_MIN_INTERVAL = 60*1000; |
| 239 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 240 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 241 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 242 | |
| 243 | // The minimum sample duration we will allow before deciding we have |
| 244 | // enough data on wake locks to start killing things. |
| 245 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 246 | |
| 247 | // The minimum sample duration we will allow before deciding we have |
| 248 | // enough data on CPU usage to start killing things. |
| 249 | 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] | 250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | // 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] | 252 | static final int BROADCAST_FG_TIMEOUT = 10*1000; |
| 253 | static final int BROADCAST_BG_TIMEOUT = 60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | // How long we wait until we timeout on key dispatching. |
| 256 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 259 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 260 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 261 | // Amount of time we wait for observers to handle a user switch before |
| 262 | // giving up on them and unfreezing the screen. |
| 263 | static final int USER_SWITCH_TIMEOUT = 2*1000; |
| 264 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 265 | // Maximum number of users we allow to be running at a time. |
| 266 | static final int MAX_RUNNING_USERS = 3; |
| 267 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 268 | // How long to wait in getTopActivityExtras for the activity to respond with the result. |
| 269 | static final int PENDING_ACTIVITY_RESULT_TIMEOUT = 2*2000; |
| 270 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 271 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | |
| 273 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 274 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 275 | public ActivityStack mMainStack; |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 276 | |
| 277 | private final boolean mHeadless; |
| 278 | |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 279 | // Whether we should show our dialogs (ANR, crash, etc) or just perform their |
| 280 | // default actuion automatically. Important for devices without direct input |
| 281 | // devices. |
| 282 | private boolean mShowDialogs = true; |
| 283 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 285 | * Description of a request to start a new activity, which has been held |
| 286 | * due to app switches being disabled. |
| 287 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 288 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 289 | ActivityRecord r; |
| 290 | ActivityRecord sourceRecord; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 291 | int startFlags; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 295 | = new ArrayList<PendingActivityLaunch>(); |
| 296 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 298 | BroadcastQueue mFgBroadcastQueue; |
| 299 | BroadcastQueue mBgBroadcastQueue; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 300 | // Convenient for easy iteration over the queues. Foreground is first |
| 301 | // so that dispatch of foreground broadcasts gets precedence. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 302 | final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 303 | |
| 304 | BroadcastQueue broadcastQueueForIntent(Intent intent) { |
| 305 | final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; |
| 306 | if (DEBUG_BACKGROUND_BROADCAST) { |
| 307 | Slog.i(TAG, "Broadcast intent " + intent + " on " |
| 308 | + (isFg ? "foreground" : "background") |
| 309 | + " queue"); |
| 310 | } |
| 311 | return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; |
| 312 | } |
| 313 | |
| 314 | BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) { |
| 315 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 316 | BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver); |
| 317 | if (r != null) { |
| 318 | return r; |
| 319 | } |
| 320 | } |
| 321 | return null; |
| 322 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | |
| 324 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | * Activity we have told the window manager to have key focus. |
| 326 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 327 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 328 | |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 329 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | * List of intents that were used to start the most recent tasks. |
| 331 | */ |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 332 | private final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 334 | public class PendingActivityExtras extends Binder implements Runnable { |
| 335 | public final ActivityRecord activity; |
| 336 | public boolean haveResult = false; |
| 337 | public Bundle result = null; |
| 338 | public PendingActivityExtras(ActivityRecord _activity) { |
| 339 | activity = _activity; |
| 340 | } |
| 341 | @Override |
| 342 | public void run() { |
| 343 | Slog.w(TAG, "getTopActivityExtras failed: timeout retrieving from " + activity); |
| 344 | synchronized (this) { |
| 345 | haveResult = true; |
| 346 | notifyAll(); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | final ArrayList<PendingActivityExtras> mPendingActivityExtras |
| 352 | = new ArrayList<PendingActivityExtras>(); |
| 353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 355 | * Process management. |
| 356 | */ |
| 357 | final ProcessList mProcessList = new ProcessList(); |
| 358 | |
| 359 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | * All of the applications we currently have running organized by name. |
| 361 | * The keys are strings of the application package name (as |
| 362 | * returned by the package manager), and the keys are ApplicationRecord |
| 363 | * objects. |
| 364 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 365 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | |
| 367 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 368 | * The currently running isolated processes. |
| 369 | */ |
| 370 | final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>(); |
| 371 | |
| 372 | /** |
| 373 | * Counter for assigning isolated process uids, to avoid frequently reusing the |
| 374 | * same ones. |
| 375 | */ |
| 376 | int mNextIsolatedProcessUid = 0; |
| 377 | |
| 378 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 379 | * The currently running heavy-weight process, if any. |
| 380 | */ |
| 381 | ProcessRecord mHeavyWeightProcess = null; |
| 382 | |
| 383 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | * The last time that various processes have crashed. |
| 385 | */ |
| 386 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 387 | |
| 388 | /** |
| 389 | * Set of applications that we consider to be bad, and will reject |
| 390 | * incoming broadcasts from (which the user has no control over). |
| 391 | * Processes are added to this set when they have crashed twice within |
| 392 | * a minimum amount of time; they are removed from it when they are |
| 393 | * later restarted (hopefully due to some user action). The value is the |
| 394 | * time it was added to the list. |
| 395 | */ |
| 396 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 397 | |
| 398 | /** |
| 399 | * All of the processes we currently have running organized by pid. |
| 400 | * The keys are the pid running the application. |
| 401 | * |
| 402 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 403 | * activity manager lock! |
| 404 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 405 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | |
| 407 | /** |
| 408 | * All of the processes that have been forced to be foreground. The key |
| 409 | * is the pid of the caller who requested it (we hold a death |
| 410 | * link on it). |
| 411 | */ |
| 412 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 413 | int pid; |
| 414 | IBinder token; |
| 415 | } |
| 416 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 417 | = new SparseArray<ForegroundToken>(); |
| 418 | |
| 419 | /** |
| 420 | * List of records for processes that someone had tried to start before the |
| 421 | * system was ready. We don't start them at that point, but ensure they |
| 422 | * are started by the time booting is complete. |
| 423 | */ |
| 424 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 425 | = new ArrayList<ProcessRecord>(); |
| 426 | |
| 427 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | * List of persistent applications that are in the process |
| 429 | * of being started. |
| 430 | */ |
| 431 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 432 | = new ArrayList<ProcessRecord>(); |
| 433 | |
| 434 | /** |
| 435 | * Processes that are being forcibly torn down. |
| 436 | */ |
| 437 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 438 | = new ArrayList<ProcessRecord>(); |
| 439 | |
| 440 | /** |
| 441 | * List of running applications, sorted by recent usage. |
| 442 | * The first entry in the list is the least recently used. |
| 443 | * It contains ApplicationRecord objects. This list does NOT include |
| 444 | * any persistent application records (since we never want to exit them). |
| 445 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 446 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | = new ArrayList<ProcessRecord>(); |
| 448 | |
| 449 | /** |
| 450 | * List of processes that should gc as soon as things are idle. |
| 451 | */ |
| 452 | final ArrayList<ProcessRecord> mProcessesToGc |
| 453 | = new ArrayList<ProcessRecord>(); |
| 454 | |
| 455 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 456 | * This is the process holding what we currently consider to be |
| 457 | * the "home" activity. |
| 458 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 459 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 460 | |
| 461 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 462 | * This is the process holding the activity the user last visited that |
| 463 | * is in a different process from the one they are currently in. |
| 464 | */ |
| 465 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 466 | |
| 467 | /** |
| 468 | * The time at which the previous process was last visible. |
| 469 | */ |
| 470 | long mPreviousProcessVisibleTime; |
| 471 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 472 | /** |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 473 | * Which uses have been started, so are allowed to run code. |
| 474 | */ |
| 475 | final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>(); |
| 476 | |
| 477 | /** |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 478 | * LRU list of history of current users. Most recently current is at the end. |
| 479 | */ |
| 480 | final ArrayList<Integer> mUserLru = new ArrayList<Integer>(); |
| 481 | |
| 482 | /** |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 483 | * Constant array of the users that are currently started. |
| 484 | */ |
| 485 | int[] mStartedUserArray = new int[] { 0 }; |
| 486 | |
| 487 | /** |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 488 | * Registered observers of the user switching mechanics. |
| 489 | */ |
| 490 | final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers |
| 491 | = new RemoteCallbackList<IUserSwitchObserver>(); |
| 492 | |
| 493 | /** |
| 494 | * Currently active user switch. |
| 495 | */ |
| 496 | Object mCurUserSwitchCallback; |
| 497 | |
| 498 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 499 | * Packages that the user has asked to have run in screen size |
| 500 | * compatibility mode instead of filling the screen. |
| 501 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 502 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 503 | |
| 504 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | * Set of PendingResultRecord objects that are currently active. |
| 506 | */ |
| 507 | final HashSet mPendingResultRecords = new HashSet(); |
| 508 | |
| 509 | /** |
| 510 | * Set of IntentSenderRecord objects that are currently active. |
| 511 | */ |
| 512 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 513 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 514 | |
| 515 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 516 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 517 | * already logged DropBox entries for. Guarded by itself. If |
| 518 | * something (rogue user app) forces this over |
| 519 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 520 | */ |
| 521 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 522 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 523 | |
| 524 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 525 | * Strict Mode background batched logging state. |
| 526 | * |
| 527 | * The string buffer is guarded by itself, and its lock is also |
| 528 | * used to determine if another batched write is already |
| 529 | * in-flight. |
| 530 | */ |
| 531 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 532 | |
| 533 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | * Keeps track of all IIntentReceivers that have been registered for |
| 535 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 536 | * a ReceiverList. |
| 537 | */ |
| 538 | final HashMap mRegisteredReceivers = new HashMap(); |
| 539 | |
| 540 | /** |
| 541 | * Resolver for broadcast intents to registered receivers. |
| 542 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 543 | */ |
| 544 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 545 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 546 | @Override |
| 547 | protected boolean allowFilterResult( |
| 548 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 549 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 550 | for (int i=dest.size()-1; i>=0; i--) { |
| 551 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 552 | return false; |
| 553 | } |
| 554 | } |
| 555 | return true; |
| 556 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 557 | |
| 558 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 559 | protected BroadcastFilter newResult(BroadcastFilter filter, int match, int userId) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 560 | if (userId == UserHandle.USER_ALL || filter.owningUserId == UserHandle.USER_ALL |
| 561 | || userId == filter.owningUserId) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 562 | return super.newResult(filter, match, userId); |
| 563 | } |
| 564 | return null; |
| 565 | } |
| 566 | |
| 567 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 568 | protected BroadcastFilter[] newArray(int size) { |
| 569 | return new BroadcastFilter[size]; |
| 570 | } |
| 571 | |
| 572 | @Override |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 573 | protected String packageForFilter(BroadcastFilter filter) { |
| 574 | return filter.packageName; |
| 575 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | /** |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 579 | * 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] | 580 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 581 | * that action (which should usually be one). The SparseArray is keyed |
| 582 | * by the user ID the sticky is for, and can include UserHandle.USER_ALL |
| 583 | * for stickies that are sent to all users. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | */ |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 585 | final SparseArray<HashMap<String, ArrayList<Intent>>> mStickyBroadcasts = |
| 586 | new SparseArray<HashMap<String, ArrayList<Intent>>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 588 | final ActiveServices mServices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | |
| 590 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 591 | * Backup/restore process management |
| 592 | */ |
| 593 | String mBackupAppName = null; |
| 594 | BackupRecord mBackupTarget = null; |
| 595 | |
| 596 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | * List of PendingThumbnailsRecord objects of clients who are still |
| 598 | * waiting to receive all of the thumbnails for a task. |
| 599 | */ |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 600 | final ArrayList<PendingThumbnailsRecord> mPendingThumbnails = |
| 601 | new ArrayList<PendingThumbnailsRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 603 | final ProviderMap mProviderMap; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 604 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | /** |
| 606 | * List of content providers who have clients waiting for them. The |
| 607 | * application is currently being launched and the provider will be |
| 608 | * removed from this list once it is published. |
| 609 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 610 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 611 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | |
| 613 | /** |
| 614 | * Global set of specific Uri permissions that have been granted. |
| 615 | */ |
| 616 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 617 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 618 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 619 | CoreSettingsObserver mCoreSettingsObserver; |
| 620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | /** |
| 622 | * Thread-local storage used to carry caller permissions over through |
| 623 | * indirect content-provider access. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | */ |
| 625 | private class Identity { |
| 626 | public int pid; |
| 627 | public int uid; |
| 628 | |
| 629 | Identity(int _pid, int _uid) { |
| 630 | pid = _pid; |
| 631 | uid = _uid; |
| 632 | } |
| 633 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 634 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 636 | |
| 637 | /** |
| 638 | * All information we have collected about the runtime performance of |
| 639 | * any user id that can impact battery performance. |
| 640 | */ |
| 641 | final BatteryStatsService mBatteryStatsService; |
| 642 | |
| 643 | /** |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 644 | * Information about component usage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | */ |
| 646 | final UsageStatsService mUsageStatsService; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 647 | |
| 648 | /** |
| 649 | * Information about and control over application operations |
| 650 | */ |
| 651 | final AppOpsService mAppOpsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | |
| 653 | /** |
| 654 | * Current configuration information. HistoryRecord objects are given |
| 655 | * a reference to this object to indicate which configuration they are |
| 656 | * currently running in, so this object must be kept immutable. |
| 657 | */ |
| 658 | Configuration mConfiguration = new Configuration(); |
| 659 | |
| 660 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 661 | * Current sequencing integer of the configuration, for skipping old |
| 662 | * configurations. |
| 663 | */ |
| 664 | int mConfigurationSeq = 0; |
| 665 | |
| 666 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 667 | * Hardware-reported OpenGLES version. |
| 668 | */ |
| 669 | final int GL_ES_VERSION; |
| 670 | |
| 671 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 673 | * For example, references to the commonly used services. |
| 674 | */ |
| 675 | HashMap<String, IBinder> mAppBindArgs; |
| 676 | |
| 677 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 678 | * Temporary to avoid allocations. Protected by main lock. |
| 679 | */ |
| 680 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 681 | |
| 682 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | * Used to control how we initialize the service. |
| 684 | */ |
| 685 | boolean mStartRunning = false; |
| 686 | ComponentName mTopComponent; |
| 687 | String mTopAction; |
| 688 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 689 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | boolean mSystemReady = false; |
| 691 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 692 | boolean mWaitingUpdate = false; |
| 693 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 694 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 695 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | |
| 697 | Context mContext; |
| 698 | |
| 699 | int mFactoryTest; |
| 700 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 701 | boolean mCheckedForSetup; |
| 702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 704 | * The time at which we will allow normal application switches again, |
| 705 | * after a call to {@link #stopAppSwitches()}. |
| 706 | */ |
| 707 | long mAppSwitchesAllowedTime; |
| 708 | |
| 709 | /** |
| 710 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 711 | * is set; any switches after that will clear the time. |
| 712 | */ |
| 713 | boolean mDidAppSwitch; |
| 714 | |
| 715 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 716 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 717 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 718 | long mLastPowerCheckRealtime; |
| 719 | |
| 720 | /** |
| 721 | * Last time (in uptime) at which we checked for power usage. |
| 722 | */ |
| 723 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 724 | |
| 725 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | * Set while we are wanting to sleep, to prevent any |
| 727 | * activities from being started/resumed. |
| 728 | */ |
| 729 | boolean mSleeping = false; |
| 730 | |
| 731 | /** |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 732 | * State of external calls telling us if the device is asleep. |
| 733 | */ |
| 734 | boolean mWentToSleep = false; |
| 735 | |
| 736 | /** |
| 737 | * State of external call telling us if the lock screen is shown. |
| 738 | */ |
| 739 | boolean mLockScreenShown = false; |
| 740 | |
| 741 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 742 | * Set if we are shutting down the system, similar to sleeping. |
| 743 | */ |
| 744 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | |
| 746 | /** |
| 747 | * Task identifier that activities are currently being started |
| 748 | * in. Incremented each time a new task is created. |
| 749 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 750 | * integers that won't wrap. |
| 751 | */ |
| 752 | int mCurTask = 1; |
| 753 | |
| 754 | /** |
| 755 | * Current sequence id for oom_adj computation traversal. |
| 756 | */ |
| 757 | int mAdjSeq = 0; |
| 758 | |
| 759 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 760 | * Current sequence id for process LRU updating. |
| 761 | */ |
| 762 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | |
| 764 | /** |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 765 | * Keep track of the non-hidden/empty process we last found, to help |
| 766 | * determine how to distribute hidden/empty processes next time. |
| 767 | */ |
| 768 | int mNumNonHiddenProcs = 0; |
| 769 | |
| 770 | /** |
| 771 | * Keep track of the number of hidden procs, to balance oom adj |
| 772 | * distribution between those and empty procs. |
| 773 | */ |
| 774 | int mNumHiddenProcs = 0; |
| 775 | |
| 776 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 777 | * Keep track of the number of service processes we last found, to |
| 778 | * determine on the next iteration which should be B services. |
| 779 | */ |
| 780 | int mNumServiceProcs = 0; |
| 781 | int mNewNumServiceProcs = 0; |
| 782 | |
| 783 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | * System monitoring: number of processes that died since the last |
| 785 | * N procs were started. |
| 786 | */ |
| 787 | int[] mProcDeaths = new int[20]; |
| 788 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 789 | /** |
| 790 | * This is set if we had to do a delayed dexopt of an app before launching |
| 791 | * it, to increasing the ANR timeouts in that case. |
| 792 | */ |
| 793 | boolean mDidDexOpt; |
| 794 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | String mDebugApp = null; |
| 796 | boolean mWaitForDebugger = false; |
| 797 | boolean mDebugTransient = false; |
| 798 | String mOrigDebugApp = null; |
| 799 | boolean mOrigWaitForDebugger = false; |
| 800 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 801 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 802 | String mProfileApp = null; |
| 803 | ProcessRecord mProfileProc = null; |
| 804 | String mProfileFile; |
| 805 | ParcelFileDescriptor mProfileFd; |
| 806 | int mProfileType = 0; |
| 807 | boolean mAutoStopProfiler = false; |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 808 | String mOpenGlTraceApp = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 810 | static class ProcessChangeItem { |
| 811 | static final int CHANGE_ACTIVITIES = 1<<0; |
| 812 | static final int CHANGE_IMPORTANCE= 1<<1; |
| 813 | int changes; |
| 814 | int uid; |
| 815 | int pid; |
| 816 | int importance; |
| 817 | boolean foregroundActivities; |
| 818 | } |
| 819 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 820 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 821 | = new RemoteCallbackList<IProcessObserver>(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 822 | ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5]; |
| 823 | |
| 824 | final ArrayList<ProcessChangeItem> mPendingProcessChanges |
| 825 | = new ArrayList<ProcessChangeItem>(); |
| 826 | final ArrayList<ProcessChangeItem> mAvailProcessChanges |
| 827 | = new ArrayList<ProcessChangeItem>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 828 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 830 | * Runtime statistics collection thread. This object's lock is used to |
| 831 | * protect all related state. |
| 832 | */ |
| 833 | final Thread mProcessStatsThread; |
| 834 | |
| 835 | /** |
| 836 | * Used to collect process stats when showing not responding dialog. |
| 837 | * Protected by mProcessStatsThread. |
| 838 | */ |
| 839 | final ProcessStats mProcessStats = new ProcessStats( |
| 840 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 841 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 842 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | long mLastWriteTime = 0; |
| 845 | |
| 846 | /** |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 847 | * Used to retain an update lock when the foreground activity is in |
| 848 | * immersive mode. |
| 849 | */ |
| 850 | final UpdateLock mUpdateLock = new UpdateLock("immersive"); |
| 851 | |
| 852 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | * Set to true after the system has finished booting. |
| 854 | */ |
| 855 | boolean mBooted = false; |
| 856 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 857 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 858 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | |
| 860 | WindowManagerService mWindowManager; |
| 861 | |
| 862 | static ActivityManagerService mSelf; |
| 863 | static ActivityThread mSystemThread; |
| 864 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 865 | private int mCurrentUserId = 0; |
| 866 | private int[] mCurrentUserArray = new int[] { 0 }; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 867 | private UserManagerService mUserManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 868 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 870 | final ProcessRecord mApp; |
| 871 | final int mPid; |
| 872 | final IApplicationThread mAppThread; |
| 873 | |
| 874 | AppDeathRecipient(ProcessRecord app, int pid, |
| 875 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 876 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | TAG, "New death recipient " + this |
| 878 | + " for thread " + thread.asBinder()); |
| 879 | mApp = app; |
| 880 | mPid = pid; |
| 881 | mAppThread = thread; |
| 882 | } |
| 883 | |
| 884 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 885 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | TAG, "Death received in " + this |
| 887 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | synchronized(ActivityManagerService.this) { |
| 889 | appDiedLocked(mApp, mPid, mAppThread); |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | static final int SHOW_ERROR_MSG = 1; |
| 895 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 896 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 897 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 898 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 899 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 901 | static final int UPDATE_TIME_ZONE = 13; |
| 902 | static final int SHOW_UID_ERROR_MSG = 14; |
| 903 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 905 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 906 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 907 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 908 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 909 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 910 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 911 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 912 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 913 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 914 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 915 | static final int DISPATCH_PROCESSES_CHANGED = 31; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 916 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 917 | static final int REPORT_MEM_USAGE = 33; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 918 | static final int REPORT_USER_SWITCH_MSG = 34; |
| 919 | static final int CONTINUE_USER_SWITCH_MSG = 35; |
| 920 | static final int USER_SWITCH_TIMEOUT_MSG = 36; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 921 | static final int IMMERSIVE_MODE_LOCK_MSG = 37; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 923 | static final int FIRST_ACTIVITY_STACK_MSG = 100; |
| 924 | static final int FIRST_BROADCAST_QUEUE_MSG = 200; |
| 925 | static final int FIRST_COMPAT_MODE_MSG = 300; |
| 926 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 928 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 929 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | |
| 931 | final Handler mHandler = new Handler() { |
| 932 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 933 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | //} |
| 935 | |
| 936 | public void handleMessage(Message msg) { |
| 937 | switch (msg.what) { |
| 938 | case SHOW_ERROR_MSG: { |
| 939 | HashMap data = (HashMap) msg.obj; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 940 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 941 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | synchronized (ActivityManagerService.this) { |
| 943 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 944 | AppErrorResult res = (AppErrorResult) data.get("result"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 946 | Slog.e(TAG, "App already has crash dialog: " + proc); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 947 | if (res != null) { |
| 948 | res.set(0); |
| 949 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | return; |
| 951 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 952 | if (!showBackground && UserHandle.getAppId(proc.uid) |
| 953 | >= Process.FIRST_APPLICATION_UID && proc.userId != mCurrentUserId |
| 954 | && proc.pid != MY_PID) { |
| 955 | Slog.w(TAG, "Skipping crash dialog of " + proc + ": background"); |
| 956 | if (res != null) { |
| 957 | res.set(0); |
| 958 | } |
| 959 | return; |
| 960 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 961 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 962 | Dialog d = new AppErrorDialog(mContext, |
| 963 | ActivityManagerService.this, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | d.show(); |
| 965 | proc.crashDialog = d; |
| 966 | } else { |
| 967 | // The device is asleep, so just pretend that the user |
| 968 | // saw a crash dialog and hit "force quit". |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 969 | if (res != null) { |
| 970 | res.set(0); |
| 971 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | } |
| 973 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 974 | |
| 975 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | } break; |
| 977 | case SHOW_NOT_RESPONDING_MSG: { |
| 978 | synchronized (ActivityManagerService.this) { |
| 979 | HashMap data = (HashMap) msg.obj; |
| 980 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 981 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 982 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | return; |
| 984 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 985 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 986 | Intent intent = new Intent("android.intent.action.ANR"); |
| 987 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 988 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 989 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 990 | } |
| 991 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 992 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 993 | 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] | 994 | |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 995 | if (mShowDialogs) { |
| 996 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 997 | mContext, proc, (ActivityRecord)data.get("activity"), |
| 998 | msg.arg1 != 0); |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 999 | d.show(); |
| 1000 | proc.anrDialog = d; |
| 1001 | } else { |
| 1002 | // Just kill the app if there is no dialog to be shown. |
| 1003 | killAppAtUsersRequest(proc, null); |
| 1004 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1006 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1007 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1009 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 1010 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 1011 | synchronized (ActivityManagerService.this) { |
| 1012 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 1013 | if (proc == null) { |
| 1014 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 1015 | break; |
| 1016 | } |
| 1017 | if (proc.crashDialog != null) { |
| 1018 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 1019 | return; |
| 1020 | } |
| 1021 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1022 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1023 | Dialog d = new StrictModeViolationDialog(mContext, |
| 1024 | ActivityManagerService.this, res, proc); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1025 | d.show(); |
| 1026 | proc.crashDialog = d; |
| 1027 | } else { |
| 1028 | // The device is asleep, so just pretend that the user |
| 1029 | // saw a crash dialog and hit "force quit". |
| 1030 | res.set(0); |
| 1031 | } |
| 1032 | } |
| 1033 | ensureBootCompleted(); |
| 1034 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | case SHOW_FACTORY_ERROR_MSG: { |
| 1036 | Dialog d = new FactoryErrorDialog( |
| 1037 | mContext, msg.getData().getCharSequence("msg")); |
| 1038 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1039 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | } break; |
| 1041 | case UPDATE_CONFIGURATION_MSG: { |
| 1042 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1043 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 1044 | } break; |
| 1045 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 1046 | synchronized (ActivityManagerService.this) { |
| 1047 | performAppGcsIfAppropriateLocked(); |
| 1048 | } |
| 1049 | } break; |
| 1050 | case WAIT_FOR_DEBUGGER_MSG: { |
| 1051 | synchronized (ActivityManagerService.this) { |
| 1052 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1053 | if (msg.arg1 != 0) { |
| 1054 | if (!app.waitedForDebugger) { |
| 1055 | Dialog d = new AppWaitingForDebuggerDialog( |
| 1056 | ActivityManagerService.this, |
| 1057 | mContext, app); |
| 1058 | app.waitDialog = d; |
| 1059 | app.waitedForDebugger = true; |
| 1060 | d.show(); |
| 1061 | } |
| 1062 | } else { |
| 1063 | if (app.waitDialog != null) { |
| 1064 | app.waitDialog.dismiss(); |
| 1065 | app.waitDialog = null; |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1070 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1071 | if (mDidDexOpt) { |
| 1072 | mDidDexOpt = false; |
| 1073 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 1074 | nmsg.obj = msg.obj; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1075 | mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1076 | return; |
| 1077 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1078 | mServices.serviceTimeout((ProcessRecord)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | } break; |
| 1080 | case UPDATE_TIME_ZONE: { |
| 1081 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1082 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1083 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1084 | if (r.thread != null) { |
| 1085 | try { |
| 1086 | r.thread.updateTimeZone(); |
| 1087 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1088 | 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] | 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1093 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1094 | case CLEAR_DNS_CACHE: { |
| 1095 | synchronized (ActivityManagerService.this) { |
| 1096 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1097 | ProcessRecord r = mLruProcesses.get(i); |
| 1098 | if (r.thread != null) { |
| 1099 | try { |
| 1100 | r.thread.clearDnsCache(); |
| 1101 | } catch (RemoteException ex) { |
| 1102 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1108 | case UPDATE_HTTP_PROXY: { |
| 1109 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1110 | String host = ""; |
| 1111 | String port = ""; |
| 1112 | String exclList = ""; |
| 1113 | if (proxy != null) { |
| 1114 | host = proxy.getHost(); |
| 1115 | port = Integer.toString(proxy.getPort()); |
| 1116 | exclList = proxy.getExclusionList(); |
| 1117 | } |
| 1118 | synchronized (ActivityManagerService.this) { |
| 1119 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1120 | ProcessRecord r = mLruProcesses.get(i); |
| 1121 | if (r.thread != null) { |
| 1122 | try { |
| 1123 | r.thread.setHttpProxy(host, port, exclList); |
| 1124 | } catch (RemoteException ex) { |
| 1125 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1126 | r.info.processName); |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1132 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1133 | String title = "System UIDs Inconsistent"; |
| 1134 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1135 | + " data partition or your device will be unstable."; |
| 1136 | Log.e(TAG, title + ": " + text); |
| 1137 | if (mShowDialogs) { |
| 1138 | // XXX This is a temporary dialog, no need to localize. |
| 1139 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1140 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1141 | d.setCancelable(false); |
| 1142 | d.setTitle(title); |
| 1143 | d.setMessage(text); |
| 1144 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1145 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1146 | mUidAlert = d; |
| 1147 | d.show(); |
| 1148 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | } break; |
| 1150 | case IM_FEELING_LUCKY_MSG: { |
| 1151 | if (mUidAlert != null) { |
| 1152 | mUidAlert.dismiss(); |
| 1153 | mUidAlert = null; |
| 1154 | } |
| 1155 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1157 | if (mDidDexOpt) { |
| 1158 | mDidDexOpt = false; |
| 1159 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1160 | nmsg.obj = msg.obj; |
| 1161 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1162 | return; |
| 1163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1165 | synchronized (ActivityManagerService.this) { |
| 1166 | processStartTimedOutLocked(app); |
| 1167 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1168 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1169 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1170 | synchronized (ActivityManagerService.this) { |
| 1171 | doPendingActivityLaunchesLocked(true); |
| 1172 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1173 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1174 | case KILL_APPLICATION_MSG: { |
| 1175 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1176 | int appid = msg.arg1; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1177 | boolean restart = (msg.arg2 == 1); |
| 1178 | String pkg = (String) msg.obj; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1179 | forceStopPackageLocked(pkg, appid, restart, false, true, false, |
| 1180 | UserHandle.USER_ALL); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1181 | } |
| 1182 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1183 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1184 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1185 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1186 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1187 | INotificationManager inm = NotificationManager.getService(); |
| 1188 | if (inm == null) { |
| 1189 | return; |
| 1190 | } |
| 1191 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1192 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1193 | ProcessRecord process = root.app; |
| 1194 | if (process == null) { |
| 1195 | return; |
| 1196 | } |
| 1197 | |
| 1198 | try { |
| 1199 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1200 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1201 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1202 | Notification notification = new Notification(); |
| 1203 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1204 | notification.when = 0; |
| 1205 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1206 | notification.tickerText = text; |
| 1207 | notification.defaults = 0; // please be quiet |
| 1208 | notification.sound = null; |
| 1209 | notification.vibrate = null; |
| 1210 | notification.setLatestEventInfo(context, text, |
| 1211 | mContext.getText(R.string.heavy_weight_notification_detail), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1212 | PendingIntent.getActivityAsUser(mContext, 0, root.intent, |
| 1213 | PendingIntent.FLAG_CANCEL_CURRENT, null, |
| 1214 | new UserHandle(root.userId))); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1215 | |
| 1216 | try { |
| 1217 | int[] outId = new int[1]; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1218 | inm.enqueueNotificationWithTag("android", "android", null, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1219 | R.string.heavy_weight_notification, |
| 1220 | notification, outId, root.userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1221 | } catch (RuntimeException e) { |
| 1222 | Slog.w(ActivityManagerService.TAG, |
| 1223 | "Error showing notification for heavy-weight app", e); |
| 1224 | } catch (RemoteException e) { |
| 1225 | } |
| 1226 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1227 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1228 | } |
| 1229 | } break; |
| 1230 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1231 | INotificationManager inm = NotificationManager.getService(); |
| 1232 | if (inm == null) { |
| 1233 | return; |
| 1234 | } |
| 1235 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1236 | inm.cancelNotificationWithTag("android", null, |
| 1237 | R.string.heavy_weight_notification, msg.arg1); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1238 | } catch (RuntimeException e) { |
| 1239 | Slog.w(ActivityManagerService.TAG, |
| 1240 | "Error canceling notification for service", e); |
| 1241 | } catch (RemoteException e) { |
| 1242 | } |
| 1243 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1244 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1245 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1246 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1247 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1248 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1249 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1250 | } |
| 1251 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1252 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1253 | synchronized (ActivityManagerService.this) { |
| 1254 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1255 | if (mCompatModeDialog != null) { |
| 1256 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1257 | ar.info.applicationInfo.packageName)) { |
| 1258 | return; |
| 1259 | } |
| 1260 | mCompatModeDialog.dismiss(); |
| 1261 | mCompatModeDialog = null; |
| 1262 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1263 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1264 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1265 | ar.packageName)) { |
| 1266 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1267 | ar.info.applicationInfo); |
| 1268 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1269 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1270 | mCompatModeDialog = new CompatModeDialog( |
| 1271 | ActivityManagerService.this, mContext, |
| 1272 | ar.info.applicationInfo); |
| 1273 | mCompatModeDialog.show(); |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1278 | break; |
| 1279 | } |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 1280 | case DISPATCH_PROCESSES_CHANGED: { |
| 1281 | dispatchProcessesChanged(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1282 | break; |
| 1283 | } |
| 1284 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1285 | final int pid = msg.arg1; |
| 1286 | final int uid = msg.arg2; |
| 1287 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1288 | break; |
| 1289 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1290 | case REPORT_MEM_USAGE: { |
| 1291 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1292 | if (!isDebuggable) { |
| 1293 | return; |
| 1294 | } |
| 1295 | synchronized (ActivityManagerService.this) { |
| 1296 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1297 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1298 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1299 | // avoid spamming. |
| 1300 | return; |
| 1301 | } |
| 1302 | mLastMemUsageReportTime = now; |
| 1303 | } |
| 1304 | Thread thread = new Thread() { |
| 1305 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1306 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1307 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1308 | StringWriter oomSw = new StringWriter(); |
| 1309 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1310 | StringWriter catSw = new StringWriter(); |
| 1311 | PrintWriter catPw = new PrintWriter(catSw); |
| 1312 | String[] emptyArgs = new String[] { }; |
| 1313 | StringBuilder tag = new StringBuilder(128); |
| 1314 | StringBuilder stack = new StringBuilder(128); |
| 1315 | tag.append("Low on memory -- "); |
| 1316 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1317 | tag, stack); |
| 1318 | dropBuilder.append(stack); |
| 1319 | dropBuilder.append('\n'); |
| 1320 | dropBuilder.append('\n'); |
| 1321 | String oomString = oomSw.toString(); |
| 1322 | dropBuilder.append(oomString); |
| 1323 | dropBuilder.append('\n'); |
| 1324 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1325 | try { |
| 1326 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1327 | "procrank", }); |
| 1328 | final InputStreamReader converter = new InputStreamReader( |
| 1329 | proc.getInputStream()); |
| 1330 | BufferedReader in = new BufferedReader(converter); |
| 1331 | String line; |
| 1332 | while (true) { |
| 1333 | line = in.readLine(); |
| 1334 | if (line == null) { |
| 1335 | break; |
| 1336 | } |
| 1337 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1338 | logBuilder.append(line); |
| 1339 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1340 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1341 | dropBuilder.append(line); |
| 1342 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1343 | } |
| 1344 | converter.close(); |
| 1345 | } catch (IOException e) { |
| 1346 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1347 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1348 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1349 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1350 | catPw.println(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1351 | mServices.dumpServicesLocked(null, catPw, emptyArgs, 0, |
| 1352 | false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1353 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1354 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1355 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1356 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1357 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1358 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1359 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1360 | synchronized (ActivityManagerService.this) { |
| 1361 | long now = SystemClock.uptimeMillis(); |
| 1362 | if (mLastMemUsageReportTime < now) { |
| 1363 | mLastMemUsageReportTime = now; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | }; |
| 1368 | thread.start(); |
| 1369 | break; |
| 1370 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1371 | case REPORT_USER_SWITCH_MSG: { |
| 1372 | dispatchUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1373 | break; |
| 1374 | } |
| 1375 | case CONTINUE_USER_SWITCH_MSG: { |
| 1376 | continueUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1377 | break; |
| 1378 | } |
| 1379 | case USER_SWITCH_TIMEOUT_MSG: { |
| 1380 | timeoutUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1381 | break; |
| 1382 | } |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1383 | case IMMERSIVE_MODE_LOCK_MSG: { |
| 1384 | final boolean nextState = (msg.arg1 != 0); |
| 1385 | if (mUpdateLock.isHeld() != nextState) { |
| 1386 | if (DEBUG_IMMERSIVE) { |
| 1387 | final ActivityRecord r = (ActivityRecord) msg.obj; |
| 1388 | Slog.d(TAG, "Applying new update lock state '" + nextState + "' for " + r); |
| 1389 | } |
| 1390 | if (nextState) { |
| 1391 | mUpdateLock.acquire(); |
| 1392 | } else { |
| 1393 | mUpdateLock.release(); |
| 1394 | } |
| 1395 | } |
| 1396 | break; |
| 1397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | }; |
| 1401 | |
| 1402 | public static void setSystemProcess() { |
| 1403 | try { |
| 1404 | ActivityManagerService m = mSelf; |
| 1405 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1406 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1407 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1408 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1409 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | if (MONITOR_CPU_USAGE) { |
| 1411 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1412 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1414 | |
| 1415 | ApplicationInfo info = |
| 1416 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1417 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1418 | mSystemThread.installSystemApplicationInfo(info); |
| 1419 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1420 | synchronized (mSelf) { |
| 1421 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1422 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1423 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1425 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1426 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1427 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | synchronized (mSelf.mPidsSelfLocked) { |
| 1429 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1430 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1431 | mSelf.updateLruProcessLocked(app, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | } |
| 1433 | } catch (PackageManager.NameNotFoundException e) { |
| 1434 | throw new RuntimeException( |
| 1435 | "Unable to find android system package", e); |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | public void setWindowManager(WindowManagerService wm) { |
| 1440 | mWindowManager = wm; |
| 1441 | } |
| 1442 | |
| 1443 | public static final Context main(int factoryTest) { |
| 1444 | AThread thr = new AThread(); |
| 1445 | thr.start(); |
| 1446 | |
| 1447 | synchronized (thr) { |
| 1448 | while (thr.mService == null) { |
| 1449 | try { |
| 1450 | thr.wait(); |
| 1451 | } catch (InterruptedException e) { |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | ActivityManagerService m = thr.mService; |
| 1457 | mSelf = m; |
| 1458 | ActivityThread at = ActivityThread.systemMain(); |
| 1459 | mSystemThread = at; |
| 1460 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1461 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | m.mContext = context; |
| 1463 | m.mFactoryTest = factoryTest; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1464 | m.mMainStack = new ActivityStack(m, context, true, thr.mLooper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | |
| 1466 | m.mBatteryStatsService.publish(context); |
| 1467 | m.mUsageStatsService.publish(context); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1468 | m.mAppOpsService.publish(context); |
| 1469 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | synchronized (thr) { |
| 1471 | thr.mReady = true; |
| 1472 | thr.notifyAll(); |
| 1473 | } |
| 1474 | |
| 1475 | m.startRunning(null, null, null, null); |
| 1476 | |
| 1477 | return context; |
| 1478 | } |
| 1479 | |
| 1480 | public static ActivityManagerService self() { |
| 1481 | return mSelf; |
| 1482 | } |
| 1483 | |
| 1484 | static class AThread extends Thread { |
| 1485 | ActivityManagerService mService; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1486 | Looper mLooper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | boolean mReady = false; |
| 1488 | |
| 1489 | public AThread() { |
| 1490 | super("ActivityManager"); |
| 1491 | } |
| 1492 | |
| 1493 | public void run() { |
| 1494 | Looper.prepare(); |
| 1495 | |
| 1496 | android.os.Process.setThreadPriority( |
| 1497 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1498 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | |
| 1500 | ActivityManagerService m = new ActivityManagerService(); |
| 1501 | |
| 1502 | synchronized (this) { |
| 1503 | mService = m; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1504 | mLooper = Looper.myLooper(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | notifyAll(); |
| 1506 | } |
| 1507 | |
| 1508 | synchronized (this) { |
| 1509 | while (!mReady) { |
| 1510 | try { |
| 1511 | wait(); |
| 1512 | } catch (InterruptedException e) { |
| 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1517 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1518 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1519 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1520 | } |
| 1521 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1522 | Looper.loop(); |
| 1523 | } |
| 1524 | } |
| 1525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | static class MemBinder extends Binder { |
| 1527 | ActivityManagerService mActivityManagerService; |
| 1528 | MemBinder(ActivityManagerService activityManagerService) { |
| 1529 | mActivityManagerService = activityManagerService; |
| 1530 | } |
| 1531 | |
| 1532 | @Override |
| 1533 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1534 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1535 | != PackageManager.PERMISSION_GRANTED) { |
| 1536 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1537 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1538 | + " without permission " + android.Manifest.permission.DUMP); |
| 1539 | return; |
| 1540 | } |
| 1541 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1542 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1543 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1547 | static class GraphicsBinder extends Binder { |
| 1548 | ActivityManagerService mActivityManagerService; |
| 1549 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1550 | mActivityManagerService = activityManagerService; |
| 1551 | } |
| 1552 | |
| 1553 | @Override |
| 1554 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1555 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1556 | != PackageManager.PERMISSION_GRANTED) { |
| 1557 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1558 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1559 | + " without permission " + android.Manifest.permission.DUMP); |
| 1560 | return; |
| 1561 | } |
| 1562 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1563 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1567 | static class DbBinder extends Binder { |
| 1568 | ActivityManagerService mActivityManagerService; |
| 1569 | DbBinder(ActivityManagerService activityManagerService) { |
| 1570 | mActivityManagerService = activityManagerService; |
| 1571 | } |
| 1572 | |
| 1573 | @Override |
| 1574 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1575 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1576 | != PackageManager.PERMISSION_GRANTED) { |
| 1577 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1578 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1579 | + " without permission " + android.Manifest.permission.DUMP); |
| 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1584 | } |
| 1585 | } |
| 1586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | static class CpuBinder extends Binder { |
| 1588 | ActivityManagerService mActivityManagerService; |
| 1589 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1590 | mActivityManagerService = activityManagerService; |
| 1591 | } |
| 1592 | |
| 1593 | @Override |
| 1594 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1595 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1596 | != PackageManager.PERMISSION_GRANTED) { |
| 1597 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1598 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1599 | + " without permission " + android.Manifest.permission.DUMP); |
| 1600 | return; |
| 1601 | } |
| 1602 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1604 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1605 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1606 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1612 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1613 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1614 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1615 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1616 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1617 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 1618 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1619 | mServices = new ActiveServices(this); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1620 | mProviderMap = new ProviderMap(this); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1621 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | File dataDir = Environment.getDataDirectory(); |
| 1623 | File systemDir = new File(dataDir, "system"); |
| 1624 | systemDir.mkdirs(); |
| 1625 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1626 | systemDir, "batterystats.bin").toString()); |
| 1627 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1628 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1629 | mOnBattery = DEBUG_POWER ? true |
| 1630 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1631 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1632 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1633 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1634 | systemDir, "usagestats").toString()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1635 | mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml")); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1636 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1638 | // User 0 is the first and only user that runs at boot. |
| 1639 | mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true)); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1640 | mUserLru.add(Integer.valueOf(0)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1641 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1642 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1643 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1644 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1645 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1646 | mConfiguration.setToDefaults(); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1647 | mConfiguration.setLocale(Locale.getDefault()); |
| 1648 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1649 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | mProcessStats.init(); |
| 1651 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1652 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | // Add ourself to the Watchdog monitors. |
| 1655 | Watchdog.getInstance().addMonitor(this); |
| 1656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1658 | public void run() { |
| 1659 | while (true) { |
| 1660 | try { |
| 1661 | try { |
| 1662 | synchronized(this) { |
| 1663 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1664 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1666 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | // + ", write delay=" + nextWriteDelay); |
| 1668 | if (nextWriteDelay < nextCpuDelay) { |
| 1669 | nextCpuDelay = nextWriteDelay; |
| 1670 | } |
| 1671 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1672 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | this.wait(nextCpuDelay); |
| 1674 | } |
| 1675 | } |
| 1676 | } catch (InterruptedException e) { |
| 1677 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | updateCpuStatsNow(); |
| 1679 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1680 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | }; |
| 1685 | mProcessStatsThread.start(); |
| 1686 | } |
| 1687 | |
| 1688 | @Override |
| 1689 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1690 | throws RemoteException { |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 1691 | if (code == SYSPROPS_TRANSACTION) { |
| 1692 | // We need to tell all apps about the system property change. |
| 1693 | ArrayList<IBinder> procs = new ArrayList<IBinder>(); |
| 1694 | synchronized(this) { |
| 1695 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 1696 | final int NA = apps.size(); |
| 1697 | for (int ia=0; ia<NA; ia++) { |
| 1698 | ProcessRecord app = apps.valueAt(ia); |
| 1699 | if (app.thread != null) { |
| 1700 | procs.add(app.thread.asBinder()); |
| 1701 | } |
| 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | int N = procs.size(); |
| 1707 | for (int i=0; i<N; i++) { |
| 1708 | Parcel data2 = Parcel.obtain(); |
| 1709 | try { |
| 1710 | procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0); |
| 1711 | } catch (RemoteException e) { |
| 1712 | } |
| 1713 | data2.recycle(); |
| 1714 | } |
| 1715 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1716 | try { |
| 1717 | return super.onTransact(code, data, reply, flags); |
| 1718 | } catch (RuntimeException e) { |
| 1719 | // The activity manager only throws security exceptions, so let's |
| 1720 | // log all others. |
| 1721 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1722 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1723 | } |
| 1724 | throw e; |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1729 | final long now = SystemClock.uptimeMillis(); |
| 1730 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1731 | return; |
| 1732 | } |
| 1733 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1734 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1735 | mProcessStatsThread.notify(); |
| 1736 | } |
| 1737 | } |
| 1738 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1739 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | void updateCpuStatsNow() { |
| 1741 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1742 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | final long now = SystemClock.uptimeMillis(); |
| 1744 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1745 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1747 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1748 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1749 | haveNewCpuStats = true; |
| 1750 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1751 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1752 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1753 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1754 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1755 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1756 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1757 | int user = mProcessStats.getLastUserTime(); |
| 1758 | int system = mProcessStats.getLastSystemTime(); |
| 1759 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1760 | int irq = mProcessStats.getLastIrqTime(); |
| 1761 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1762 | int idle = mProcessStats.getLastIdleTime(); |
| 1763 | |
| 1764 | int total = user + system + iowait + irq + softIrq + idle; |
| 1765 | if (total == 0) total = 1; |
| 1766 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1767 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1769 | (user * 100) / total, |
| 1770 | (system * 100) / total, |
| 1771 | (iowait * 100) / total, |
| 1772 | (irq * 100) / total, |
| 1773 | (softIrq * 100) / total); |
| 1774 | } |
| 1775 | } |
| 1776 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1777 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1778 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1779 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | synchronized(mPidsSelfLocked) { |
| 1781 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1782 | if (mOnBattery) { |
| 1783 | int perc = bstats.startAddingCpuLocked(); |
| 1784 | int totalUTime = 0; |
| 1785 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1786 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1787 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1788 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1789 | if (!st.working) { |
| 1790 | continue; |
| 1791 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1793 | int otherUTime = (st.rel_utime*perc)/100; |
| 1794 | int otherSTime = (st.rel_stime*perc)/100; |
| 1795 | totalUTime += otherUTime; |
| 1796 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1797 | if (pr != null) { |
| 1798 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1799 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1800 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1801 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1802 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1803 | } else { |
| 1804 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1805 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1806 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1807 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1808 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1809 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1810 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | } |
| 1812 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1813 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1814 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1818 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1819 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1820 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1821 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1827 | @Override |
| 1828 | public void batteryNeedsCpuUpdate() { |
| 1829 | updateCpuStatsNow(); |
| 1830 | } |
| 1831 | |
| 1832 | @Override |
| 1833 | public void batteryPowerChanged(boolean onBattery) { |
| 1834 | // When plugging in, update the CPU stats first before changing |
| 1835 | // the plug state. |
| 1836 | updateCpuStatsNow(); |
| 1837 | synchronized (this) { |
| 1838 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1839 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1844 | /** |
| 1845 | * Initialize the application bind args. These are passed to each |
| 1846 | * process when the bindApplication() IPC is sent to the process. They're |
| 1847 | * lazily setup to make sure the services are running when they're asked for. |
| 1848 | */ |
| 1849 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1850 | if (mAppBindArgs == null) { |
| 1851 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1852 | |
| 1853 | // Setup the application init args |
| 1854 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1855 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1856 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1857 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1858 | } |
| 1859 | return mAppBindArgs; |
| 1860 | } |
| 1861 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1862 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | if (mFocusedActivity != r) { |
| 1864 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1865 | if (r != null) { |
| 1866 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1867 | } |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1868 | applyUpdateLockStateLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1872 | final void applyUpdateLockStateLocked(ActivityRecord r) { |
| 1873 | // Modifications to the UpdateLock state are done on our handler, outside |
| 1874 | // the activity manager's locks. The new state is determined based on the |
| 1875 | // state *now* of the relevant activity record. The object is passed to |
| 1876 | // the handler solely for logging detail, not to be consulted/modified. |
| 1877 | final boolean nextState = r != null && r.immersive; |
| 1878 | mHandler.sendMessage( |
| 1879 | mHandler.obtainMessage(IMMERSIVE_MODE_LOCK_MSG, (nextState) ? 1 : 0, 0, r)); |
| 1880 | } |
| 1881 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1882 | private final void updateLruProcessInternalLocked(ProcessRecord app, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | // 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] | 1884 | int lrui = mLruProcesses.indexOf(app); |
| 1885 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1886 | |
| 1887 | int i = mLruProcesses.size()-1; |
| 1888 | int skipTop = 0; |
| 1889 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1890 | app.lruSeq = mLruSeq; |
| 1891 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1892 | // compute the new weight for this process. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1893 | app.lastActivityTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1894 | if (app.activities.size() > 0) { |
| 1895 | // If this process has activities, we more strongly want to keep |
| 1896 | // it around. |
| 1897 | app.lruWeight = app.lastActivityTime; |
| 1898 | } else if (app.pubProviders.size() > 0) { |
| 1899 | // If this process contains content providers, we want to keep |
| 1900 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1901 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1902 | // 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] | 1903 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1904 | } else { |
| 1905 | // If this process doesn't have activities, we less strongly |
| 1906 | // want to keep it around, and generally want to avoid getting |
| 1907 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1908 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1909 | // 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] | 1910 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1911 | } |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1912 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1913 | while (i >= 0) { |
| 1914 | ProcessRecord p = mLruProcesses.get(i); |
| 1915 | // If this app shouldn't be in front of the first N background |
| 1916 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1917 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1918 | skipTop--; |
| 1919 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1920 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1921 | mLruProcesses.add(i+1, app); |
| 1922 | break; |
| 1923 | } |
| 1924 | i--; |
| 1925 | } |
| 1926 | if (i < 0) { |
| 1927 | mLruProcesses.add(0, app); |
| 1928 | } |
| 1929 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1930 | // If the app is currently using a content provider or service, |
| 1931 | // bump those processes as well. |
| 1932 | if (app.connections.size() > 0) { |
| 1933 | for (ConnectionRecord cr : app.connections) { |
| 1934 | if (cr.binding != null && cr.binding.service != null |
| 1935 | && cr.binding.service.app != null |
| 1936 | && cr.binding.service.app.lruSeq != mLruSeq) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1937 | updateLruProcessInternalLocked(cr.binding.service.app, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1941 | for (int j=app.conProviders.size()-1; j>=0; j--) { |
| 1942 | ContentProviderRecord cpr = app.conProviders.get(j).provider; |
| 1943 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1944 | updateLruProcessInternalLocked(cpr.proc, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1945 | } |
| 1946 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1947 | } |
| 1948 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1949 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1950 | boolean oomAdj) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1951 | mLruSeq++; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1952 | updateLruProcessInternalLocked(app, 0); |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1953 | |
| 1954 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
| 1955 | if (oomAdj) { |
| 1956 | updateOomAdjLocked(); |
| 1957 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1958 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1959 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1960 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1961 | String processName, int uid) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1962 | if (uid == Process.SYSTEM_UID) { |
Amith Yamasani | 0184ce9 | 2012-03-28 22:41:41 -0700 | [diff] [blame] | 1963 | // The system gets to run in any process. If there are multiple |
| 1964 | // processes with the same uid, just pick the first (this |
| 1965 | // should never happen). |
| 1966 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1967 | processName); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1968 | if (procs == null) return null; |
| 1969 | final int N = procs.size(); |
| 1970 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1971 | if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1972 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1973 | } |
| 1974 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1975 | return proc; |
| 1976 | } |
| 1977 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1978 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1979 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1980 | try { |
| 1981 | if (pm.performDexOpt(packageName)) { |
| 1982 | mDidDexOpt = true; |
| 1983 | } |
| 1984 | } catch (RemoteException e) { |
| 1985 | } |
| 1986 | } |
| 1987 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1988 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1989 | int transit = mWindowManager.getPendingAppTransition(); |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 1990 | return transit == AppTransition.TRANSIT_ACTIVITY_OPEN |
| 1991 | || transit == AppTransition.TRANSIT_TASK_OPEN |
| 1992 | || transit == AppTransition.TRANSIT_TASK_TO_FRONT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1993 | } |
| 1994 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1995 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1997 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 1998 | boolean isolated) { |
| 1999 | ProcessRecord app; |
| 2000 | if (!isolated) { |
| 2001 | app = getProcessRecordLocked(processName, info.uid); |
| 2002 | } else { |
| 2003 | // If this is an isolated process, it can't re-use an existing process. |
| 2004 | app = null; |
| 2005 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2006 | // We don't have to do anything more if: |
| 2007 | // (1) There is an existing application record; and |
| 2008 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 2009 | // object attached to it so we know it couldn't have crashed; and |
| 2010 | // (3) There is a pid assigned to it, so it is either starting or |
| 2011 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2012 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2013 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 2014 | + " thread=" + (app != null ? app.thread : null) |
| 2015 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2016 | if (app != null && app.pid > 0) { |
| 2017 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 2018 | // We already have the app running, or are waiting for it to |
| 2019 | // 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] | 2020 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 2021 | // If this is a new package in the process, add the package to the list |
| 2022 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2023 | return app; |
| 2024 | } else { |
| 2025 | // An application record is attached to a previous process, |
| 2026 | // clean it up now. |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 2027 | if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2028 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2029 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2030 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2031 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2032 | String hostingNameStr = hostingName != null |
| 2033 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2034 | |
| 2035 | if (!isolated) { |
| 2036 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 2037 | // If we are in the background, then check to see if this process |
| 2038 | // is bad. If so, we will just silently fail. |
| 2039 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 2040 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 2041 | + "/" + info.processName); |
| 2042 | return null; |
| 2043 | } |
| 2044 | } else { |
| 2045 | // When the user is explicitly starting a process, then clear its |
| 2046 | // crash count so that we won't make it bad until they see at |
| 2047 | // least one crash dialog again, and make the process good again |
| 2048 | // if it had been bad. |
| 2049 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2050 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2051 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 2052 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2053 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, |
| 2054 | UserHandle.getUserId(info.uid), info.uid, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2055 | info.processName); |
| 2056 | mBadProcesses.remove(info.processName, info.uid); |
| 2057 | if (app != null) { |
| 2058 | app.bad = false; |
| 2059 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | } |
| 2061 | } |
| 2062 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2063 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2064 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2065 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 2066 | if (app == null) { |
| 2067 | Slog.w(TAG, "Failed making new process record for " |
| 2068 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 2069 | return null; |
| 2070 | } |
| 2071 | mProcessNames.put(processName, app.uid, app); |
| 2072 | if (isolated) { |
| 2073 | mIsolatedProcesses.put(app.uid, app); |
| 2074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | } else { |
| 2076 | // If this is a new package in the process, add the package to the list |
| 2077 | app.addPackage(info.packageName); |
| 2078 | } |
| 2079 | |
| 2080 | // If the system is not ready yet, then hold off on starting this |
| 2081 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2082 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2083 | && !isAllowedWhileBooting(info) |
| 2084 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2085 | if (!mProcessesOnHold.contains(app)) { |
| 2086 | mProcessesOnHold.add(app); |
| 2087 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2088 | 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] | 2089 | return app; |
| 2090 | } |
| 2091 | |
| 2092 | startProcessLocked(app, hostingType, hostingNameStr); |
| 2093 | return (app.pid != 0) ? app : null; |
| 2094 | } |
| 2095 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2096 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 2097 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 2098 | } |
| 2099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2100 | private final void startProcessLocked(ProcessRecord app, |
| 2101 | String hostingType, String hostingNameStr) { |
| 2102 | if (app.pid > 0 && app.pid != MY_PID) { |
| 2103 | synchronized (mPidsSelfLocked) { |
| 2104 | mPidsSelfLocked.remove(app.pid); |
| 2105 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 2106 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2107 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2108 | } |
| 2109 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2110 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 2111 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2112 | mProcessesOnHold.remove(app); |
| 2113 | |
| 2114 | updateCpuStats(); |
| 2115 | |
| 2116 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 2117 | mProcDeaths[0] = 0; |
| 2118 | |
| 2119 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2120 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2122 | int[] gids = null; |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2123 | int mountExternal = Zygote.MOUNT_EXTERNAL_NONE; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2124 | if (!app.isolated) { |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2125 | int[] permGids = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2126 | try { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2127 | final PackageManager pm = mContext.getPackageManager(); |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2128 | permGids = pm.getPackageGids(app.info.packageName); |
Jeff Sharkey | e217ee4 | 2012-08-28 16:23:01 -0700 | [diff] [blame] | 2129 | |
| 2130 | if (Environment.isExternalStorageEmulated()) { |
| 2131 | if (pm.checkPermission( |
| 2132 | android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE, |
| 2133 | app.info.packageName) == PERMISSION_GRANTED) { |
| 2134 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL; |
| 2135 | } else { |
| 2136 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER; |
| 2137 | } |
| 2138 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2139 | } catch (PackageManager.NameNotFoundException e) { |
| 2140 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 2141 | } |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2142 | |
| 2143 | /* |
| 2144 | * Add shared application GID so applications can share some |
| 2145 | * resources like shared libraries |
| 2146 | */ |
| 2147 | if (permGids == null) { |
| 2148 | gids = new int[1]; |
| 2149 | } else { |
| 2150 | gids = new int[permGids.length + 1]; |
| 2151 | System.arraycopy(permGids, 0, gids, 1, permGids.length); |
| 2152 | } |
| 2153 | gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2154 | } |
| 2155 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 2156 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2157 | && mTopComponent != null |
| 2158 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 2159 | uid = 0; |
| 2160 | } |
| 2161 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 2162 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 2163 | uid = 0; |
| 2164 | } |
| 2165 | } |
| 2166 | int debugFlags = 0; |
| 2167 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 2168 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 2169 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 2170 | // awkward to turn on otherwise. |
| 2171 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2172 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 2173 | // Run the app in safe mode if its manifest requests so or the |
| 2174 | // system is booted in safe mode. |
| 2175 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 2176 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 2177 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 2178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2179 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 2180 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 2181 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 2182 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 2183 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 2184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2185 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 2186 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 2187 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2188 | |
| 2189 | // Start the process. It will either succeed and return a result containing |
| 2190 | // the PID of the new process, or else throw a RuntimeException. |
| 2191 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2192 | app.processName, uid, uid, gids, debugFlags, mountExternal, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 2193 | app.info.targetSdkVersion, null, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2195 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2196 | synchronized (bs) { |
| 2197 | if (bs.isOnBattery()) { |
| 2198 | app.batteryStats.incStartsLocked(); |
| 2199 | } |
| 2200 | } |
| 2201 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2202 | EventLog.writeEvent(EventLogTags.AM_PROC_START, |
| 2203 | UserHandle.getUserId(uid), startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2204 | app.processName, hostingType, |
| 2205 | hostingNameStr != null ? hostingNameStr : ""); |
| 2206 | |
| 2207 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2208 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2209 | } |
| 2210 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2211 | StringBuilder buf = mStringBuilder; |
| 2212 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2213 | buf.append("Start proc "); |
| 2214 | buf.append(app.processName); |
| 2215 | buf.append(" for "); |
| 2216 | buf.append(hostingType); |
| 2217 | if (hostingNameStr != null) { |
| 2218 | buf.append(" "); |
| 2219 | buf.append(hostingNameStr); |
| 2220 | } |
| 2221 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2222 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2223 | buf.append(" uid="); |
| 2224 | buf.append(uid); |
| 2225 | buf.append(" gids={"); |
| 2226 | if (gids != null) { |
| 2227 | for (int gi=0; gi<gids.length; gi++) { |
| 2228 | if (gi != 0) buf.append(", "); |
| 2229 | buf.append(gids[gi]); |
| 2230 | |
| 2231 | } |
| 2232 | } |
| 2233 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2234 | Slog.i(TAG, buf.toString()); |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2235 | app.setPid(startResult.pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2236 | app.usingWrapper = startResult.usingWrapper; |
| 2237 | app.removed = false; |
| 2238 | synchronized (mPidsSelfLocked) { |
| 2239 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2240 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2241 | msg.obj = app; |
| 2242 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2243 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2244 | } |
| 2245 | } catch (RuntimeException e) { |
| 2246 | // XXX do better error recovery. |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2247 | app.setPid(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2248 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2252 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2253 | if (resumed) { |
| 2254 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2255 | } else { |
| 2256 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2257 | } |
| 2258 | } |
| 2259 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2260 | boolean startHomeActivityLocked(int userId) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2261 | if (mHeadless) { |
| 2262 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2263 | // when running headless. |
| 2264 | ensureBootCompleted(); |
| 2265 | return false; |
| 2266 | } |
| 2267 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2268 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2269 | && mTopAction == null) { |
| 2270 | // We are running in factory test mode, but unable to find |
| 2271 | // the factory test app, so just sit around displaying the |
| 2272 | // error message and don't try to start anything. |
| 2273 | return false; |
| 2274 | } |
| 2275 | Intent intent = new Intent( |
| 2276 | mTopAction, |
| 2277 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2278 | intent.setComponent(mTopComponent); |
| 2279 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2280 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2281 | } |
| 2282 | ActivityInfo aInfo = |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2283 | resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2284 | if (aInfo != null) { |
| 2285 | intent.setComponent(new ComponentName( |
| 2286 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2287 | // Don't do this if the home app is currently being |
| 2288 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2289 | aInfo = new ActivityInfo(aInfo); |
| 2290 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2291 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2292 | aInfo.applicationInfo.uid); |
| 2293 | if (app == null || app.instrumentationClass == null) { |
| 2294 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2295 | mMainStack.startActivityLocked(null, intent, null, aInfo, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2296 | null, null, 0, 0, 0, null, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2297 | } |
| 2298 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2299 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2300 | return true; |
| 2301 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2302 | |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2303 | private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) { |
| 2304 | ActivityInfo ai = null; |
| 2305 | ComponentName comp = intent.getComponent(); |
| 2306 | try { |
| 2307 | if (comp != null) { |
| 2308 | ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId); |
| 2309 | } else { |
| 2310 | ResolveInfo info = AppGlobals.getPackageManager().resolveIntent( |
| 2311 | intent, |
| 2312 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
| 2313 | flags, userId); |
| 2314 | |
| 2315 | if (info != null) { |
| 2316 | ai = info.activityInfo; |
| 2317 | } |
| 2318 | } |
| 2319 | } catch (RemoteException e) { |
| 2320 | // ignore |
| 2321 | } |
| 2322 | |
| 2323 | return ai; |
| 2324 | } |
| 2325 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2326 | /** |
| 2327 | * Starts the "new version setup screen" if appropriate. |
| 2328 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2329 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2330 | // Only do this once per boot. |
| 2331 | if (mCheckedForSetup) { |
| 2332 | return; |
| 2333 | } |
| 2334 | |
| 2335 | // We will show this screen if the current one is a different |
| 2336 | // version than the last one shown, and we are not running in |
| 2337 | // low-level factory test mode. |
| 2338 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2339 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2340 | Settings.Global.getInt(resolver, |
| 2341 | Settings.Global.DEVICE_PROVISIONED, 0) != 0) { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2342 | mCheckedForSetup = true; |
| 2343 | |
| 2344 | // See if we should be showing the platform update setup UI. |
| 2345 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2346 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2347 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2348 | |
| 2349 | // We don't allow third party apps to replace this. |
| 2350 | ResolveInfo ri = null; |
| 2351 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2352 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2353 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2354 | ri = ris.get(i); |
| 2355 | break; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | if (ri != null) { |
| 2360 | String vers = ri.activityInfo.metaData != null |
| 2361 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2362 | : null; |
| 2363 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2364 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2365 | Intent.METADATA_SETUP_VERSION); |
| 2366 | } |
| 2367 | String lastVers = Settings.Secure.getString( |
| 2368 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2369 | if (vers != null && !vers.equals(lastVers)) { |
| 2370 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2371 | intent.setComponent(new ComponentName( |
| 2372 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2373 | mMainStack.startActivityLocked(null, intent, null, ri.activityInfo, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2374 | null, null, 0, 0, 0, null, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | } |
| 2378 | } |
| 2379 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2380 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2381 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2382 | } |
| 2383 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2384 | void enforceNotIsolatedCaller(String caller) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2385 | if (UserHandle.isIsolated(Binder.getCallingUid())) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2386 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2387 | } |
| 2388 | } |
| 2389 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2390 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2391 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2392 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2393 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2394 | } |
| 2395 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2396 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2397 | public void setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2398 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2399 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2400 | synchronized (this) { |
| 2401 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2402 | } |
| 2403 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2404 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2405 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2406 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2407 | synchronized (this) { |
| 2408 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2409 | } |
| 2410 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2411 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2412 | public void setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2413 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2414 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2415 | synchronized (this) { |
| 2416 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2420 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2421 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2422 | synchronized (this) { |
| 2423 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2428 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2429 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2430 | synchronized (this) { |
| 2431 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2432 | } |
| 2433 | } |
| 2434 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2435 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2436 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2437 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2439 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2440 | private void dispatchProcessesChanged() { |
| 2441 | int N; |
| 2442 | synchronized (this) { |
| 2443 | N = mPendingProcessChanges.size(); |
| 2444 | if (mActiveProcessChanges.length < N) { |
| 2445 | mActiveProcessChanges = new ProcessChangeItem[N]; |
| 2446 | } |
| 2447 | mPendingProcessChanges.toArray(mActiveProcessChanges); |
| 2448 | mAvailProcessChanges.addAll(mPendingProcessChanges); |
| 2449 | mPendingProcessChanges.clear(); |
| 2450 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes"); |
| 2451 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2452 | int i = mProcessObservers.beginBroadcast(); |
| 2453 | while (i > 0) { |
| 2454 | i--; |
| 2455 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2456 | if (observer != null) { |
| 2457 | try { |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2458 | for (int j=0; j<N; j++) { |
| 2459 | ProcessChangeItem item = mActiveProcessChanges[j]; |
| 2460 | if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) { |
| 2461 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid=" |
| 2462 | + item.pid + " uid=" + item.uid + ": " |
| 2463 | + item.foregroundActivities); |
| 2464 | observer.onForegroundActivitiesChanged(item.pid, item.uid, |
| 2465 | item.foregroundActivities); |
| 2466 | } |
| 2467 | if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) { |
| 2468 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid=" |
| 2469 | + item.pid + " uid=" + item.uid + ": " + item.importance); |
| 2470 | observer.onImportanceChanged(item.pid, item.uid, |
| 2471 | item.importance); |
| 2472 | } |
| 2473 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2474 | } catch (RemoteException e) { |
| 2475 | } |
| 2476 | } |
| 2477 | } |
| 2478 | mProcessObservers.finishBroadcast(); |
| 2479 | } |
| 2480 | |
| 2481 | private void dispatchProcessDied(int pid, int uid) { |
| 2482 | int i = mProcessObservers.beginBroadcast(); |
| 2483 | while (i > 0) { |
| 2484 | i--; |
| 2485 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2486 | if (observer != null) { |
| 2487 | try { |
| 2488 | observer.onProcessDied(pid, uid); |
| 2489 | } catch (RemoteException e) { |
| 2490 | } |
| 2491 | } |
| 2492 | } |
| 2493 | mProcessObservers.finishBroadcast(); |
| 2494 | } |
| 2495 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2496 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2497 | final int N = mPendingActivityLaunches.size(); |
| 2498 | if (N <= 0) { |
| 2499 | return; |
| 2500 | } |
| 2501 | for (int i=0; i<N; i++) { |
| 2502 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2503 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2504 | pal.startFlags, doResume && i == (N-1), null); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2505 | } |
| 2506 | mPendingActivityLaunches.clear(); |
| 2507 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2508 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2509 | public final int startActivity(IApplicationThread caller, String callingPackage, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2510 | Intent intent, String resolvedType, IBinder resultTo, |
| 2511 | String resultWho, int requestCode, int startFlags, |
| 2512 | String profileFile, ParcelFileDescriptor profileFd, Bundle options) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2513 | return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo, |
| 2514 | resultWho, requestCode, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2515 | startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId()); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2518 | public final int startActivityAsUser(IApplicationThread caller, String callingPackage, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2519 | Intent intent, String resolvedType, IBinder resultTo, |
| 2520 | String resultWho, int requestCode, int startFlags, |
| 2521 | String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2522 | enforceNotIsolatedCaller("startActivity"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2523 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | d888399 | 2012-09-07 15:58:52 -0700 | [diff] [blame] | 2524 | false, true, "startActivity", null); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2525 | return mMainStack.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2526 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2527 | null, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2528 | } |
| 2529 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2530 | public final WaitResult startActivityAndWait(IApplicationThread caller, String callingPackage, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2531 | Intent intent, String resolvedType, IBinder resultTo, |
| 2532 | String resultWho, int requestCode, int startFlags, String profileFile, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2533 | ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2534 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2535 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2536 | false, true, "startActivityAndWait", null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2537 | WaitResult res = new WaitResult(); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2538 | mMainStack.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2539 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2540 | res, null, options, UserHandle.getCallingUserId()); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2541 | return res; |
| 2542 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 2543 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2544 | public final int startActivityWithConfig(IApplicationThread caller, String callingPackage, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2545 | Intent intent, String resolvedType, IBinder resultTo, |
| 2546 | String resultWho, int requestCode, int startFlags, Configuration config, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2547 | Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2548 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2549 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2550 | false, true, "startActivityWithConfig", null); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2551 | int ret = mMainStack.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2552 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2553 | null, null, null, config, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2554 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2557 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2558 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2559 | IBinder resultTo, String resultWho, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2560 | int flagsMask, int flagsValues, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2561 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2562 | // Refuse possible leaked file descriptors |
| 2563 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2564 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2565 | } |
| 2566 | |
| 2567 | IIntentSender sender = intent.getTarget(); |
| 2568 | if (!(sender instanceof PendingIntentRecord)) { |
| 2569 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2570 | } |
| 2571 | |
| 2572 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2573 | |
| 2574 | synchronized (this) { |
| 2575 | // If this is coming from the currently resumed activity, it is |
| 2576 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2577 | if (mMainStack.mResumedActivity != null |
| 2578 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2579 | Binder.getCallingUid()) { |
| 2580 | mAppSwitchesAllowedTime = 0; |
| 2581 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2582 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2583 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null, |
| 2584 | resultTo, resultWho, requestCode, flagsMask, flagsValues, options); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2585 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2588 | public boolean startNextMatchingActivity(IBinder callingActivity, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2589 | Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2590 | // Refuse possible leaked file descriptors |
| 2591 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2592 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2593 | } |
| 2594 | |
| 2595 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2596 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2597 | if (r == null) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2598 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2599 | return false; |
| 2600 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2601 | if (r.app == null || r.app.thread == null) { |
| 2602 | // The caller is not running... d'oh! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2603 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | return false; |
| 2605 | } |
| 2606 | intent = new Intent(intent); |
| 2607 | // The caller is not allowed to change the data. |
| 2608 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2609 | // And we are resetting to find the next component... |
| 2610 | intent.setComponent(null); |
| 2611 | |
| 2612 | ActivityInfo aInfo = null; |
| 2613 | try { |
| 2614 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2615 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2616 | intent, r.resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2617 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2618 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2619 | |
| 2620 | // Look for the original activity in the list... |
| 2621 | final int N = resolves != null ? resolves.size() : 0; |
| 2622 | for (int i=0; i<N; i++) { |
| 2623 | ResolveInfo rInfo = resolves.get(i); |
| 2624 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2625 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2626 | // We found the current one... the next matching is |
| 2627 | // after it. |
| 2628 | i++; |
| 2629 | if (i<N) { |
| 2630 | aInfo = resolves.get(i).activityInfo; |
| 2631 | } |
| 2632 | break; |
| 2633 | } |
| 2634 | } |
| 2635 | } catch (RemoteException e) { |
| 2636 | } |
| 2637 | |
| 2638 | if (aInfo == null) { |
| 2639 | // Nobody who is next! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2640 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2641 | return false; |
| 2642 | } |
| 2643 | |
| 2644 | intent.setComponent(new ComponentName( |
| 2645 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2646 | intent.setFlags(intent.getFlags()&~( |
| 2647 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2648 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2649 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2650 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2651 | |
| 2652 | // Okay now we need to start the new activity, replacing the |
| 2653 | // currently running activity. This is a little tricky because |
| 2654 | // we want to start the new one as if the current one is finished, |
| 2655 | // but not finish the current one first so that there is no flicker. |
| 2656 | // And thus... |
| 2657 | final boolean wasFinishing = r.finishing; |
| 2658 | r.finishing = true; |
| 2659 | |
| 2660 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2661 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2662 | final String resultWho = r.resultWho; |
| 2663 | final int requestCode = r.requestCode; |
| 2664 | r.resultTo = null; |
| 2665 | if (resultTo != null) { |
| 2666 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2667 | } |
| 2668 | |
| 2669 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2670 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2671 | r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2672 | resultWho, requestCode, -1, r.launchedFromUid, r.launchedFromPackage, 0, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2673 | options, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | Binder.restoreCallingIdentity(origId); |
| 2675 | |
| 2676 | r.finishing = wasFinishing; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2677 | if (res != ActivityManager.START_SUCCESS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2678 | return false; |
| 2679 | } |
| 2680 | return true; |
| 2681 | } |
| 2682 | } |
| 2683 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2684 | final int startActivityInPackage(int uid, String callingPackage, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2685 | Intent intent, String resolvedType, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2686 | String resultWho, int requestCode, int startFlags, Bundle options, int userId) { |
| 2687 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2688 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2689 | false, true, "startActivityInPackage", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2690 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2691 | int ret = mMainStack.startActivityMayWait(null, uid, callingPackage, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2692 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2693 | null, null, null, null, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2694 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2695 | } |
| 2696 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2697 | public final int startActivities(IApplicationThread caller, String callingPackage, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2698 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options, |
| 2699 | int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2700 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 16979ea | 2012-09-24 18:14:47 -0700 | [diff] [blame] | 2701 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2702 | false, true, "startActivity", null); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2703 | int ret = mMainStack.startActivities(caller, -1, callingPackage, intents, |
| 2704 | resolvedTypes, resultTo, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2705 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2706 | } |
| 2707 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2708 | final int startActivitiesInPackage(int uid, String callingPackage, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2709 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2710 | Bundle options, int userId) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2711 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2712 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2713 | false, true, "startActivityInPackage", null); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2714 | int ret = mMainStack.startActivities(null, uid, callingPackage, intents, resolvedTypes, |
| 2715 | resultTo, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2716 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2717 | } |
| 2718 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2719 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2721 | // Quick case: check if the top-most recent task is the same. |
| 2722 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2723 | return; |
| 2724 | } |
| 2725 | // 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] | 2726 | for (int i=0; i<N; i++) { |
| 2727 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2728 | if (task.userId == tr.userId |
| 2729 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2730 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | mRecentTasks.remove(i); |
| 2732 | i--; |
| 2733 | N--; |
| 2734 | if (task.intent == null) { |
| 2735 | // If the new recent task we are adding is not fully |
| 2736 | // specified, then replace it with the existing recent task. |
| 2737 | task = tr; |
| 2738 | } |
| 2739 | } |
| 2740 | } |
| 2741 | if (N >= MAX_RECENT_TASKS) { |
| 2742 | mRecentTasks.remove(N-1); |
| 2743 | } |
| 2744 | mRecentTasks.add(0, task); |
| 2745 | } |
| 2746 | |
| 2747 | public void setRequestedOrientation(IBinder token, |
| 2748 | int requestedOrientation) { |
| 2749 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2750 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2751 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2752 | return; |
| 2753 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2754 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2755 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2756 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2757 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2758 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2759 | if (config != null) { |
| 2760 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2761 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2762 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | Binder.restoreCallingIdentity(origId); |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | public int getRequestedOrientation(IBinder token) { |
| 2770 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2771 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2772 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2773 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2774 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2775 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2779 | /** |
| 2780 | * This is the internal entry point for handling Activity.finish(). |
| 2781 | * |
| 2782 | * @param token The Binder token referencing the Activity we want to finish. |
| 2783 | * @param resultCode Result code, if any, from this Activity. |
| 2784 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2785 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2786 | * @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] | 2787 | */ |
| 2788 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2789 | // Refuse possible leaked file descriptors |
| 2790 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2791 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2792 | } |
| 2793 | |
| 2794 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2795 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2796 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2797 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2798 | if (next != null) { |
| 2799 | // ask watcher if this is allowed |
| 2800 | boolean resumeOK = true; |
| 2801 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2802 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2803 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2804 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | if (!resumeOK) { |
| 2808 | return false; |
| 2809 | } |
| 2810 | } |
| 2811 | } |
| 2812 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2813 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2814 | resultData, "app-request", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2815 | Binder.restoreCallingIdentity(origId); |
| 2816 | return res; |
| 2817 | } |
| 2818 | } |
| 2819 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2820 | public final void finishHeavyWeightApp() { |
| 2821 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2822 | != PackageManager.PERMISSION_GRANTED) { |
| 2823 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2824 | + Binder.getCallingPid() |
| 2825 | + ", uid=" + Binder.getCallingUid() |
| 2826 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2827 | Slog.w(TAG, msg); |
| 2828 | throw new SecurityException(msg); |
| 2829 | } |
| 2830 | |
| 2831 | synchronized(this) { |
| 2832 | if (mHeavyWeightProcess == null) { |
| 2833 | return; |
| 2834 | } |
| 2835 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2836 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2837 | mHeavyWeightProcess.activities); |
| 2838 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2839 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2840 | if (!r.finishing) { |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 2841 | mMainStack.finishActivityLocked(r, Activity.RESULT_CANCELED, |
| 2842 | null, "finish-heavy", true); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2846 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 2847 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2848 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2852 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2853 | String message) { |
| 2854 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2855 | != PackageManager.PERMISSION_GRANTED) { |
| 2856 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2857 | + Binder.getCallingPid() |
| 2858 | + ", uid=" + Binder.getCallingUid() |
| 2859 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2860 | Slog.w(TAG, msg); |
| 2861 | throw new SecurityException(msg); |
| 2862 | } |
| 2863 | |
| 2864 | synchronized(this) { |
| 2865 | ProcessRecord proc = null; |
| 2866 | |
| 2867 | // Figure out which process to kill. We don't trust that initialPid |
| 2868 | // still has any relation to current pids, so must scan through the |
| 2869 | // list. |
| 2870 | synchronized (mPidsSelfLocked) { |
| 2871 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2872 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2873 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2874 | continue; |
| 2875 | } |
| 2876 | if (p.pid == initialPid) { |
| 2877 | proc = p; |
| 2878 | break; |
| 2879 | } |
| 2880 | for (String str : p.pkgList) { |
| 2881 | if (str.equals(packageName)) { |
| 2882 | proc = p; |
| 2883 | } |
| 2884 | } |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2889 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2890 | + " initialPid=" + initialPid |
| 2891 | + " packageName=" + packageName); |
| 2892 | return; |
| 2893 | } |
| 2894 | |
| 2895 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2896 | if (proc.pid == Process.myPid()) { |
| 2897 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2898 | return; |
| 2899 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2900 | long ident = Binder.clearCallingIdentity(); |
| 2901 | try { |
| 2902 | proc.thread.scheduleCrash(message); |
| 2903 | } catch (RemoteException e) { |
| 2904 | } |
| 2905 | Binder.restoreCallingIdentity(ident); |
| 2906 | } |
| 2907 | } |
| 2908 | } |
| 2909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2910 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2911 | int requestCode) { |
| 2912 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2913 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2914 | mMainStack.finishSubActivityLocked(token, resultWho, requestCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2915 | Binder.restoreCallingIdentity(origId); |
| 2916 | } |
| 2917 | } |
| 2918 | |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2919 | public boolean finishActivityAffinity(IBinder token) { |
| 2920 | synchronized(this) { |
| 2921 | final long origId = Binder.clearCallingIdentity(); |
| 2922 | boolean res = mMainStack.finishActivityAffinityLocked(token); |
| 2923 | Binder.restoreCallingIdentity(origId); |
| 2924 | return res; |
| 2925 | } |
| 2926 | } |
| 2927 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 2928 | @Override |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2929 | public boolean willActivityBeVisible(IBinder token) { |
| 2930 | synchronized(this) { |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 2931 | return mMainStack.willActivityBeVisibleLocked(token); |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2932 | } |
| 2933 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 2934 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2935 | public void overridePendingTransition(IBinder token, String packageName, |
| 2936 | int enterAnim, int exitAnim) { |
| 2937 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2938 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2939 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2940 | return; |
| 2941 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2942 | |
| 2943 | final long origId = Binder.clearCallingIdentity(); |
| 2944 | |
| 2945 | if (self.state == ActivityState.RESUMED |
| 2946 | || self.state == ActivityState.PAUSING) { |
| 2947 | mWindowManager.overridePendingAppTransition(packageName, |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2948 | enterAnim, exitAnim, null); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | Binder.restoreCallingIdentity(origId); |
| 2952 | } |
| 2953 | } |
| 2954 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2955 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2956 | * Main function for removing an existing process from the activity manager |
| 2957 | * as a result of that process going away. Clears out all connections |
| 2958 | * to the process. |
| 2959 | */ |
| 2960 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2961 | boolean restarting, boolean allowRestart) { |
| 2962 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2963 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2964 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2965 | } |
| 2966 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2967 | if (mProfileProc == app) { |
| 2968 | clearProfilerLocked(); |
| 2969 | } |
| 2970 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2971 | // Just in case... |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2972 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 2973 | if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG, |
| 2974 | "App died while pausing: " + mMainStack.mPausingActivity); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2975 | mMainStack.mPausingActivity = null; |
| 2976 | } |
| 2977 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2978 | mMainStack.mLastPausedActivity = null; |
| 2979 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2980 | |
| 2981 | // Remove this application's activities from active lists. |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 2982 | boolean hasVisibleActivities = mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2983 | |
| 2984 | app.activities.clear(); |
| 2985 | |
| 2986 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2987 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2988 | + " running instrumentation " + app.instrumentationClass); |
| 2989 | Bundle info = new Bundle(); |
| 2990 | info.putString("shortMsg", "Process crashed."); |
| 2991 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2992 | } |
| 2993 | |
| 2994 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2995 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2996 | // If there was nothing to resume, and we are not already |
| 2997 | // restarting this process, but there is a visible activity that |
| 2998 | // is hosted by the process... then make sure all visible |
| 2999 | // activities are running, taking care of restarting this |
| 3000 | // process. |
| 3001 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3002 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3003 | } |
| 3004 | } |
| 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 3009 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3010 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3011 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3012 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3013 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 3014 | return i; |
| 3015 | } |
| 3016 | } |
| 3017 | return -1; |
| 3018 | } |
| 3019 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3020 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3021 | IApplicationThread thread) { |
| 3022 | if (thread == null) { |
| 3023 | return null; |
| 3024 | } |
| 3025 | |
| 3026 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3027 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3028 | } |
| 3029 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3030 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3031 | IApplicationThread thread) { |
| 3032 | |
| 3033 | mProcDeaths[0]++; |
| 3034 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3035 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 3036 | synchronized (stats) { |
| 3037 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 3038 | } |
| 3039 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3040 | // Clean up already done if the process has been re-started. |
| 3041 | if (app.pid == pid && app.thread != null && |
| 3042 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 3043 | if (!app.killedBackground) { |
| 3044 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 3045 | + ") has died."); |
| 3046 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3047 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3048 | if (DEBUG_CLEANUP) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3049 | TAG, "Dying app: " + app + ", pid: " + pid |
| 3050 | + ", thread: " + thread.asBinder()); |
| 3051 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3052 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3053 | |
| 3054 | if (doLowMem) { |
| 3055 | // If there are no longer any background processes running, |
| 3056 | // and the app that died was not running instrumentation, |
| 3057 | // then tell everyone we are now low on memory. |
| 3058 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3059 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3060 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3061 | 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] | 3062 | haveBg = true; |
| 3063 | break; |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3068 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3069 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3070 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3071 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 3072 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3073 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 3074 | // The low memory report is overriding any current |
| 3075 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 3076 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3077 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3078 | rec.lastRequestedGc = 0; |
| 3079 | } else { |
| 3080 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3081 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3082 | rec.reportLowMemory = true; |
| 3083 | rec.lastLowMemory = now; |
| 3084 | mProcessesToGc.remove(rec); |
| 3085 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3086 | } |
| 3087 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 3088 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3089 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3090 | } |
| 3091 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3092 | } else if (app.pid != pid) { |
| 3093 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3094 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3095 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3096 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3097 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3098 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3099 | + thread.asBinder()); |
| 3100 | } |
| 3101 | } |
| 3102 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3103 | /** |
| 3104 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3105 | * @param clearTraces causes the dump file to be erased prior to the new |
| 3106 | * traces being written, if true; when false, the new traces will be |
| 3107 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3108 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 3109 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3110 | * @param nativeProcs optional list of native process names to dump stack crawls |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3111 | * @return file containing stack traces, or null if no dump file is configured |
| 3112 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3113 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3114 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3115 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3116 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3117 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3118 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3119 | |
| 3120 | File tracesFile = new File(tracesPath); |
| 3121 | try { |
| 3122 | File tracesDir = tracesFile.getParentFile(); |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3123 | if (!tracesDir.exists()) { |
| 3124 | tracesFile.mkdirs(); |
| 3125 | if (!SELinux.restorecon(tracesDir)) { |
| 3126 | return null; |
| 3127 | } |
| 3128 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3129 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3130 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3131 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3132 | tracesFile.createNewFile(); |
| 3133 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3134 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3135 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3136 | return null; |
| 3137 | } |
| 3138 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3139 | dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3140 | return tracesFile; |
| 3141 | } |
| 3142 | |
| 3143 | private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3144 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3145 | // Use a FileObserver to detect when traces finish writing. |
| 3146 | // The order of traces is considered important to maintain for legibility. |
| 3147 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 3148 | public synchronized void onEvent(int event, String path) { notify(); } |
| 3149 | }; |
| 3150 | |
| 3151 | try { |
| 3152 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3153 | |
| 3154 | // First collect all of the stacks of the most important pids. |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3155 | if (firstPids != null) { |
| 3156 | try { |
| 3157 | int num = firstPids.size(); |
| 3158 | for (int i = 0; i < num; i++) { |
| 3159 | synchronized (observer) { |
| 3160 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3161 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3162 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3163 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3164 | } catch (InterruptedException e) { |
| 3165 | Log.wtf(TAG, e); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3166 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | // Next measure CPU usage. |
| 3170 | if (processStats != null) { |
| 3171 | processStats.init(); |
| 3172 | System.gc(); |
| 3173 | processStats.update(); |
| 3174 | try { |
| 3175 | synchronized (processStats) { |
| 3176 | processStats.wait(500); // measure over 1/2 second. |
| 3177 | } |
| 3178 | } catch (InterruptedException e) { |
| 3179 | } |
| 3180 | processStats.update(); |
| 3181 | |
| 3182 | // We'll take the stack crawls of just the top apps using CPU. |
| 3183 | final int N = processStats.countWorkingStats(); |
| 3184 | int numProcs = 0; |
| 3185 | for (int i=0; i<N && numProcs<5; i++) { |
| 3186 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3187 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3188 | numProcs++; |
| 3189 | try { |
| 3190 | synchronized (observer) { |
| 3191 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3192 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3193 | } |
| 3194 | } catch (InterruptedException e) { |
| 3195 | Log.wtf(TAG, e); |
| 3196 | } |
| 3197 | |
| 3198 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3199 | } |
| 3200 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3201 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3202 | } finally { |
| 3203 | observer.stopWatching(); |
| 3204 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3205 | |
| 3206 | if (nativeProcs != null) { |
| 3207 | int[] pids = Process.getPidsForCommands(nativeProcs); |
| 3208 | if (pids != null) { |
| 3209 | for (int pid : pids) { |
| 3210 | Debug.dumpNativeBacktraceToFile(pid, tracesPath); |
| 3211 | } |
| 3212 | } |
| 3213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3214 | } |
| 3215 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3216 | final void logAppTooSlow(ProcessRecord app, long startTime, String msg) { |
Dianne Hackborn | 69dc66e | 2012-03-26 10:50:54 -0700 | [diff] [blame] | 3217 | if (true || IS_USER_BUILD) { |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3218 | return; |
| 3219 | } |
| 3220 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3221 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3222 | return; |
| 3223 | } |
| 3224 | |
| 3225 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 3226 | StrictMode.allowThreadDiskWrites(); |
| 3227 | try { |
| 3228 | final File tracesFile = new File(tracesPath); |
| 3229 | final File tracesDir = tracesFile.getParentFile(); |
| 3230 | final File tracesTmp = new File(tracesDir, "__tmp__"); |
| 3231 | try { |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3232 | if (!tracesDir.exists()) { |
| 3233 | tracesFile.mkdirs(); |
| 3234 | if (!SELinux.restorecon(tracesDir.getPath())) { |
| 3235 | return; |
| 3236 | } |
| 3237 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3238 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3239 | |
| 3240 | if (tracesFile.exists()) { |
| 3241 | tracesTmp.delete(); |
| 3242 | tracesFile.renameTo(tracesTmp); |
| 3243 | } |
| 3244 | StringBuilder sb = new StringBuilder(); |
| 3245 | Time tobj = new Time(); |
| 3246 | tobj.set(System.currentTimeMillis()); |
| 3247 | sb.append(tobj.format("%Y-%m-%d %H:%M:%S")); |
| 3248 | sb.append(": "); |
| 3249 | TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb); |
| 3250 | sb.append(" since "); |
| 3251 | sb.append(msg); |
| 3252 | FileOutputStream fos = new FileOutputStream(tracesFile); |
| 3253 | fos.write(sb.toString().getBytes()); |
| 3254 | if (app == null) { |
| 3255 | fos.write("\n*** No application process!".getBytes()); |
| 3256 | } |
| 3257 | fos.close(); |
| 3258 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3259 | } catch (IOException e) { |
| 3260 | Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e); |
| 3261 | return; |
| 3262 | } |
| 3263 | |
| 3264 | if (app != null) { |
| 3265 | ArrayList<Integer> firstPids = new ArrayList<Integer>(); |
| 3266 | firstPids.add(app.pid); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3267 | dumpStackTraces(tracesPath, firstPids, null, null, null); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | File lastTracesFile = null; |
| 3271 | File curTracesFile = null; |
| 3272 | for (int i=9; i>=0; i--) { |
| 3273 | String name = String.format("slow%02d.txt", i); |
| 3274 | curTracesFile = new File(tracesDir, name); |
| 3275 | if (curTracesFile.exists()) { |
| 3276 | if (lastTracesFile != null) { |
| 3277 | curTracesFile.renameTo(lastTracesFile); |
| 3278 | } else { |
| 3279 | curTracesFile.delete(); |
| 3280 | } |
| 3281 | } |
| 3282 | lastTracesFile = curTracesFile; |
| 3283 | } |
| 3284 | tracesFile.renameTo(curTracesFile); |
| 3285 | if (tracesTmp.exists()) { |
| 3286 | tracesTmp.renameTo(tracesFile); |
| 3287 | } |
| 3288 | } finally { |
| 3289 | StrictMode.setThreadPolicy(oldPolicy); |
| 3290 | } |
| 3291 | } |
| 3292 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3293 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 3294 | ActivityRecord parent, boolean aboveSystem, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3295 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3296 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3297 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3298 | if (mController != null) { |
| 3299 | try { |
| 3300 | // 0 == continue, -1 = kill process immediately |
| 3301 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3302 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3303 | } catch (RemoteException e) { |
| 3304 | mController = null; |
| 3305 | } |
| 3306 | } |
| 3307 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3308 | long anrTime = SystemClock.uptimeMillis(); |
| 3309 | if (MONITOR_CPU_USAGE) { |
| 3310 | updateCpuStatsNow(); |
| 3311 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3312 | |
| 3313 | synchronized (this) { |
| 3314 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3315 | if (mShuttingDown) { |
| 3316 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3317 | return; |
| 3318 | } else if (app.notResponding) { |
| 3319 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3320 | return; |
| 3321 | } else if (app.crashing) { |
| 3322 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3323 | return; |
| 3324 | } |
| 3325 | |
| 3326 | // In case we come through here for the same app before completing |
| 3327 | // this one, mark as anring now so we will bail out. |
| 3328 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3329 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3330 | // Log the ANR to the event log. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3331 | EventLog.writeEvent(EventLogTags.AM_ANR, app.userId, app.pid, |
| 3332 | app.processName, app.info.flags, annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3333 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3334 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3335 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3336 | |
| 3337 | int parentPid = app.pid; |
| 3338 | 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] | 3339 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3340 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3341 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3342 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3343 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3344 | ProcessRecord r = mLruProcesses.get(i); |
| 3345 | if (r != null && r.thread != null) { |
| 3346 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3347 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3348 | if (r.persistent) { |
| 3349 | firstPids.add(pid); |
| 3350 | } else { |
| 3351 | lastPids.put(pid, Boolean.TRUE); |
| 3352 | } |
| 3353 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3354 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3355 | } |
| 3356 | } |
| 3357 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3358 | // Log the ANR to the main log. |
Jeff Brown | deb6ed8 | 2012-04-10 14:26:26 -0700 | [diff] [blame] | 3359 | StringBuilder info = new StringBuilder(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3360 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3361 | info.append("ANR in ").append(app.processName); |
| 3362 | if (activity != null && activity.shortComponentName != null) { |
| 3363 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3364 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3365 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3366 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3367 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3368 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3369 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3370 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3371 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3372 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3373 | final ProcessStats processStats = new ProcessStats(true); |
| 3374 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3375 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3376 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3377 | String cpuInfo = null; |
| 3378 | if (MONITOR_CPU_USAGE) { |
| 3379 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3380 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3381 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3382 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3383 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3384 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3385 | } |
| 3386 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3387 | info.append(processStats.printCurrentState(anrTime)); |
| 3388 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3389 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3390 | if (tracesFile == null) { |
| 3391 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3392 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3393 | } |
| 3394 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3395 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3396 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3397 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3398 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3399 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3400 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3401 | 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] | 3402 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3403 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3404 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3405 | } |
| 3406 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3407 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3408 | } |
| 3409 | } |
| 3410 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3411 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3412 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3413 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3414 | |
| 3415 | synchronized (this) { |
| 3416 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3417 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3418 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3419 | app.processName, app.setAdj, "background ANR"); |
| 3420 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3421 | return; |
| 3422 | } |
| 3423 | |
| 3424 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3425 | makeAppNotRespondingLocked(app, |
| 3426 | activity != null ? activity.shortComponentName : null, |
| 3427 | annotation != null ? "ANR " + annotation : "ANR", |
| 3428 | info.toString()); |
| 3429 | |
| 3430 | // Bring up the infamous App Not Responding dialog |
| 3431 | Message msg = Message.obtain(); |
| 3432 | HashMap map = new HashMap(); |
| 3433 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3434 | msg.obj = map; |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 3435 | msg.arg1 = aboveSystem ? 1 : 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3436 | map.put("app", app); |
| 3437 | if (activity != null) { |
| 3438 | map.put("activity", activity); |
| 3439 | } |
| 3440 | |
| 3441 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3442 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3443 | } |
| 3444 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3445 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3446 | if (!mLaunchWarningShown) { |
| 3447 | mLaunchWarningShown = true; |
| 3448 | mHandler.post(new Runnable() { |
| 3449 | @Override |
| 3450 | public void run() { |
| 3451 | synchronized (ActivityManagerService.this) { |
| 3452 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3453 | d.show(); |
| 3454 | mHandler.postDelayed(new Runnable() { |
| 3455 | @Override |
| 3456 | public void run() { |
| 3457 | synchronized (ActivityManagerService.this) { |
| 3458 | d.dismiss(); |
| 3459 | mLaunchWarningShown = false; |
| 3460 | } |
| 3461 | } |
| 3462 | }, 4000); |
| 3463 | } |
| 3464 | } |
| 3465 | }); |
| 3466 | } |
| 3467 | } |
| 3468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3469 | public boolean clearApplicationUserData(final String packageName, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3470 | final IPackageDataObserver observer, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3471 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3472 | int uid = Binder.getCallingUid(); |
| 3473 | int pid = Binder.getCallingPid(); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3474 | userId = handleIncomingUser(pid, uid, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3475 | userId, false, true, "clearApplicationUserData", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3476 | long callingId = Binder.clearCallingIdentity(); |
| 3477 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3478 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3479 | int pkgUid = -1; |
| 3480 | synchronized(this) { |
| 3481 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3482 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3483 | } catch (RemoteException e) { |
| 3484 | } |
| 3485 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3486 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3487 | return false; |
| 3488 | } |
| 3489 | if (uid == pkgUid || checkComponentPermission( |
| 3490 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3491 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3492 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3493 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3494 | } else { |
| 3495 | throw new SecurityException(pid+" does not have permission:"+ |
| 3496 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3497 | "for process:"+packageName); |
| 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | try { |
| 3502 | //clear application user data |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3503 | pm.clearApplicationUserData(packageName, observer, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3504 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3505 | Uri.fromParts("package", packageName, null)); |
| 3506 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3507 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3508 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3509 | } catch (RemoteException e) { |
| 3510 | } |
| 3511 | } finally { |
| 3512 | Binder.restoreCallingIdentity(callingId); |
| 3513 | } |
| 3514 | return true; |
| 3515 | } |
| 3516 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3517 | public void killBackgroundProcesses(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3518 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3519 | != PackageManager.PERMISSION_GRANTED && |
| 3520 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3521 | != PackageManager.PERMISSION_GRANTED) { |
| 3522 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3523 | + Binder.getCallingPid() |
| 3524 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3525 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3526 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3527 | throw new SecurityException(msg); |
| 3528 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3529 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3530 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3531 | userId, true, true, "killBackgroundProcesses", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3532 | long callingId = Binder.clearCallingIdentity(); |
| 3533 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3534 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3535 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3536 | int appId = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3537 | try { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3538 | appId = UserHandle.getAppId(pm.getPackageUid(packageName, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3539 | } catch (RemoteException e) { |
| 3540 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3541 | if (appId == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3542 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3543 | return; |
| 3544 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3545 | killPackageProcessesLocked(packageName, appId, userId, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3546 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3547 | } |
| 3548 | } finally { |
| 3549 | Binder.restoreCallingIdentity(callingId); |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | public void killAllBackgroundProcesses() { |
| 3554 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3555 | != PackageManager.PERMISSION_GRANTED) { |
| 3556 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3557 | + Binder.getCallingPid() |
| 3558 | + ", uid=" + Binder.getCallingUid() |
| 3559 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3560 | Slog.w(TAG, msg); |
| 3561 | throw new SecurityException(msg); |
| 3562 | } |
| 3563 | |
| 3564 | long callingId = Binder.clearCallingIdentity(); |
| 3565 | try { |
| 3566 | synchronized(this) { |
| 3567 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3568 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3569 | final int NA = apps.size(); |
| 3570 | for (int ia=0; ia<NA; ia++) { |
| 3571 | ProcessRecord app = apps.valueAt(ia); |
| 3572 | if (app.persistent) { |
| 3573 | // we don't kill persistent processes |
| 3574 | continue; |
| 3575 | } |
| 3576 | if (app.removed) { |
| 3577 | procs.add(app); |
| 3578 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3579 | app.removed = true; |
| 3580 | procs.add(app); |
| 3581 | } |
| 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | int N = procs.size(); |
| 3586 | for (int i=0; i<N; i++) { |
| 3587 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3588 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3589 | } |
| 3590 | } finally { |
| 3591 | Binder.restoreCallingIdentity(callingId); |
| 3592 | } |
| 3593 | } |
| 3594 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3595 | public void forceStopPackage(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3596 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3597 | != PackageManager.PERMISSION_GRANTED) { |
| 3598 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3599 | + Binder.getCallingPid() |
| 3600 | + ", uid=" + Binder.getCallingUid() |
| 3601 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3602 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3603 | throw new SecurityException(msg); |
| 3604 | } |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3605 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3606 | userId, true, true, "forceStopPackage", null); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3607 | long callingId = Binder.clearCallingIdentity(); |
| 3608 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3609 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3610 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3611 | int[] users = userId == UserHandle.USER_ALL |
| 3612 | ? getUsersLocked() : new int[] { userId }; |
| 3613 | for (int user : users) { |
| 3614 | int pkgUid = -1; |
| 3615 | try { |
| 3616 | pkgUid = pm.getPackageUid(packageName, user); |
| 3617 | } catch (RemoteException e) { |
| 3618 | } |
| 3619 | if (pkgUid == -1) { |
| 3620 | Slog.w(TAG, "Invalid packageName: " + packageName); |
| 3621 | continue; |
| 3622 | } |
| 3623 | try { |
| 3624 | pm.setPackageStoppedState(packageName, true, user); |
| 3625 | } catch (RemoteException e) { |
| 3626 | } catch (IllegalArgumentException e) { |
| 3627 | Slog.w(TAG, "Failed trying to unstop package " |
| 3628 | + packageName + ": " + e); |
| 3629 | } |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 3630 | if (isUserRunningLocked(user, false)) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3631 | forceStopPackageLocked(packageName, pkgUid); |
| 3632 | } |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3633 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3634 | } |
| 3635 | } finally { |
| 3636 | Binder.restoreCallingIdentity(callingId); |
| 3637 | } |
| 3638 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3639 | |
| 3640 | /* |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3641 | * The pkg name and app id have to be specified. |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3642 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3643 | public void killApplicationWithAppId(String pkg, int appid) { |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3644 | if (pkg == null) { |
| 3645 | return; |
| 3646 | } |
| 3647 | // Make sure the uid is valid. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3648 | if (appid < 0) { |
| 3649 | Slog.w(TAG, "Invalid appid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3650 | return; |
| 3651 | } |
| 3652 | int callerUid = Binder.getCallingUid(); |
| 3653 | // Only the system server can kill an application |
| 3654 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3655 | // Post an aysnc message to kill the application |
| 3656 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3657 | msg.arg1 = appid; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3658 | msg.arg2 = 0; |
| 3659 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3660 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3661 | } else { |
| 3662 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3663 | pkg); |
| 3664 | } |
| 3665 | } |
| 3666 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3667 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3668 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3669 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3670 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3671 | final int uid = Binder.getCallingUid(); |
| 3672 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3673 | try { |
| 3674 | synchronized (this) { |
| 3675 | // Only allow this from foreground processes, so that background |
| 3676 | // applications can't abuse it to prevent system UI from being shown. |
| 3677 | if (uid >= Process.FIRST_APPLICATION_UID) { |
| 3678 | ProcessRecord proc; |
| 3679 | synchronized (mPidsSelfLocked) { |
| 3680 | proc = mPidsSelfLocked.get(pid); |
| 3681 | } |
| 3682 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 3683 | Slog.w(TAG, "Ignoring closeSystemDialogs " + reason |
| 3684 | + " from background process " + proc); |
| 3685 | return; |
| 3686 | } |
| 3687 | } |
| 3688 | closeSystemDialogsLocked(reason); |
| 3689 | } |
| 3690 | } finally { |
| 3691 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3692 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3693 | } |
| 3694 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3695 | void closeSystemDialogsLocked(String reason) { |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3696 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3697 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3698 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3699 | if (reason != null) { |
| 3700 | intent.putExtra("reason", reason); |
| 3701 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3702 | mWindowManager.closeSystemDialogs(reason); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3703 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 3704 | mMainStack.closeSystemDialogsLocked(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3705 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3706 | broadcastIntentLocked(null, null, intent, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 3707 | null, 0, null, null, null, AppOpsManager.OP_NONE, false, false, -1, |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3708 | Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3709 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3710 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3711 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3712 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3713 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3714 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3715 | for (int i=pids.length-1; i>=0; i--) { |
| 3716 | infos[i] = new Debug.MemoryInfo(); |
| 3717 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3718 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3719 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3720 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3721 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3722 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3723 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3724 | long[] pss = new long[pids.length]; |
| 3725 | for (int i=pids.length-1; i>=0; i--) { |
| 3726 | pss[i] = Debug.getPss(pids[i]); |
| 3727 | } |
| 3728 | return pss; |
| 3729 | } |
| 3730 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3731 | public void killApplicationProcess(String processName, int uid) { |
| 3732 | if (processName == null) { |
| 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | int callerUid = Binder.getCallingUid(); |
| 3737 | // Only the system server can kill an application |
| 3738 | if (callerUid == Process.SYSTEM_UID) { |
| 3739 | synchronized (this) { |
| 3740 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3741 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3742 | try { |
| 3743 | app.thread.scheduleSuicide(); |
| 3744 | } catch (RemoteException e) { |
| 3745 | // If the other end already died, then our work here is done. |
| 3746 | } |
| 3747 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3748 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3749 | + processName + " / " + uid); |
| 3750 | } |
| 3751 | } |
| 3752 | } else { |
| 3753 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3754 | processName); |
| 3755 | } |
| 3756 | } |
| 3757 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3758 | private void forceStopPackageLocked(final String packageName, int uid) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3759 | forceStopPackageLocked(packageName, UserHandle.getAppId(uid), false, |
| 3760 | false, true, false, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3761 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3762 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3763 | if (!mProcessesReady) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3764 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3765 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3766 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3767 | intent.putExtra(Intent.EXTRA_UID, uid); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 3768 | intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3769 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 3770 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3771 | false, false, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3772 | MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3773 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3774 | |
| 3775 | private void forceStopUserLocked(int userId) { |
| 3776 | forceStopPackageLocked(null, -1, false, false, true, false, userId); |
| 3777 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 3778 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3779 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3780 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 3781 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 3782 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3783 | false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 3784 | MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3785 | } |
| 3786 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3787 | private final boolean killPackageProcessesLocked(String packageName, int appId, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3788 | int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart, |
| 3789 | boolean doit, boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3790 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3791 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3792 | // Remove all processes this package may have touched: all with the |
| 3793 | // same UID (except for the system or root user), and all whose name |
| 3794 | // matches the package name. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3795 | final String procNamePrefix = packageName != null ? (packageName + ":") : null; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3796 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3797 | final int NA = apps.size(); |
| 3798 | for (int ia=0; ia<NA; ia++) { |
| 3799 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3800 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3801 | // we don't kill persistent processes |
| 3802 | continue; |
| 3803 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3804 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3805 | if (doit) { |
| 3806 | procs.add(app); |
| 3807 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3808 | continue; |
| 3809 | } |
| 3810 | |
| 3811 | // Skip process if it doesn't meet our oom adj requirement. |
| 3812 | if (app.setAdj < minOomAdj) { |
| 3813 | continue; |
| 3814 | } |
| 3815 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3816 | // If no package is specified, we call all processes under the |
| 3817 | // give user id. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3818 | if (packageName == null) { |
| 3819 | if (app.userId != userId) { |
| 3820 | continue; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3821 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3822 | // Package has been specified, we want to hit all processes |
| 3823 | // that match it. We need to qualify this by the processes |
| 3824 | // that are running under the specified app and user ID. |
| 3825 | } else { |
| 3826 | if (UserHandle.getAppId(app.uid) != appId) { |
| 3827 | continue; |
| 3828 | } |
| 3829 | if (userId != UserHandle.USER_ALL && app.userId != userId) { |
| 3830 | continue; |
| 3831 | } |
| 3832 | if (!app.pkgList.contains(packageName)) { |
| 3833 | continue; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3834 | } |
| 3835 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3836 | |
| 3837 | // Process has passed all conditions, kill it! |
| 3838 | if (!doit) { |
| 3839 | return true; |
| 3840 | } |
| 3841 | app.removed = true; |
| 3842 | procs.add(app); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3843 | } |
| 3844 | } |
| 3845 | |
| 3846 | int N = procs.size(); |
| 3847 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3848 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3849 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3850 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3851 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3852 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3853 | private final boolean forceStopPackageLocked(String name, int appId, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3854 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3855 | boolean evenPersistent, int userId) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3856 | int i; |
| 3857 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3858 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3859 | if (userId == UserHandle.USER_ALL && name == null) { |
| 3860 | Slog.w(TAG, "Can't force stop all processes of all users, that is insane!"); |
| 3861 | } |
| 3862 | |
| 3863 | if (appId < 0 && name != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3864 | try { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3865 | appId = UserHandle.getAppId( |
| 3866 | AppGlobals.getPackageManager().getPackageUid(name, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3867 | } catch (RemoteException e) { |
| 3868 | } |
| 3869 | } |
| 3870 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3871 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3872 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3873 | Slog.i(TAG, "Force stopping package " + name + " appid=" + appId |
| 3874 | + " user=" + userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3875 | } else { |
| 3876 | Slog.i(TAG, "Force stopping user " + userId); |
| 3877 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3878 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3879 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3880 | while (badApps.hasNext()) { |
| 3881 | SparseArray<Long> ba = badApps.next(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3882 | for (i=ba.size()-1; i>=0; i--) { |
| 3883 | boolean remove = false; |
| 3884 | final int entUid = ba.keyAt(i); |
| 3885 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3886 | if (userId == UserHandle.USER_ALL) { |
| 3887 | if (UserHandle.getAppId(entUid) == appId) { |
| 3888 | remove = true; |
| 3889 | } |
| 3890 | } else { |
| 3891 | if (entUid == UserHandle.getUid(userId, appId)) { |
| 3892 | remove = true; |
| 3893 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3894 | } |
| 3895 | } else if (UserHandle.getUserId(entUid) == userId) { |
| 3896 | remove = true; |
| 3897 | } |
| 3898 | if (remove) { |
| 3899 | ba.removeAt(i); |
| 3900 | } |
| 3901 | } |
| 3902 | if (ba.size() == 0) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3903 | badApps.remove(); |
| 3904 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3905 | } |
| 3906 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3907 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3908 | boolean didSomething = killPackageProcessesLocked(name, appId, userId, |
Dianne Hackborn | e62fa82 | 2013-01-09 18:31:37 -0800 | [diff] [blame] | 3909 | -100, callerWillRestart, true, doit, evenPersistent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3910 | name == null ? ("force stop user " + userId) : ("force stop " + name)); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 3911 | |
| 3912 | if (mMainStack.forceStopPackageLocked(name, doit, evenPersistent, userId)) { |
| 3913 | if (!doit) { |
| 3914 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3915 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 3916 | didSomething = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3917 | } |
| 3918 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3919 | if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) { |
| 3920 | if (!doit) { |
| 3921 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3922 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3923 | didSomething = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3924 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3925 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3926 | if (name == null) { |
| 3927 | // Remove all sticky broadcasts from this user. |
| 3928 | mStickyBroadcasts.remove(userId); |
| 3929 | } |
| 3930 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3931 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3932 | if (mProviderMap.collectForceStopProviders(name, appId, doit, evenPersistent, |
| 3933 | userId, providers)) { |
| 3934 | if (!doit) { |
| 3935 | return true; |
| 3936 | } |
| 3937 | didSomething = true; |
| 3938 | } |
| 3939 | N = providers.size(); |
| 3940 | for (i=0; i<N; i++) { |
| 3941 | removeDyingProviderLocked(null, providers.get(i), true); |
| 3942 | } |
| 3943 | |
Dianne Hackborn | 861a3b2 | 2012-10-12 15:25:20 -0700 | [diff] [blame] | 3944 | if (name == null) { |
| 3945 | // Remove pending intents. For now we only do this when force |
| 3946 | // stopping users, because we have some problems when doing this |
| 3947 | // for packages -- app widgets are not currently cleaned up for |
| 3948 | // such packages, so they can be left with bad pending intents. |
| 3949 | if (mIntentSenderRecords.size() > 0) { |
| 3950 | Iterator<WeakReference<PendingIntentRecord>> it |
| 3951 | = mIntentSenderRecords.values().iterator(); |
| 3952 | while (it.hasNext()) { |
| 3953 | WeakReference<PendingIntentRecord> wpir = it.next(); |
| 3954 | if (wpir == null) { |
| 3955 | it.remove(); |
| 3956 | continue; |
| 3957 | } |
| 3958 | PendingIntentRecord pir = wpir.get(); |
| 3959 | if (pir == null) { |
| 3960 | it.remove(); |
| 3961 | continue; |
| 3962 | } |
| 3963 | if (name == null) { |
| 3964 | // Stopping user, remove all objects for the user. |
| 3965 | if (pir.key.userId != userId) { |
| 3966 | // Not the same user, skip it. |
| 3967 | continue; |
| 3968 | } |
| 3969 | } else { |
| 3970 | if (UserHandle.getAppId(pir.uid) != appId) { |
| 3971 | // Different app id, skip it. |
| 3972 | continue; |
| 3973 | } |
| 3974 | if (userId != UserHandle.USER_ALL && pir.key.userId != userId) { |
| 3975 | // Different user, skip it. |
| 3976 | continue; |
| 3977 | } |
| 3978 | if (!pir.key.packageName.equals(name)) { |
| 3979 | // Different package, skip it. |
| 3980 | continue; |
| 3981 | } |
| 3982 | } |
| 3983 | if (!doit) { |
| 3984 | return true; |
| 3985 | } |
| 3986 | didSomething = true; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3987 | it.remove(); |
Dianne Hackborn | 861a3b2 | 2012-10-12 15:25:20 -0700 | [diff] [blame] | 3988 | pir.canceled = true; |
| 3989 | if (pir.key.activity != null) { |
| 3990 | pir.key.activity.pendingResults.remove(pir.ref); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3991 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3992 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3993 | } |
| 3994 | } |
| 3995 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3996 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3997 | if (purgeCache && name != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3998 | AttributeCache ac = AttributeCache.instance(); |
| 3999 | if (ac != null) { |
| 4000 | ac.removePackage(name); |
| 4001 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4002 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 4003 | if (mBooted) { |
| 4004 | mMainStack.resumeTopActivityLocked(null); |
| 4005 | mMainStack.scheduleIdleLocked(); |
| 4006 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4007 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4008 | |
| 4009 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4010 | } |
| 4011 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4012 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4013 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4014 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4015 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4016 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4017 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4018 | + "/" + uid + ")"); |
| 4019 | |
| 4020 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4021 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4022 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4023 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4024 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4025 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4026 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4027 | boolean needRestart = false; |
| 4028 | if (app.pid > 0 && app.pid != MY_PID) { |
| 4029 | int pid = app.pid; |
| 4030 | synchronized (mPidsSelfLocked) { |
| 4031 | mPidsSelfLocked.remove(pid); |
| 4032 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4033 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4034 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4035 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 4036 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4037 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4038 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4039 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4040 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4041 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4042 | } else { |
| 4043 | needRestart = true; |
| 4044 | } |
| 4045 | } |
| 4046 | } else { |
| 4047 | mRemovedProcesses.add(app); |
| 4048 | } |
| 4049 | |
| 4050 | return needRestart; |
| 4051 | } |
| 4052 | |
| 4053 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 4054 | final int pid = app.pid; |
| 4055 | boolean gone = false; |
| 4056 | synchronized (mPidsSelfLocked) { |
| 4057 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 4058 | if (knownApp != null && knownApp.thread == null) { |
| 4059 | mPidsSelfLocked.remove(pid); |
| 4060 | gone = true; |
| 4061 | } |
| 4062 | } |
| 4063 | |
| 4064 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4065 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4066 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, app.userId, |
| 4067 | pid, app.uid, app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4068 | mProcessNames.remove(app.processName, app.uid); |
| 4069 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4070 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4071 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4072 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4073 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4074 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4075 | // Take care of any launching providers waiting for this process. |
| 4076 | checkAppInLaunchingProvidersLocked(app, true); |
| 4077 | // Take care of any services that are waiting for the process. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4078 | mServices.processStartTimedOutLocked(app); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4079 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4080 | app.processName, app.setAdj, "start timeout"); |
| 4081 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4082 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4083 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4084 | try { |
| 4085 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 4086 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 4087 | bm.agentDisconnected(app.info.packageName); |
| 4088 | } catch (RemoteException e) { |
| 4089 | // Can't happen; the backup manager is local |
| 4090 | } |
| 4091 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4092 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4093 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4094 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4095 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4096 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4097 | 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] | 4098 | } |
| 4099 | } |
| 4100 | |
| 4101 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 4102 | int pid) { |
| 4103 | |
| 4104 | // Find the application record that is being attached... either via |
| 4105 | // the pid if we are running in multiple processes, or just pull the |
| 4106 | // next app record if we are emulating process with anonymous threads. |
| 4107 | ProcessRecord app; |
| 4108 | if (pid != MY_PID && pid >= 0) { |
| 4109 | synchronized (mPidsSelfLocked) { |
| 4110 | app = mPidsSelfLocked.get(pid); |
| 4111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4112 | } else { |
| 4113 | app = null; |
| 4114 | } |
| 4115 | |
| 4116 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4117 | 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] | 4118 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4119 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4120 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4121 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4122 | } else { |
| 4123 | try { |
| 4124 | thread.scheduleExit(); |
| 4125 | } catch (Exception e) { |
| 4126 | // Ignore exceptions. |
| 4127 | } |
| 4128 | } |
| 4129 | return false; |
| 4130 | } |
| 4131 | |
| 4132 | // If this application record is still attached to a previous |
| 4133 | // process, clean it up now. |
| 4134 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4135 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | // Tell the process all about itself. |
| 4139 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4140 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4141 | TAG, "Binding process pid " + pid + " to record " + app); |
| 4142 | |
| 4143 | String processName = app.processName; |
| 4144 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4145 | AppDeathRecipient adr = new AppDeathRecipient( |
| 4146 | app, pid, thread); |
| 4147 | thread.asBinder().linkToDeath(adr, 0); |
| 4148 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4149 | } catch (RemoteException e) { |
| 4150 | app.resetPackageList(); |
| 4151 | startProcessLocked(app, "link fail", processName); |
| 4152 | return false; |
| 4153 | } |
| 4154 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4155 | 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] | 4156 | |
| 4157 | app.thread = thread; |
| 4158 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 4159 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 4160 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4161 | app.forcingToForeground = null; |
| 4162 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 4163 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4164 | app.debugging = false; |
| 4165 | |
| 4166 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4167 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4168 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4169 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4170 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4171 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4172 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4173 | } |
| 4174 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4175 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4176 | TAG, "New app record " + app |
| 4177 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 4178 | try { |
| 4179 | int testMode = IApplicationThread.DEBUG_OFF; |
| 4180 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 4181 | testMode = mWaitForDebugger |
| 4182 | ? IApplicationThread.DEBUG_WAIT |
| 4183 | : IApplicationThread.DEBUG_ON; |
| 4184 | app.debugging = true; |
| 4185 | if (mDebugTransient) { |
| 4186 | mDebugApp = mOrigDebugApp; |
| 4187 | mWaitForDebugger = mOrigWaitForDebugger; |
| 4188 | } |
| 4189 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4190 | String profileFile = app.instrumentationProfileFile; |
| 4191 | ParcelFileDescriptor profileFd = null; |
| 4192 | boolean profileAutoStop = false; |
| 4193 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 4194 | mProfileProc = app; |
| 4195 | profileFile = mProfileFile; |
| 4196 | profileFd = mProfileFd; |
| 4197 | profileAutoStop = mAutoStopProfiler; |
| 4198 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4199 | boolean enableOpenGlTrace = false; |
| 4200 | if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) { |
| 4201 | enableOpenGlTrace = true; |
| 4202 | mOpenGlTraceApp = null; |
| 4203 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4204 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4205 | // If the app is being launched for restore or full backup, set it up specially |
| 4206 | boolean isRestrictedBackupMode = false; |
| 4207 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 4208 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4209 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4210 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 4211 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4212 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 4213 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 4214 | ? app.instrumentationInfo.packageName |
| 4215 | : app.info.packageName); |
| 4216 | if (app.instrumentationClass != null) { |
| 4217 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 4218 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4219 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 4220 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4221 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 4222 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 4223 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4224 | if (profileFd != null) { |
| 4225 | profileFd = profileFd.dup(); |
| 4226 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4227 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4228 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 4229 | app.instrumentationArguments, app.instrumentationWatcher, |
| 4230 | app.instrumentationUiAutomationConnection, testMode, enableOpenGlTrace, |
| 4231 | isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 4232 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 4233 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4234 | updateLruProcessLocked(app, false); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 4235 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4236 | } catch (Exception e) { |
| 4237 | // todo: Yikes! What should we do? For now we will try to |
| 4238 | // start another process, but that could easily get us in |
| 4239 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4240 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4241 | |
| 4242 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4243 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4244 | startProcessLocked(app, "bind fail", processName); |
| 4245 | return false; |
| 4246 | } |
| 4247 | |
| 4248 | // Remove this record from the list of starting applications. |
| 4249 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4250 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 4251 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4252 | mProcessesOnHold.remove(app); |
| 4253 | |
| 4254 | boolean badApp = false; |
| 4255 | boolean didSomething = false; |
| 4256 | |
| 4257 | // 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] | 4258 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 4259 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4260 | 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] | 4261 | && processName.equals(hr.processName)) { |
| 4262 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 4263 | if (mHeadless) { |
| 4264 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 4265 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4266 | didSomething = true; |
| 4267 | } |
| 4268 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4269 | 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] | 4270 | + hr.intent.getComponent().flattenToShortString(), e); |
| 4271 | badApp = true; |
| 4272 | } |
| 4273 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4274 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4275 | } |
| 4276 | } |
| 4277 | |
| 4278 | // Find any services that should be running in this process... |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4279 | if (!badApp) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4280 | try { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4281 | didSomething |= mServices.attachApplicationLocked(app, processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4282 | } catch (Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4283 | badApp = true; |
| 4284 | } |
| 4285 | } |
| 4286 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4287 | // Check if a next-broadcast receiver is in this process... |
| 4288 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4289 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4290 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4291 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4292 | // 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] | 4293 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4297 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4298 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4299 | 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] | 4300 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4301 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4302 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 4303 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 4304 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4305 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4306 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4307 | e.printStackTrace(); |
| 4308 | } |
| 4309 | } |
| 4310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4311 | if (badApp) { |
| 4312 | // todo: Also need to kill application to deal with all |
| 4313 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4314 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4315 | return false; |
| 4316 | } |
| 4317 | |
| 4318 | if (!didSomething) { |
| 4319 | updateOomAdjLocked(); |
| 4320 | } |
| 4321 | |
| 4322 | return true; |
| 4323 | } |
| 4324 | |
| 4325 | public final void attachApplication(IApplicationThread thread) { |
| 4326 | synchronized (this) { |
| 4327 | int callingPid = Binder.getCallingPid(); |
| 4328 | final long origId = Binder.clearCallingIdentity(); |
| 4329 | attachApplicationLocked(thread, callingPid); |
| 4330 | Binder.restoreCallingIdentity(origId); |
| 4331 | } |
| 4332 | } |
| 4333 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4334 | 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] | 4335 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4336 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4337 | if (stopProfiling) { |
| 4338 | synchronized (this) { |
| 4339 | if (mProfileProc == r.app) { |
| 4340 | if (mProfileFd != null) { |
| 4341 | try { |
| 4342 | mProfileFd.close(); |
| 4343 | } catch (IOException e) { |
| 4344 | } |
| 4345 | clearProfilerLocked(); |
| 4346 | } |
| 4347 | } |
| 4348 | } |
| 4349 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4350 | Binder.restoreCallingIdentity(origId); |
| 4351 | } |
| 4352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4353 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4354 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4355 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4356 | mWindowManager.enableScreenAfterBoot(); |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 4357 | |
| 4358 | synchronized (this) { |
| 4359 | updateEventDispatchingLocked(); |
| 4360 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4361 | } |
| 4362 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4363 | public void showBootMessage(final CharSequence msg, final boolean always) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4364 | enforceNotIsolatedCaller("showBootMessage"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4365 | mWindowManager.showBootMessage(msg, always); |
| 4366 | } |
| 4367 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4368 | public void dismissKeyguardOnNextActivity() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4369 | enforceNotIsolatedCaller("dismissKeyguardOnNextActivity"); |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4370 | final long token = Binder.clearCallingIdentity(); |
| 4371 | try { |
| 4372 | synchronized (this) { |
| 4373 | if (mLockScreenShown) { |
| 4374 | mLockScreenShown = false; |
| 4375 | comeOutOfSleepIfNeededLocked(); |
| 4376 | } |
| 4377 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
Dianne Hackborn | 1e88e98 | 2012-04-24 18:35:55 -0700 | [diff] [blame] | 4378 | } |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4379 | } finally { |
| 4380 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4381 | } |
| 4382 | } |
| 4383 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4384 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4385 | IntentFilter pkgFilter = new IntentFilter(); |
| 4386 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4387 | pkgFilter.addDataScheme("package"); |
| 4388 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4389 | @Override |
| 4390 | public void onReceive(Context context, Intent intent) { |
| 4391 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4392 | if (pkgs != null) { |
| 4393 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4394 | synchronized (ActivityManagerService.this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4395 | if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) { |
| 4396 | setResultCode(Activity.RESULT_OK); |
| 4397 | return; |
| 4398 | } |
| 4399 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4400 | } |
| 4401 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4402 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4403 | }, pkgFilter); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4404 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4405 | synchronized (this) { |
| 4406 | // Ensure that any processes we had put on hold are now started |
| 4407 | // up. |
| 4408 | final int NP = mProcessesOnHold.size(); |
| 4409 | if (NP > 0) { |
| 4410 | ArrayList<ProcessRecord> procs = |
| 4411 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4412 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4413 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4414 | + procs.get(ip)); |
| 4415 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4420 | // Start looking for apps that are abusing wake locks. |
| 4421 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4422 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4423 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4424 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4425 | SystemProperties.set("dev.bootcomplete", "1"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4426 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 4427 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 4428 | if (uss.mState == UserStartedState.STATE_BOOTING) { |
| 4429 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4430 | final int userId = mStartedUsers.keyAt(i); |
| 4431 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 4432 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 4433 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4434 | null, null, 0, null, null, |
| 4435 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 4436 | AppOpsManager.OP_NONE, false, false, MY_PID, Process.SYSTEM_UID, |
| 4437 | userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4438 | } |
Amith Yamasani | 4860cfc | 2012-08-08 19:14:18 -0700 | [diff] [blame] | 4439 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4440 | } |
| 4441 | } |
| 4442 | } |
| 4443 | |
| 4444 | final void ensureBootCompleted() { |
| 4445 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4446 | boolean enableScreen; |
| 4447 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4448 | booting = mBooting; |
| 4449 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4450 | enableScreen = !mBooted; |
| 4451 | mBooted = true; |
| 4452 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4453 | |
| 4454 | if (booting) { |
| 4455 | finishBooting(); |
| 4456 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4457 | |
| 4458 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4459 | enableScreenAfterBoot(); |
| 4460 | } |
| 4461 | } |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 4462 | |
| 4463 | public final void activityResumed(IBinder token) { |
| 4464 | final long origId = Binder.clearCallingIdentity(); |
| 4465 | mMainStack.activityResumed(token); |
| 4466 | Binder.restoreCallingIdentity(origId); |
| 4467 | } |
| 4468 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4469 | public final void activityPaused(IBinder token) { |
| 4470 | final long origId = Binder.clearCallingIdentity(); |
| 4471 | mMainStack.activityPaused(token, false); |
| 4472 | Binder.restoreCallingIdentity(origId); |
| 4473 | } |
| 4474 | |
| 4475 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4476 | CharSequence description) { |
| 4477 | if (localLOGV) Slog.v( |
| 4478 | TAG, "Activity stopped: token=" + token); |
| 4479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4480 | // Refuse possible leaked file descriptors |
| 4481 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4482 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4483 | } |
| 4484 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4485 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4486 | |
| 4487 | final long origId = Binder.clearCallingIdentity(); |
| 4488 | |
| 4489 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4490 | r = mMainStack.isInStackLocked(token); |
| 4491 | if (r != null) { |
| 4492 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4493 | } |
| 4494 | } |
| 4495 | |
| 4496 | if (r != null) { |
| 4497 | sendPendingThumbnail(r, null, null, null, false); |
| 4498 | } |
| 4499 | |
| 4500 | trimApplications(); |
| 4501 | |
| 4502 | Binder.restoreCallingIdentity(origId); |
| 4503 | } |
| 4504 | |
| 4505 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4506 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4507 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | public String getCallingPackage(IBinder token) { |
| 4511 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4512 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4513 | 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] | 4514 | } |
| 4515 | } |
| 4516 | |
| 4517 | public ComponentName getCallingActivity(IBinder token) { |
| 4518 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4519 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4520 | return r != null ? r.intent.getComponent() : null; |
| 4521 | } |
| 4522 | } |
| 4523 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4524 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4525 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4526 | if (r == null) { |
| 4527 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4528 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4529 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4533 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4534 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4535 | if (r == null) { |
| 4536 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4537 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4538 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | |
| 4542 | public String getPackageForToken(IBinder token) { |
| 4543 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4544 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4545 | if (r == null) { |
| 4546 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4547 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4548 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | public IIntentSender getIntentSender(int type, |
| 4553 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4554 | int requestCode, Intent[] intents, String[] resolvedTypes, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4555 | int flags, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4556 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4557 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4558 | if (intents != null) { |
| 4559 | if (intents.length < 1) { |
| 4560 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4561 | } |
| 4562 | for (int i=0; i<intents.length; i++) { |
| 4563 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4564 | if (intent != null) { |
| 4565 | if (intent.hasFileDescriptors()) { |
| 4566 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4567 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4568 | if (type == ActivityManager.INTENT_SENDER_BROADCAST && |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4569 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4570 | throw new IllegalArgumentException( |
| 4571 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4572 | } |
| 4573 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4574 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4575 | } |
| 4576 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4577 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4578 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4579 | } |
| 4580 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4581 | if (options != null) { |
| 4582 | if (options.hasFileDescriptors()) { |
| 4583 | throw new IllegalArgumentException("File descriptors passed in options"); |
| 4584 | } |
| 4585 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4587 | synchronized(this) { |
| 4588 | int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4589 | int origUserId = userId; |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 4590 | userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId, |
Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 4591 | type == ActivityManager.INTENT_SENDER_BROADCAST, false, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4592 | "getIntentSender", null); |
| 4593 | if (origUserId == UserHandle.USER_CURRENT) { |
| 4594 | // We don't want to evaluate this until the pending intent is |
| 4595 | // actually executed. However, we do want to always do the |
| 4596 | // security checking for it above. |
| 4597 | userId = UserHandle.USER_CURRENT; |
| 4598 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4599 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4600 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4601 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4602 | .getPackageUid(packageName, UserHandle.getUserId(callingUid)); |
| 4603 | if (!UserHandle.isSameApp(callingUid, uid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4604 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4605 | + Binder.getCallingPid() |
| 4606 | + ", uid=" + Binder.getCallingUid() |
| 4607 | + ", (need uid=" + uid + ")" |
| 4608 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4609 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4610 | throw new SecurityException(msg); |
| 4611 | } |
| 4612 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4613 | |
| 4614 | return getIntentSenderLocked(type, packageName, callingUid, userId, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4615 | token, resultWho, requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4617 | } catch (RemoteException e) { |
| 4618 | throw new SecurityException(e); |
| 4619 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4620 | } |
| 4621 | } |
| 4622 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4623 | IIntentSender getIntentSenderLocked(int type, String packageName, |
| 4624 | int callingUid, int userId, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4625 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags, |
| 4626 | Bundle options) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4627 | if (DEBUG_MU) |
| 4628 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4629 | ActivityRecord activity = null; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4630 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4631 | activity = mMainStack.isInStackLocked(token); |
| 4632 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4633 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4634 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4635 | if (activity.finishing) { |
| 4636 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4637 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4641 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4642 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4643 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4644 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4645 | |
| 4646 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4647 | type, packageName, activity, resultWho, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4648 | requestCode, intents, resolvedTypes, flags, options, userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4649 | WeakReference<PendingIntentRecord> ref; |
| 4650 | ref = mIntentSenderRecords.get(key); |
| 4651 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4652 | if (rec != null) { |
| 4653 | if (!cancelCurrent) { |
| 4654 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4655 | if (rec.key.requestIntent != null) { |
Adam Powell | 501d4a5 | 2012-04-30 15:03:57 -0700 | [diff] [blame] | 4656 | rec.key.requestIntent.replaceExtras(intents != null ? |
| 4657 | intents[intents.length - 1] : null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4658 | } |
| 4659 | if (intents != null) { |
| 4660 | intents[intents.length-1] = rec.key.requestIntent; |
| 4661 | rec.key.allIntents = intents; |
| 4662 | rec.key.allResolvedTypes = resolvedTypes; |
| 4663 | } else { |
| 4664 | rec.key.allIntents = null; |
| 4665 | rec.key.allResolvedTypes = null; |
| 4666 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4667 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4668 | return rec; |
| 4669 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4670 | rec.canceled = true; |
| 4671 | mIntentSenderRecords.remove(key); |
| 4672 | } |
| 4673 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4674 | return rec; |
| 4675 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4676 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4677 | mIntentSenderRecords.put(key, rec.ref); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4678 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4679 | if (activity.pendingResults == null) { |
| 4680 | activity.pendingResults |
| 4681 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4682 | } |
| 4683 | activity.pendingResults.add(rec.ref); |
| 4684 | } |
| 4685 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | public void cancelIntentSender(IIntentSender sender) { |
| 4689 | if (!(sender instanceof PendingIntentRecord)) { |
| 4690 | return; |
| 4691 | } |
| 4692 | synchronized(this) { |
| 4693 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4694 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4695 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4696 | .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId()); |
| 4697 | if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4698 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4699 | + Binder.getCallingPid() |
| 4700 | + ", uid=" + Binder.getCallingUid() |
| 4701 | + " is not allowed to cancel packges " |
| 4702 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4703 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4704 | throw new SecurityException(msg); |
| 4705 | } |
| 4706 | } catch (RemoteException e) { |
| 4707 | throw new SecurityException(e); |
| 4708 | } |
| 4709 | cancelIntentSenderLocked(rec, true); |
| 4710 | } |
| 4711 | } |
| 4712 | |
| 4713 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4714 | rec.canceled = true; |
| 4715 | mIntentSenderRecords.remove(rec.key); |
| 4716 | if (cleanActivity && rec.key.activity != null) { |
| 4717 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4718 | } |
| 4719 | } |
| 4720 | |
| 4721 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4722 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4723 | return null; |
| 4724 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4725 | try { |
| 4726 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4727 | return res.key.packageName; |
| 4728 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4729 | } |
| 4730 | return null; |
| 4731 | } |
| 4732 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4733 | public int getUidForIntentSender(IIntentSender sender) { |
| 4734 | if (sender instanceof PendingIntentRecord) { |
| 4735 | try { |
| 4736 | PendingIntentRecord res = (PendingIntentRecord)sender; |
| 4737 | return res.uid; |
| 4738 | } catch (ClassCastException e) { |
| 4739 | } |
| 4740 | } |
| 4741 | return -1; |
| 4742 | } |
| 4743 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4744 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4745 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4746 | return false; |
| 4747 | } |
| 4748 | try { |
| 4749 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4750 | if (res.key.allIntents == null) { |
| 4751 | return false; |
| 4752 | } |
| 4753 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4754 | Intent intent = res.key.allIntents[i]; |
| 4755 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4756 | return false; |
| 4757 | } |
| 4758 | } |
| 4759 | return true; |
| 4760 | } catch (ClassCastException e) { |
| 4761 | } |
| 4762 | return false; |
| 4763 | } |
| 4764 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 4765 | public boolean isIntentSenderAnActivity(IIntentSender pendingResult) { |
| 4766 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4767 | return false; |
| 4768 | } |
| 4769 | try { |
| 4770 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4771 | if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) { |
| 4772 | return true; |
| 4773 | } |
| 4774 | return false; |
| 4775 | } catch (ClassCastException e) { |
| 4776 | } |
| 4777 | return false; |
| 4778 | } |
| 4779 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 4780 | public Intent getIntentForIntentSender(IIntentSender pendingResult) { |
| 4781 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4782 | return null; |
| 4783 | } |
| 4784 | try { |
| 4785 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4786 | return res.key.requestIntent != null ? new Intent(res.key.requestIntent) : null; |
| 4787 | } catch (ClassCastException e) { |
| 4788 | } |
| 4789 | return null; |
| 4790 | } |
| 4791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4792 | public void setProcessLimit(int max) { |
| 4793 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4794 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4795 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4796 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4797 | mProcessLimitOverride = max; |
| 4798 | } |
| 4799 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4803 | synchronized (this) { |
| 4804 | return mProcessLimitOverride; |
| 4805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | void foregroundTokenDied(ForegroundToken token) { |
| 4809 | synchronized (ActivityManagerService.this) { |
| 4810 | synchronized (mPidsSelfLocked) { |
| 4811 | ForegroundToken cur |
| 4812 | = mForegroundProcesses.get(token.pid); |
| 4813 | if (cur != token) { |
| 4814 | return; |
| 4815 | } |
| 4816 | mForegroundProcesses.remove(token.pid); |
| 4817 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4818 | if (pr == null) { |
| 4819 | return; |
| 4820 | } |
| 4821 | pr.forcingToForeground = null; |
| 4822 | pr.foregroundServices = false; |
| 4823 | } |
| 4824 | updateOomAdjLocked(); |
| 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4829 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4830 | "setProcessForeground()"); |
| 4831 | synchronized(this) { |
| 4832 | boolean changed = false; |
| 4833 | |
| 4834 | synchronized (mPidsSelfLocked) { |
| 4835 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4836 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4837 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4838 | return; |
| 4839 | } |
| 4840 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4841 | if (oldToken != null) { |
| 4842 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4843 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4844 | if (pr != null) { |
| 4845 | pr.forcingToForeground = null; |
| 4846 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4847 | changed = true; |
| 4848 | } |
| 4849 | if (isForeground && token != null) { |
| 4850 | ForegroundToken newToken = new ForegroundToken() { |
| 4851 | public void binderDied() { |
| 4852 | foregroundTokenDied(this); |
| 4853 | } |
| 4854 | }; |
| 4855 | newToken.pid = pid; |
| 4856 | newToken.token = token; |
| 4857 | try { |
| 4858 | token.linkToDeath(newToken, 0); |
| 4859 | mForegroundProcesses.put(pid, newToken); |
| 4860 | pr.forcingToForeground = token; |
| 4861 | changed = true; |
| 4862 | } catch (RemoteException e) { |
| 4863 | // If the process died while doing this, we will later |
| 4864 | // do the cleanup with the process death link. |
| 4865 | } |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | if (changed) { |
| 4870 | updateOomAdjLocked(); |
| 4871 | } |
| 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | // ========================================================= |
| 4876 | // PERMISSIONS |
| 4877 | // ========================================================= |
| 4878 | |
| 4879 | static class PermissionController extends IPermissionController.Stub { |
| 4880 | ActivityManagerService mActivityManagerService; |
| 4881 | PermissionController(ActivityManagerService activityManagerService) { |
| 4882 | mActivityManagerService = activityManagerService; |
| 4883 | } |
| 4884 | |
| 4885 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4886 | return mActivityManagerService.checkPermission(permission, pid, |
| 4887 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | /** |
| 4892 | * This can be called with or without the global lock held. |
| 4893 | */ |
| 4894 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4895 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4896 | // We might be performing an operation on behalf of an indirect binder |
| 4897 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4898 | // client identity accordingly before proceeding. |
| 4899 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4900 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4901 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4902 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4903 | uid = tlsIdentity.uid; |
| 4904 | pid = tlsIdentity.pid; |
| 4905 | } |
| 4906 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4907 | if (pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4908 | return PackageManager.PERMISSION_GRANTED; |
| 4909 | } |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4910 | |
| 4911 | return ActivityManager.checkComponentPermission(permission, uid, |
| 4912 | owningUid, exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | /** |
| 4916 | * As the only public entry point for permissions checking, this method |
| 4917 | * can enforce the semantic that requesting a check on a null global |
| 4918 | * permission is automatically denied. (Internally a null permission |
| 4919 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4920 | * when only uid-based security is needed.) |
| 4921 | * |
| 4922 | * This can be called with or without the global lock held. |
| 4923 | */ |
| 4924 | public int checkPermission(String permission, int pid, int uid) { |
| 4925 | if (permission == null) { |
| 4926 | return PackageManager.PERMISSION_DENIED; |
| 4927 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4928 | return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | /** |
| 4932 | * Binder IPC calls go through the public entry point. |
| 4933 | * This can be called with or without the global lock held. |
| 4934 | */ |
| 4935 | int checkCallingPermission(String permission) { |
| 4936 | return checkPermission(permission, |
| 4937 | Binder.getCallingPid(), |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4938 | UserHandle.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | /** |
| 4942 | * This can be called with or without the global lock held. |
| 4943 | */ |
| 4944 | void enforceCallingPermission(String permission, String func) { |
| 4945 | if (checkCallingPermission(permission) |
| 4946 | == PackageManager.PERMISSION_GRANTED) { |
| 4947 | return; |
| 4948 | } |
| 4949 | |
| 4950 | String msg = "Permission Denial: " + func + " from pid=" |
| 4951 | + Binder.getCallingPid() |
| 4952 | + ", uid=" + Binder.getCallingUid() |
| 4953 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4954 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4955 | throw new SecurityException(msg); |
| 4956 | } |
| 4957 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4958 | /** |
| 4959 | * Determine if UID is holding permissions required to access {@link Uri} in |
| 4960 | * the given {@link ProviderInfo}. Final permission checking is always done |
| 4961 | * in {@link ContentProvider}. |
| 4962 | */ |
| 4963 | private final boolean checkHoldingPermissionsLocked( |
| 4964 | IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4965 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4966 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4967 | |
| 4968 | if (pi.applicationInfo.uid == uid) { |
| 4969 | return true; |
| 4970 | } else if (!pi.exported) { |
| 4971 | return false; |
| 4972 | } |
| 4973 | |
| 4974 | boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4975 | boolean writeMet = (modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4976 | try { |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4977 | // check if target holds top-level <provider> permissions |
| 4978 | if (!readMet && pi.readPermission != null |
| 4979 | && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) { |
| 4980 | readMet = true; |
| 4981 | } |
| 4982 | if (!writeMet && pi.writePermission != null |
| 4983 | && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) { |
| 4984 | writeMet = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4985 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4986 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4987 | // track if unprotected read/write is allowed; any denied |
| 4988 | // <path-permission> below removes this ability |
| 4989 | boolean allowDefaultRead = pi.readPermission == null; |
| 4990 | boolean allowDefaultWrite = pi.writePermission == null; |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4991 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4992 | // check if target holds any <path-permission> that match uri |
| 4993 | final PathPermission[] pps = pi.pathPermissions; |
| 4994 | if (pps != null) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4995 | final String path = uri.getPath(); |
| 4996 | int i = pps.length; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4997 | while (i > 0 && (!readMet || !writeMet)) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4998 | i--; |
| 4999 | PathPermission pp = pps[i]; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5000 | if (pp.match(path)) { |
| 5001 | if (!readMet) { |
| 5002 | final String pprperm = pp.getReadPermission(); |
| 5003 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 5004 | + pprperm + " for " + pp.getPath() |
| 5005 | + ": match=" + pp.match(path) |
| 5006 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 5007 | if (pprperm != null) { |
| 5008 | if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) { |
| 5009 | readMet = true; |
| 5010 | } else { |
| 5011 | allowDefaultRead = false; |
| 5012 | } |
| 5013 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5014 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5015 | if (!writeMet) { |
| 5016 | final String ppwperm = pp.getWritePermission(); |
| 5017 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 5018 | + ppwperm + " for " + pp.getPath() |
| 5019 | + ": match=" + pp.match(path) |
| 5020 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 5021 | if (ppwperm != null) { |
| 5022 | if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) { |
| 5023 | writeMet = true; |
| 5024 | } else { |
| 5025 | allowDefaultWrite = false; |
| 5026 | } |
| 5027 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5028 | } |
| 5029 | } |
| 5030 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5031 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5032 | |
| 5033 | // grant unprotected <provider> read/write, if not blocked by |
| 5034 | // <path-permission> above |
| 5035 | if (allowDefaultRead) readMet = true; |
| 5036 | if (allowDefaultWrite) writeMet = true; |
| 5037 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5038 | } catch (RemoteException e) { |
| 5039 | return false; |
| 5040 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5041 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5042 | return readMet && writeMet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 5046 | int modeFlags) { |
| 5047 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5048 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5049 | return true; |
| 5050 | } |
| 5051 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 5052 | if (perms == null) return false; |
| 5053 | UriPermission perm = perms.get(uri); |
| 5054 | if (perm == null) return false; |
| 5055 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 5056 | } |
| 5057 | |
| 5058 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5059 | enforceNotIsolatedCaller("checkUriPermission"); |
| 5060 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5061 | // Another redirected-binder-call permissions check as in |
| 5062 | // {@link checkComponentPermission}. |
| 5063 | Identity tlsIdentity = sCallerIdentity.get(); |
| 5064 | if (tlsIdentity != null) { |
| 5065 | uid = tlsIdentity.uid; |
| 5066 | pid = tlsIdentity.pid; |
| 5067 | } |
| 5068 | |
| 5069 | // Our own process gets to do everything. |
| 5070 | if (pid == MY_PID) { |
| 5071 | return PackageManager.PERMISSION_GRANTED; |
| 5072 | } |
| 5073 | synchronized(this) { |
| 5074 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 5075 | ? PackageManager.PERMISSION_GRANTED |
| 5076 | : PackageManager.PERMISSION_DENIED; |
| 5077 | } |
| 5078 | } |
| 5079 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5080 | /** |
| 5081 | * Check if the targetPkg can be granted permission to access uri by |
| 5082 | * the callingUid using the given modeFlags. Throws a security exception |
| 5083 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 5084 | * if the URI permission grant should be performed; returns -1 if it is not |
| 5085 | * needed (for example targetPkg already has permission to access the URI). |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5086 | * If you already know the uid of the target, you can supply it in |
| 5087 | * lastTargetUid else set that to -1. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5088 | */ |
| 5089 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5090 | Uri uri, int modeFlags, int lastTargetUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5091 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5092 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5093 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5094 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5095 | } |
| 5096 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5097 | if (targetPkg != null) { |
| 5098 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5099 | "Checking grant " + targetPkg + " permission to " + uri); |
| 5100 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5101 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5102 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5103 | |
| 5104 | // If this is not a content: uri, we can't do anything with it. |
| 5105 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5106 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5107 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5108 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | String name = uri.getAuthority(); |
| 5112 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5113 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5114 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5115 | if (cpr != null) { |
| 5116 | pi = cpr.info; |
| 5117 | } else { |
| 5118 | try { |
| 5119 | pi = pm.resolveContentProvider(name, |
Dianne Hackborn | 11f0cb7 | 2012-09-16 17:12:34 -0700 | [diff] [blame] | 5120 | PackageManager.GET_URI_PERMISSION_PATTERNS, |
| 5121 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5122 | } catch (RemoteException ex) { |
| 5123 | } |
| 5124 | } |
| 5125 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5126 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5127 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5128 | } |
| 5129 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5130 | int targetUid = lastTargetUid; |
| 5131 | if (targetUid < 0 && targetPkg != null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5132 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5133 | targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5134 | if (targetUid < 0) { |
| 5135 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5136 | "Can't grant URI permission no uid for: " + targetPkg); |
| 5137 | return -1; |
| 5138 | } |
| 5139 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5140 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5141 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5142 | } |
| 5143 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5144 | if (targetUid >= 0) { |
| 5145 | // First... does the target actually need this permission? |
| 5146 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 5147 | // No need to grant the target this permission. |
| 5148 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5149 | "Target " + targetPkg + " already has full permission to " + uri); |
| 5150 | return -1; |
| 5151 | } |
| 5152 | } else { |
| 5153 | // First... there is no target package, so can anyone access it? |
| 5154 | boolean allowed = pi.exported; |
| 5155 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5156 | if (pi.readPermission != null) { |
| 5157 | allowed = false; |
| 5158 | } |
| 5159 | } |
| 5160 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5161 | if (pi.writePermission != null) { |
| 5162 | allowed = false; |
| 5163 | } |
| 5164 | } |
| 5165 | if (allowed) { |
| 5166 | return -1; |
| 5167 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5168 | } |
| 5169 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5170 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5171 | if (!pi.grantUriPermissions) { |
| 5172 | throw new SecurityException("Provider " + pi.packageName |
| 5173 | + "/" + pi.name |
| 5174 | + " does not allow granting of Uri permissions (uri " |
| 5175 | + uri + ")"); |
| 5176 | } |
| 5177 | if (pi.uriPermissionPatterns != null) { |
| 5178 | final int N = pi.uriPermissionPatterns.length; |
| 5179 | boolean allowed = false; |
| 5180 | for (int i=0; i<N; i++) { |
| 5181 | if (pi.uriPermissionPatterns[i] != null |
| 5182 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 5183 | allowed = true; |
| 5184 | break; |
| 5185 | } |
| 5186 | } |
| 5187 | if (!allowed) { |
| 5188 | throw new SecurityException("Provider " + pi.packageName |
| 5189 | + "/" + pi.name |
| 5190 | + " does not allow granting of permission to path of Uri " |
| 5191 | + uri); |
| 5192 | } |
| 5193 | } |
| 5194 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5195 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5196 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5197 | if (callingUid != Process.myUid()) { |
| 5198 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 5199 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5200 | throw new SecurityException("Uid " + callingUid |
| 5201 | + " does not have permission to uri " + uri); |
| 5202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5203 | } |
| 5204 | } |
| 5205 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5206 | return targetUid; |
| 5207 | } |
| 5208 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5209 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 5210 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5211 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5212 | synchronized(this) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5213 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5214 | } |
| 5215 | } |
| 5216 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5217 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 5218 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 5219 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5220 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5221 | if (modeFlags == 0) { |
| 5222 | return; |
| 5223 | } |
| 5224 | |
| 5225 | // So here we are: the caller has the assumed permission |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5226 | // to the uri, and the target doesn't. Let's now give this to |
| 5227 | // the target. |
| 5228 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5229 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5230 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5232 | HashMap<Uri, UriPermission> targetUris |
| 5233 | = mGrantedUriPermissions.get(targetUid); |
| 5234 | if (targetUris == null) { |
| 5235 | targetUris = new HashMap<Uri, UriPermission>(); |
| 5236 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 5237 | } |
| 5238 | |
| 5239 | UriPermission perm = targetUris.get(uri); |
| 5240 | if (perm == null) { |
| 5241 | perm = new UriPermission(targetUid, uri); |
| 5242 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5243 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5245 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5246 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5247 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 5248 | } else { |
| 5249 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5250 | perm.readOwners.add(owner); |
| 5251 | owner.addReadPermission(perm); |
| 5252 | } |
| 5253 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5254 | perm.writeOwners.add(owner); |
| 5255 | owner.addWritePermission(perm); |
| 5256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5257 | } |
| 5258 | } |
| 5259 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5260 | void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri, |
| 5261 | int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5262 | if (targetPkg == null) { |
| 5263 | throw new NullPointerException("targetPkg"); |
| 5264 | } |
| 5265 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5266 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5267 | if (targetUid < 0) { |
| 5268 | return; |
| 5269 | } |
| 5270 | |
| 5271 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 5272 | } |
| 5273 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5274 | static class NeededUriGrants extends ArrayList<Uri> { |
| 5275 | final String targetPkg; |
| 5276 | final int targetUid; |
| 5277 | final int flags; |
| 5278 | |
| 5279 | NeededUriGrants(String _targetPkg, int _targetUid, int _flags) { |
| 5280 | targetPkg = _targetPkg; |
| 5281 | targetUid = _targetUid; |
| 5282 | flags = _flags; |
| 5283 | } |
| 5284 | } |
| 5285 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5286 | /** |
| 5287 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 5288 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5289 | NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 5290 | String targetPkg, Intent intent, int mode, NeededUriGrants needed) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5291 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5292 | "Checking URI perm to data=" + (intent != null ? intent.getData() : null) |
| 5293 | + " clip=" + (intent != null ? intent.getClipData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5294 | + " from " + intent + "; flags=0x" |
| 5295 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 5296 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5297 | if (targetPkg == null) { |
| 5298 | throw new NullPointerException("targetPkg"); |
| 5299 | } |
| 5300 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5301 | if (intent == null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5302 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5303 | } |
| 5304 | Uri data = intent.getData(); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5305 | ClipData clip = intent.getClipData(); |
| 5306 | if (data == null && clip == null) { |
| 5307 | return null; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5308 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5309 | if (data != null) { |
| 5310 | int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 5311 | mode, needed != null ? needed.targetUid : -1); |
| 5312 | if (target > 0) { |
| 5313 | if (needed == null) { |
| 5314 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5315 | } |
| 5316 | needed.add(data); |
| 5317 | } |
| 5318 | } |
| 5319 | if (clip != null) { |
| 5320 | for (int i=0; i<clip.getItemCount(); i++) { |
| 5321 | Uri uri = clip.getItemAt(i).getUri(); |
| 5322 | if (uri != null) { |
| 5323 | int target = -1; |
| 5324 | target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, |
| 5325 | mode, needed != null ? needed.targetUid : -1); |
| 5326 | if (target > 0) { |
| 5327 | if (needed == null) { |
| 5328 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5329 | } |
| 5330 | needed.add(uri); |
| 5331 | } |
| 5332 | } else { |
| 5333 | Intent clipIntent = clip.getItemAt(i).getIntent(); |
| 5334 | if (clipIntent != null) { |
| 5335 | NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked( |
| 5336 | callingUid, targetPkg, clipIntent, mode, needed); |
| 5337 | if (newNeeded != null) { |
| 5338 | needed = newNeeded; |
| 5339 | } |
| 5340 | } |
| 5341 | } |
| 5342 | } |
| 5343 | } |
| 5344 | |
| 5345 | return needed; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | /** |
| 5349 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 5350 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5351 | void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed, |
| 5352 | UriPermissionOwner owner) { |
| 5353 | if (needed != null) { |
| 5354 | for (int i=0; i<needed.size(); i++) { |
| 5355 | grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg, |
| 5356 | needed.get(i), needed.flags, owner); |
| 5357 | } |
| 5358 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 5362 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5363 | NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, |
Dianne Hackborn | d9781fe | 2012-03-08 18:04:18 -0800 | [diff] [blame] | 5364 | intent, intent != null ? intent.getFlags() : 0, null); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5365 | if (needed == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5366 | return; |
| 5367 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5368 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5369 | grantUriPermissionUncheckedFromIntentLocked(needed, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 5373 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5374 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5375 | synchronized(this) { |
| 5376 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5377 | if (r == null) { |
| 5378 | throw new SecurityException("Unable to find app for caller " |
| 5379 | + caller |
| 5380 | + " when granting permission to uri " + uri); |
| 5381 | } |
| 5382 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5383 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5384 | } |
| 5385 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5386 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5389 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5390 | null); |
| 5391 | } |
| 5392 | } |
| 5393 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5394 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5395 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5396 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 5397 | HashMap<Uri, UriPermission> perms |
| 5398 | = mGrantedUriPermissions.get(perm.uid); |
| 5399 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5400 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5401 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5402 | perms.remove(perm.uri); |
| 5403 | if (perms.size() == 0) { |
| 5404 | mGrantedUriPermissions.remove(perm.uid); |
| 5405 | } |
| 5406 | } |
| 5407 | } |
| 5408 | } |
| 5409 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5410 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 5411 | int modeFlags) { |
| 5412 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5413 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5414 | if (modeFlags == 0) { |
| 5415 | return; |
| 5416 | } |
| 5417 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5418 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5419 | "Revoking all granted permissions to " + uri); |
| 5420 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5421 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5422 | |
| 5423 | final String authority = uri.getAuthority(); |
| 5424 | ProviderInfo pi = null; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5425 | int userId = UserHandle.getUserId(callingUid); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5426 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5427 | if (cpr != null) { |
| 5428 | pi = cpr.info; |
| 5429 | } else { |
| 5430 | try { |
| 5431 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5432 | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5433 | } catch (RemoteException ex) { |
| 5434 | } |
| 5435 | } |
| 5436 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5437 | Slog.w(TAG, "No content provider found for permission revoke: " + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5438 | return; |
| 5439 | } |
| 5440 | |
| 5441 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5442 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5443 | // Right now, if you are not the original owner of the permission, |
| 5444 | // you are not allowed to revoke it. |
| 5445 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5446 | throw new SecurityException("Uid " + callingUid |
| 5447 | + " does not have permission to uri " + uri); |
| 5448 | //} |
| 5449 | } |
| 5450 | |
| 5451 | // Go through all of the permissions and remove any that match. |
| 5452 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 5453 | if (SEGMENTS != null) { |
| 5454 | final int NS = SEGMENTS.size(); |
| 5455 | int N = mGrantedUriPermissions.size(); |
| 5456 | for (int i=0; i<N; i++) { |
| 5457 | HashMap<Uri, UriPermission> perms |
| 5458 | = mGrantedUriPermissions.valueAt(i); |
| 5459 | Iterator<UriPermission> it = perms.values().iterator(); |
| 5460 | toploop: |
| 5461 | while (it.hasNext()) { |
| 5462 | UriPermission perm = it.next(); |
| 5463 | Uri targetUri = perm.uri; |
| 5464 | if (!authority.equals(targetUri.getAuthority())) { |
| 5465 | continue; |
| 5466 | } |
| 5467 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5468 | if (targetSegments == null) { |
| 5469 | continue; |
| 5470 | } |
| 5471 | if (targetSegments.size() < NS) { |
| 5472 | continue; |
| 5473 | } |
| 5474 | for (int j=0; j<NS; j++) { |
| 5475 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5476 | continue toploop; |
| 5477 | } |
| 5478 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5479 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5480 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5481 | perm.clearModes(modeFlags); |
| 5482 | if (perm.modeFlags == 0) { |
| 5483 | it.remove(); |
| 5484 | } |
| 5485 | } |
| 5486 | if (perms.size() == 0) { |
| 5487 | mGrantedUriPermissions.remove( |
| 5488 | mGrantedUriPermissions.keyAt(i)); |
| 5489 | N--; |
| 5490 | i--; |
| 5491 | } |
| 5492 | } |
| 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5497 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5498 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5499 | synchronized(this) { |
| 5500 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5501 | if (r == null) { |
| 5502 | throw new SecurityException("Unable to find app for caller " |
| 5503 | + caller |
| 5504 | + " when revoking permission to uri " + uri); |
| 5505 | } |
| 5506 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5507 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5508 | return; |
| 5509 | } |
| 5510 | |
| 5511 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5512 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5513 | if (modeFlags == 0) { |
| 5514 | return; |
| 5515 | } |
| 5516 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5517 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5518 | |
| 5519 | final String authority = uri.getAuthority(); |
| 5520 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5521 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5522 | if (cpr != null) { |
| 5523 | pi = cpr.info; |
| 5524 | } else { |
| 5525 | try { |
| 5526 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5527 | PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5528 | } catch (RemoteException ex) { |
| 5529 | } |
| 5530 | } |
| 5531 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5532 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5533 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5534 | return; |
| 5535 | } |
| 5536 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5537 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5538 | } |
| 5539 | } |
| 5540 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5541 | @Override |
| 5542 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5543 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5544 | synchronized(this) { |
| 5545 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5546 | return owner.getExternalTokenLocked(); |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | @Override |
| 5551 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5552 | Uri uri, int modeFlags) { |
| 5553 | synchronized(this) { |
| 5554 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5555 | if (owner == null) { |
| 5556 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5557 | } |
| 5558 | if (fromUid != Binder.getCallingUid()) { |
| 5559 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5560 | // Only system code can grant URI permissions on behalf |
| 5561 | // of other users. |
| 5562 | throw new SecurityException("nice try"); |
| 5563 | } |
| 5564 | } |
| 5565 | if (targetPkg == null) { |
| 5566 | throw new IllegalArgumentException("null target"); |
| 5567 | } |
| 5568 | if (uri == null) { |
| 5569 | throw new IllegalArgumentException("null uri"); |
| 5570 | } |
| 5571 | |
| 5572 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5573 | } |
| 5574 | } |
| 5575 | |
| 5576 | @Override |
| 5577 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5578 | synchronized(this) { |
| 5579 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5580 | if (owner == null) { |
| 5581 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5582 | } |
| 5583 | |
| 5584 | if (uri == null) { |
| 5585 | owner.removeUriPermissionsLocked(mode); |
| 5586 | } else { |
| 5587 | owner.removeUriPermissionLocked(uri, mode); |
| 5588 | } |
| 5589 | } |
| 5590 | } |
| 5591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5592 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5593 | synchronized (this) { |
| 5594 | ProcessRecord app = |
| 5595 | who != null ? getRecordForAppLocked(who) : null; |
| 5596 | if (app == null) return; |
| 5597 | |
| 5598 | Message msg = Message.obtain(); |
| 5599 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5600 | msg.obj = app; |
| 5601 | msg.arg1 = waiting ? 1 : 0; |
| 5602 | mHandler.sendMessage(msg); |
| 5603 | } |
| 5604 | } |
| 5605 | |
| 5606 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5607 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5608 | final long hiddenAppMem = mProcessList.getMemLevel(ProcessList.HIDDEN_APP_MIN_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5609 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 5610 | outInfo.totalMem = Process.getTotalMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5611 | outInfo.threshold = homeAppMem; |
| 5612 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5613 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5614 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5615 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5616 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5617 | ProcessList.VISIBLE_APP_ADJ); |
| 5618 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5619 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | // ========================================================= |
| 5623 | // TASK MANAGEMENT |
| 5624 | // ========================================================= |
| 5625 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5626 | @Override |
| 5627 | public List<RunningTaskInfo> getTasks(int maxNum, int flags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5628 | IThumbnailReceiver receiver) { |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5629 | ArrayList<RunningTaskInfo> list = new ArrayList<RunningTaskInfo>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5630 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5631 | PendingThumbnailsRecord pending = new PendingThumbnailsRecord(receiver); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5632 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5633 | |
| 5634 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5635 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5636 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5637 | + ", receiver=" + receiver); |
| 5638 | |
| 5639 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5640 | != PackageManager.PERMISSION_GRANTED) { |
| 5641 | if (receiver != null) { |
| 5642 | // If the caller wants to wait for pending thumbnails, |
| 5643 | // it ain't gonna get them. |
| 5644 | try { |
| 5645 | receiver.finished(); |
| 5646 | } catch (RemoteException ex) { |
| 5647 | } |
| 5648 | } |
| 5649 | String msg = "Permission Denial: getTasks() from pid=" |
| 5650 | + Binder.getCallingPid() |
| 5651 | + ", uid=" + Binder.getCallingUid() |
| 5652 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5653 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5654 | throw new SecurityException(msg); |
| 5655 | } |
| 5656 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5657 | topRecord = mMainStack.getTasksLocked(maxNum, receiver, pending, list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5658 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5659 | if (!pending.pendingRecords.isEmpty()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5660 | mPendingThumbnails.add(pending); |
| 5661 | } |
| 5662 | } |
| 5663 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5664 | 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] | 5665 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5666 | if (topRecord != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5667 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5668 | try { |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5669 | IApplicationThread topThumbnail = topRecord.app.thread; |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5670 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5671 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5672 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5673 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5674 | } |
| 5675 | } |
| 5676 | |
| 5677 | if (pending == null && receiver != null) { |
| 5678 | // In this case all thumbnails were available and the client |
| 5679 | // is being asked to be told when the remaining ones come in... |
| 5680 | // which is unusually, since the top-most currently running |
| 5681 | // activity should never have a canned thumbnail! Oh well. |
| 5682 | try { |
| 5683 | receiver.finished(); |
| 5684 | } catch (RemoteException ex) { |
| 5685 | } |
| 5686 | } |
| 5687 | |
| 5688 | return list; |
| 5689 | } |
| 5690 | |
| 5691 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5692 | int flags, int userId) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 5693 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 5694 | false, true, "getRecentTasks", null); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5695 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5696 | synchronized (this) { |
| 5697 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5698 | "getRecentTasks()"); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5699 | final boolean detailed = checkCallingPermission( |
| 5700 | android.Manifest.permission.GET_DETAILED_TASKS) |
| 5701 | == PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5702 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5703 | IPackageManager pm = AppGlobals.getPackageManager(); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5705 | final int N = mRecentTasks.size(); |
| 5706 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5707 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5708 | maxNum < N ? maxNum : N); |
| 5709 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5710 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5711 | // Only add calling user's recent tasks |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5712 | if (tr.userId != userId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5713 | // Return the entry if desired by the caller. We always return |
| 5714 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5715 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5716 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5717 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5718 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5719 | if (i == 0 |
| 5720 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5721 | || (tr.intent == null) |
| 5722 | || ((tr.intent.getFlags() |
| 5723 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5724 | ActivityManager.RecentTaskInfo rti |
| 5725 | = new ActivityManager.RecentTaskInfo(); |
| 5726 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5727 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5728 | rti.baseIntent = new Intent( |
| 5729 | tr.intent != null ? tr.intent : tr.affinityIntent); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5730 | if (!detailed) { |
| 5731 | rti.baseIntent.replaceExtras((Bundle)null); |
| 5732 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5733 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5734 | rti.description = tr.lastDescription; |
| 5735 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5736 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5737 | // Check whether this activity is currently available. |
| 5738 | try { |
| 5739 | if (rti.origActivity != null) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5740 | if (pm.getActivityInfo(rti.origActivity, 0, userId) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5741 | == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5742 | continue; |
| 5743 | } |
| 5744 | } else if (rti.baseIntent != null) { |
| 5745 | if (pm.queryIntentActivities(rti.baseIntent, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5746 | null, 0, userId) == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5747 | continue; |
| 5748 | } |
| 5749 | } |
| 5750 | } catch (RemoteException e) { |
| 5751 | // Will never happen. |
| 5752 | } |
| 5753 | } |
| 5754 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5755 | res.add(rti); |
| 5756 | maxNum--; |
| 5757 | } |
| 5758 | } |
| 5759 | return res; |
| 5760 | } |
| 5761 | } |
| 5762 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5763 | private TaskRecord taskForIdLocked(int id) { |
| 5764 | final int N = mRecentTasks.size(); |
| 5765 | for (int i=0; i<N; i++) { |
| 5766 | TaskRecord tr = mRecentTasks.get(i); |
| 5767 | if (tr.taskId == id) { |
| 5768 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5769 | } |
| 5770 | } |
| 5771 | return null; |
| 5772 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5773 | |
| 5774 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5775 | synchronized (this) { |
| 5776 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5777 | "getTaskThumbnails()"); |
| 5778 | TaskRecord tr = taskForIdLocked(id); |
| 5779 | if (tr != null) { |
| 5780 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5781 | } |
| 5782 | } |
| 5783 | return null; |
| 5784 | } |
| 5785 | |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 5786 | public Bitmap getTaskTopThumbnail(int id) { |
| 5787 | synchronized (this) { |
| 5788 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5789 | "getTaskTopThumbnail()"); |
| 5790 | TaskRecord tr = taskForIdLocked(id); |
| 5791 | if (tr != null) { |
| 5792 | return mMainStack.getTaskTopThumbnailLocked(tr); |
| 5793 | } |
| 5794 | } |
| 5795 | return null; |
| 5796 | } |
| 5797 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5798 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5799 | synchronized (this) { |
| 5800 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5801 | "removeSubTask()"); |
| 5802 | long ident = Binder.clearCallingIdentity(); |
| 5803 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5804 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex, |
| 5805 | true) != null; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5806 | } finally { |
| 5807 | Binder.restoreCallingIdentity(ident); |
| 5808 | } |
| 5809 | } |
| 5810 | } |
| 5811 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5812 | private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) { |
| 5813 | final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5814 | Intent baseIntent = new Intent( |
| 5815 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5816 | ComponentName component = baseIntent.getComponent(); |
| 5817 | if (component == null) { |
| 5818 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5819 | return; |
| 5820 | } |
| 5821 | |
| 5822 | // Find any running services associated with this app. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 5823 | mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5824 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5825 | if (killProcesses) { |
| 5826 | // Find any running processes associated with this app. |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5827 | final String pkg = component.getPackageName(); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5828 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5829 | HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap(); |
| 5830 | for (SparseArray<ProcessRecord> uids : pmap.values()) { |
| 5831 | for (int i=0; i<uids.size(); i++) { |
| 5832 | ProcessRecord proc = uids.valueAt(i); |
| 5833 | if (proc.userId != tr.userId) { |
| 5834 | continue; |
| 5835 | } |
| 5836 | if (!proc.pkgList.contains(pkg)) { |
| 5837 | continue; |
| 5838 | } |
| 5839 | procs.add(proc); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5840 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5841 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5842 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5843 | // Kill the running processes. |
| 5844 | for (int i=0; i<procs.size(); i++) { |
| 5845 | ProcessRecord pr = procs.get(i); |
| 5846 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5847 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 5848 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.userId, pr.pid, |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5849 | pr.processName, pr.setAdj, "remove task"); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5850 | pr.killedBackground = true; |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5851 | Process.killProcessQuiet(pr.pid); |
| 5852 | } else { |
| 5853 | pr.waitingToKill = "remove task"; |
| 5854 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5855 | } |
| 5856 | } |
| 5857 | } |
| 5858 | |
| 5859 | public boolean removeTask(int taskId, int flags) { |
| 5860 | synchronized (this) { |
| 5861 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5862 | "removeTask()"); |
| 5863 | long ident = Binder.clearCallingIdentity(); |
| 5864 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5865 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1, |
| 5866 | false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5867 | if (r != null) { |
| 5868 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5869 | cleanUpRemovedTaskLocked(r.task, flags); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5870 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5871 | } else { |
| 5872 | TaskRecord tr = null; |
| 5873 | int i=0; |
| 5874 | while (i < mRecentTasks.size()) { |
| 5875 | TaskRecord t = mRecentTasks.get(i); |
| 5876 | if (t.taskId == taskId) { |
| 5877 | tr = t; |
| 5878 | break; |
| 5879 | } |
| 5880 | i++; |
| 5881 | } |
| 5882 | if (tr != null) { |
| 5883 | if (tr.numActivities <= 0) { |
| 5884 | // Caller is just removing a recent task that is |
| 5885 | // not actively running. That is easy! |
| 5886 | mRecentTasks.remove(i); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5887 | cleanUpRemovedTaskLocked(tr, flags); |
| 5888 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5889 | } else { |
| 5890 | Slog.w(TAG, "removeTask: task " + taskId |
| 5891 | + " does not have activities to remove, " |
| 5892 | + " but numActivities=" + tr.numActivities |
| 5893 | + ": " + tr); |
| 5894 | } |
| 5895 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5896 | } |
| 5897 | } finally { |
| 5898 | Binder.restoreCallingIdentity(ident); |
| 5899 | } |
| 5900 | } |
| 5901 | return false; |
| 5902 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5903 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5904 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5905 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5906 | */ |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5907 | public void moveTaskToFront(int task, int flags, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5908 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5909 | "moveTaskToFront()"); |
| 5910 | |
| 5911 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5912 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5913 | Binder.getCallingUid(), "Task to front")) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5914 | ActivityOptions.abort(options); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5915 | return; |
| 5916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5917 | final long origId = Binder.clearCallingIdentity(); |
| 5918 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5919 | TaskRecord tr = taskForIdLocked(task); |
| 5920 | if (tr != null) { |
| 5921 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5922 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5923 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5924 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5925 | // Caller wants the home activity moved with it. To accomplish this, |
| 5926 | // we'll just move the home task to the top first. |
| 5927 | mMainStack.moveHomeToFrontLocked(); |
| 5928 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5929 | mMainStack.moveTaskToFrontLocked(tr, null, options); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5930 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5931 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5932 | if (mMainStack.findTaskToMoveToFrontLocked(task, flags, options)) { |
| 5933 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5934 | } |
| 5935 | } finally { |
| 5936 | Binder.restoreCallingIdentity(origId); |
| 5937 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5938 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5939 | } |
| 5940 | } |
| 5941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5942 | public void moveTaskToBack(int task) { |
| 5943 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5944 | "moveTaskToBack()"); |
| 5945 | |
| 5946 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5947 | if (mMainStack.mResumedActivity != null |
| 5948 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5949 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5950 | Binder.getCallingUid(), "Task to back")) { |
| 5951 | return; |
| 5952 | } |
| 5953 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5954 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5955 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5956 | Binder.restoreCallingIdentity(origId); |
| 5957 | } |
| 5958 | } |
| 5959 | |
| 5960 | /** |
| 5961 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5962 | * of the history stack. The activity's order within the task is unchanged. |
| 5963 | * |
| 5964 | * @param token A reference to the activity we wish to move |
| 5965 | * @param nonRoot If false then this only works if the activity is the root |
| 5966 | * of a task; if true it will work for any activity in a task. |
| 5967 | * @return Returns true if the move completed, false if not. |
| 5968 | */ |
| 5969 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5970 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5971 | synchronized(this) { |
| 5972 | final long origId = Binder.clearCallingIdentity(); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 5973 | int taskId = mMainStack.getTaskForActivityLocked(token, !nonRoot); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5974 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5975 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5976 | } |
| 5977 | Binder.restoreCallingIdentity(origId); |
| 5978 | } |
| 5979 | return false; |
| 5980 | } |
| 5981 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5982 | public void moveTaskBackwards(int task) { |
| 5983 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5984 | "moveTaskBackwards()"); |
| 5985 | |
| 5986 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5987 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5988 | Binder.getCallingUid(), "Task backwards")) { |
| 5989 | return; |
| 5990 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5991 | final long origId = Binder.clearCallingIdentity(); |
| 5992 | moveTaskBackwardsLocked(task); |
| 5993 | Binder.restoreCallingIdentity(origId); |
| 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5998 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5999 | } |
| 6000 | |
| 6001 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 6002 | synchronized(this) { |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 6003 | return mMainStack.getTaskForActivityLocked(token, onlyRoot); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6004 | } |
| 6005 | } |
| 6006 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6007 | // ========================================================= |
| 6008 | // THUMBNAILS |
| 6009 | // ========================================================= |
| 6010 | |
| 6011 | public void reportThumbnail(IBinder token, |
| 6012 | Bitmap thumbnail, CharSequence description) { |
| 6013 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 6014 | final long origId = Binder.clearCallingIdentity(); |
| 6015 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 6016 | Binder.restoreCallingIdentity(origId); |
| 6017 | } |
| 6018 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6019 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6020 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 6021 | TaskRecord task = null; |
| 6022 | ArrayList receivers = null; |
| 6023 | |
| 6024 | //System.out.println("Send pending thumbnail: " + r); |
| 6025 | |
| 6026 | synchronized(this) { |
| 6027 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6028 | r = mMainStack.isInStackLocked(token); |
| 6029 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6030 | return; |
| 6031 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6032 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 6033 | if (thumbnail == null && r.thumbHolder != null) { |
| 6034 | thumbnail = r.thumbHolder.lastThumbnail; |
| 6035 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6036 | } |
| 6037 | if (thumbnail == null && !always) { |
| 6038 | // If there is no thumbnail, and this entry is not actually |
| 6039 | // going away, then abort for now and pick up the next |
| 6040 | // thumbnail we get. |
| 6041 | return; |
| 6042 | } |
| 6043 | task = r.task; |
| 6044 | |
| 6045 | int N = mPendingThumbnails.size(); |
| 6046 | int i=0; |
| 6047 | while (i<N) { |
| 6048 | PendingThumbnailsRecord pr = |
| 6049 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 6050 | //System.out.println("Looking in " + pr.pendingRecords); |
| 6051 | if (pr.pendingRecords.remove(r)) { |
| 6052 | if (receivers == null) { |
| 6053 | receivers = new ArrayList(); |
| 6054 | } |
| 6055 | receivers.add(pr); |
| 6056 | if (pr.pendingRecords.size() == 0) { |
| 6057 | pr.finished = true; |
| 6058 | mPendingThumbnails.remove(i); |
| 6059 | N--; |
| 6060 | continue; |
| 6061 | } |
| 6062 | } |
| 6063 | i++; |
| 6064 | } |
| 6065 | } |
| 6066 | |
| 6067 | if (receivers != null) { |
| 6068 | final int N = receivers.size(); |
| 6069 | for (int i=0; i<N; i++) { |
| 6070 | try { |
| 6071 | PendingThumbnailsRecord pr = |
| 6072 | (PendingThumbnailsRecord)receivers.get(i); |
| 6073 | pr.receiver.newThumbnail( |
| 6074 | task != null ? task.taskId : -1, thumbnail, description); |
| 6075 | if (pr.finished) { |
| 6076 | pr.receiver.finished(); |
| 6077 | } |
| 6078 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6079 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6080 | } |
| 6081 | } |
| 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | // ========================================================= |
| 6086 | // CONTENT PROVIDERS |
| 6087 | // ========================================================= |
| 6088 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6089 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 6090 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6091 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6092 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6093 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6094 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6095 | } catch (RemoteException ex) { |
| 6096 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6097 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6098 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 6099 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6100 | if (providers != null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6101 | int N = providers.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6102 | for (int i=0; i<N; i++) { |
| 6103 | ProviderInfo cpi = |
| 6104 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6105 | boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6106 | cpi.name, cpi.flags); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6107 | if (singleton && UserHandle.getUserId(app.uid) != 0) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6108 | // This is a singleton provider, but a user besides the |
| 6109 | // default user is asking to initialize a process it runs |
| 6110 | // in... well, no, it doesn't actually run in this process, |
| 6111 | // it runs in the process of the default user. Get rid of it. |
| 6112 | providers.remove(i); |
| 6113 | N--; |
| 6114 | continue; |
| 6115 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6116 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6117 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6118 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6119 | if (cpr == null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6120 | cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6121 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6122 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6123 | if (DEBUG_MU) |
| 6124 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6125 | app.pubProviders.put(cpi.name, cpr); |
| 6126 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 6127 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6128 | } |
| 6129 | } |
| 6130 | return providers; |
| 6131 | } |
| 6132 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 6133 | /** |
| 6134 | * Check if {@link ProcessRecord} has a possible chance at accessing the |
| 6135 | * given {@link ProviderInfo}. Final permission checking is always done |
| 6136 | * in {@link ContentProvider}. |
| 6137 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6138 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6139 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6140 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6141 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6142 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6143 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6144 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6145 | return null; |
| 6146 | } |
| 6147 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6148 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6149 | == PackageManager.PERMISSION_GRANTED) { |
| 6150 | return null; |
| 6151 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6152 | |
| 6153 | PathPermission[] pps = cpi.pathPermissions; |
| 6154 | if (pps != null) { |
| 6155 | int i = pps.length; |
| 6156 | while (i > 0) { |
| 6157 | i--; |
| 6158 | PathPermission pp = pps[i]; |
| 6159 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6160 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6161 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6162 | return null; |
| 6163 | } |
| 6164 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6165 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6166 | == PackageManager.PERMISSION_GRANTED) { |
| 6167 | return null; |
| 6168 | } |
| 6169 | } |
| 6170 | } |
| 6171 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6172 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 6173 | if (perms != null) { |
| 6174 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 6175 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 6176 | return null; |
| 6177 | } |
| 6178 | } |
| 6179 | } |
| 6180 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6181 | String msg; |
| 6182 | if (!cpi.exported) { |
| 6183 | msg = "Permission Denial: opening provider " + cpi.name |
| 6184 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6185 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 6186 | + cpi.applicationInfo.uid; |
| 6187 | } else { |
| 6188 | msg = "Permission Denial: opening provider " + cpi.name |
| 6189 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6190 | + ", uid=" + callingUid + ") requires " |
| 6191 | + cpi.readPermission + " or " + cpi.writePermission; |
| 6192 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6193 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6194 | return msg; |
| 6195 | } |
| 6196 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6197 | ContentProviderConnection incProviderCountLocked(ProcessRecord r, |
| 6198 | final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6199 | if (r != null) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6200 | for (int i=0; i<r.conProviders.size(); i++) { |
| 6201 | ContentProviderConnection conn = r.conProviders.get(i); |
| 6202 | if (conn.provider == cpr) { |
| 6203 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6204 | "Adding provider requested by " |
| 6205 | + r.processName + " from process " |
| 6206 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6207 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6208 | if (stable) { |
| 6209 | conn.stableCount++; |
| 6210 | conn.numStableIncs++; |
| 6211 | } else { |
| 6212 | conn.unstableCount++; |
| 6213 | conn.numUnstableIncs++; |
| 6214 | } |
| 6215 | return conn; |
| 6216 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6217 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6218 | ContentProviderConnection conn = new ContentProviderConnection(cpr, r); |
| 6219 | if (stable) { |
| 6220 | conn.stableCount = 1; |
| 6221 | conn.numStableIncs = 1; |
| 6222 | } else { |
| 6223 | conn.unstableCount = 1; |
| 6224 | conn.numUnstableIncs = 1; |
| 6225 | } |
| 6226 | cpr.connections.add(conn); |
| 6227 | r.conProviders.add(conn); |
| 6228 | return conn; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6229 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6230 | cpr.addExternalProcessHandleLocked(externalProcessToken); |
| 6231 | return null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6232 | } |
| 6233 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6234 | boolean decProviderCountLocked(ContentProviderConnection conn, |
| 6235 | ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
| 6236 | if (conn != null) { |
| 6237 | cpr = conn.provider; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6238 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6239 | "Removing provider requested by " |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6240 | + conn.client.processName + " from process " |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6241 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6242 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6243 | if (stable) { |
| 6244 | conn.stableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6245 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6246 | conn.unstableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6247 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6248 | if (conn.stableCount == 0 && conn.unstableCount == 0) { |
| 6249 | cpr.connections.remove(conn); |
| 6250 | conn.client.conProviders.remove(conn); |
| 6251 | return true; |
| 6252 | } |
| 6253 | return false; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6254 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6255 | cpr.removeExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6256 | return false; |
| 6257 | } |
| 6258 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6259 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6260 | String name, IBinder token, boolean stable, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6261 | ContentProviderRecord cpr; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6262 | ContentProviderConnection conn = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6263 | ProviderInfo cpi = null; |
| 6264 | |
| 6265 | synchronized(this) { |
| 6266 | ProcessRecord r = null; |
| 6267 | if (caller != null) { |
| 6268 | r = getRecordForAppLocked(caller); |
| 6269 | if (r == null) { |
| 6270 | throw new SecurityException( |
| 6271 | "Unable to find app for caller " + caller |
| 6272 | + " (pid=" + Binder.getCallingPid() |
| 6273 | + ") when getting content provider " + name); |
| 6274 | } |
| 6275 | } |
| 6276 | |
| 6277 | // First check if this content provider has been published... |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6278 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6279 | boolean providerRunning = cpr != null; |
| 6280 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6281 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6282 | String msg; |
| 6283 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6284 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | if (r != null && cpr.canRunHere(r)) { |
| 6288 | // This provider has been published or is in the process |
| 6289 | // of being published... but it is also allowed to run |
| 6290 | // in the caller's process, so don't make a connection |
| 6291 | // and just let the caller instantiate its own instance. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6292 | ContentProviderHolder holder = cpr.newHolder(null); |
| 6293 | // don't give caller the provider object, it needs |
| 6294 | // to make its own. |
| 6295 | holder.provider = null; |
| 6296 | return holder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6297 | } |
| 6298 | |
| 6299 | final long origId = Binder.clearCallingIdentity(); |
| 6300 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6301 | // 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] | 6302 | // return it right away. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6303 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6304 | if (conn != null && (conn.stableCount+conn.unstableCount) == 1) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6305 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 6306 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6307 | // make sure to count it as being accessed and thus |
| 6308 | // back up on the LRU list. This is good because |
| 6309 | // content providers are often expensive to start. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6310 | updateLruProcessLocked(cpr.proc, false); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6311 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 6312 | } |
| 6313 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6314 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6315 | if (false) { |
| 6316 | if (cpr.name.flattenToShortString().equals( |
| 6317 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 6318 | Slog.v(TAG, "****************** KILLING " |
| 6319 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6320 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6321 | } |
| 6322 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6323 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6324 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 6325 | // NOTE: there is still a race here where a signal could be |
| 6326 | // pending on the process even though we managed to update its |
| 6327 | // adj level. Not sure what to do about this, but at least |
| 6328 | // the race is now smaller. |
| 6329 | if (!success) { |
| 6330 | // Uh oh... it looks like the provider's process |
| 6331 | // has been killed on us. We need to wait for a new |
| 6332 | // process to be started, and make sure its death |
| 6333 | // doesn't kill our process. |
| 6334 | Slog.i(TAG, |
| 6335 | "Existing provider " + cpr.name.flattenToShortString() |
| 6336 | + " is crashing; detaching " + r); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6337 | boolean lastRef = decProviderCountLocked(conn, cpr, token, stable); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6338 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6339 | if (!lastRef) { |
| 6340 | // This wasn't the last ref our process had on |
| 6341 | // the provider... we have now been killed, bail. |
| 6342 | return null; |
| 6343 | } |
| 6344 | providerRunning = false; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6345 | conn = null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6346 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6347 | } |
| 6348 | |
| 6349 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6350 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6351 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6352 | boolean singleton; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6353 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6354 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6355 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6356 | resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6357 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6358 | } catch (RemoteException ex) { |
| 6359 | } |
| 6360 | if (cpi == null) { |
| 6361 | return null; |
| 6362 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6363 | singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6364 | cpi.name, cpi.flags); |
| 6365 | if (singleton) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 6366 | userId = 0; |
| 6367 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6368 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6369 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6370 | String msg; |
| 6371 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6372 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6373 | } |
| 6374 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6375 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6376 | && !cpi.processName.equals("system")) { |
| 6377 | // If this content provider does not run in the system |
| 6378 | // process, and the system is not yet ready to run other |
| 6379 | // processes, then fail fast instead of hanging. |
| 6380 | throw new IllegalArgumentException( |
| 6381 | "Attempt to launch content provider before system ready"); |
| 6382 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6383 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 6384 | // Make sure that the user who owns this provider is started. If not, |
| 6385 | // we don't want to allow it to run. |
| 6386 | if (mStartedUsers.get(userId) == null) { |
| 6387 | Slog.w(TAG, "Unable to launch app " |
| 6388 | + cpi.applicationInfo.packageName + "/" |
| 6389 | + cpi.applicationInfo.uid + " for provider " |
| 6390 | + name + ": user " + userId + " is stopped"); |
| 6391 | return null; |
| 6392 | } |
| 6393 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6394 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6395 | cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6396 | final boolean firstClass = cpr == null; |
| 6397 | if (firstClass) { |
| 6398 | try { |
| 6399 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6400 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6401 | getApplicationInfo( |
| 6402 | cpi.applicationInfo.packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6403 | STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6404 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6405 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6406 | + cpi.name); |
| 6407 | return null; |
| 6408 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6409 | ai = getAppInfoForUser(ai, userId); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6410 | cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6411 | } catch (RemoteException ex) { |
| 6412 | // pm is in same process, this will never happen. |
| 6413 | } |
| 6414 | } |
| 6415 | |
| 6416 | if (r != null && cpr.canRunHere(r)) { |
| 6417 | // If this is a multiprocess provider, then just return its |
| 6418 | // info and allow the caller to instantiate it. Only do |
| 6419 | // this if the provider is the same user as the caller's |
| 6420 | // process, or can run as root (so can be in any process). |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6421 | return cpr.newHolder(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6422 | } |
| 6423 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6424 | if (DEBUG_PROVIDER) { |
| 6425 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6426 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6427 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | // This is single process, and our app is now connecting to it. |
| 6431 | // See if we are already in the process of launching this |
| 6432 | // provider. |
| 6433 | final int N = mLaunchingProviders.size(); |
| 6434 | int i; |
| 6435 | for (i=0; i<N; i++) { |
| 6436 | if (mLaunchingProviders.get(i) == cpr) { |
| 6437 | break; |
| 6438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | // If the provider is not already being launched, then get it |
| 6442 | // started. |
| 6443 | if (i >= N) { |
| 6444 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6445 | |
| 6446 | try { |
| 6447 | // Content provider is now in use, its package can't be stopped. |
| 6448 | try { |
| 6449 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6450 | cpr.appInfo.packageName, false, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6451 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6452 | } catch (IllegalArgumentException e) { |
| 6453 | Slog.w(TAG, "Failed trying to unstop package " |
| 6454 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6455 | } |
| 6456 | |
| 6457 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6458 | cpr.appInfo, false, 0, "content provider", |
| 6459 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6460 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6461 | if (proc == null) { |
| 6462 | Slog.w(TAG, "Unable to launch app " |
| 6463 | + cpi.applicationInfo.packageName + "/" |
| 6464 | + cpi.applicationInfo.uid + " for provider " |
| 6465 | + name + ": process is bad"); |
| 6466 | return null; |
| 6467 | } |
| 6468 | cpr.launchingApp = proc; |
| 6469 | mLaunchingProviders.add(cpr); |
| 6470 | } finally { |
| 6471 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6472 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6473 | } |
| 6474 | |
| 6475 | // Make sure the provider is published (the same provider class |
| 6476 | // may be published under multiple names). |
| 6477 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6478 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6479 | } |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6480 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6481 | mProviderMap.putProviderByName(name, cpr); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6482 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6483 | if (conn != null) { |
| 6484 | conn.waiting = true; |
| 6485 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6486 | } |
| 6487 | } |
| 6488 | |
| 6489 | // Wait for the provider to be published... |
| 6490 | synchronized (cpr) { |
| 6491 | while (cpr.provider == null) { |
| 6492 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6493 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6494 | + cpi.applicationInfo.packageName + "/" |
| 6495 | + cpi.applicationInfo.uid + " for provider " |
| 6496 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6497 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6498 | UserHandle.getUserId(cpi.applicationInfo.uid), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6499 | cpi.applicationInfo.packageName, |
| 6500 | cpi.applicationInfo.uid, name); |
| 6501 | return null; |
| 6502 | } |
| 6503 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6504 | if (DEBUG_MU) { |
| 6505 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6506 | + cpr.launchingApp); |
| 6507 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6508 | if (conn != null) { |
| 6509 | conn.waiting = true; |
| 6510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6511 | cpr.wait(); |
| 6512 | } catch (InterruptedException ex) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6513 | } finally { |
| 6514 | if (conn != null) { |
| 6515 | conn.waiting = false; |
| 6516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6517 | } |
| 6518 | } |
| 6519 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6520 | return cpr != null ? cpr.newHolder(conn) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6521 | } |
| 6522 | |
| 6523 | public final ContentProviderHolder getContentProvider( |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6524 | IApplicationThread caller, String name, int userId, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6525 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6526 | if (caller == null) { |
| 6527 | String msg = "null IApplicationThread when getting content provider " |
| 6528 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6529 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6530 | throw new SecurityException(msg); |
| 6531 | } |
| 6532 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6533 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6534 | false, true, "getContentProvider", null); |
| 6535 | return getContentProviderImpl(caller, name, null, stable, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6536 | } |
| 6537 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6538 | public ContentProviderHolder getContentProviderExternal( |
| 6539 | String name, int userId, IBinder token) { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6540 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6541 | "Do not have permission in call getContentProviderExternal()"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6542 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6543 | false, true, "getContentProvider", null); |
| 6544 | return getContentProviderExternalUnchecked(name, token, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6545 | } |
| 6546 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6547 | private ContentProviderHolder getContentProviderExternalUnchecked(String name, |
| 6548 | IBinder token, int userId) { |
| 6549 | return getContentProviderImpl(null, name, token, true, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6550 | } |
| 6551 | |
| 6552 | /** |
| 6553 | * Drop a content provider from a ProcessRecord's bookkeeping |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6554 | */ |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6555 | public void removeContentProvider(IBinder connection, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6556 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6557 | synchronized (this) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6558 | ContentProviderConnection conn; |
| 6559 | try { |
| 6560 | conn = (ContentProviderConnection)connection; |
| 6561 | } catch (ClassCastException e) { |
| 6562 | String msg ="removeContentProvider: " + connection |
| 6563 | + " not a ContentProviderConnection"; |
| 6564 | Slog.w(TAG, msg); |
| 6565 | throw new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6566 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6567 | if (conn == null) { |
| 6568 | throw new NullPointerException("connection is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6569 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6570 | if (decProviderCountLocked(conn, null, null, stable)) { |
| 6571 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6572 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6573 | } |
| 6574 | } |
| 6575 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6576 | public void removeContentProviderExternal(String name, IBinder token) { |
| 6577 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6578 | "Do not have permission in call removeContentProviderExternal()"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6579 | removeContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6580 | } |
| 6581 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6582 | private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6583 | synchronized (this) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6584 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6585 | if(cpr == null) { |
| 6586 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6587 | 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] | 6588 | return; |
| 6589 | } |
| 6590 | |
| 6591 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6592 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6593 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6594 | if (localCpr.hasExternalProcessHandles()) { |
| 6595 | if (localCpr.removeExternalProcessHandleLocked(token)) { |
| 6596 | updateOomAdjLocked(); |
| 6597 | } else { |
| 6598 | Slog.e(TAG, "Attmpt to remove content provider " + localCpr |
| 6599 | + " with no external reference for token: " |
| 6600 | + token + "."); |
| 6601 | } |
| 6602 | } else { |
| 6603 | Slog.e(TAG, "Attmpt to remove content provider: " + localCpr |
| 6604 | + " with no external references."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6605 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6606 | } |
| 6607 | } |
| 6608 | |
| 6609 | public final void publishContentProviders(IApplicationThread caller, |
| 6610 | List<ContentProviderHolder> providers) { |
| 6611 | if (providers == null) { |
| 6612 | return; |
| 6613 | } |
| 6614 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6615 | enforceNotIsolatedCaller("publishContentProviders"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6616 | synchronized (this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6617 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6618 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6619 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6620 | if (r == null) { |
| 6621 | throw new SecurityException( |
| 6622 | "Unable to find app for caller " + caller |
| 6623 | + " (pid=" + Binder.getCallingPid() |
| 6624 | + ") when publishing content providers"); |
| 6625 | } |
| 6626 | |
| 6627 | final long origId = Binder.clearCallingIdentity(); |
| 6628 | |
| 6629 | final int N = providers.size(); |
| 6630 | for (int i=0; i<N; i++) { |
| 6631 | ContentProviderHolder src = providers.get(i); |
| 6632 | if (src == null || src.info == null || src.provider == null) { |
| 6633 | continue; |
| 6634 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6635 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6636 | if (DEBUG_MU) |
| 6637 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6638 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6639 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6640 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6641 | String names[] = dst.info.authority.split(";"); |
| 6642 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6643 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6644 | } |
| 6645 | |
| 6646 | int NL = mLaunchingProviders.size(); |
| 6647 | int j; |
| 6648 | for (j=0; j<NL; j++) { |
| 6649 | if (mLaunchingProviders.get(j) == dst) { |
| 6650 | mLaunchingProviders.remove(j); |
| 6651 | j--; |
| 6652 | NL--; |
| 6653 | } |
| 6654 | } |
| 6655 | synchronized (dst) { |
| 6656 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6657 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6658 | dst.notifyAll(); |
| 6659 | } |
| 6660 | updateOomAdjLocked(r); |
| 6661 | } |
| 6662 | } |
| 6663 | |
| 6664 | Binder.restoreCallingIdentity(origId); |
| 6665 | } |
| 6666 | } |
| 6667 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6668 | public boolean refContentProvider(IBinder connection, int stable, int unstable) { |
| 6669 | ContentProviderConnection conn; |
| 6670 | try { |
| 6671 | conn = (ContentProviderConnection)connection; |
| 6672 | } catch (ClassCastException e) { |
| 6673 | String msg ="refContentProvider: " + connection |
| 6674 | + " not a ContentProviderConnection"; |
| 6675 | Slog.w(TAG, msg); |
| 6676 | throw new IllegalArgumentException(msg); |
| 6677 | } |
| 6678 | if (conn == null) { |
| 6679 | throw new NullPointerException("connection is null"); |
| 6680 | } |
| 6681 | |
| 6682 | synchronized (this) { |
| 6683 | if (stable > 0) { |
| 6684 | conn.numStableIncs += stable; |
| 6685 | } |
| 6686 | stable = conn.stableCount + stable; |
| 6687 | if (stable < 0) { |
| 6688 | throw new IllegalStateException("stableCount < 0: " + stable); |
| 6689 | } |
| 6690 | |
| 6691 | if (unstable > 0) { |
| 6692 | conn.numUnstableIncs += unstable; |
| 6693 | } |
| 6694 | unstable = conn.unstableCount + unstable; |
| 6695 | if (unstable < 0) { |
| 6696 | throw new IllegalStateException("unstableCount < 0: " + unstable); |
| 6697 | } |
| 6698 | |
| 6699 | if ((stable+unstable) <= 0) { |
| 6700 | throw new IllegalStateException("ref counts can't go to zero here: stable=" |
| 6701 | + stable + " unstable=" + unstable); |
| 6702 | } |
| 6703 | conn.stableCount = stable; |
| 6704 | conn.unstableCount = unstable; |
| 6705 | return !conn.dead; |
| 6706 | } |
| 6707 | } |
| 6708 | |
| 6709 | public void unstableProviderDied(IBinder connection) { |
| 6710 | ContentProviderConnection conn; |
| 6711 | try { |
| 6712 | conn = (ContentProviderConnection)connection; |
| 6713 | } catch (ClassCastException e) { |
| 6714 | String msg ="refContentProvider: " + connection |
| 6715 | + " not a ContentProviderConnection"; |
| 6716 | Slog.w(TAG, msg); |
| 6717 | throw new IllegalArgumentException(msg); |
| 6718 | } |
| 6719 | if (conn == null) { |
| 6720 | throw new NullPointerException("connection is null"); |
| 6721 | } |
| 6722 | |
| 6723 | // Safely retrieve the content provider associated with the connection. |
| 6724 | IContentProvider provider; |
| 6725 | synchronized (this) { |
| 6726 | provider = conn.provider.provider; |
| 6727 | } |
| 6728 | |
| 6729 | if (provider == null) { |
| 6730 | // Um, yeah, we're way ahead of you. |
| 6731 | return; |
| 6732 | } |
| 6733 | |
| 6734 | // Make sure the caller is being honest with us. |
| 6735 | if (provider.asBinder().pingBinder()) { |
| 6736 | // Er, no, still looks good to us. |
| 6737 | synchronized (this) { |
| 6738 | Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid() |
| 6739 | + " says " + conn + " died, but we don't agree"); |
| 6740 | return; |
| 6741 | } |
| 6742 | } |
| 6743 | |
| 6744 | // Well look at that! It's dead! |
| 6745 | synchronized (this) { |
| 6746 | if (conn.provider.provider != provider) { |
| 6747 | // But something changed... good enough. |
| 6748 | return; |
| 6749 | } |
| 6750 | |
| 6751 | ProcessRecord proc = conn.provider.proc; |
| 6752 | if (proc == null || proc.thread == null) { |
| 6753 | // Seems like the process is already cleaned up. |
| 6754 | return; |
| 6755 | } |
| 6756 | |
| 6757 | // As far as we're concerned, this is just like receiving a |
| 6758 | // death notification... just a bit prematurely. |
| 6759 | Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid |
| 6760 | + ") early provider death"); |
Dianne Hackborn | bd145db | 2012-06-05 16:20:46 -0700 | [diff] [blame] | 6761 | final long ident = Binder.clearCallingIdentity(); |
| 6762 | try { |
| 6763 | appDiedLocked(proc, proc.pid, proc.thread); |
| 6764 | } finally { |
| 6765 | Binder.restoreCallingIdentity(ident); |
| 6766 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6767 | } |
| 6768 | } |
| 6769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6770 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6771 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6772 | synchronized (mSelf) { |
| 6773 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6774 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6775 | if (providers != null) { |
| 6776 | for (int i=providers.size()-1; i>=0; i--) { |
| 6777 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6778 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6779 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6780 | + ": not system .apk"); |
| 6781 | providers.remove(i); |
| 6782 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6783 | } |
| 6784 | } |
| 6785 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6786 | if (providers != null) { |
| 6787 | mSystemThread.installSystemProviders(providers); |
| 6788 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6789 | |
| 6790 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6791 | |
| 6792 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6793 | } |
| 6794 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6795 | /** |
| 6796 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6797 | * to access its content provider. |
| 6798 | * |
| 6799 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6800 | * |
| 6801 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6802 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6803 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6804 | public String getProviderMimeType(Uri uri, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6805 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6806 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6807 | userId, false, true, "getProviderMimeType", null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6808 | final String name = uri.getAuthority(); |
| 6809 | final long ident = Binder.clearCallingIdentity(); |
| 6810 | ContentProviderHolder holder = null; |
| 6811 | |
| 6812 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6813 | holder = getContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6814 | if (holder != null) { |
| 6815 | return holder.provider.getType(uri); |
| 6816 | } |
| 6817 | } catch (RemoteException e) { |
| 6818 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6819 | return null; |
| 6820 | } finally { |
| 6821 | if (holder != null) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6822 | removeContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6823 | } |
| 6824 | Binder.restoreCallingIdentity(ident); |
| 6825 | } |
| 6826 | |
| 6827 | return null; |
| 6828 | } |
| 6829 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6830 | // ========================================================= |
| 6831 | // GLOBAL MANAGEMENT |
| 6832 | // ========================================================= |
| 6833 | |
| 6834 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6835 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6836 | String proc = customProcess != null ? customProcess : info.processName; |
| 6837 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6838 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6839 | int uid = info.uid; |
| 6840 | if (isolated) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6841 | int userId = UserHandle.getUserId(uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6842 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 6843 | uid = 0; |
| 6844 | while (true) { |
| 6845 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 6846 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 6847 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 6848 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6849 | uid = UserHandle.getUid(userId, mNextIsolatedProcessUid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6850 | mNextIsolatedProcessUid++; |
| 6851 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 6852 | // No process for this uid, use it. |
| 6853 | break; |
| 6854 | } |
| 6855 | stepsLeft--; |
| 6856 | if (stepsLeft <= 0) { |
| 6857 | return null; |
| 6858 | } |
| 6859 | } |
| 6860 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6861 | synchronized (stats) { |
| 6862 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6863 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6864 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6865 | } |
| 6866 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6867 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 6868 | ProcessRecord app; |
| 6869 | if (!isolated) { |
| 6870 | app = getProcessRecordLocked(info.processName, info.uid); |
| 6871 | } else { |
| 6872 | app = null; |
| 6873 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6874 | |
| 6875 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6876 | app = newProcessRecordLocked(null, info, null, isolated); |
| 6877 | mProcessNames.put(info.processName, app.uid, app); |
| 6878 | if (isolated) { |
| 6879 | mIsolatedProcesses.put(app.uid, app); |
| 6880 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6881 | updateLruProcessLocked(app, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6882 | } |
| 6883 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6884 | // This package really, really can not be stopped. |
| 6885 | try { |
| 6886 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6887 | info.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6888 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6889 | } catch (IllegalArgumentException e) { |
| 6890 | Slog.w(TAG, "Failed trying to unstop package " |
| 6891 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6892 | } |
| 6893 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6894 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6895 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6896 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6897 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6898 | } |
| 6899 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6900 | mPersistentStartingProcesses.add(app); |
| 6901 | startProcessLocked(app, "added application", app.processName); |
| 6902 | } |
| 6903 | |
| 6904 | return app; |
| 6905 | } |
| 6906 | |
| 6907 | public void unhandledBack() { |
| 6908 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6909 | "unhandledBack()"); |
| 6910 | |
| 6911 | synchronized(this) { |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 6912 | final long origId = Binder.clearCallingIdentity(); |
| 6913 | try { |
| 6914 | mMainStack.unhandledBackLocked(); |
| 6915 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6916 | Binder.restoreCallingIdentity(origId); |
| 6917 | } |
| 6918 | } |
| 6919 | } |
| 6920 | |
| 6921 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6922 | enforceNotIsolatedCaller("openContentUri"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6923 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6924 | String name = uri.getAuthority(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6925 | ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6926 | ParcelFileDescriptor pfd = null; |
| 6927 | if (cph != null) { |
| 6928 | // We record the binder invoker's uid in thread-local storage before |
| 6929 | // going to the content provider to open the file. Later, in the code |
| 6930 | // that handles all permissions checks, we look for this uid and use |
| 6931 | // that rather than the Activity Manager's own uid. The effect is that |
| 6932 | // we do the check against the caller's permissions even though it looks |
| 6933 | // to the content provider like the Activity Manager itself is making |
| 6934 | // the request. |
| 6935 | sCallerIdentity.set(new Identity( |
| 6936 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6937 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 6938 | pfd = cph.provider.openFile(null, uri, "r"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6939 | } catch (FileNotFoundException e) { |
| 6940 | // do nothing; pfd will be returned null |
| 6941 | } finally { |
| 6942 | // Ensure that whatever happens, we clean up the identity state |
| 6943 | sCallerIdentity.remove(); |
| 6944 | } |
| 6945 | |
| 6946 | // 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] | 6947 | removeContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6948 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6949 | 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] | 6950 | } |
| 6951 | return pfd; |
| 6952 | } |
| 6953 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6954 | // Actually is sleeping or shutting down or whatever else in the future |
| 6955 | // is an inactive state. |
| 6956 | public boolean isSleeping() { |
| 6957 | return mSleeping || mShuttingDown; |
| 6958 | } |
| 6959 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6960 | public void goingToSleep() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6961 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 6962 | != PackageManager.PERMISSION_GRANTED) { |
| 6963 | throw new SecurityException("Requires permission " |
| 6964 | + android.Manifest.permission.DEVICE_POWER); |
| 6965 | } |
| 6966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6967 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6968 | mWentToSleep = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 6969 | updateEventDispatchingLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6970 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6971 | if (!mSleeping) { |
| 6972 | mSleeping = true; |
| 6973 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6974 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6975 | // Initialize the wake times of all processes. |
| 6976 | checkExcessivePowerUsageLocked(false); |
| 6977 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6978 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6979 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
| 6980 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6981 | } |
| 6982 | } |
| 6983 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6984 | public boolean shutdown(int timeout) { |
| 6985 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6986 | != PackageManager.PERMISSION_GRANTED) { |
| 6987 | throw new SecurityException("Requires permission " |
| 6988 | + android.Manifest.permission.SHUTDOWN); |
| 6989 | } |
| 6990 | |
| 6991 | boolean timedout = false; |
| 6992 | |
| 6993 | synchronized(this) { |
| 6994 | mShuttingDown = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 6995 | updateEventDispatchingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6996 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6997 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6998 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6999 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7000 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 7001 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7002 | long delay = endTime - System.currentTimeMillis(); |
| 7003 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7004 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7005 | timedout = true; |
| 7006 | break; |
| 7007 | } |
| 7008 | try { |
| 7009 | this.wait(); |
| 7010 | } catch (InterruptedException e) { |
| 7011 | } |
| 7012 | } |
| 7013 | } |
| 7014 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 7015 | |
| 7016 | mAppOpsService.shutdown(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7017 | mUsageStatsService.shutdown(); |
| 7018 | mBatteryStatsService.shutdown(); |
| 7019 | |
| 7020 | return timedout; |
| 7021 | } |
| 7022 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7023 | public final void activitySlept(IBinder token) { |
| 7024 | if (localLOGV) Slog.v( |
| 7025 | TAG, "Activity slept: token=" + token); |
| 7026 | |
| 7027 | ActivityRecord r = null; |
| 7028 | |
| 7029 | final long origId = Binder.clearCallingIdentity(); |
| 7030 | |
| 7031 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7032 | r = mMainStack.isInStackLocked(token); |
| 7033 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7034 | mMainStack.activitySleptLocked(r); |
| 7035 | } |
| 7036 | } |
| 7037 | |
| 7038 | Binder.restoreCallingIdentity(origId); |
| 7039 | } |
| 7040 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7041 | private void comeOutOfSleepIfNeededLocked() { |
| 7042 | if (!mWentToSleep && !mLockScreenShown) { |
| 7043 | if (mSleeping) { |
| 7044 | mSleeping = false; |
| 7045 | mMainStack.awakeFromSleepingLocked(); |
| 7046 | mMainStack.resumeTopActivityLocked(null); |
| 7047 | } |
| 7048 | } |
| 7049 | } |
| 7050 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7051 | public void wakingUp() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7052 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7053 | != PackageManager.PERMISSION_GRANTED) { |
| 7054 | throw new SecurityException("Requires permission " |
| 7055 | + android.Manifest.permission.DEVICE_POWER); |
| 7056 | } |
| 7057 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7058 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7059 | mWentToSleep = false; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7060 | updateEventDispatchingLocked(); |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7061 | comeOutOfSleepIfNeededLocked(); |
| 7062 | } |
| 7063 | } |
| 7064 | |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7065 | private void updateEventDispatchingLocked() { |
| 7066 | mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown); |
| 7067 | } |
| 7068 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7069 | public void setLockScreenShown(boolean shown) { |
| 7070 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7071 | != PackageManager.PERMISSION_GRANTED) { |
| 7072 | throw new SecurityException("Requires permission " |
| 7073 | + android.Manifest.permission.DEVICE_POWER); |
| 7074 | } |
| 7075 | |
| 7076 | synchronized(this) { |
| 7077 | mLockScreenShown = shown; |
| 7078 | comeOutOfSleepIfNeededLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7079 | } |
| 7080 | } |
| 7081 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7082 | public void stopAppSwitches() { |
| 7083 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7084 | != PackageManager.PERMISSION_GRANTED) { |
| 7085 | throw new SecurityException("Requires permission " |
| 7086 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7087 | } |
| 7088 | |
| 7089 | synchronized(this) { |
| 7090 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 7091 | + APP_SWITCH_DELAY_TIME; |
| 7092 | mDidAppSwitch = false; |
| 7093 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7094 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7095 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 7096 | } |
| 7097 | } |
| 7098 | |
| 7099 | public void resumeAppSwitches() { |
| 7100 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7101 | != PackageManager.PERMISSION_GRANTED) { |
| 7102 | throw new SecurityException("Requires permission " |
| 7103 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7104 | } |
| 7105 | |
| 7106 | synchronized(this) { |
| 7107 | // Note that we don't execute any pending app switches... we will |
| 7108 | // let those wait until either the timeout, or the next start |
| 7109 | // activity request. |
| 7110 | mAppSwitchesAllowedTime = 0; |
| 7111 | } |
| 7112 | } |
| 7113 | |
| 7114 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 7115 | String name) { |
| 7116 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 7117 | return true; |
| 7118 | } |
| 7119 | |
| 7120 | final int perm = checkComponentPermission( |
| 7121 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 7122 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7123 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 7124 | return true; |
| 7125 | } |
| 7126 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7127 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7128 | return false; |
| 7129 | } |
| 7130 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7131 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 7132 | boolean persistent) { |
| 7133 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 7134 | "setDebugApp()"); |
| 7135 | |
| 7136 | // Note that this is not really thread safe if there are multiple |
| 7137 | // callers into it at the same time, but that's not a situation we |
| 7138 | // care about. |
| 7139 | if (persistent) { |
| 7140 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7141 | Settings.Global.putString( |
| 7142 | resolver, Settings.Global.DEBUG_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7143 | packageName); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7144 | Settings.Global.putInt( |
| 7145 | resolver, Settings.Global.WAIT_FOR_DEBUGGER, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7146 | waitForDebugger ? 1 : 0); |
| 7147 | } |
| 7148 | |
| 7149 | synchronized (this) { |
| 7150 | if (!persistent) { |
| 7151 | mOrigDebugApp = mDebugApp; |
| 7152 | mOrigWaitForDebugger = mWaitForDebugger; |
| 7153 | } |
| 7154 | mDebugApp = packageName; |
| 7155 | mWaitForDebugger = waitForDebugger; |
| 7156 | mDebugTransient = !persistent; |
| 7157 | if (packageName != null) { |
| 7158 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 7159 | forceStopPackageLocked(packageName, -1, false, false, true, true, |
| 7160 | UserHandle.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7161 | Binder.restoreCallingIdentity(origId); |
| 7162 | } |
| 7163 | } |
| 7164 | } |
| 7165 | |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 7166 | void setOpenGlTraceApp(ApplicationInfo app, String processName) { |
| 7167 | synchronized (this) { |
| 7168 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7169 | if (!isDebuggable) { |
| 7170 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7171 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7172 | } |
| 7173 | } |
| 7174 | |
| 7175 | mOpenGlTraceApp = processName; |
| 7176 | } |
| 7177 | } |
| 7178 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 7179 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 7180 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 7181 | synchronized (this) { |
| 7182 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7183 | if (!isDebuggable) { |
| 7184 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7185 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7186 | } |
| 7187 | } |
| 7188 | mProfileApp = processName; |
| 7189 | mProfileFile = profileFile; |
| 7190 | if (mProfileFd != null) { |
| 7191 | try { |
| 7192 | mProfileFd.close(); |
| 7193 | } catch (IOException e) { |
| 7194 | } |
| 7195 | mProfileFd = null; |
| 7196 | } |
| 7197 | mProfileFd = profileFd; |
| 7198 | mProfileType = 0; |
| 7199 | mAutoStopProfiler = autoStopProfiler; |
| 7200 | } |
| 7201 | } |
| 7202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7203 | public void setAlwaysFinish(boolean enabled) { |
| 7204 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 7205 | "setAlwaysFinish()"); |
| 7206 | |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7207 | Settings.Global.putInt( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7208 | mContext.getContentResolver(), |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7209 | Settings.Global.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7210 | |
| 7211 | synchronized (this) { |
| 7212 | mAlwaysFinishActivities = enabled; |
| 7213 | } |
| 7214 | } |
| 7215 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7216 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7217 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7218 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7219 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7220 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7221 | } |
| 7222 | } |
| 7223 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 7224 | public boolean isUserAMonkey() { |
| 7225 | // For now the fact that there is a controller implies |
| 7226 | // we have a monkey. |
| 7227 | synchronized (this) { |
| 7228 | return mController != null; |
| 7229 | } |
| 7230 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7231 | |
| 7232 | public void requestBugReport() { |
Jeff Sharkey | b932319 | 2013-02-05 13:32:18 -0800 | [diff] [blame] | 7233 | enforceCallingPermission(android.Manifest.permission.DUMP, "requestBugReport"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7234 | SystemProperties.set("ctl.start", "bugreport"); |
| 7235 | } |
| 7236 | |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 7237 | public long inputDispatchingTimedOut(int pid, boolean aboveSystem) { |
| 7238 | if (checkCallingPermission(android.Manifest.permission.FILTER_EVENTS) |
| 7239 | != PackageManager.PERMISSION_GRANTED) { |
| 7240 | throw new SecurityException("Requires permission " |
| 7241 | + android.Manifest.permission.FILTER_EVENTS); |
| 7242 | } |
| 7243 | |
| 7244 | ProcessRecord proc; |
| 7245 | |
| 7246 | // TODO: Unify this code with ActivityRecord.keyDispatchingTimedOut(). |
| 7247 | synchronized (this) { |
| 7248 | synchronized (mPidsSelfLocked) { |
| 7249 | proc = mPidsSelfLocked.get(pid); |
| 7250 | } |
| 7251 | if (proc != null) { |
| 7252 | if (proc.debugging) { |
| 7253 | return -1; |
| 7254 | } |
| 7255 | |
| 7256 | if (mDidDexOpt) { |
| 7257 | // Give more time since we were dexopting. |
| 7258 | mDidDexOpt = false; |
| 7259 | return -1; |
| 7260 | } |
| 7261 | |
| 7262 | if (proc.instrumentationClass != null) { |
| 7263 | Bundle info = new Bundle(); |
| 7264 | info.putString("shortMsg", "keyDispatchingTimedOut"); |
| 7265 | info.putString("longMsg", "Timed out while dispatching key event"); |
| 7266 | finishInstrumentationLocked(proc, Activity.RESULT_CANCELED, info); |
| 7267 | proc = null; |
| 7268 | } |
| 7269 | } |
| 7270 | } |
| 7271 | |
| 7272 | if (proc != null) { |
| 7273 | appNotResponding(proc, null, null, aboveSystem, "keyDispatchingTimedOut"); |
| 7274 | if (proc.instrumentationClass != null || proc.usingWrapper) { |
| 7275 | return INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT; |
| 7276 | } |
| 7277 | } |
| 7278 | |
| 7279 | return KEY_DISPATCHING_TIMEOUT; |
| 7280 | } |
| 7281 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 7282 | public Bundle getTopActivityExtras(int requestType) { |
| 7283 | enforceCallingPermission(android.Manifest.permission.GET_TOP_ACTIVITY_INFO, |
| 7284 | "getTopActivityExtras()"); |
| 7285 | PendingActivityExtras pae; |
| 7286 | Bundle extras = new Bundle(); |
| 7287 | synchronized (this) { |
| 7288 | ActivityRecord activity = mMainStack.mResumedActivity; |
| 7289 | if (activity == null) { |
| 7290 | Slog.w(TAG, "getTopActivityExtras failed: no resumed activity"); |
| 7291 | return null; |
| 7292 | } |
| 7293 | extras.putString(Intent.EXTRA_ASSIST_PACKAGE, activity.packageName); |
| 7294 | if (activity.app == null || activity.app.thread == null) { |
| 7295 | Slog.w(TAG, "getTopActivityExtras failed: no process for " + activity); |
| 7296 | return extras; |
| 7297 | } |
| 7298 | if (activity.app.pid == Binder.getCallingPid()) { |
| 7299 | Slog.w(TAG, "getTopActivityExtras failed: request process same as " + activity); |
| 7300 | return extras; |
| 7301 | } |
| 7302 | pae = new PendingActivityExtras(activity); |
| 7303 | try { |
| 7304 | activity.app.thread.requestActivityExtras(activity.appToken, pae, requestType); |
| 7305 | mPendingActivityExtras.add(pae); |
| 7306 | mHandler.postDelayed(pae, PENDING_ACTIVITY_RESULT_TIMEOUT); |
| 7307 | } catch (RemoteException e) { |
| 7308 | Slog.w(TAG, "getTopActivityExtras failed: crash calling " + activity); |
| 7309 | return extras; |
| 7310 | } |
| 7311 | } |
| 7312 | synchronized (pae) { |
| 7313 | while (!pae.haveResult) { |
| 7314 | try { |
| 7315 | pae.wait(); |
| 7316 | } catch (InterruptedException e) { |
| 7317 | } |
| 7318 | } |
| 7319 | if (pae.result != null) { |
| 7320 | extras.putBundle(Intent.EXTRA_ASSIST_CONTEXT, pae.result); |
| 7321 | } |
| 7322 | } |
| 7323 | synchronized (this) { |
| 7324 | mPendingActivityExtras.remove(pae); |
| 7325 | mHandler.removeCallbacks(pae); |
| 7326 | } |
| 7327 | return extras; |
| 7328 | } |
| 7329 | |
| 7330 | public void reportTopActivityExtras(IBinder token, Bundle extras) { |
| 7331 | PendingActivityExtras pae = (PendingActivityExtras)token; |
| 7332 | synchronized (pae) { |
| 7333 | pae.result = extras; |
| 7334 | pae.haveResult = true; |
| 7335 | pae.notifyAll(); |
| 7336 | } |
| 7337 | } |
| 7338 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7339 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7340 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 7341 | "registerProcessObserver()"); |
| 7342 | synchronized (this) { |
| 7343 | mProcessObservers.register(observer); |
| 7344 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7345 | } |
| 7346 | |
| 7347 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7348 | synchronized (this) { |
| 7349 | mProcessObservers.unregister(observer); |
| 7350 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7351 | } |
| 7352 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7353 | public void setImmersive(IBinder token, boolean immersive) { |
| 7354 | synchronized(this) { |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 7355 | final ActivityRecord r = mMainStack.isInStackLocked(token); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7356 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7357 | throw new IllegalArgumentException(); |
| 7358 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7359 | r.immersive = immersive; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 7360 | |
| 7361 | // update associated state if we're frontmost |
| 7362 | if (r == mFocusedActivity) { |
| 7363 | if (DEBUG_IMMERSIVE) { |
| 7364 | Slog.d(TAG, "Frontmost changed immersion: "+ r); |
| 7365 | } |
| 7366 | applyUpdateLockStateLocked(r); |
| 7367 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7368 | } |
| 7369 | } |
| 7370 | |
| 7371 | public boolean isImmersive(IBinder token) { |
| 7372 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7373 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7374 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7375 | throw new IllegalArgumentException(); |
| 7376 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7377 | return r.immersive; |
| 7378 | } |
| 7379 | } |
| 7380 | |
| 7381 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7382 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7383 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7384 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7385 | return (r != null) ? r.immersive : false; |
| 7386 | } |
| 7387 | } |
| 7388 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7389 | public final void enterSafeMode() { |
| 7390 | synchronized(this) { |
| 7391 | // It only makes sense to do this before the system is ready |
| 7392 | // and started launching other packages. |
| 7393 | if (!mSystemReady) { |
| 7394 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7395 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7396 | } catch (RemoteException e) { |
| 7397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7398 | } |
| 7399 | } |
| 7400 | } |
| 7401 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7402 | public final void showSafeModeOverlay() { |
| 7403 | View v = LayoutInflater.from(mContext).inflate( |
| 7404 | com.android.internal.R.layout.safe_mode, null); |
| 7405 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 7406 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 7407 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 7408 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 7409 | lp.gravity = Gravity.BOTTOM | Gravity.START; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7410 | lp.format = v.getBackground().getOpacity(); |
| 7411 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 7412 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
Craig Mautner | 5962b12 | 2012-10-05 14:45:52 -0700 | [diff] [blame] | 7413 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7414 | ((WindowManager)mContext.getSystemService( |
| 7415 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 7416 | } |
| 7417 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7418 | public void noteWakeupAlarm(IIntentSender sender) { |
| 7419 | if (!(sender instanceof PendingIntentRecord)) { |
| 7420 | return; |
| 7421 | } |
| 7422 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 7423 | synchronized (stats) { |
| 7424 | if (mBatteryStatsService.isOnBattery()) { |
| 7425 | mBatteryStatsService.enforceCallingPermission(); |
| 7426 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 7427 | int MY_UID = Binder.getCallingUid(); |
| 7428 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 7429 | BatteryStatsImpl.Uid.Pkg pkg = |
| 7430 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 7431 | pkg.incWakeupsLocked(); |
| 7432 | } |
| 7433 | } |
| 7434 | } |
| 7435 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7436 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7437 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7438 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7439 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7440 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7441 | // XXX Note: don't acquire main activity lock here, because the window |
| 7442 | // manager calls in with its locks held. |
| 7443 | |
| 7444 | boolean killed = false; |
| 7445 | synchronized (mPidsSelfLocked) { |
| 7446 | int[] types = new int[pids.length]; |
| 7447 | int worstType = 0; |
| 7448 | for (int i=0; i<pids.length; i++) { |
| 7449 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7450 | if (proc != null) { |
| 7451 | int type = proc.setAdj; |
| 7452 | types[i] = type; |
| 7453 | if (type > worstType) { |
| 7454 | worstType = type; |
| 7455 | } |
| 7456 | } |
| 7457 | } |
| 7458 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7459 | // 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] | 7460 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7461 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 7462 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7463 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7464 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7465 | |
| 7466 | // If this is not a secure call, don't let it kill processes that |
| 7467 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7468 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 7469 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7470 | } |
| 7471 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7472 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7473 | for (int i=0; i<pids.length; i++) { |
| 7474 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7475 | if (proc == null) { |
| 7476 | continue; |
| 7477 | } |
| 7478 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7479 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7480 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7481 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.userId, proc.pid, |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7482 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7483 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7484 | proc.killedBackground = true; |
| 7485 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7486 | } |
| 7487 | } |
| 7488 | } |
| 7489 | return killed; |
| 7490 | } |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7491 | |
| 7492 | @Override |
| 7493 | public boolean killProcessesBelowForeground(String reason) { |
| 7494 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7495 | throw new SecurityException("killProcessesBelowForeground() only available to system"); |
| 7496 | } |
| 7497 | |
| 7498 | return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason); |
| 7499 | } |
| 7500 | |
| 7501 | private boolean killProcessesBelowAdj(int belowAdj, String reason) { |
| 7502 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7503 | throw new SecurityException("killProcessesBelowAdj() only available to system"); |
| 7504 | } |
| 7505 | |
| 7506 | boolean killed = false; |
| 7507 | synchronized (mPidsSelfLocked) { |
| 7508 | final int size = mPidsSelfLocked.size(); |
| 7509 | for (int i = 0; i < size; i++) { |
| 7510 | final int pid = mPidsSelfLocked.keyAt(i); |
| 7511 | final ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7512 | if (proc == null) continue; |
| 7513 | |
| 7514 | final int adj = proc.setAdj; |
| 7515 | if (adj > belowAdj && !proc.killedBackground) { |
| 7516 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7517 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.userId, |
| 7518 | proc.pid, proc.processName, adj, reason); |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7519 | killed = true; |
| 7520 | proc.killedBackground = true; |
| 7521 | Process.killProcessQuiet(pid); |
| 7522 | } |
| 7523 | } |
| 7524 | } |
| 7525 | return killed; |
| 7526 | } |
| 7527 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7528 | public final void startRunning(String pkg, String cls, String action, |
| 7529 | String data) { |
| 7530 | synchronized(this) { |
| 7531 | if (mStartRunning) { |
| 7532 | return; |
| 7533 | } |
| 7534 | mStartRunning = true; |
| 7535 | mTopComponent = pkg != null && cls != null |
| 7536 | ? new ComponentName(pkg, cls) : null; |
| 7537 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 7538 | mTopData = data; |
| 7539 | if (!mSystemReady) { |
| 7540 | return; |
| 7541 | } |
| 7542 | } |
| 7543 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7544 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7545 | } |
| 7546 | |
| 7547 | private void retrieveSettings() { |
| 7548 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7549 | String debugApp = Settings.Global.getString( |
| 7550 | resolver, Settings.Global.DEBUG_APP); |
| 7551 | boolean waitForDebugger = Settings.Global.getInt( |
| 7552 | resolver, Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0; |
| 7553 | boolean alwaysFinishActivities = Settings.Global.getInt( |
| 7554 | resolver, Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7555 | |
| 7556 | Configuration configuration = new Configuration(); |
| 7557 | Settings.System.getConfiguration(resolver, configuration); |
| 7558 | |
| 7559 | synchronized (this) { |
| 7560 | mDebugApp = mOrigDebugApp = debugApp; |
| 7561 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 7562 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 7563 | // This happens before any activities are started, so we can |
| 7564 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 7565 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7566 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7567 | } |
| 7568 | } |
| 7569 | |
| 7570 | public boolean testIsSystemReady() { |
| 7571 | // no need to synchronize(this) just to read & return the value |
| 7572 | return mSystemReady; |
| 7573 | } |
| 7574 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7575 | private static File getCalledPreBootReceiversFile() { |
| 7576 | File dataDir = Environment.getDataDirectory(); |
| 7577 | File systemDir = new File(dataDir, "system"); |
| 7578 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 7579 | return fname; |
| 7580 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7581 | |
| 7582 | static final int LAST_DONE_VERSION = 10000; |
| 7583 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7584 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 7585 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 7586 | File file = getCalledPreBootReceiversFile(); |
| 7587 | FileInputStream fis = null; |
| 7588 | try { |
| 7589 | fis = new FileInputStream(file); |
| 7590 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7591 | int fvers = dis.readInt(); |
| 7592 | if (fvers == LAST_DONE_VERSION) { |
| 7593 | String vers = dis.readUTF(); |
| 7594 | String codename = dis.readUTF(); |
| 7595 | String build = dis.readUTF(); |
| 7596 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 7597 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 7598 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 7599 | int num = dis.readInt(); |
| 7600 | while (num > 0) { |
| 7601 | num--; |
| 7602 | String pkg = dis.readUTF(); |
| 7603 | String cls = dis.readUTF(); |
| 7604 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 7605 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7606 | } |
| 7607 | } |
| 7608 | } catch (FileNotFoundException e) { |
| 7609 | } catch (IOException e) { |
| 7610 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 7611 | } finally { |
| 7612 | if (fis != null) { |
| 7613 | try { |
| 7614 | fis.close(); |
| 7615 | } catch (IOException e) { |
| 7616 | } |
| 7617 | } |
| 7618 | } |
| 7619 | return lastDoneReceivers; |
| 7620 | } |
| 7621 | |
| 7622 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 7623 | File file = getCalledPreBootReceiversFile(); |
| 7624 | FileOutputStream fos = null; |
| 7625 | DataOutputStream dos = null; |
| 7626 | try { |
| 7627 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 7628 | fos = new FileOutputStream(file); |
| 7629 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7630 | dos.writeInt(LAST_DONE_VERSION); |
| 7631 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7632 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7633 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7634 | dos.writeInt(list.size()); |
| 7635 | for (int i=0; i<list.size(); i++) { |
| 7636 | dos.writeUTF(list.get(i).getPackageName()); |
| 7637 | dos.writeUTF(list.get(i).getClassName()); |
| 7638 | } |
| 7639 | } catch (IOException e) { |
| 7640 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 7641 | file.delete(); |
| 7642 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 7643 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7644 | if (dos != null) { |
| 7645 | try { |
| 7646 | dos.close(); |
| 7647 | } catch (IOException e) { |
| 7648 | // TODO Auto-generated catch block |
| 7649 | e.printStackTrace(); |
| 7650 | } |
| 7651 | } |
| 7652 | } |
| 7653 | } |
| 7654 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7655 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7656 | synchronized(this) { |
| 7657 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7658 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7659 | return; |
| 7660 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7661 | |
| 7662 | // Check to see if there are any update receivers to run. |
| 7663 | if (!mDidUpdate) { |
| 7664 | if (mWaitingUpdate) { |
| 7665 | return; |
| 7666 | } |
| 7667 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 7668 | List<ResolveInfo> ris = null; |
| 7669 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7670 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7671 | intent, null, 0, 0); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7672 | } catch (RemoteException e) { |
| 7673 | } |
| 7674 | if (ris != null) { |
| 7675 | for (int i=ris.size()-1; i>=0; i--) { |
| 7676 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 7677 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7678 | ris.remove(i); |
| 7679 | } |
| 7680 | } |
| 7681 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7682 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7683 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7684 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7685 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7686 | for (int i=0; i<ris.size(); i++) { |
| 7687 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7688 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7689 | if (lastDoneReceivers.contains(comp)) { |
| 7690 | ris.remove(i); |
| 7691 | i--; |
| 7692 | } |
| 7693 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 7694 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7695 | final int[] users = getUsersLocked(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7696 | for (int i=0; i<ris.size(); i++) { |
| 7697 | ActivityInfo ai = ris.get(i).activityInfo; |
| 7698 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7699 | doneReceivers.add(comp); |
| 7700 | intent.setComponent(comp); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7701 | for (int j=0; j<users.length; j++) { |
| 7702 | IIntentReceiver finisher = null; |
| 7703 | if (i == ris.size()-1 && j == users.length-1) { |
| 7704 | finisher = new IIntentReceiver.Stub() { |
| 7705 | public void performReceive(Intent intent, int resultCode, |
| 7706 | String data, Bundle extras, boolean ordered, |
| 7707 | boolean sticky, int sendingUser) { |
| 7708 | // The raw IIntentReceiver interface is called |
| 7709 | // with the AM lock held, so redispatch to |
| 7710 | // execute our code without the lock. |
| 7711 | mHandler.post(new Runnable() { |
| 7712 | public void run() { |
| 7713 | synchronized (ActivityManagerService.this) { |
| 7714 | mDidUpdate = true; |
| 7715 | } |
| 7716 | writeLastDonePreBootReceivers(doneReceivers); |
| 7717 | showBootMessage(mContext.getText( |
| 7718 | R.string.android_upgrading_complete), |
| 7719 | false); |
| 7720 | systemReady(goingCallback); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7721 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7722 | }); |
| 7723 | } |
| 7724 | }; |
| 7725 | } |
| 7726 | Slog.i(TAG, "Sending system update to " + intent.getComponent() |
| 7727 | + " for user " + users[j]); |
| 7728 | broadcastIntentLocked(null, null, intent, null, finisher, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 7729 | 0, null, null, null, AppOpsManager.OP_NONE, |
| 7730 | true, false, MY_PID, Process.SYSTEM_UID, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7731 | users[j]); |
| 7732 | if (finisher != null) { |
| 7733 | mWaitingUpdate = true; |
| 7734 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7735 | } |
| 7736 | } |
| 7737 | } |
| 7738 | if (mWaitingUpdate) { |
| 7739 | return; |
| 7740 | } |
| 7741 | mDidUpdate = true; |
| 7742 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 7743 | |
| 7744 | mAppOpsService.systemReady(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7745 | mSystemReady = true; |
| 7746 | if (!mStartRunning) { |
| 7747 | return; |
| 7748 | } |
| 7749 | } |
| 7750 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7751 | ArrayList<ProcessRecord> procsToKill = null; |
| 7752 | synchronized(mPidsSelfLocked) { |
| 7753 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7754 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7755 | if (!isAllowedWhileBooting(proc.info)){ |
| 7756 | if (procsToKill == null) { |
| 7757 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7758 | } |
| 7759 | procsToKill.add(proc); |
| 7760 | } |
| 7761 | } |
| 7762 | } |
| 7763 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7764 | synchronized(this) { |
| 7765 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7766 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7767 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7768 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7769 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7770 | } |
| 7771 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7772 | |
| 7773 | // Now that we have cleaned up any update processes, we |
| 7774 | // are ready to start launching real processes and know that |
| 7775 | // we won't trample on them any more. |
| 7776 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7777 | } |
| 7778 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7779 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7780 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7781 | SystemClock.uptimeMillis()); |
| 7782 | |
| 7783 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7784 | // Make sure we have no pre-ready processes sitting around. |
| 7785 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7786 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7787 | ResolveInfo ri = mContext.getPackageManager() |
| 7788 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7789 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7790 | CharSequence errorMsg = null; |
| 7791 | if (ri != null) { |
| 7792 | ActivityInfo ai = ri.activityInfo; |
| 7793 | ApplicationInfo app = ai.applicationInfo; |
| 7794 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7795 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7796 | mTopData = null; |
| 7797 | mTopComponent = new ComponentName(app.packageName, |
| 7798 | ai.name); |
| 7799 | } else { |
| 7800 | errorMsg = mContext.getResources().getText( |
| 7801 | com.android.internal.R.string.factorytest_not_system); |
| 7802 | } |
| 7803 | } else { |
| 7804 | errorMsg = mContext.getResources().getText( |
| 7805 | com.android.internal.R.string.factorytest_no_action); |
| 7806 | } |
| 7807 | if (errorMsg != null) { |
| 7808 | mTopAction = null; |
| 7809 | mTopData = null; |
| 7810 | mTopComponent = null; |
| 7811 | Message msg = Message.obtain(); |
| 7812 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7813 | msg.getData().putCharSequence("msg", errorMsg); |
| 7814 | mHandler.sendMessage(msg); |
| 7815 | } |
| 7816 | } |
| 7817 | } |
| 7818 | |
| 7819 | retrieveSettings(); |
| 7820 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7821 | if (goingCallback != null) goingCallback.run(); |
| 7822 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7823 | synchronized (this) { |
| 7824 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7825 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7826 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7827 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7828 | if (apps != null) { |
| 7829 | int N = apps.size(); |
| 7830 | int i; |
| 7831 | for (i=0; i<N; i++) { |
| 7832 | ApplicationInfo info |
| 7833 | = (ApplicationInfo)apps.get(i); |
| 7834 | if (info != null && |
| 7835 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7836 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7837 | } |
| 7838 | } |
| 7839 | } |
| 7840 | } catch (RemoteException ex) { |
| 7841 | // pm is in same process, this will never happen. |
| 7842 | } |
| 7843 | } |
| 7844 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7845 | // Start up initial activity. |
| 7846 | mBooting = true; |
| 7847 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7848 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7849 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7850 | Message msg = Message.obtain(); |
| 7851 | msg.what = SHOW_UID_ERROR_MSG; |
| 7852 | mHandler.sendMessage(msg); |
| 7853 | } |
| 7854 | } catch (RemoteException e) { |
| 7855 | } |
| 7856 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7857 | long ident = Binder.clearCallingIdentity(); |
| 7858 | try { |
| 7859 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7860 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 7861 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7862 | intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId); |
| 7863 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 7864 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7865 | false, false, MY_PID, Process.SYSTEM_UID, mCurrentUserId); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 7866 | intent = new Intent(Intent.ACTION_USER_STARTING); |
| 7867 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 7868 | intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId); |
| 7869 | broadcastIntentLocked(null, null, intent, |
| 7870 | null, new IIntentReceiver.Stub() { |
| 7871 | @Override |
| 7872 | public void performReceive(Intent intent, int resultCode, String data, |
| 7873 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) |
| 7874 | throws RemoteException { |
| 7875 | } |
| 7876 | }, 0, null, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 7877 | android.Manifest.permission.INTERACT_ACROSS_USERS, AppOpsManager.OP_NONE, |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 7878 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7879 | } finally { |
| 7880 | Binder.restoreCallingIdentity(ident); |
| 7881 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7882 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7883 | sendUserSwitchBroadcastsLocked(-1, mCurrentUserId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7884 | } |
| 7885 | } |
| 7886 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7887 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7888 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7889 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7890 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7891 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7892 | startAppProblemLocked(app); |
| 7893 | app.stopFreezingAllLocked(); |
| 7894 | return handleAppCrashLocked(app); |
| 7895 | } |
| 7896 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7897 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7898 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7899 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7900 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7901 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7902 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7903 | startAppProblemLocked(app); |
| 7904 | app.stopFreezingAllLocked(); |
| 7905 | } |
| 7906 | |
| 7907 | /** |
| 7908 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7909 | * |
| 7910 | * @param app The ProcessRecord in which the error occurred. |
| 7911 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7912 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7913 | * @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] | 7914 | * @param shortMsg Short message describing the crash. |
| 7915 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7916 | * @param stackTrace Full crash stack trace, may be null. |
| 7917 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7918 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7919 | */ |
| 7920 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7921 | 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] | 7922 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7923 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7924 | report.condition = condition; |
| 7925 | report.processName = app.processName; |
| 7926 | report.pid = app.pid; |
| 7927 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7928 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7929 | report.shortMsg = shortMsg; |
| 7930 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7931 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7932 | |
| 7933 | return report; |
| 7934 | } |
| 7935 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7936 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7937 | synchronized (this) { |
| 7938 | app.crashing = false; |
| 7939 | app.crashingReport = null; |
| 7940 | app.notResponding = false; |
| 7941 | app.notRespondingReport = null; |
| 7942 | if (app.anrDialog == fromDialog) { |
| 7943 | app.anrDialog = null; |
| 7944 | } |
| 7945 | if (app.waitDialog == fromDialog) { |
| 7946 | app.waitDialog = null; |
| 7947 | } |
| 7948 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7949 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7950 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7951 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7952 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7953 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7954 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7955 | } |
| 7956 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7957 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7958 | private boolean handleAppCrashLocked(ProcessRecord app) { |
Mike Lockwood | 86548c4 | 2011-09-13 17:21:46 -0400 | [diff] [blame] | 7959 | if (mHeadless) { |
| 7960 | Log.e(TAG, "handleAppCrashLocked: " + app.processName); |
| 7961 | return false; |
| 7962 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7963 | long now = SystemClock.uptimeMillis(); |
| 7964 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7965 | Long crashTime; |
| 7966 | if (!app.isolated) { |
| 7967 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 7968 | } else { |
| 7969 | crashTime = null; |
| 7970 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7971 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7972 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7973 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7974 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7975 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7976 | app.userId, app.info.processName, app.uid); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 7977 | mMainStack.handleAppCrashLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7978 | if (!app.persistent) { |
| 7979 | // We don't want to start this process again until the user |
| 7980 | // explicitly does so... but for persistent process, we really |
| 7981 | // need to keep it running. If a persistent process is actually |
| 7982 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7983 | 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] | 7984 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7985 | if (!app.isolated) { |
| 7986 | // XXX We don't have a way to mark isolated processes |
| 7987 | // as bad, since they don't have a peristent identity. |
| 7988 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 7989 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 7990 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7991 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7992 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 7993 | // Don't let services in this process be restarted and potentially |
| 7994 | // annoy the user repeatedly. Unless it is persistent, since those |
| 7995 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7996 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7997 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7998 | return false; |
| 7999 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8000 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8001 | } else { |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 8002 | mMainStack.finishTopRunningActivityLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8003 | } |
| 8004 | |
| 8005 | // Bump up the crash count of any services currently running in the proc. |
| 8006 | if (app.services.size() != 0) { |
| 8007 | // Any services running in the application need to be placed |
| 8008 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8009 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8010 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8011 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8012 | sr.crashCount++; |
| 8013 | } |
| 8014 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8015 | |
| 8016 | // If the crashing process is what we consider to be the "home process" and it has been |
| 8017 | // replaced by a third-party app, clear the package preferred activities from packages |
| 8018 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 8019 | // from blocking the user to manually clear the list. |
| 8020 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 8021 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 8022 | Iterator it = mHomeProcess.activities.iterator(); |
| 8023 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 8024 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8025 | if (r.isHomeActivity) { |
| 8026 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 8027 | try { |
| 8028 | ActivityThread.getPackageManager() |
| 8029 | .clearPackagePreferredActivities(r.packageName); |
| 8030 | } catch (RemoteException c) { |
| 8031 | // pm is in same process, this will never happen. |
| 8032 | } |
| 8033 | } |
| 8034 | } |
| 8035 | } |
| 8036 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8037 | if (!app.isolated) { |
| 8038 | // XXX Can't keep track of crash times for isolated processes, |
| 8039 | // because they don't have a perisistent identity. |
| 8040 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 8041 | } |
| 8042 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8043 | return true; |
| 8044 | } |
| 8045 | |
| 8046 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 8047 | if (app.userId == mCurrentUserId) { |
| 8048 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 8049 | mContext, app.info.packageName, app.info.flags); |
| 8050 | } else { |
| 8051 | // If this app is not running under the current user, then we |
| 8052 | // can't give it a report button because that would require |
| 8053 | // launching the report UI under a different user. |
| 8054 | app.errorReportReceiver = null; |
| 8055 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8056 | skipCurrentReceiverLocked(app); |
| 8057 | } |
| 8058 | |
| 8059 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 8060 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 8061 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8062 | } |
| 8063 | } |
| 8064 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8065 | /** |
| 8066 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 8067 | * The application process will exit immediately after this call returns. |
| 8068 | * @param app object of the crashing app, null for the system server |
| 8069 | * @param crashInfo describing the exception |
| 8070 | */ |
| 8071 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8072 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8073 | final String processName = app == null ? "system_server" |
| 8074 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8075 | |
| 8076 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8077 | UserHandle.getUserId(Binder.getCallingUid()), processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8078 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8079 | crashInfo.exceptionClassName, |
| 8080 | crashInfo.exceptionMessage, |
| 8081 | crashInfo.throwFileName, |
| 8082 | crashInfo.throwLineNumber); |
| 8083 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8084 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8085 | |
| 8086 | crashApplication(r, crashInfo); |
| 8087 | } |
| 8088 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8089 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8090 | IBinder app, |
| 8091 | int violationMask, |
| 8092 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8093 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 8094 | if (r == null) { |
| 8095 | return; |
| 8096 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8097 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8098 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 8099 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8100 | boolean logIt = true; |
| 8101 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 8102 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 8103 | logIt = false; |
| 8104 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8105 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8106 | // the relative pain numbers, without logging all |
| 8107 | // the stack traces repeatedly. We'd want to do |
| 8108 | // likewise in the client code, which also does |
| 8109 | // dup suppression, before the Binder call. |
| 8110 | } else { |
| 8111 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 8112 | mAlreadyLoggedViolatedStacks.clear(); |
| 8113 | } |
| 8114 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 8115 | } |
| 8116 | } |
| 8117 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8118 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8119 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8120 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8121 | |
| 8122 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 8123 | AppErrorResult result = new AppErrorResult(); |
| 8124 | synchronized (this) { |
| 8125 | final long origId = Binder.clearCallingIdentity(); |
| 8126 | |
| 8127 | Message msg = Message.obtain(); |
| 8128 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 8129 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 8130 | data.put("result", result); |
| 8131 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8132 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8133 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8134 | msg.obj = data; |
| 8135 | mHandler.sendMessage(msg); |
| 8136 | |
| 8137 | Binder.restoreCallingIdentity(origId); |
| 8138 | } |
| 8139 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 8140 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8141 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8142 | } |
| 8143 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8144 | // Depending on the policy in effect, there could be a bunch of |
| 8145 | // these in quick succession so we try to batch these together to |
| 8146 | // minimize disk writes, number of dropbox entries, and maximize |
| 8147 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8148 | private void logStrictModeViolationToDropBox( |
| 8149 | ProcessRecord process, |
| 8150 | StrictMode.ViolationInfo info) { |
| 8151 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8152 | return; |
| 8153 | } |
| 8154 | final boolean isSystemApp = process == null || |
| 8155 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 8156 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8157 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8158 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 8159 | final DropBoxManager dbox = (DropBoxManager) |
| 8160 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8161 | |
| 8162 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8163 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8164 | |
| 8165 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8166 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8167 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 8168 | synchronized (sb) { |
| 8169 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8170 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8171 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 8172 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8173 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 8174 | if (info.violationNumThisLoop != 0) { |
| 8175 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 8176 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 8177 | if (info.numAnimationsRunning != 0) { |
| 8178 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 8179 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 8180 | if (info.broadcastIntentAction != null) { |
| 8181 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 8182 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8183 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8184 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8185 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 8186 | if (info.numInstances != -1) { |
| 8187 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 8188 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8189 | if (info.tags != null) { |
| 8190 | for (String tag : info.tags) { |
| 8191 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 8192 | } |
| 8193 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8194 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8195 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 8196 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8197 | } |
| 8198 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8199 | |
| 8200 | // Only buffer up to ~64k. Various logging bits truncate |
| 8201 | // things at 128k. |
| 8202 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8203 | } |
| 8204 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8205 | // Flush immediately if the buffer's grown too large, or this |
| 8206 | // is a non-system app. Non-system apps are isolated with a |
| 8207 | // different tag & policy and not batched. |
| 8208 | // |
| 8209 | // Batching is useful during internal testing with |
| 8210 | // StrictMode settings turned up high. Without batching, |
| 8211 | // thousands of separate files could be created on boot. |
| 8212 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8213 | new Thread("Error dump: " + dropboxTag) { |
| 8214 | @Override |
| 8215 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8216 | String report; |
| 8217 | synchronized (sb) { |
| 8218 | report = sb.toString(); |
| 8219 | sb.delete(0, sb.length()); |
| 8220 | sb.trimToSize(); |
| 8221 | } |
| 8222 | if (report.length() != 0) { |
| 8223 | dbox.addText(dropboxTag, report); |
| 8224 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8225 | } |
| 8226 | }.start(); |
| 8227 | return; |
| 8228 | } |
| 8229 | |
| 8230 | // System app batching: |
| 8231 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8232 | // An existing dropbox-writing thread is outstanding, so |
| 8233 | // we don't need to start it up. The existing thread will |
| 8234 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8235 | return; |
| 8236 | } |
| 8237 | |
| 8238 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 8239 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8240 | new Thread("Error dump: " + dropboxTag) { |
| 8241 | @Override |
| 8242 | public void run() { |
| 8243 | // 5 second sleep to let stacks arrive and be batched together |
| 8244 | try { |
| 8245 | Thread.sleep(5000); // 5 seconds |
| 8246 | } catch (InterruptedException e) {} |
| 8247 | |
| 8248 | String errorReport; |
| 8249 | synchronized (mStrictModeBuffer) { |
| 8250 | errorReport = mStrictModeBuffer.toString(); |
| 8251 | if (errorReport.length() == 0) { |
| 8252 | return; |
| 8253 | } |
| 8254 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 8255 | mStrictModeBuffer.trimToSize(); |
| 8256 | } |
| 8257 | dbox.addText(dropboxTag, errorReport); |
| 8258 | } |
| 8259 | }.start(); |
| 8260 | } |
| 8261 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8262 | /** |
| 8263 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 8264 | * @param app object of the crashing app, null for the system server |
| 8265 | * @param tag reported by the caller |
| 8266 | * @param crashInfo describing the context of the error |
| 8267 | * @return true if the process should exit immediately (WTF is fatal) |
| 8268 | */ |
| 8269 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8270 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8271 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8272 | final String processName = app == null ? "system_server" |
| 8273 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8274 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8275 | EventLog.writeEvent(EventLogTags.AM_WTF, |
| 8276 | UserHandle.getUserId(Binder.getCallingUid()), Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8277 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8278 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8279 | tag, crashInfo.exceptionMessage); |
| 8280 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8281 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8282 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 8283 | if (r != null && r.pid != Process.myPid() && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 8284 | Settings.Global.getInt(mContext.getContentResolver(), |
| 8285 | Settings.Global.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8286 | crashApplication(r, crashInfo); |
| 8287 | return true; |
| 8288 | } else { |
| 8289 | return false; |
| 8290 | } |
| 8291 | } |
| 8292 | |
| 8293 | /** |
| 8294 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 8295 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 8296 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8297 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8298 | if (app == null) { |
| 8299 | return null; |
| 8300 | } |
| 8301 | |
| 8302 | synchronized (this) { |
| 8303 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 8304 | final int NA = apps.size(); |
| 8305 | for (int ia=0; ia<NA; ia++) { |
| 8306 | ProcessRecord p = apps.valueAt(ia); |
| 8307 | if (p.thread != null && p.thread.asBinder() == app) { |
| 8308 | return p; |
| 8309 | } |
| 8310 | } |
| 8311 | } |
| 8312 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8313 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 8314 | + " from pid=" + Binder.getCallingPid() |
| 8315 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8316 | return null; |
| 8317 | } |
| 8318 | } |
| 8319 | |
| 8320 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8321 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 8322 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8323 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8324 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 8325 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8326 | // Watchdog thread ends up invoking this function (with |
| 8327 | // a null ProcessRecord) to add the stack file to dropbox. |
| 8328 | // Do not acquire a lock on this (am) in such cases, as it |
| 8329 | // could cause a potential deadlock, if and when watchdog |
| 8330 | // is invoked due to unavailability of lock on am and it |
| 8331 | // would prevent watchdog from killing system_server. |
| 8332 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8333 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8334 | return; |
| 8335 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 8336 | // Note: ProcessRecord 'process' is guarded by the service |
| 8337 | // instance. (notably process.pkgList, which could otherwise change |
| 8338 | // concurrently during execution of this method) |
| 8339 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8340 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8341 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8342 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8343 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 8344 | for (String pkg : process.pkgList) { |
| 8345 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8346 | try { |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 8347 | PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId()); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8348 | if (pi != null) { |
| 8349 | sb.append(" v").append(pi.versionCode); |
| 8350 | if (pi.versionName != null) { |
| 8351 | sb.append(" (").append(pi.versionName).append(")"); |
| 8352 | } |
| 8353 | } |
| 8354 | } catch (RemoteException e) { |
| 8355 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8356 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8357 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8358 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8359 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8360 | } |
| 8361 | |
| 8362 | private static String processClass(ProcessRecord process) { |
| 8363 | if (process == null || process.pid == MY_PID) { |
| 8364 | return "system_server"; |
| 8365 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 8366 | return "system_app"; |
| 8367 | } else { |
| 8368 | return "data_app"; |
| 8369 | } |
| 8370 | } |
| 8371 | |
| 8372 | /** |
| 8373 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 8374 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 8375 | * @param process which caused the error, null means the system server |
| 8376 | * @param activity which triggered the error, null if unknown |
| 8377 | * @param parent activity related to the error, null if unknown |
| 8378 | * @param subject line related to the error, null if absent |
| 8379 | * @param report in long form describing the error, null if absent |
| 8380 | * @param logFile to include in the report, null if none |
| 8381 | * @param crashInfo giving an application stack trace, null if absent |
| 8382 | */ |
| 8383 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8384 | ProcessRecord process, String processName, ActivityRecord activity, |
| 8385 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8386 | final String report, final File logFile, |
| 8387 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 8388 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 8389 | // otherwise the watchdog may be prevented from resetting the system. |
| 8390 | |
| 8391 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 8392 | final DropBoxManager dbox = (DropBoxManager) |
| 8393 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8394 | |
| 8395 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8396 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8397 | |
| 8398 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8399 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8400 | if (activity != null) { |
| 8401 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 8402 | } |
| 8403 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 8404 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 8405 | } |
| 8406 | if (parent != null && parent != activity) { |
| 8407 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 8408 | } |
| 8409 | if (subject != null) { |
| 8410 | sb.append("Subject: ").append(subject).append("\n"); |
| 8411 | } |
| 8412 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 8413 | if (Debug.isDebuggerConnected()) { |
| 8414 | sb.append("Debugger: Connected\n"); |
| 8415 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8416 | sb.append("\n"); |
| 8417 | |
| 8418 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 8419 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8420 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 8421 | @Override |
| 8422 | public void run() { |
| 8423 | if (report != null) { |
| 8424 | sb.append(report); |
| 8425 | } |
| 8426 | if (logFile != null) { |
| 8427 | try { |
| 8428 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 8429 | } catch (IOException e) { |
| 8430 | Slog.e(TAG, "Error reading " + logFile, e); |
| 8431 | } |
| 8432 | } |
| 8433 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 8434 | sb.append(crashInfo.stackTrace); |
| 8435 | } |
| 8436 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 8437 | String setting = Settings.Global.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 8438 | int lines = Settings.Global.getInt(mContext.getContentResolver(), setting, 0); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8439 | if (lines > 0) { |
| 8440 | sb.append("\n"); |
| 8441 | |
| 8442 | // Merge several logcat streams, and take the last N lines |
| 8443 | InputStreamReader input = null; |
| 8444 | try { |
| 8445 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 8446 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 8447 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 8448 | |
| 8449 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 8450 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 8451 | input = new InputStreamReader(logcat.getInputStream()); |
| 8452 | |
| 8453 | int num; |
| 8454 | char[] buf = new char[8192]; |
| 8455 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 8456 | } catch (IOException e) { |
| 8457 | Slog.e(TAG, "Error running logcat", e); |
| 8458 | } finally { |
| 8459 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 8460 | } |
| 8461 | } |
| 8462 | |
| 8463 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8464 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8465 | }; |
| 8466 | |
Dianne Hackborn | 56385cc | 2012-04-30 15:07:47 -0700 | [diff] [blame] | 8467 | if (process == null) { |
| 8468 | // If process is null, we are being called from some internal code |
| 8469 | // and may be about to die -- run this synchronously. |
| 8470 | worker.run(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8471 | } else { |
| 8472 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8473 | } |
| 8474 | } |
| 8475 | |
| 8476 | /** |
| 8477 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 8478 | * Deal with edge cases (intercepts from instrumented applications, |
| 8479 | * ActivityController, error intent receivers, that sort of thing). |
| 8480 | * @param r the application crashing |
| 8481 | * @param crashInfo describing the failure |
| 8482 | */ |
| 8483 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8484 | long timeMillis = System.currentTimeMillis(); |
| 8485 | String shortMsg = crashInfo.exceptionClassName; |
| 8486 | String longMsg = crashInfo.exceptionMessage; |
| 8487 | String stackTrace = crashInfo.stackTrace; |
| 8488 | if (shortMsg != null && longMsg != null) { |
| 8489 | longMsg = shortMsg + ": " + longMsg; |
| 8490 | } else if (shortMsg != null) { |
| 8491 | longMsg = shortMsg; |
| 8492 | } |
| 8493 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8494 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8495 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8496 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8497 | try { |
| 8498 | String name = r != null ? r.processName : null; |
| 8499 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8500 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8501 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8502 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8503 | + " at watcher's request"); |
| 8504 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8505 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8506 | } |
| 8507 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8508 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8509 | } |
| 8510 | } |
| 8511 | |
| 8512 | final long origId = Binder.clearCallingIdentity(); |
| 8513 | |
| 8514 | // If this process is running instrumentation, finish it. |
| 8515 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8516 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8517 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8518 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 8519 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8520 | Bundle info = new Bundle(); |
| 8521 | info.putString("shortMsg", shortMsg); |
| 8522 | info.putString("longMsg", longMsg); |
| 8523 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 8524 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8525 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8526 | } |
| 8527 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8528 | // If we can't identify the process or it's already exceeded its crash quota, |
| 8529 | // quit right away without showing a crash dialog. |
| 8530 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8531 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8532 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8533 | } |
| 8534 | |
| 8535 | Message msg = Message.obtain(); |
| 8536 | msg.what = SHOW_ERROR_MSG; |
| 8537 | HashMap data = new HashMap(); |
| 8538 | data.put("result", result); |
| 8539 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8540 | msg.obj = data; |
| 8541 | mHandler.sendMessage(msg); |
| 8542 | |
| 8543 | Binder.restoreCallingIdentity(origId); |
| 8544 | } |
| 8545 | |
| 8546 | int res = result.get(); |
| 8547 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8548 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8549 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8550 | if (r != null && !r.isolated) { |
| 8551 | // XXX Can't keep track of crash time for isolated processes, |
| 8552 | // since they don't have a persistent identity. |
| 8553 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8554 | SystemClock.uptimeMillis()); |
| 8555 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8556 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8557 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8558 | } |
| 8559 | } |
| 8560 | |
| 8561 | if (appErrorIntent != null) { |
| 8562 | try { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 8563 | mContext.startActivityAsUser(appErrorIntent, new UserHandle(r.userId)); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8564 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8565 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8566 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8567 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8568 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8569 | |
| 8570 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 8571 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 8572 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8573 | if (report == null) { |
| 8574 | return null; |
| 8575 | } |
| 8576 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 8577 | result.setComponent(r.errorReportReceiver); |
| 8578 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 8579 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 8580 | return result; |
| 8581 | } |
| 8582 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8583 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 8584 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8585 | if (r.errorReportReceiver == null) { |
| 8586 | return null; |
| 8587 | } |
| 8588 | |
| 8589 | if (!r.crashing && !r.notResponding) { |
| 8590 | return null; |
| 8591 | } |
| 8592 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8593 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 8594 | report.packageName = r.info.packageName; |
| 8595 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 8596 | report.processName = r.processName; |
| 8597 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 8598 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8599 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8600 | if (r.crashing) { |
| 8601 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 8602 | report.crashInfo = crashInfo; |
| 8603 | } else if (r.notResponding) { |
| 8604 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 8605 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8606 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8607 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 8608 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 8609 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8610 | } |
| 8611 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8612 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8613 | } |
| 8614 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8615 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8616 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8617 | // assume our apps are happy - lazy create the list |
| 8618 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 8619 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8620 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8621 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8622 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8623 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 8624 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8625 | synchronized (this) { |
| 8626 | |
| 8627 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8628 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8629 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8630 | if (!allUsers && app.userId != userId) { |
| 8631 | continue; |
| 8632 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8633 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 8634 | // This one's in trouble, so we'll generate a report for it |
| 8635 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 8636 | ActivityManager.ProcessErrorStateInfo report = null; |
| 8637 | if (app.crashing) { |
| 8638 | report = app.crashingReport; |
| 8639 | } else if (app.notResponding) { |
| 8640 | report = app.notRespondingReport; |
| 8641 | } |
| 8642 | |
| 8643 | if (report != null) { |
| 8644 | if (errList == null) { |
| 8645 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 8646 | } |
| 8647 | errList.add(report); |
| 8648 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8649 | 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] | 8650 | " crashing = " + app.crashing + |
| 8651 | " notResponding = " + app.notResponding); |
| 8652 | } |
| 8653 | } |
| 8654 | } |
| 8655 | } |
| 8656 | |
| 8657 | return errList; |
| 8658 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8659 | |
| 8660 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8661 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8662 | if (currApp != null) { |
| 8663 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 8664 | } |
| 8665 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8666 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 8667 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8668 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 8669 | if (currApp != null) { |
| 8670 | currApp.lru = 0; |
| 8671 | } |
| 8672 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8673 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8674 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 8675 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8676 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 8677 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8678 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 8679 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8680 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 8681 | } else { |
| 8682 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 8683 | } |
| 8684 | } |
| 8685 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8686 | private void fillInProcMemInfo(ProcessRecord app, |
| 8687 | ActivityManager.RunningAppProcessInfo outInfo) { |
| 8688 | outInfo.pid = app.pid; |
| 8689 | outInfo.uid = app.info.uid; |
| 8690 | if (mHeavyWeightProcess == app) { |
| 8691 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
| 8692 | } |
| 8693 | if (app.persistent) { |
| 8694 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 8695 | } |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8696 | if (app.hasActivities) { |
| 8697 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES; |
| 8698 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8699 | outInfo.lastTrimLevel = app.trimMemoryLevel; |
| 8700 | int adj = app.curAdj; |
| 8701 | outInfo.importance = oomAdjToImportance(adj, outInfo); |
| 8702 | outInfo.importanceReasonCode = app.adjTypeCode; |
| 8703 | } |
| 8704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8705 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8706 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8707 | // Lazy instantiation of list |
| 8708 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8709 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8710 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8711 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8712 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8713 | synchronized (this) { |
| 8714 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8715 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8716 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8717 | if (!allUsers && app.userId != userId) { |
| 8718 | continue; |
| 8719 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8720 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 8721 | // Generate process state info for running application |
| 8722 | ActivityManager.RunningAppProcessInfo currApp = |
| 8723 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 8724 | app.pid, app.getPackageList()); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8725 | fillInProcMemInfo(app, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8726 | if (app.adjSource instanceof ProcessRecord) { |
| 8727 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8728 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 8729 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8730 | } else if (app.adjSource instanceof ActivityRecord) { |
| 8731 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8732 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 8733 | } |
| 8734 | if (app.adjTarget instanceof ComponentName) { |
| 8735 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 8736 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8737 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8738 | // + " lru=" + currApp.lru); |
| 8739 | if (runList == null) { |
| 8740 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 8741 | } |
| 8742 | runList.add(currApp); |
| 8743 | } |
| 8744 | } |
| 8745 | } |
| 8746 | return runList; |
| 8747 | } |
| 8748 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8749 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8750 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8751 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8752 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8753 | if (runningApps != null && runningApps.size() > 0) { |
| 8754 | Set<String> extList = new HashSet<String>(); |
| 8755 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8756 | if (app.pkgList != null) { |
| 8757 | for (String pkg : app.pkgList) { |
| 8758 | extList.add(pkg); |
| 8759 | } |
| 8760 | } |
| 8761 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8762 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8763 | for (String pkg : extList) { |
| 8764 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 8765 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId()); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8766 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8767 | retList.add(info); |
| 8768 | } |
| 8769 | } catch (RemoteException e) { |
| 8770 | } |
| 8771 | } |
| 8772 | } |
| 8773 | return retList; |
| 8774 | } |
| 8775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8776 | @Override |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8777 | public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) { |
| 8778 | enforceNotIsolatedCaller("getMyMemoryState"); |
| 8779 | synchronized (this) { |
| 8780 | ProcessRecord proc; |
| 8781 | synchronized (mPidsSelfLocked) { |
| 8782 | proc = mPidsSelfLocked.get(Binder.getCallingPid()); |
| 8783 | } |
| 8784 | fillInProcMemInfo(proc, outInfo); |
| 8785 | } |
| 8786 | } |
| 8787 | |
| 8788 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8789 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8790 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 8791 | != PackageManager.PERMISSION_GRANTED) { |
| 8792 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8793 | + Binder.getCallingPid() |
| 8794 | + ", uid=" + Binder.getCallingUid() |
| 8795 | + " without permission " |
| 8796 | + android.Manifest.permission.DUMP); |
| 8797 | return; |
| 8798 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8799 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8800 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8801 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8802 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8803 | |
| 8804 | int opti = 0; |
| 8805 | while (opti < args.length) { |
| 8806 | String opt = args[opti]; |
| 8807 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8808 | break; |
| 8809 | } |
| 8810 | opti++; |
| 8811 | if ("-a".equals(opt)) { |
| 8812 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8813 | } else if ("-c".equals(opt)) { |
| 8814 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8815 | } else if ("-h".equals(opt)) { |
| 8816 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8817 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8818 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8819 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 8820 | pw.println(" b[roadcasts] [PACKAGE_NAME] [history [-s]]: broadcast state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8821 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 8822 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8823 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8824 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8825 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8826 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8827 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8828 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8829 | pw.println(" all: dump all activities"); |
| 8830 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8831 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8832 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 8833 | pw.println(" a partial substring in a component name, a"); |
| 8834 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8835 | pw.println(" -a: include all available server state."); |
| 8836 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8837 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8838 | } else { |
| 8839 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8840 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8841 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8842 | |
| 8843 | long origId = Binder.clearCallingIdentity(); |
| 8844 | boolean more = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8845 | // Is the caller requesting to dump a particular piece of data? |
| 8846 | if (opti < args.length) { |
| 8847 | String cmd = args[opti]; |
| 8848 | opti++; |
| 8849 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 8850 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8851 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8852 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8853 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8854 | String[] newArgs; |
| 8855 | String name; |
| 8856 | if (opti >= args.length) { |
| 8857 | name = null; |
| 8858 | newArgs = EMPTY_STRING_ARRAY; |
| 8859 | } else { |
| 8860 | name = args[opti]; |
| 8861 | opti++; |
| 8862 | newArgs = new String[args.length - opti]; |
| 8863 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8864 | args.length - opti); |
| 8865 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8866 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8867 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8868 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8869 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8870 | String[] newArgs; |
| 8871 | String name; |
| 8872 | if (opti >= args.length) { |
| 8873 | name = null; |
| 8874 | newArgs = EMPTY_STRING_ARRAY; |
| 8875 | } else { |
| 8876 | name = args[opti]; |
| 8877 | opti++; |
| 8878 | newArgs = new String[args.length - opti]; |
| 8879 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8880 | args.length - opti); |
| 8881 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8882 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8883 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8884 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8885 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8886 | String[] newArgs; |
| 8887 | String name; |
| 8888 | if (opti >= args.length) { |
| 8889 | name = null; |
| 8890 | newArgs = EMPTY_STRING_ARRAY; |
| 8891 | } else { |
| 8892 | name = args[opti]; |
| 8893 | opti++; |
| 8894 | newArgs = new String[args.length - opti]; |
| 8895 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8896 | args.length - opti); |
| 8897 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8898 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8899 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8900 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8901 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8902 | synchronized (this) { |
| 8903 | dumpOomLocked(fd, pw, args, opti, true); |
| 8904 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8905 | } else if ("provider".equals(cmd)) { |
| 8906 | String[] newArgs; |
| 8907 | String name; |
| 8908 | if (opti >= args.length) { |
| 8909 | name = null; |
| 8910 | newArgs = EMPTY_STRING_ARRAY; |
| 8911 | } else { |
| 8912 | name = args[opti]; |
| 8913 | opti++; |
| 8914 | newArgs = new String[args.length - opti]; |
| 8915 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8916 | } |
| 8917 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8918 | pw.println("No providers match: " + name); |
| 8919 | pw.println("Use -h for help."); |
| 8920 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8921 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8922 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8923 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8924 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8925 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8926 | String[] newArgs; |
| 8927 | String name; |
| 8928 | if (opti >= args.length) { |
| 8929 | name = null; |
| 8930 | newArgs = EMPTY_STRING_ARRAY; |
| 8931 | } else { |
| 8932 | name = args[opti]; |
| 8933 | opti++; |
| 8934 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8935 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8936 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8937 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8938 | if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8939 | pw.println("No services match: " + name); |
| 8940 | pw.println("Use -h for help."); |
| 8941 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8942 | } else if ("package".equals(cmd)) { |
| 8943 | String[] newArgs; |
| 8944 | if (opti >= args.length) { |
| 8945 | pw.println("package: no package name specified"); |
| 8946 | pw.println("Use -h for help."); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8947 | } else { |
| 8948 | dumpPackage = args[opti]; |
| 8949 | opti++; |
| 8950 | newArgs = new String[args.length - opti]; |
| 8951 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8952 | args.length - opti); |
| 8953 | args = newArgs; |
| 8954 | opti = 0; |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8955 | more = true; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8956 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8957 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 8958 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8959 | mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8960 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8961 | } else { |
| 8962 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8963 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 8964 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 8965 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8966 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8967 | } |
| 8968 | if (!more) { |
| 8969 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8970 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8971 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8972 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8973 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8974 | // No piece of data specified, dump everything. |
| 8975 | synchronized (this) { |
| 8976 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8977 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8978 | if (needSep) { |
| 8979 | pw.println(" "); |
| 8980 | } |
| 8981 | if (dumpAll) { |
| 8982 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8983 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8984 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8985 | if (needSep) { |
| 8986 | pw.println(" "); |
| 8987 | } |
| 8988 | if (dumpAll) { |
| 8989 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8990 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8991 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8992 | if (needSep) { |
| 8993 | pw.println(" "); |
| 8994 | } |
| 8995 | if (dumpAll) { |
| 8996 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8997 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8998 | needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8999 | if (needSep) { |
| 9000 | pw.println(" "); |
| 9001 | } |
| 9002 | if (dumpAll) { |
| 9003 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9004 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9005 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9006 | if (needSep) { |
| 9007 | pw.println(" "); |
| 9008 | } |
| 9009 | if (dumpAll) { |
| 9010 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9011 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9012 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9013 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9014 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9015 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9016 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9017 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9018 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9019 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 9020 | pw.println(" Main stack:"); |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 9021 | mMainStack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9022 | pw.println(" "); |
| 9023 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9024 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 9025 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9026 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9027 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9028 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9029 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9030 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9031 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9032 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9033 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9034 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9035 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9036 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9037 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9038 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 9039 | pw.println(" "); |
| 9040 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9041 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9042 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9043 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9044 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9045 | pw.println(" "); |
| 9046 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9047 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9048 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9049 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9050 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9051 | pw.println(" "); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 9052 | if (mMainStack.mPausingActivity != null) { |
| 9053 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9054 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9055 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9056 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9057 | if (dumpAll) { |
| 9058 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 9059 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9060 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 9061 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9062 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9063 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9064 | if (mRecentTasks.size() > 0) { |
| 9065 | pw.println(); |
| 9066 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9067 | |
| 9068 | final int N = mRecentTasks.size(); |
| 9069 | for (int i=0; i<N; i++) { |
| 9070 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9071 | if (dumpPackage != null) { |
| 9072 | if (tr.realActivity == null || |
| 9073 | !dumpPackage.equals(tr.realActivity)) { |
| 9074 | continue; |
| 9075 | } |
| 9076 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9077 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 9078 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9079 | if (dumpAll) { |
| 9080 | mRecentTasks.get(i).dump(pw, " "); |
| 9081 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9082 | } |
| 9083 | } |
| 9084 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9085 | if (dumpAll) { |
| 9086 | pw.println(" "); |
| 9087 | pw.println(" mCurTask: " + mCurTask); |
| 9088 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9089 | |
| 9090 | return true; |
| 9091 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9092 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9093 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9094 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9095 | boolean needSep = false; |
| 9096 | int numPers = 0; |
| 9097 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9098 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 9099 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9100 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9101 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 9102 | final int NA = procs.size(); |
| 9103 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9104 | ProcessRecord r = procs.valueAt(ia); |
| 9105 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9106 | continue; |
| 9107 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9108 | if (!needSep) { |
| 9109 | pw.println(" All known processes:"); |
| 9110 | needSep = true; |
| 9111 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9112 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 9113 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 9114 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9115 | r.dump(pw, " "); |
| 9116 | if (r.persistent) { |
| 9117 | numPers++; |
| 9118 | } |
| 9119 | } |
| 9120 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9121 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 9122 | |
| 9123 | if (mIsolatedProcesses.size() > 0) { |
| 9124 | if (needSep) pw.println(" "); |
| 9125 | needSep = true; |
| 9126 | pw.println(" Isolated process list (sorted by uid):"); |
| 9127 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 9128 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 9129 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9130 | continue; |
| 9131 | } |
| 9132 | pw.println(String.format("%sIsolated #%2d: %s", |
| 9133 | " ", i, r.toString())); |
| 9134 | } |
| 9135 | } |
| 9136 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9137 | if (mLruProcesses.size() > 0) { |
| 9138 | if (needSep) pw.println(" "); |
| 9139 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9140 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9141 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9142 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9143 | needSep = true; |
| 9144 | } |
| 9145 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9146 | if (dumpAll) { |
| 9147 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9148 | boolean printed = false; |
| 9149 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 9150 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 9151 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9152 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9153 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9154 | if (!printed) { |
| 9155 | if (needSep) pw.println(" "); |
| 9156 | needSep = true; |
| 9157 | pw.println(" PID mappings:"); |
| 9158 | printed = true; |
| 9159 | } |
| 9160 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 9161 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9162 | } |
| 9163 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9164 | } |
| 9165 | |
| 9166 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9167 | synchronized (mPidsSelfLocked) { |
| 9168 | boolean printed = false; |
| 9169 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 9170 | ProcessRecord r = mPidsSelfLocked.get( |
| 9171 | mForegroundProcesses.valueAt(i).pid); |
| 9172 | if (dumpPackage != null && (r == null |
| 9173 | || !dumpPackage.equals(r.info.packageName))) { |
| 9174 | continue; |
| 9175 | } |
| 9176 | if (!printed) { |
| 9177 | if (needSep) pw.println(" "); |
| 9178 | needSep = true; |
| 9179 | pw.println(" Foreground Processes:"); |
| 9180 | printed = true; |
| 9181 | } |
| 9182 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 9183 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 9184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9185 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9186 | } |
| 9187 | |
| 9188 | if (mPersistentStartingProcesses.size() > 0) { |
| 9189 | if (needSep) pw.println(" "); |
| 9190 | needSep = true; |
| 9191 | pw.println(" Persisent processes that are starting:"); |
| 9192 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9193 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9194 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9195 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9196 | if (mRemovedProcesses.size() > 0) { |
| 9197 | if (needSep) pw.println(" "); |
| 9198 | needSep = true; |
| 9199 | pw.println(" Processes that are being removed:"); |
| 9200 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9201 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9202 | } |
| 9203 | |
| 9204 | if (mProcessesOnHold.size() > 0) { |
| 9205 | if (needSep) pw.println(" "); |
| 9206 | needSep = true; |
| 9207 | pw.println(" Processes that are on old until the system is ready:"); |
| 9208 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9209 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9211 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9212 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9213 | |
| 9214 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9215 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9216 | long now = SystemClock.uptimeMillis(); |
| 9217 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9218 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9219 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9220 | SparseArray<Long> uids = procs.getValue(); |
| 9221 | final int N = uids.size(); |
| 9222 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9223 | int puid = uids.keyAt(i); |
| 9224 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9225 | if (dumpPackage != null && (r == null |
| 9226 | || !dumpPackage.equals(r.info.packageName))) { |
| 9227 | continue; |
| 9228 | } |
| 9229 | if (!printed) { |
| 9230 | if (needSep) pw.println(" "); |
| 9231 | needSep = true; |
| 9232 | pw.println(" Time since processes crashed:"); |
| 9233 | printed = true; |
| 9234 | } |
| 9235 | pw.print(" Process "); pw.print(pname); |
| 9236 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9237 | pw.print(": last crashed "); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 9238 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 9239 | pw.println(" ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9240 | } |
| 9241 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9243 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9244 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9245 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9246 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9247 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9248 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9249 | SparseArray<Long> uids = procs.getValue(); |
| 9250 | final int N = uids.size(); |
| 9251 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9252 | int puid = uids.keyAt(i); |
| 9253 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9254 | if (dumpPackage != null && (r == null |
| 9255 | || !dumpPackage.equals(r.info.packageName))) { |
| 9256 | continue; |
| 9257 | } |
| 9258 | if (!printed) { |
| 9259 | if (needSep) pw.println(" "); |
| 9260 | needSep = true; |
| 9261 | pw.println(" Bad processes:"); |
| 9262 | } |
| 9263 | pw.print(" Bad process "); pw.print(pname); |
| 9264 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9265 | pw.print(": crashed at time "); |
| 9266 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9267 | } |
| 9268 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9269 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9270 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9271 | pw.println(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9272 | pw.println(" mStartedUsers:"); |
| 9273 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 9274 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 9275 | pw.print(" User #"); pw.print(uss.mHandle.getIdentifier()); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9276 | pw.print(": "); uss.dump("", pw); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9277 | } |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 9278 | pw.print(" mStartedUserArray: ["); |
| 9279 | for (int i=0; i<mStartedUserArray.length; i++) { |
| 9280 | if (i > 0) pw.print(", "); |
| 9281 | pw.print(mStartedUserArray[i]); |
| 9282 | } |
| 9283 | pw.println("]"); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9284 | pw.print(" mUserLru: ["); |
| 9285 | for (int i=0; i<mUserLru.size(); i++) { |
| 9286 | if (i > 0) pw.print(", "); |
| 9287 | pw.print(mUserLru.get(i)); |
| 9288 | } |
| 9289 | pw.println("]"); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 9290 | if (dumpAll) { |
| 9291 | pw.print(" mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray)); |
| 9292 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9293 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9294 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 9295 | if (dumpAll) { |
| 9296 | StringBuilder sb = new StringBuilder(128); |
| 9297 | sb.append(" mPreviousProcessVisibleTime: "); |
| 9298 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 9299 | pw.println(sb); |
| 9300 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9301 | if (mHeavyWeightProcess != null) { |
| 9302 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9303 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9304 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9305 | if (dumpAll) { |
| 9306 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 9307 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9308 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9309 | for (Map.Entry<String, Integer> entry |
| 9310 | : mCompatModePackages.getPackages().entrySet()) { |
| 9311 | String pkg = entry.getKey(); |
| 9312 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9313 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 9314 | continue; |
| 9315 | } |
| 9316 | if (!printed) { |
| 9317 | pw.println(" mScreenCompatPackages:"); |
| 9318 | printed = true; |
| 9319 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9320 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 9321 | pw.print(mode); pw.println(); |
| 9322 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 9323 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9324 | } |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 9325 | if (mSleeping || mWentToSleep || mLockScreenShown) { |
| 9326 | pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep |
| 9327 | + " mLockScreenShown " + mLockScreenShown); |
| 9328 | } |
| 9329 | if (mShuttingDown) { |
| 9330 | pw.println(" mShuttingDown=" + mShuttingDown); |
| 9331 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9332 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 9333 | || mOrigWaitForDebugger) { |
| 9334 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 9335 | + " mDebugTransient=" + mDebugTransient |
| 9336 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 9337 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 9338 | if (mOpenGlTraceApp != null) { |
| 9339 | pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp); |
| 9340 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 9341 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 9342 | || mProfileFd != null) { |
| 9343 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 9344 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 9345 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 9346 | + mAutoStopProfiler); |
| 9347 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9348 | if (mAlwaysFinishActivities || mController != null) { |
| 9349 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 9350 | + " mController=" + mController); |
| 9351 | } |
| 9352 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9353 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9354 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9355 | + " mProcessesReady=" + mProcessesReady |
| 9356 | + " mSystemReady=" + mSystemReady); |
| 9357 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9358 | + " mBooted=" + mBooted |
| 9359 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9360 | pw.print(" mLastPowerCheckRealtime="); |
| 9361 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 9362 | pw.println(""); |
| 9363 | pw.print(" mLastPowerCheckUptime="); |
| 9364 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 9365 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9366 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 9367 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 9368 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 9369 | pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs |
| 9370 | + " mNumHiddenProcs=" + mNumHiddenProcs |
| 9371 | + " mNumServiceProcs=" + mNumServiceProcs |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9372 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9373 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9374 | |
| 9375 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9376 | } |
| 9377 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9378 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9379 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9380 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9381 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9382 | long now = SystemClock.uptimeMillis(); |
| 9383 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 9384 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9385 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 9386 | continue; |
| 9387 | } |
| 9388 | if (!printed) { |
| 9389 | if (needSep) pw.println(" "); |
| 9390 | needSep = true; |
| 9391 | pw.println(" Processes that are waiting to GC:"); |
| 9392 | printed = true; |
| 9393 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9394 | pw.print(" Process "); pw.println(proc); |
| 9395 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 9396 | pw.print(", last gced="); |
| 9397 | pw.print(now-proc.lastRequestedGc); |
| 9398 | pw.print(" ms ago, last lowMem="); |
| 9399 | pw.print(now-proc.lastLowMemory); |
| 9400 | pw.println(" ms ago"); |
| 9401 | |
| 9402 | } |
| 9403 | } |
| 9404 | return needSep; |
| 9405 | } |
| 9406 | |
| 9407 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 9408 | int opti, boolean dumpAll) { |
| 9409 | boolean needSep = false; |
| 9410 | |
| 9411 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9412 | if (needSep) pw.println(" "); |
| 9413 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9414 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9415 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9416 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9417 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 9418 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 9419 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9420 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9421 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9422 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9423 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9424 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9425 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9426 | 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] | 9427 | 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] | 9428 | |
| 9429 | if (needSep) pw.println(" "); |
| 9430 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9431 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9432 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9433 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9434 | needSep = true; |
| 9435 | } |
| 9436 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9437 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9438 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9439 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9440 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9441 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9442 | if (mHeavyWeightProcess != null) { |
| 9443 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9444 | } |
| 9445 | |
| 9446 | return true; |
| 9447 | } |
| 9448 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9449 | /** |
| 9450 | * There are three ways to call this: |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9451 | * - no provider specified: dump all the providers |
| 9452 | * - a flattened component name that matched an existing provider was specified as the |
| 9453 | * first arg: dump that one provider |
| 9454 | * - the first arg isn't the flattened component name of an existing provider: |
| 9455 | * dump all providers whose component contains the first arg as a substring |
| 9456 | */ |
| 9457 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9458 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 9459 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9460 | } |
| 9461 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9462 | static class ItemMatcher { |
| 9463 | ArrayList<ComponentName> components; |
| 9464 | ArrayList<String> strings; |
| 9465 | ArrayList<Integer> objects; |
| 9466 | boolean all; |
| 9467 | |
| 9468 | ItemMatcher() { |
| 9469 | all = true; |
| 9470 | } |
| 9471 | |
| 9472 | void build(String name) { |
| 9473 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 9474 | if (componentName != null) { |
| 9475 | if (components == null) { |
| 9476 | components = new ArrayList<ComponentName>(); |
| 9477 | } |
| 9478 | components.add(componentName); |
| 9479 | all = false; |
| 9480 | } else { |
| 9481 | int objectId = 0; |
| 9482 | // Not a '/' separated full component name; maybe an object ID? |
| 9483 | try { |
| 9484 | objectId = Integer.parseInt(name, 16); |
| 9485 | if (objects == null) { |
| 9486 | objects = new ArrayList<Integer>(); |
| 9487 | } |
| 9488 | objects.add(objectId); |
| 9489 | all = false; |
| 9490 | } catch (RuntimeException e) { |
| 9491 | // Not an integer; just do string match. |
| 9492 | if (strings == null) { |
| 9493 | strings = new ArrayList<String>(); |
| 9494 | } |
| 9495 | strings.add(name); |
| 9496 | all = false; |
| 9497 | } |
| 9498 | } |
| 9499 | } |
| 9500 | |
| 9501 | int build(String[] args, int opti) { |
| 9502 | for (; opti<args.length; opti++) { |
| 9503 | String name = args[opti]; |
| 9504 | if ("--".equals(name)) { |
| 9505 | return opti+1; |
| 9506 | } |
| 9507 | build(name); |
| 9508 | } |
| 9509 | return opti; |
| 9510 | } |
| 9511 | |
| 9512 | boolean match(Object object, ComponentName comp) { |
| 9513 | if (all) { |
| 9514 | return true; |
| 9515 | } |
| 9516 | if (components != null) { |
| 9517 | for (int i=0; i<components.size(); i++) { |
| 9518 | if (components.get(i).equals(comp)) { |
| 9519 | return true; |
| 9520 | } |
| 9521 | } |
| 9522 | } |
| 9523 | if (objects != null) { |
| 9524 | for (int i=0; i<objects.size(); i++) { |
| 9525 | if (System.identityHashCode(object) == objects.get(i)) { |
| 9526 | return true; |
| 9527 | } |
| 9528 | } |
| 9529 | } |
| 9530 | if (strings != null) { |
| 9531 | String flat = comp.flattenToString(); |
| 9532 | for (int i=0; i<strings.size(); i++) { |
| 9533 | if (flat.contains(strings.get(i))) { |
| 9534 | return true; |
| 9535 | } |
| 9536 | } |
| 9537 | } |
| 9538 | return false; |
| 9539 | } |
| 9540 | } |
| 9541 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9542 | /** |
| 9543 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9544 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9545 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 9546 | * dump all activity whose component contains the cmd as a substring |
| 9547 | * - A hex number of the ActivityRecord object instance. |
| 9548 | */ |
| 9549 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9550 | int opti, boolean dumpAll) { |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 9551 | ArrayList<ActivityRecord> activities; |
| 9552 | |
| 9553 | synchronized (this) { |
| 9554 | activities = mMainStack.getDumpActivitiesLocked(name); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9555 | } |
| 9556 | |
| 9557 | if (activities.size() <= 0) { |
| 9558 | return false; |
| 9559 | } |
| 9560 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9561 | String[] newArgs = new String[args.length - opti]; |
| 9562 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9563 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9564 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9565 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9566 | for (int i=activities.size()-1; i>=0; i--) { |
| 9567 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9568 | if (needSep) { |
| 9569 | pw.println(); |
| 9570 | } |
| 9571 | needSep = true; |
| 9572 | synchronized (this) { |
| 9573 | if (lastTask != r.task) { |
| 9574 | lastTask = r.task; |
| 9575 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 9576 | pw.print(" id="); pw.println(lastTask.taskId); |
| 9577 | if (dumpAll) { |
| 9578 | lastTask.dump(pw, " "); |
| 9579 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9580 | } |
| 9581 | } |
| 9582 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9583 | } |
| 9584 | return true; |
| 9585 | } |
| 9586 | |
| 9587 | /** |
| 9588 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 9589 | * there is a thread associated with the activity. |
| 9590 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9591 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9592 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 9593 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9594 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9595 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 9596 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9597 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9598 | if (r.app != null) pw.println(r.app.pid); |
| 9599 | else pw.println("(not running)"); |
| 9600 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9601 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9602 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9603 | } |
| 9604 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9605 | // flush anything that is already in the PrintWriter since the thread is going |
| 9606 | // to write to the file descriptor directly |
| 9607 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9608 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9609 | TransferPipe tp = new TransferPipe(); |
| 9610 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9611 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9612 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9613 | tp.go(fd); |
| 9614 | } finally { |
| 9615 | tp.kill(); |
| 9616 | } |
| 9617 | } catch (IOException e) { |
| 9618 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9619 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9620 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9621 | } |
| 9622 | } |
| 9623 | } |
| 9624 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9625 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9626 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9627 | boolean needSep = false; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9628 | boolean onlyHistory = false; |
| 9629 | |
| 9630 | if ("history".equals(dumpPackage)) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 9631 | if (opti < args.length && "-s".equals(args[opti])) { |
| 9632 | dumpAll = false; |
| 9633 | } |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9634 | onlyHistory = true; |
| 9635 | dumpPackage = null; |
| 9636 | } |
| 9637 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9638 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9639 | if (!onlyHistory && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9640 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9641 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9642 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 9643 | while (it.hasNext()) { |
| 9644 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9645 | if (dumpPackage != null && (r.app == null || |
| 9646 | !dumpPackage.equals(r.app.info.packageName))) { |
| 9647 | continue; |
| 9648 | } |
| 9649 | if (!printed) { |
| 9650 | pw.println(" Registered Receivers:"); |
| 9651 | needSep = true; |
| 9652 | printed = true; |
| 9653 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9654 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9655 | r.dump(pw, " "); |
| 9656 | } |
| 9657 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9658 | |
| 9659 | if (mReceiverResolver.dump(pw, needSep ? |
| 9660 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9661 | " ", dumpPackage, false)) { |
| 9662 | needSep = true; |
| 9663 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9664 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9665 | |
| 9666 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9667 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9668 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9669 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9670 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9671 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9672 | if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9673 | for (int user=0; user<mStickyBroadcasts.size(); user++) { |
| 9674 | if (needSep) { |
| 9675 | pw.println(); |
| 9676 | } |
| 9677 | needSep = true; |
| 9678 | pw.print(" Sticky broadcasts for user "); |
| 9679 | pw.print(mStickyBroadcasts.keyAt(user)); pw.println(":"); |
| 9680 | StringBuilder sb = new StringBuilder(128); |
| 9681 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9682 | : mStickyBroadcasts.valueAt(user).entrySet()) { |
| 9683 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
| 9684 | if (dumpAll) { |
| 9685 | pw.println(":"); |
| 9686 | ArrayList<Intent> intents = ent.getValue(); |
| 9687 | final int N = intents.size(); |
| 9688 | for (int i=0; i<N; i++) { |
| 9689 | sb.setLength(0); |
| 9690 | sb.append(" Intent: "); |
| 9691 | intents.get(i).toShortString(sb, false, true, false, false); |
| 9692 | pw.println(sb.toString()); |
| 9693 | Bundle bundle = intents.get(i).getExtras(); |
| 9694 | if (bundle != null) { |
| 9695 | pw.print(" "); |
| 9696 | pw.println(bundle.toString()); |
| 9697 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9698 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9699 | } else { |
| 9700 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9701 | } |
| 9702 | } |
| 9703 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9704 | } |
| 9705 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9706 | if (!onlyHistory && dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9707 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9708 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9709 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9710 | + queue.mBroadcastsScheduled); |
| 9711 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9712 | pw.println(" mHandler:"); |
| 9713 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9714 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9715 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9716 | |
| 9717 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9718 | } |
| 9719 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9720 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9721 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 9722 | boolean needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9723 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9724 | ItemMatcher matcher = new ItemMatcher(); |
| 9725 | matcher.build(args, opti); |
| 9726 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9727 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9728 | |
| 9729 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9730 | |
| 9731 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9732 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9733 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9734 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9735 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9736 | continue; |
| 9737 | } |
| 9738 | if (!printed) { |
| 9739 | if (needSep) pw.println(" "); |
| 9740 | needSep = true; |
| 9741 | pw.println(" Launching content providers:"); |
| 9742 | printed = true; |
| 9743 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9744 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9745 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9746 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9747 | } |
| 9748 | |
| 9749 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9750 | if (needSep) pw.println(); |
| 9751 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9752 | pw.println("Granted Uri Permissions:"); |
| 9753 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9754 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9755 | HashMap<Uri, UriPermission> perms |
| 9756 | = mGrantedUriPermissions.valueAt(i); |
| 9757 | pw.print(" * UID "); pw.print(uid); |
| 9758 | pw.println(" holds:"); |
| 9759 | for (UriPermission perm : perms.values()) { |
| 9760 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9761 | if (dumpAll) { |
| 9762 | perm.dump(pw, " "); |
| 9763 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9764 | } |
| 9765 | } |
| 9766 | needSep = true; |
| 9767 | } |
| 9768 | |
| 9769 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9770 | } |
| 9771 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9772 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9773 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9774 | boolean needSep = false; |
| 9775 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9776 | if (mIntentSenderRecords.size() > 0) { |
| 9777 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9778 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9779 | = mIntentSenderRecords.values().iterator(); |
| 9780 | while (it.hasNext()) { |
| 9781 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9782 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9783 | if (dumpPackage != null && (rec == null |
| 9784 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9785 | continue; |
| 9786 | } |
| 9787 | if (!printed) { |
| 9788 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9789 | printed = true; |
| 9790 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9791 | needSep = true; |
| 9792 | if (rec != null) { |
| 9793 | pw.print(" * "); pw.println(rec); |
| 9794 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9795 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9796 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9797 | } else { |
| 9798 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9799 | } |
| 9800 | } |
| 9801 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9802 | |
| 9803 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9804 | } |
| 9805 | |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 9806 | static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9807 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9808 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9809 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9810 | boolean needNL = false; |
| 9811 | final String innerPrefix = prefix + " "; |
| 9812 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9813 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9814 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9815 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9816 | continue; |
| 9817 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9818 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9819 | if (needNL) { |
| 9820 | pw.println(" "); |
| 9821 | needNL = false; |
| 9822 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9823 | if (lastTask != r.task) { |
| 9824 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9825 | pw.print(prefix); |
| 9826 | pw.print(full ? "* " : " "); |
| 9827 | pw.println(lastTask); |
| 9828 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9829 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9830 | } else if (complete) { |
| 9831 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9832 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9833 | pw.print(prefix); pw.print(" "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9834 | pw.println(lastTask.intent.toInsecureStringWithClip()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9835 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9836 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9837 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9838 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9839 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9840 | pw.println(r); |
| 9841 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9842 | r.dump(pw, innerPrefix); |
| 9843 | } else if (complete) { |
| 9844 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9845 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9846 | if (r.app != null) { |
| 9847 | pw.print(innerPrefix); pw.println(r.app); |
| 9848 | } |
| 9849 | } |
| 9850 | if (client && r.app != null && r.app.thread != null) { |
| 9851 | // flush anything that is already in the PrintWriter since the thread is going |
| 9852 | // to write to the file descriptor directly |
| 9853 | pw.flush(); |
| 9854 | try { |
| 9855 | TransferPipe tp = new TransferPipe(); |
| 9856 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9857 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9858 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9859 | // Short timeout, since blocking here can |
| 9860 | // deadlock with the application. |
| 9861 | tp.go(fd, 2000); |
| 9862 | } finally { |
| 9863 | tp.kill(); |
| 9864 | } |
| 9865 | } catch (IOException e) { |
| 9866 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9867 | } catch (RemoteException e) { |
| 9868 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9869 | } |
| 9870 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9872 | } |
| 9873 | } |
| 9874 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9875 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9876 | if (val == base) { |
| 9877 | if (space == null) return prefix; |
| 9878 | return prefix + " "; |
| 9879 | } |
| 9880 | return prefix + "+" + Integer.toString(val-base); |
| 9881 | } |
| 9882 | |
| 9883 | private static final int dumpProcessList(PrintWriter pw, |
| 9884 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9885 | String prefix, String normalLabel, String persistentLabel, |
| 9886 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9887 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9888 | final int N = list.size()-1; |
| 9889 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9890 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9891 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9892 | continue; |
| 9893 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9894 | pw.println(String.format("%s%s #%2d: %s", |
| 9895 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9896 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9897 | if (r.persistent) { |
| 9898 | numPers++; |
| 9899 | } |
| 9900 | } |
| 9901 | return numPers; |
| 9902 | } |
| 9903 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9904 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9905 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9906 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9907 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9908 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9909 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9910 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9911 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9912 | ProcessRecord r = origList.get(i); |
| 9913 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9914 | continue; |
| 9915 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9916 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9917 | } |
| 9918 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9919 | if (list.size() <= 0) { |
| 9920 | return false; |
| 9921 | } |
| 9922 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9923 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 9924 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 9925 | @Override |
| 9926 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 9927 | Pair<ProcessRecord, Integer> object2) { |
| 9928 | if (object1.first.setAdj != object2.first.setAdj) { |
| 9929 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 9930 | } |
| 9931 | if (object1.second.intValue() != object2.second.intValue()) { |
| 9932 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 9933 | } |
| 9934 | return 0; |
| 9935 | } |
| 9936 | }; |
| 9937 | |
| 9938 | Collections.sort(list, comparator); |
| 9939 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9940 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 9941 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 9942 | final long curUptime = SystemClock.uptimeMillis(); |
| 9943 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 9944 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9945 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9946 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9947 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9948 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9949 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9950 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 9951 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9952 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 9953 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9954 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 9955 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9956 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 9957 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9958 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9959 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9960 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 9961 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9962 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 9963 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9964 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 9965 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 9966 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9967 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9968 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 9969 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9970 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 9971 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9972 | } else { |
| 9973 | oomAdj = Integer.toString(r.setAdj); |
| 9974 | } |
| 9975 | String schedGroup; |
| 9976 | switch (r.setSchedGroup) { |
| 9977 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 9978 | schedGroup = "B"; |
| 9979 | break; |
| 9980 | case Process.THREAD_GROUP_DEFAULT: |
| 9981 | schedGroup = "F"; |
| 9982 | break; |
| 9983 | default: |
| 9984 | schedGroup = Integer.toString(r.setSchedGroup); |
| 9985 | break; |
| 9986 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9987 | String foreground; |
| 9988 | if (r.foregroundActivities) { |
| 9989 | foreground = "A"; |
| 9990 | } else if (r.foregroundServices) { |
| 9991 | foreground = "S"; |
| 9992 | } else { |
| 9993 | foreground = " "; |
| 9994 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9995 | 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] | 9996 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9997 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 9998 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9999 | if (r.adjSource != null || r.adjTarget != null) { |
| 10000 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10001 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10002 | if (r.adjTarget instanceof ComponentName) { |
| 10003 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 10004 | } else if (r.adjTarget != null) { |
| 10005 | pw.print(r.adjTarget.toString()); |
| 10006 | } else { |
| 10007 | pw.print("{null}"); |
| 10008 | } |
| 10009 | pw.print("<="); |
| 10010 | if (r.adjSource instanceof ProcessRecord) { |
| 10011 | pw.print("Proc{"); |
| 10012 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 10013 | pw.println("}"); |
| 10014 | } else if (r.adjSource != null) { |
| 10015 | pw.println(r.adjSource.toString()); |
| 10016 | } else { |
| 10017 | pw.println("{null}"); |
| 10018 | } |
| 10019 | } |
| 10020 | if (inclDetails) { |
| 10021 | pw.print(prefix); |
| 10022 | pw.print(" "); |
| 10023 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 10024 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 10025 | pw.print(" client="); pw.print(r.clientHiddenAdj); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 10026 | pw.print(" empty="); pw.print(r.emptyAdj); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10027 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 10028 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 10029 | pw.print(" cur="); pw.print(r.curAdj); |
| 10030 | pw.print(" set="); pw.println(r.setAdj); |
| 10031 | pw.print(prefix); |
| 10032 | pw.print(" "); |
| 10033 | pw.print("keeping="); pw.print(r.keeping); |
| 10034 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10035 | pw.print(" empty="); pw.print(r.empty); |
| 10036 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10037 | |
| 10038 | if (!r.keeping) { |
| 10039 | if (r.lastWakeTime != 0) { |
| 10040 | long wtime; |
| 10041 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 10042 | synchronized (stats) { |
| 10043 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 10044 | r.pid, curRealtime); |
| 10045 | } |
| 10046 | long timeUsed = wtime - r.lastWakeTime; |
| 10047 | pw.print(prefix); |
| 10048 | pw.print(" "); |
| 10049 | pw.print("keep awake over "); |
| 10050 | TimeUtils.formatDuration(realtimeSince, pw); |
| 10051 | pw.print(" used "); |
| 10052 | TimeUtils.formatDuration(timeUsed, pw); |
| 10053 | pw.print(" ("); |
| 10054 | pw.print((timeUsed*100)/realtimeSince); |
| 10055 | pw.println("%)"); |
| 10056 | } |
| 10057 | if (r.lastCpuTime != 0) { |
| 10058 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 10059 | pw.print(prefix); |
| 10060 | pw.print(" "); |
| 10061 | pw.print("run cpu over "); |
| 10062 | TimeUtils.formatDuration(uptimeSince, pw); |
| 10063 | pw.print(" used "); |
| 10064 | TimeUtils.formatDuration(timeUsed, pw); |
| 10065 | pw.print(" ("); |
| 10066 | pw.print((timeUsed*100)/uptimeSince); |
| 10067 | pw.println("%)"); |
| 10068 | } |
| 10069 | } |
| 10070 | } |
| 10071 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10072 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10073 | } |
| 10074 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10075 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10076 | ArrayList<ProcessRecord> procs; |
| 10077 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10078 | if (args != null && args.length > start |
| 10079 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10080 | procs = new ArrayList<ProcessRecord>(); |
| 10081 | int pid = -1; |
| 10082 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10083 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10084 | } catch (NumberFormatException e) { |
| 10085 | |
| 10086 | } |
| 10087 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 10088 | ProcessRecord proc = mLruProcesses.get(i); |
| 10089 | if (proc.pid == pid) { |
| 10090 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10091 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10092 | procs.add(proc); |
| 10093 | } |
| 10094 | } |
| 10095 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10096 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10097 | return null; |
| 10098 | } |
| 10099 | } else { |
| 10100 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 10101 | } |
| 10102 | } |
| 10103 | return procs; |
| 10104 | } |
| 10105 | |
| 10106 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 10107 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10108 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10109 | if (procs == null) { |
| 10110 | return; |
| 10111 | } |
| 10112 | |
| 10113 | long uptime = SystemClock.uptimeMillis(); |
| 10114 | long realtime = SystemClock.elapsedRealtime(); |
| 10115 | pw.println("Applications Graphics Acceleration Info:"); |
| 10116 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10117 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10118 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10119 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10120 | if (r.thread != null) { |
| 10121 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10122 | pw.flush(); |
| 10123 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10124 | TransferPipe tp = new TransferPipe(); |
| 10125 | try { |
| 10126 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10127 | tp.go(fd); |
| 10128 | } finally { |
| 10129 | tp.kill(); |
| 10130 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10131 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10132 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10133 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10134 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10135 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10136 | pw.flush(); |
| 10137 | } |
| 10138 | } |
| 10139 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10140 | } |
| 10141 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 10142 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 10143 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 10144 | if (procs == null) { |
| 10145 | return; |
| 10146 | } |
| 10147 | |
| 10148 | pw.println("Applications Database Info:"); |
| 10149 | |
| 10150 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10151 | ProcessRecord r = procs.get(i); |
| 10152 | if (r.thread != null) { |
| 10153 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10154 | pw.flush(); |
| 10155 | try { |
| 10156 | TransferPipe tp = new TransferPipe(); |
| 10157 | try { |
| 10158 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10159 | tp.go(fd); |
| 10160 | } finally { |
| 10161 | tp.kill(); |
| 10162 | } |
| 10163 | } catch (IOException e) { |
| 10164 | pw.println("Failure while dumping the app: " + r); |
| 10165 | pw.flush(); |
| 10166 | } catch (RemoteException e) { |
| 10167 | pw.println("Got a RemoteException while dumping the app " + r); |
| 10168 | pw.flush(); |
| 10169 | } |
| 10170 | } |
| 10171 | } |
| 10172 | } |
| 10173 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10174 | final static class MemItem { |
| 10175 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10176 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10177 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10178 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10179 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10180 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10181 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10182 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10183 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10184 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10185 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10186 | } |
| 10187 | } |
| 10188 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10189 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10190 | boolean sort) { |
| 10191 | if (sort) { |
| 10192 | Collections.sort(items, new Comparator<MemItem>() { |
| 10193 | @Override |
| 10194 | public int compare(MemItem lhs, MemItem rhs) { |
| 10195 | if (lhs.pss < rhs.pss) { |
| 10196 | return 1; |
| 10197 | } else if (lhs.pss > rhs.pss) { |
| 10198 | return -1; |
| 10199 | } |
| 10200 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10201 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10202 | }); |
| 10203 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10204 | |
| 10205 | for (int i=0; i<items.size(); i++) { |
| 10206 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10207 | 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] | 10208 | if (mi.subitems != null) { |
| 10209 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 10210 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10211 | } |
| 10212 | } |
| 10213 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10214 | // These are in KB. |
| 10215 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 10216 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 10217 | 120*1024, 160*1024, 200*1024, |
| 10218 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 10219 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 10220 | }; |
| 10221 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10222 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 10223 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10224 | int start = label.lastIndexOf('.'); |
| 10225 | if (start >= 0) start++; |
| 10226 | else start = 0; |
| 10227 | int end = label.length(); |
| 10228 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 10229 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 10230 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 10231 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10232 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10233 | out.append(label, start, end); |
| 10234 | return; |
| 10235 | } |
| 10236 | } |
| 10237 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10238 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10239 | out.append(label, start, end); |
| 10240 | } |
| 10241 | |
| 10242 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 10243 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 10244 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 10245 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 10246 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 10247 | }; |
| 10248 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 10249 | "System", "Persistent", "Foreground", |
| 10250 | "Visible", "Perceptible", "Heavy Weight", |
| 10251 | "Backup", "A Services", "Home", "Previous", |
| 10252 | "B Services", "Background" |
| 10253 | }; |
| 10254 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10255 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10256 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10257 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10258 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10259 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10260 | |
| 10261 | int opti = 0; |
| 10262 | while (opti < args.length) { |
| 10263 | String opt = args[opti]; |
| 10264 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 10265 | break; |
| 10266 | } |
| 10267 | opti++; |
| 10268 | if ("-a".equals(opt)) { |
| 10269 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10270 | } else if ("--oom".equals(opt)) { |
| 10271 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10272 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10273 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10274 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10275 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10276 | pw.println("If [process] is specified it can be the name or "); |
| 10277 | pw.println("pid of a specific process to dump."); |
| 10278 | return; |
| 10279 | } else { |
| 10280 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 10281 | } |
| 10282 | } |
| 10283 | |
| 10284 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10285 | if (procs == null) { |
| 10286 | return; |
| 10287 | } |
| 10288 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 10289 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10290 | long uptime = SystemClock.uptimeMillis(); |
| 10291 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10292 | |
| 10293 | if (procs.size() == 1 || isCheckinRequest) { |
| 10294 | dumpAll = true; |
| 10295 | } |
| 10296 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10297 | if (isCheckinRequest) { |
| 10298 | // short checkin version |
| 10299 | pw.println(uptime + "," + realtime); |
| 10300 | pw.flush(); |
| 10301 | } else { |
| 10302 | pw.println("Applications Memory Usage (kB):"); |
| 10303 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10304 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10305 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10306 | String[] innerArgs = new String[args.length-opti]; |
| 10307 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 10308 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10309 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 10310 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 10311 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 10312 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10313 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 10314 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 10315 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10316 | |
| 10317 | long totalPss = 0; |
| 10318 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10319 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10320 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10321 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10322 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10323 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 10324 | pw.flush(); |
| 10325 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10326 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10327 | if (dumpAll) { |
| 10328 | try { |
| 10329 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 10330 | } catch (RemoteException e) { |
| 10331 | if (!isCheckinRequest) { |
| 10332 | pw.println("Got RemoteException!"); |
| 10333 | pw.flush(); |
| 10334 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10335 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10336 | } else { |
| 10337 | mi = new Debug.MemoryInfo(); |
| 10338 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10339 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10340 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10341 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10342 | long myTotalPss = mi.getTotalPss(); |
| 10343 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10344 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10345 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10346 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10347 | |
| 10348 | nativePss += mi.nativePss; |
| 10349 | dalvikPss += mi.dalvikPss; |
| 10350 | otherPss += mi.otherPss; |
| 10351 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 10352 | long mem = mi.getOtherPss(j); |
| 10353 | miscPss[j] += mem; |
| 10354 | otherPss -= mem; |
| 10355 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10356 | |
| 10357 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10358 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 10359 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10360 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10361 | if (oomProcs[oomIndex] == null) { |
| 10362 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 10363 | } |
| 10364 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10365 | break; |
| 10366 | } |
| 10367 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10369 | } |
| 10370 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10371 | |
| 10372 | if (!isCheckinRequest && procs.size() > 1) { |
| 10373 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 10374 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10375 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10376 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10377 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10378 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10379 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10380 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10381 | } |
| 10382 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10383 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10384 | for (int j=0; j<oomPss.length; j++) { |
| 10385 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10386 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10387 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10388 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10389 | item.subitems = oomProcs[j]; |
| 10390 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10391 | } |
| 10392 | } |
| 10393 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10394 | if (outTag != null || outStack != null) { |
| 10395 | if (outTag != null) { |
| 10396 | appendMemBucket(outTag, totalPss, "total", false); |
| 10397 | } |
| 10398 | if (outStack != null) { |
| 10399 | appendMemBucket(outStack, totalPss, "total", true); |
| 10400 | } |
| 10401 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10402 | for (int i=0; i<oomMems.size(); i++) { |
| 10403 | MemItem miCat = oomMems.get(i); |
| 10404 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10405 | continue; |
| 10406 | } |
| 10407 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10408 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10409 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10410 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10411 | outTag.append(" / "); |
| 10412 | } |
| 10413 | if (outStack != null) { |
| 10414 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10415 | if (firstLine) { |
| 10416 | outStack.append(":"); |
| 10417 | firstLine = false; |
| 10418 | } |
| 10419 | outStack.append("\n\t at "); |
| 10420 | } else { |
| 10421 | outStack.append("$"); |
| 10422 | } |
| 10423 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10424 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10425 | MemItem mi = miCat.subitems.get(j); |
| 10426 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10427 | if (outTag != null) { |
| 10428 | outTag.append(" "); |
| 10429 | } |
| 10430 | if (outStack != null) { |
| 10431 | outStack.append("$"); |
| 10432 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10433 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10434 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10435 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10436 | } |
| 10437 | if (outStack != null) { |
| 10438 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10439 | } |
| 10440 | } |
| 10441 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10442 | outStack.append("("); |
| 10443 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10444 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10445 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10446 | outStack.append(":"); |
| 10447 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10448 | } |
| 10449 | } |
| 10450 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10451 | } |
| 10452 | } |
| 10453 | } |
| 10454 | } |
| 10455 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10456 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10457 | pw.println(); |
| 10458 | pw.println("Total PSS by process:"); |
| 10459 | dumpMemItems(pw, " ", procMems, true); |
| 10460 | pw.println(); |
| 10461 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10462 | pw.println("Total PSS by OOM adjustment:"); |
| 10463 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10464 | if (!oomOnly) { |
| 10465 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10466 | out.println(); |
| 10467 | out.println("Total PSS by category:"); |
| 10468 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10469 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10470 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10471 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | d3e677b | 2012-04-05 14:58:18 -0700 | [diff] [blame] | 10472 | final int[] SINGLE_LONG_FORMAT = new int[] { |
| 10473 | Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG |
| 10474 | }; |
| 10475 | long[] longOut = new long[1]; |
| 10476 | Process.readProcFile("/sys/kernel/mm/ksm/pages_shared", |
| 10477 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10478 | long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10479 | longOut[0] = 0; |
| 10480 | Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing", |
| 10481 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10482 | long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10483 | longOut[0] = 0; |
| 10484 | Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared", |
| 10485 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10486 | long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10487 | longOut[0] = 0; |
| 10488 | Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile", |
| 10489 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10490 | long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10491 | pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared "); |
| 10492 | pw.print(shared); pw.println(" kB"); |
| 10493 | pw.print(" "); pw.print(unshared); pw.print(" kB unshared; "); |
| 10494 | pw.print(voltile); pw.println(" kB volatile"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10495 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10496 | } |
| 10497 | |
| 10498 | /** |
| 10499 | * Searches array of arguments for the specified string |
| 10500 | * @param args array of argument strings |
| 10501 | * @param value value to search for |
| 10502 | * @return true if the value is contained in the array |
| 10503 | */ |
| 10504 | private static boolean scanArgs(String[] args, String value) { |
| 10505 | if (args != null) { |
| 10506 | for (String arg : args) { |
| 10507 | if (value.equals(arg)) { |
| 10508 | return true; |
| 10509 | } |
| 10510 | } |
| 10511 | } |
| 10512 | return false; |
| 10513 | } |
| 10514 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10515 | private final boolean removeDyingProviderLocked(ProcessRecord proc, |
| 10516 | ContentProviderRecord cpr, boolean always) { |
| 10517 | final boolean inLaunching = mLaunchingProviders.contains(cpr); |
| 10518 | |
| 10519 | if (!inLaunching || always) { |
| 10520 | synchronized (cpr) { |
| 10521 | cpr.launchingApp = null; |
| 10522 | cpr.notifyAll(); |
| 10523 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10524 | mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid)); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10525 | String names[] = cpr.info.authority.split(";"); |
| 10526 | for (int j = 0; j < names.length; j++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10527 | mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10528 | } |
| 10529 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10530 | |
| 10531 | for (int i=0; i<cpr.connections.size(); i++) { |
| 10532 | ContentProviderConnection conn = cpr.connections.get(i); |
| 10533 | if (conn.waiting) { |
| 10534 | // If this connection is waiting for the provider, then we don't |
| 10535 | // need to mess with its process unless we are always removing |
| 10536 | // or for some reason the provider is not currently launching. |
| 10537 | if (inLaunching && !always) { |
| 10538 | continue; |
| 10539 | } |
| 10540 | } |
| 10541 | ProcessRecord capp = conn.client; |
| 10542 | conn.dead = true; |
| 10543 | if (conn.stableCount > 0) { |
| 10544 | if (!capp.persistent && capp.thread != null |
| 10545 | && capp.pid != 0 |
| 10546 | && capp.pid != MY_PID) { |
| 10547 | Slog.i(TAG, "Kill " + capp.processName |
| 10548 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 10549 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 10550 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.userId, capp.pid, |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10551 | capp.processName, capp.setAdj, "dying provider " |
| 10552 | + cpr.name.toShortString()); |
| 10553 | Process.killProcessQuiet(capp.pid); |
| 10554 | } |
| 10555 | } else if (capp.thread != null && conn.provider.provider != null) { |
| 10556 | try { |
| 10557 | capp.thread.unstableProviderDied(conn.provider.provider.asBinder()); |
| 10558 | } catch (RemoteException e) { |
| 10559 | } |
| 10560 | // In the protocol here, we don't expect the client to correctly |
| 10561 | // clean up this connection, we'll just remove it. |
| 10562 | cpr.connections.remove(i); |
| 10563 | conn.client.conProviders.remove(conn); |
| 10564 | } |
| 10565 | } |
| 10566 | |
| 10567 | if (inLaunching && always) { |
| 10568 | mLaunchingProviders.remove(cpr); |
| 10569 | } |
| 10570 | return inLaunching; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10571 | } |
| 10572 | |
| 10573 | /** |
| 10574 | * Main code for cleaning up a process when it has gone away. This is |
| 10575 | * called both as a result of the process dying, or directly when stopping |
| 10576 | * a process when running in single process mode. |
| 10577 | */ |
| 10578 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10579 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10580 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10581 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10582 | } |
| 10583 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10584 | mProcessesToGc.remove(app); |
| 10585 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10586 | // Dismiss any open dialogs. |
| 10587 | if (app.crashDialog != null) { |
| 10588 | app.crashDialog.dismiss(); |
| 10589 | app.crashDialog = null; |
| 10590 | } |
| 10591 | if (app.anrDialog != null) { |
| 10592 | app.anrDialog.dismiss(); |
| 10593 | app.anrDialog = null; |
| 10594 | } |
| 10595 | if (app.waitDialog != null) { |
| 10596 | app.waitDialog.dismiss(); |
| 10597 | app.waitDialog = null; |
| 10598 | } |
| 10599 | |
| 10600 | app.crashing = false; |
| 10601 | app.notResponding = false; |
| 10602 | |
| 10603 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10604 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10605 | app.thread = null; |
| 10606 | app.forcingToForeground = null; |
| 10607 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10608 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10609 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10610 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10611 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10612 | mServices.killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10613 | |
| 10614 | boolean restart = false; |
| 10615 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10616 | // Remove published content providers. |
| 10617 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10618 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10619 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10620 | ContentProviderRecord cpr = it.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10621 | |
| 10622 | final boolean always = app.bad || !allowRestart; |
| 10623 | if (removeDyingProviderLocked(app, cpr, always) || always) { |
| 10624 | // We left the provider in the launching list, need to |
| 10625 | // restart it. |
| 10626 | restart = true; |
| 10627 | } |
| 10628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10629 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10630 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10631 | } |
| 10632 | app.pubProviders.clear(); |
| 10633 | } |
| 10634 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10635 | // Take care of any launching providers waiting for this process. |
| 10636 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10637 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10638 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10639 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10640 | // Unregister from connected content providers. |
| 10641 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10642 | for (int i=0; i<app.conProviders.size(); i++) { |
| 10643 | ContentProviderConnection conn = app.conProviders.get(i); |
| 10644 | conn.provider.connections.remove(conn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10645 | } |
| 10646 | app.conProviders.clear(); |
| 10647 | } |
| 10648 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10649 | // At this point there may be remaining entries in mLaunchingProviders |
| 10650 | // where we were the only one waiting, so they are no longer of use. |
| 10651 | // Look for these and clean up if found. |
| 10652 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10653 | // the actual situation to identify what is actually going on. |
| 10654 | if (false) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10655 | for (int i=0; i<mLaunchingProviders.size(); i++) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10656 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10657 | mLaunchingProviders.get(i); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10658 | if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10659 | synchronized (cpr) { |
| 10660 | cpr.launchingApp = null; |
| 10661 | cpr.notifyAll(); |
| 10662 | } |
| 10663 | } |
| 10664 | } |
| 10665 | } |
| 10666 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10667 | skipCurrentReceiverLocked(app); |
| 10668 | |
| 10669 | // Unregister any receivers. |
| 10670 | if (app.receivers.size() > 0) { |
| 10671 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10672 | while (it.hasNext()) { |
| 10673 | removeReceiverLocked(it.next()); |
| 10674 | } |
| 10675 | app.receivers.clear(); |
| 10676 | } |
| 10677 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10678 | // If the app is undergoing backup, tell the backup manager about it |
| 10679 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10680 | if (DEBUG_BACKUP || DEBUG_CLEANUP) Slog.d(TAG, "App " |
| 10681 | + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10682 | try { |
| 10683 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10684 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10685 | bm.agentDisconnected(app.info.packageName); |
| 10686 | } catch (RemoteException e) { |
| 10687 | // can't happen; backup manager is local |
| 10688 | } |
| 10689 | } |
| 10690 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 10691 | for (int i = mPendingProcessChanges.size()-1; i>=0; i--) { |
| 10692 | ProcessChangeItem item = mPendingProcessChanges.get(i); |
| 10693 | if (item.pid == app.pid) { |
| 10694 | mPendingProcessChanges.remove(i); |
| 10695 | mAvailProcessChanges.add(item); |
| 10696 | } |
| 10697 | } |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10698 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10699 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10700 | // If the caller is restarting this app, then leave it in its |
| 10701 | // current lists and let the caller take care of it. |
| 10702 | if (restarting) { |
| 10703 | return; |
| 10704 | } |
| 10705 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10706 | if (!app.persistent || app.isolated) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10707 | if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10708 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10709 | mProcessNames.remove(app.processName, app.uid); |
| 10710 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10711 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10712 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 10713 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10714 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10715 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10716 | } else if (!app.removed) { |
| 10717 | // This app is persistent, so we need to keep its record around. |
| 10718 | // If it is not already on the pending app list, add it there |
| 10719 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10720 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10721 | mPersistentStartingProcesses.add(app); |
| 10722 | restart = true; |
| 10723 | } |
| 10724 | } |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10725 | if ((DEBUG_PROCESSES || DEBUG_CLEANUP) && mProcessesOnHold.contains(app)) Slog.v(TAG, |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10726 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10727 | mProcessesOnHold.remove(app); |
| 10728 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10729 | if (app == mHomeProcess) { |
| 10730 | mHomeProcess = null; |
| 10731 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10732 | if (app == mPreviousProcess) { |
| 10733 | mPreviousProcess = null; |
| 10734 | } |
| 10735 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10736 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10737 | // We have components that still need to be running in the |
| 10738 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10739 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10740 | startProcessLocked(app, "restart", app.processName); |
| 10741 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10742 | // Goodbye! |
| 10743 | synchronized (mPidsSelfLocked) { |
| 10744 | mPidsSelfLocked.remove(app.pid); |
| 10745 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10746 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10747 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10748 | } |
| 10749 | } |
| 10750 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10751 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10752 | // Look through the content providers we are waiting to have launched, |
| 10753 | // and if any run in this process then either schedule a restart of |
| 10754 | // the process or kill the client waiting for it if this process has |
| 10755 | // gone bad. |
| 10756 | int NL = mLaunchingProviders.size(); |
| 10757 | boolean restart = false; |
| 10758 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10759 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10760 | if (cpr.launchingApp == app) { |
| 10761 | if (!alwaysBad && !app.bad) { |
| 10762 | restart = true; |
| 10763 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10764 | removeDyingProviderLocked(app, cpr, true); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 10765 | // cpr should have been removed from mLaunchingProviders |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10766 | NL = mLaunchingProviders.size(); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 10767 | i--; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10768 | } |
| 10769 | } |
| 10770 | } |
| 10771 | return restart; |
| 10772 | } |
| 10773 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10774 | // ========================================================= |
| 10775 | // SERVICES |
| 10776 | // ========================================================= |
| 10777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10778 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10779 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10780 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10781 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10782 | return mServices.getRunningServiceInfoLocked(maxNum, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10783 | } |
| 10784 | } |
| 10785 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10786 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10787 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10788 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10789 | return mServices.getRunningServiceControlPanelLocked(name); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10790 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10791 | } |
| 10792 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10793 | public ComponentName startService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10794 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10795 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10796 | // Refuse possible leaked file descriptors |
| 10797 | if (service != null && service.hasFileDescriptors() == true) { |
| 10798 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10799 | } |
| 10800 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10801 | if (DEBUG_SERVICE) |
| 10802 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10803 | synchronized(this) { |
| 10804 | final int callingPid = Binder.getCallingPid(); |
| 10805 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10806 | checkValidCaller(callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10807 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10808 | ComponentName res = mServices.startServiceLocked(caller, service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10809 | resolvedType, callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10810 | Binder.restoreCallingIdentity(origId); |
| 10811 | return res; |
| 10812 | } |
| 10813 | } |
| 10814 | |
| 10815 | ComponentName startServiceInPackage(int uid, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10816 | Intent service, String resolvedType, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10817 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10818 | if (DEBUG_SERVICE) |
| 10819 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10820 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10821 | ComponentName res = mServices.startServiceLocked(null, service, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10822 | resolvedType, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10823 | Binder.restoreCallingIdentity(origId); |
| 10824 | return res; |
| 10825 | } |
| 10826 | } |
| 10827 | |
| 10828 | public int stopService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10829 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10830 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10831 | // Refuse possible leaked file descriptors |
| 10832 | if (service != null && service.hasFileDescriptors() == true) { |
| 10833 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10834 | } |
| 10835 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10836 | checkValidCaller(Binder.getCallingUid(), userId); |
| 10837 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10838 | synchronized(this) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10839 | return mServices.stopServiceLocked(caller, service, resolvedType, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10840 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10841 | } |
| 10842 | |
| 10843 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10844 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10845 | // Refuse possible leaked file descriptors |
| 10846 | if (service != null && service.hasFileDescriptors() == true) { |
| 10847 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10848 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10849 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10850 | return mServices.peekServiceLocked(service, resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10851 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10852 | } |
| 10853 | |
| 10854 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 10855 | int startId) { |
| 10856 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10857 | return mServices.stopServiceTokenLocked(className, token, startId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10858 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10859 | } |
| 10860 | |
| 10861 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10862 | int id, Notification notification, boolean removeNotification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10863 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10864 | mServices.setServiceForegroundLocked(className, token, id, notification, |
| 10865 | removeNotification); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10866 | } |
| 10867 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10868 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10869 | public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 10870 | boolean requireFull, String name, String callerPackage) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10871 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 10872 | if (callingUserId != userId) { |
| 10873 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 10874 | if ((requireFull || checkComponentPermission( |
| 10875 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10876 | callingPid, callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) |
| 10877 | && checkComponentPermission( |
| 10878 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 10879 | callingPid, callingUid, -1, true) |
| 10880 | != PackageManager.PERMISSION_GRANTED) { |
| 10881 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 10882 | // In this case, they would like to just execute as their |
| 10883 | // owner user instead of failing. |
| 10884 | userId = callingUserId; |
| 10885 | } else { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10886 | StringBuilder builder = new StringBuilder(128); |
| 10887 | builder.append("Permission Denial: "); |
| 10888 | builder.append(name); |
| 10889 | if (callerPackage != null) { |
| 10890 | builder.append(" from "); |
| 10891 | builder.append(callerPackage); |
| 10892 | } |
| 10893 | builder.append(" asks to run as user "); |
| 10894 | builder.append(userId); |
| 10895 | builder.append(" but is calling from user "); |
| 10896 | builder.append(UserHandle.getUserId(callingUid)); |
| 10897 | builder.append("; this requires "); |
| 10898 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 10899 | if (!requireFull) { |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 10900 | builder.append(" or "); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10901 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 10902 | } |
| 10903 | String msg = builder.toString(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10904 | Slog.w(TAG, msg); |
| 10905 | throw new SecurityException(msg); |
| 10906 | } |
| 10907 | } |
| 10908 | } |
| 10909 | if (userId == UserHandle.USER_CURRENT |
| 10910 | || userId == UserHandle.USER_CURRENT_OR_SELF) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 10911 | // Note that we may be accessing this outside of a lock... |
| 10912 | // shouldn't be a big deal, if this is being called outside |
| 10913 | // of a locked context there is intrinsically a race with |
| 10914 | // the value the caller will receive and someone else changing it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10915 | userId = mCurrentUserId; |
| 10916 | } |
| 10917 | if (!allowAll && userId < 0) { |
| 10918 | throw new IllegalArgumentException( |
| 10919 | "Call does not support special user #" + userId); |
| 10920 | } |
| 10921 | } |
| 10922 | return userId; |
| 10923 | } |
| 10924 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10925 | boolean isSingleton(String componentProcessName, ApplicationInfo aInfo, |
| 10926 | String className, int flags) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10927 | boolean result = false; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10928 | if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10929 | if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) { |
| 10930 | if (ActivityManager.checkUidPermission( |
| 10931 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10932 | aInfo.uid) != PackageManager.PERMISSION_GRANTED) { |
| 10933 | ComponentName comp = new ComponentName(aInfo.packageName, className); |
| 10934 | String msg = "Permission Denial: Component " + comp.flattenToShortString() |
| 10935 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 10936 | + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 10937 | Slog.w(TAG, msg); |
| 10938 | throw new SecurityException(msg); |
| 10939 | } |
| 10940 | result = true; |
| 10941 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10942 | } else if (componentProcessName == aInfo.packageName) { |
| 10943 | result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 10944 | } else if ("system".equals(componentProcessName)) { |
| 10945 | result = true; |
| 10946 | } |
| 10947 | if (DEBUG_MU) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10948 | Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo |
| 10949 | + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10950 | } |
| 10951 | return result; |
| 10952 | } |
| 10953 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10954 | public int bindService(IApplicationThread caller, IBinder token, |
| 10955 | Intent service, String resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10956 | IServiceConnection connection, int flags, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10957 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10958 | // Refuse possible leaked file descriptors |
| 10959 | if (service != null && service.hasFileDescriptors() == true) { |
| 10960 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10961 | } |
| 10962 | |
| 10963 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10964 | return mServices.bindServiceLocked(caller, token, service, resolvedType, |
| 10965 | connection, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10966 | } |
| 10967 | } |
| 10968 | |
| 10969 | public boolean unbindService(IServiceConnection connection) { |
| 10970 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10971 | return mServices.unbindServiceLocked(connection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10972 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10973 | } |
| 10974 | |
| 10975 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 10976 | // Refuse possible leaked file descriptors |
| 10977 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10978 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10979 | } |
| 10980 | |
| 10981 | synchronized(this) { |
| 10982 | if (!(token instanceof ServiceRecord)) { |
| 10983 | throw new IllegalArgumentException("Invalid service token"); |
| 10984 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10985 | mServices.publishServiceLocked((ServiceRecord)token, intent, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10986 | } |
| 10987 | } |
| 10988 | |
| 10989 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 10990 | // Refuse possible leaked file descriptors |
| 10991 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10992 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10993 | } |
| 10994 | |
| 10995 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10996 | mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10997 | } |
| 10998 | } |
| 10999 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11000 | 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] | 11001 | synchronized(this) { |
| 11002 | if (!(token instanceof ServiceRecord)) { |
| 11003 | throw new IllegalArgumentException("Invalid service token"); |
| 11004 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11005 | mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11006 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11007 | } |
| 11008 | |
| 11009 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11010 | // BACKUP AND RESTORE |
| 11011 | // ========================================================= |
| 11012 | |
| 11013 | // Cause the target app to be launched if necessary and its backup agent |
| 11014 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11015 | // activity manager to announce its creation. |
| 11016 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11017 | if (DEBUG_BACKUP) Slog.v(TAG, "bindBackupAgent: app=" + app + " mode=" + backupMode); |
| 11018 | enforceCallingPermission("android.permission.BACKUP", "bindBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11019 | |
| 11020 | synchronized(this) { |
| 11021 | // !!! TODO: currently no check here that we're already bound |
| 11022 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11023 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11024 | synchronized (stats) { |
| 11025 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11026 | } |
| 11027 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11028 | // Backup agent is now in use, its package can't be stopped. |
| 11029 | try { |
| 11030 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11031 | app.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11032 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11033 | } catch (IllegalArgumentException e) { |
| 11034 | Slog.w(TAG, "Failed trying to unstop package " |
| 11035 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11036 | } |
| 11037 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11038 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11039 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11040 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11041 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11042 | // startProcessLocked() returns existing proc's record if it's already running |
| 11043 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11044 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11045 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11046 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11047 | return false; |
| 11048 | } |
| 11049 | |
| 11050 | r.app = proc; |
| 11051 | mBackupTarget = r; |
| 11052 | mBackupAppName = app.packageName; |
| 11053 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11054 | // Try not to kill the process during backup |
| 11055 | updateOomAdjLocked(proc); |
| 11056 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11057 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11058 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11059 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11060 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11061 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11062 | proc.thread.scheduleCreateBackupAgent(app, |
| 11063 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11064 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11065 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11066 | } |
| 11067 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11068 | 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] | 11069 | } |
| 11070 | // Invariants: at this point, the target app process exists and the application |
| 11071 | // is either already running or in the process of coming up. mBackupTarget and |
| 11072 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11073 | // know that it's scheduled for a backup-agent operation. |
| 11074 | } |
| 11075 | |
| 11076 | return true; |
| 11077 | } |
| 11078 | |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11079 | @Override |
| 11080 | public void clearPendingBackup() { |
| 11081 | if (DEBUG_BACKUP) Slog.v(TAG, "clearPendingBackup"); |
| 11082 | enforceCallingPermission("android.permission.BACKUP", "clearPendingBackup"); |
| 11083 | |
| 11084 | synchronized (this) { |
| 11085 | mBackupTarget = null; |
| 11086 | mBackupAppName = null; |
| 11087 | } |
| 11088 | } |
| 11089 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11090 | // A backup agent has just come up |
| 11091 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11092 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11093 | + " = " + agent); |
| 11094 | |
| 11095 | synchronized(this) { |
| 11096 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11097 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11098 | return; |
| 11099 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11100 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11101 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11102 | long oldIdent = Binder.clearCallingIdentity(); |
| 11103 | try { |
| 11104 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 11105 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 11106 | bm.agentConnected(agentPackageName, agent); |
| 11107 | } catch (RemoteException e) { |
| 11108 | // can't happen; the backup manager service is local |
| 11109 | } catch (Exception e) { |
| 11110 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 11111 | e.printStackTrace(); |
| 11112 | } finally { |
| 11113 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11114 | } |
| 11115 | } |
| 11116 | |
| 11117 | // done with this agent |
| 11118 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11119 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11120 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11121 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11122 | return; |
| 11123 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11124 | |
| 11125 | synchronized(this) { |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11126 | try { |
| 11127 | if (mBackupAppName == null) { |
| 11128 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
| 11129 | return; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11130 | } |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11131 | |
| 11132 | if (!mBackupAppName.equals(appInfo.packageName)) { |
| 11133 | Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target"); |
| 11134 | return; |
| 11135 | } |
| 11136 | |
| 11137 | // Not backing this app up any more; reset its OOM adjustment |
| 11138 | final ProcessRecord proc = mBackupTarget.app; |
| 11139 | updateOomAdjLocked(proc); |
| 11140 | |
| 11141 | // If the app crashed during backup, 'thread' will be null here |
| 11142 | if (proc.thread != null) { |
| 11143 | try { |
| 11144 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 11145 | compatibilityInfoForPackageLocked(appInfo)); |
| 11146 | } catch (Exception e) { |
| 11147 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
| 11148 | e.printStackTrace(); |
| 11149 | } |
| 11150 | } |
| 11151 | } finally { |
| 11152 | mBackupTarget = null; |
| 11153 | mBackupAppName = null; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11154 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11155 | } |
| 11156 | } |
| 11157 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11158 | // BROADCASTS |
| 11159 | // ========================================================= |
| 11160 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11161 | private final List getStickiesLocked(String action, IntentFilter filter, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11162 | List cur, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11163 | final ContentResolver resolver = mContext.getContentResolver(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11164 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11165 | if (stickies == null) { |
| 11166 | return cur; |
| 11167 | } |
| 11168 | final ArrayList<Intent> list = stickies.get(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11169 | if (list == null) { |
| 11170 | return cur; |
| 11171 | } |
| 11172 | int N = list.size(); |
| 11173 | for (int i=0; i<N; i++) { |
| 11174 | Intent intent = list.get(i); |
| 11175 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 11176 | if (cur == null) { |
| 11177 | cur = new ArrayList<Intent>(); |
| 11178 | } |
| 11179 | cur.add(intent); |
| 11180 | } |
| 11181 | } |
| 11182 | return cur; |
| 11183 | } |
| 11184 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11185 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 11186 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 11187 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 11188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11189 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11190 | void skipPendingBroadcastLocked(int pid) { |
| 11191 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 11192 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11193 | queue.skipPendingBroadcastLocked(pid); |
| 11194 | } |
| 11195 | } |
| 11196 | |
| 11197 | // The app just attached; send any pending broadcasts that it should receive |
| 11198 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 11199 | boolean didSomething = false; |
| 11200 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11201 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11202 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11203 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11204 | } |
| 11205 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11206 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11207 | IIntentReceiver receiver, IntentFilter filter, String permission, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11208 | enforceNotIsolatedCaller("registerReceiver"); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11209 | int callingUid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11210 | int callingPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11211 | synchronized(this) { |
| 11212 | ProcessRecord callerApp = null; |
| 11213 | if (caller != null) { |
| 11214 | callerApp = getRecordForAppLocked(caller); |
| 11215 | if (callerApp == null) { |
| 11216 | throw new SecurityException( |
| 11217 | "Unable to find app for caller " + caller |
| 11218 | + " (pid=" + Binder.getCallingPid() |
| 11219 | + ") when registering receiver " + receiver); |
| 11220 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11221 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 11222 | !callerApp.pkgList.contains(callerPackage)) { |
| 11223 | throw new SecurityException("Given caller package " + callerPackage |
| 11224 | + " is not running in process " + callerApp); |
| 11225 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11226 | callingUid = callerApp.info.uid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11227 | callingPid = callerApp.pid; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11228 | } else { |
| 11229 | callerPackage = null; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11230 | callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11231 | callingPid = Binder.getCallingPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11232 | } |
| 11233 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11234 | userId = this.handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11235 | true, true, "registerReceiver", callerPackage); |
| 11236 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11237 | List allSticky = null; |
| 11238 | |
| 11239 | // Look for any matching sticky broadcasts... |
| 11240 | Iterator actions = filter.actionsIterator(); |
| 11241 | if (actions != null) { |
| 11242 | while (actions.hasNext()) { |
| 11243 | String action = (String)actions.next(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11244 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11245 | UserHandle.USER_ALL); |
| 11246 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11247 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11248 | } |
| 11249 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11250 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11251 | UserHandle.USER_ALL); |
| 11252 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11253 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11254 | } |
| 11255 | |
| 11256 | // The first sticky in the list is returned directly back to |
| 11257 | // the client. |
| 11258 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 11259 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11260 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11261 | + ": " + sticky); |
| 11262 | |
| 11263 | if (receiver == null) { |
| 11264 | return sticky; |
| 11265 | } |
| 11266 | |
| 11267 | ReceiverList rl |
| 11268 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11269 | if (rl == null) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11270 | rl = new ReceiverList(this, callerApp, callingPid, callingUid, |
| 11271 | userId, receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11272 | if (rl.app != null) { |
| 11273 | rl.app.receivers.add(rl); |
| 11274 | } else { |
| 11275 | try { |
| 11276 | receiver.asBinder().linkToDeath(rl, 0); |
| 11277 | } catch (RemoteException e) { |
| 11278 | return sticky; |
| 11279 | } |
| 11280 | rl.linkedToDeath = true; |
| 11281 | } |
| 11282 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11283 | } else if (rl.uid != callingUid) { |
| 11284 | throw new IllegalArgumentException( |
| 11285 | "Receiver requested to register for uid " + callingUid |
| 11286 | + " was previously registered for uid " + rl.uid); |
| 11287 | } else if (rl.pid != callingPid) { |
| 11288 | throw new IllegalArgumentException( |
| 11289 | "Receiver requested to register for pid " + callingPid |
| 11290 | + " was previously registered for pid " + rl.pid); |
| 11291 | } else if (rl.userId != userId) { |
| 11292 | throw new IllegalArgumentException( |
| 11293 | "Receiver requested to register for user " + userId |
| 11294 | + " was previously registered for user " + rl.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11295 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11296 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11297 | permission, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11298 | rl.add(bf); |
| 11299 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11300 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11301 | } |
| 11302 | mReceiverResolver.addFilter(bf); |
| 11303 | |
| 11304 | // Enqueue broadcasts for all existing stickies that match |
| 11305 | // this filter. |
| 11306 | if (allSticky != null) { |
| 11307 | ArrayList receivers = new ArrayList(); |
| 11308 | receivers.add(bf); |
| 11309 | |
| 11310 | int N = allSticky.size(); |
| 11311 | for (int i=0; i<N; i++) { |
| 11312 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11313 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11314 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 11315 | null, -1, -1, null, AppOpsManager.OP_NONE, receivers, null, 0, |
| 11316 | null, null, false, true, true, -1); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11317 | queue.enqueueParallelBroadcastLocked(r); |
| 11318 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11319 | } |
| 11320 | } |
| 11321 | |
| 11322 | return sticky; |
| 11323 | } |
| 11324 | } |
| 11325 | |
| 11326 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11327 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11328 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11329 | final long origId = Binder.clearCallingIdentity(); |
| 11330 | try { |
| 11331 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11332 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11333 | synchronized(this) { |
| 11334 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11335 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11336 | if (rl != null) { |
| 11337 | if (rl.curBroadcast != null) { |
| 11338 | BroadcastRecord r = rl.curBroadcast; |
| 11339 | final boolean doNext = finishReceiverLocked( |
| 11340 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11341 | r.resultExtras, r.resultAbort, true); |
| 11342 | if (doNext) { |
| 11343 | doTrim = true; |
| 11344 | r.queue.processNextBroadcast(false); |
| 11345 | } |
| 11346 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11347 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11348 | if (rl.app != null) { |
| 11349 | rl.app.receivers.remove(rl); |
| 11350 | } |
| 11351 | removeReceiverLocked(rl); |
| 11352 | if (rl.linkedToDeath) { |
| 11353 | rl.linkedToDeath = false; |
| 11354 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 11355 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11356 | } |
| 11357 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11358 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11359 | // If we actually concluded any broadcasts, we might now be able |
| 11360 | // to trim the recipients' apps from our working set |
| 11361 | if (doTrim) { |
| 11362 | trimApplications(); |
| 11363 | return; |
| 11364 | } |
| 11365 | |
| 11366 | } finally { |
| 11367 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11369 | } |
| 11370 | |
| 11371 | void removeReceiverLocked(ReceiverList rl) { |
| 11372 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 11373 | int N = rl.size(); |
| 11374 | for (int i=0; i<N; i++) { |
| 11375 | mReceiverResolver.removeFilter(rl.get(i)); |
| 11376 | } |
| 11377 | } |
| 11378 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11379 | private final void sendPackageBroadcastLocked(int cmd, String[] packages, int userId) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11380 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 11381 | ProcessRecord r = mLruProcesses.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11382 | if (r.thread != null && (userId == UserHandle.USER_ALL || r.userId == userId)) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11383 | try { |
| 11384 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 11385 | } catch (RemoteException ex) { |
| 11386 | } |
| 11387 | } |
| 11388 | } |
| 11389 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11390 | |
| 11391 | private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType, |
| 11392 | int[] users) { |
| 11393 | List<ResolveInfo> receivers = null; |
| 11394 | try { |
| 11395 | HashSet<ComponentName> singleUserReceivers = null; |
| 11396 | boolean scannedFirstReceivers = false; |
| 11397 | for (int user : users) { |
| 11398 | List<ResolveInfo> newReceivers = AppGlobals.getPackageManager() |
| 11399 | .queryIntentReceivers(intent, resolvedType, STOCK_PM_FLAGS, user); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 11400 | if (user != 0 && newReceivers != null) { |
| 11401 | // If this is not the primary user, we need to check for |
| 11402 | // any receivers that should be filtered out. |
| 11403 | for (int i=0; i<newReceivers.size(); i++) { |
| 11404 | ResolveInfo ri = newReceivers.get(i); |
| 11405 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_PRIMARY_USER_ONLY) != 0) { |
| 11406 | newReceivers.remove(i); |
| 11407 | i--; |
| 11408 | } |
| 11409 | } |
| 11410 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11411 | if (newReceivers != null && newReceivers.size() == 0) { |
| 11412 | newReceivers = null; |
| 11413 | } |
| 11414 | if (receivers == null) { |
| 11415 | receivers = newReceivers; |
| 11416 | } else if (newReceivers != null) { |
| 11417 | // We need to concatenate the additional receivers |
| 11418 | // found with what we have do far. This would be easy, |
| 11419 | // but we also need to de-dup any receivers that are |
| 11420 | // singleUser. |
| 11421 | if (!scannedFirstReceivers) { |
| 11422 | // Collect any single user receivers we had already retrieved. |
| 11423 | scannedFirstReceivers = true; |
| 11424 | for (int i=0; i<receivers.size(); i++) { |
| 11425 | ResolveInfo ri = receivers.get(i); |
| 11426 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11427 | ComponentName cn = new ComponentName( |
| 11428 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11429 | if (singleUserReceivers == null) { |
| 11430 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11431 | } |
| 11432 | singleUserReceivers.add(cn); |
| 11433 | } |
| 11434 | } |
| 11435 | } |
| 11436 | // Add the new results to the existing results, tracking |
| 11437 | // and de-dupping single user receivers. |
| 11438 | for (int i=0; i<newReceivers.size(); i++) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 11439 | ResolveInfo ri = newReceivers.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11440 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11441 | ComponentName cn = new ComponentName( |
| 11442 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11443 | if (singleUserReceivers == null) { |
| 11444 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11445 | } |
| 11446 | if (!singleUserReceivers.contains(cn)) { |
| 11447 | singleUserReceivers.add(cn); |
| 11448 | receivers.add(ri); |
| 11449 | } |
| 11450 | } else { |
| 11451 | receivers.add(ri); |
| 11452 | } |
| 11453 | } |
| 11454 | } |
| 11455 | } |
| 11456 | } catch (RemoteException ex) { |
| 11457 | // pm is in same process, this will never happen. |
| 11458 | } |
| 11459 | return receivers; |
| 11460 | } |
| 11461 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11462 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 11463 | String callerPackage, Intent intent, String resolvedType, |
| 11464 | IIntentReceiver resultTo, int resultCode, String resultData, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 11465 | Bundle map, String requiredPermission, int appOp, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11466 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 11467 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11468 | intent = new Intent(intent); |
| 11469 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11470 | // By default broadcasts do not go to stopped apps. |
| 11471 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 11472 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11473 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11474 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 11475 | + " ordered=" + ordered + " userid=" + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11476 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11477 | 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] | 11478 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11479 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11480 | userId = handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11481 | true, false, "broadcast", callerPackage); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11482 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11483 | // Make sure that the user who is receiving this broadcast is started. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11484 | // If not, we will just skip it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11485 | if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 11486 | if (callingUid != Process.SYSTEM_UID || (intent.getFlags() |
| 11487 | & Intent.FLAG_RECEIVER_BOOT_UPGRADE) == 0) { |
| 11488 | Slog.w(TAG, "Skipping broadcast of " + intent |
| 11489 | + ": user " + userId + " is stopped"); |
| 11490 | return ActivityManager.BROADCAST_SUCCESS; |
| 11491 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11492 | } |
| 11493 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11494 | /* |
| 11495 | * Prevent non-system code (defined here to be non-persistent |
| 11496 | * processes) from sending protected broadcasts. |
| 11497 | */ |
Dianne Hackborn | c7ba771 | 2012-09-26 23:22:59 -0700 | [diff] [blame] | 11498 | int callingAppId = UserHandle.getAppId(callingUid); |
| 11499 | if (callingAppId == Process.SYSTEM_UID || callingAppId == Process.PHONE_UID |
| 11500 | || callingAppId == Process.SHELL_UID || callingAppId == Process.BLUETOOTH_UID || |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11501 | callingUid == 0) { |
| 11502 | // Always okay. |
| 11503 | } else if (callerApp == null || !callerApp.persistent) { |
| 11504 | try { |
| 11505 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
| 11506 | intent.getAction())) { |
| 11507 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11508 | + intent.getAction() + " from pid=" |
| 11509 | + callingPid + ", uid=" + callingUid; |
| 11510 | Slog.w(TAG, msg); |
| 11511 | throw new SecurityException(msg); |
| 11512 | } |
| 11513 | } catch (RemoteException e) { |
| 11514 | Slog.w(TAG, "Remote exception", e); |
| 11515 | return ActivityManager.BROADCAST_SUCCESS; |
| 11516 | } |
| 11517 | } |
| 11518 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11519 | // Handle special intents: if this broadcast is from the package |
| 11520 | // manager about a package being removed, we need to remove all of |
| 11521 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11522 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11523 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11524 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11525 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11526 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11527 | || uidRemoved) { |
| 11528 | if (checkComponentPermission( |
| 11529 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11530 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11531 | == PackageManager.PERMISSION_GRANTED) { |
| 11532 | if (uidRemoved) { |
| 11533 | final Bundle intentExtras = intent.getExtras(); |
| 11534 | final int uid = intentExtras != null |
| 11535 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11536 | if (uid >= 0) { |
| 11537 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11538 | synchronized (bs) { |
| 11539 | bs.removeUidStatsLocked(uid); |
| 11540 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 11541 | mAppOpsService.uidRemoved(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11542 | } |
| 11543 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11544 | // If resources are unavailable just force stop all |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11545 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11546 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11547 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11548 | if (list != null && (list.length > 0)) { |
| 11549 | for (String pkg : list) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11550 | forceStopPackageLocked(pkg, -1, false, true, true, false, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11551 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11552 | sendPackageBroadcastLocked( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11553 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11554 | } |
| 11555 | } else { |
| 11556 | Uri data = intent.getData(); |
| 11557 | String ssp; |
| 11558 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11559 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11560 | forceStopPackageLocked(ssp, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11561 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, |
| 11562 | false, userId); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11563 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11564 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11565 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11566 | new String[] {ssp}, userId); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 11567 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 11568 | mAppOpsService.packageRemoved( |
| 11569 | intent.getIntExtra(Intent.EXTRA_UID, -1), ssp); |
| 11570 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11571 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11572 | } |
| 11573 | } |
| 11574 | } |
| 11575 | } else { |
| 11576 | String msg = "Permission Denial: " + intent.getAction() |
| 11577 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11578 | + ", uid=" + callingUid + ")" |
| 11579 | + " requires " |
| 11580 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11581 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11582 | throw new SecurityException(msg); |
| 11583 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11584 | |
| 11585 | // Special case for adding a package: by default turn on compatibility |
| 11586 | // mode. |
| 11587 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 11588 | Uri data = intent.getData(); |
| 11589 | String ssp; |
| 11590 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11591 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 11592 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11593 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11594 | } |
| 11595 | |
| 11596 | /* |
| 11597 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11598 | * of all currently running processes. This message will get queued up before the broadcast |
| 11599 | * happens. |
| 11600 | */ |
| 11601 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11602 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11603 | } |
| 11604 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11605 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11606 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11607 | } |
| 11608 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11609 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11610 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11611 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11612 | } |
| 11613 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11614 | // Add to the sticky list if requested. |
| 11615 | if (sticky) { |
| 11616 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11617 | callingPid, callingUid) |
| 11618 | != PackageManager.PERMISSION_GRANTED) { |
| 11619 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11620 | + callingPid + ", uid=" + callingUid |
| 11621 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11622 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11623 | throw new SecurityException(msg); |
| 11624 | } |
| 11625 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11626 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11627 | + " and enforce permission " + requiredPermission); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11628 | return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11629 | } |
| 11630 | if (intent.getComponent() != null) { |
| 11631 | throw new SecurityException( |
| 11632 | "Sticky broadcasts can't target a specific component"); |
| 11633 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11634 | // We use userId directly here, since the "all" target is maintained |
| 11635 | // as a separate set of sticky broadcasts. |
| 11636 | if (userId != UserHandle.USER_ALL) { |
| 11637 | // But first, if this is not a broadcast to all users, then |
| 11638 | // make sure it doesn't conflict with an existing broadcast to |
| 11639 | // all users. |
| 11640 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get( |
| 11641 | UserHandle.USER_ALL); |
| 11642 | if (stickies != null) { |
| 11643 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11644 | if (list != null) { |
| 11645 | int N = list.size(); |
| 11646 | int i; |
| 11647 | for (i=0; i<N; i++) { |
| 11648 | if (intent.filterEquals(list.get(i))) { |
| 11649 | throw new IllegalArgumentException( |
| 11650 | "Sticky broadcast " + intent + " for user " |
| 11651 | + userId + " conflicts with existing global broadcast"); |
| 11652 | } |
| 11653 | } |
| 11654 | } |
| 11655 | } |
| 11656 | } |
| 11657 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11658 | if (stickies == null) { |
| 11659 | stickies = new HashMap<String, ArrayList<Intent>>(); |
| 11660 | mStickyBroadcasts.put(userId, stickies); |
| 11661 | } |
| 11662 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11663 | if (list == null) { |
| 11664 | list = new ArrayList<Intent>(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11665 | stickies.put(intent.getAction(), list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11666 | } |
| 11667 | int N = list.size(); |
| 11668 | int i; |
| 11669 | for (i=0; i<N; i++) { |
| 11670 | if (intent.filterEquals(list.get(i))) { |
| 11671 | // This sticky already exists, replace it. |
| 11672 | list.set(i, new Intent(intent)); |
| 11673 | break; |
| 11674 | } |
| 11675 | } |
| 11676 | if (i >= N) { |
| 11677 | list.add(new Intent(intent)); |
| 11678 | } |
| 11679 | } |
| 11680 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11681 | int[] users; |
| 11682 | if (userId == UserHandle.USER_ALL) { |
| 11683 | // Caller wants broadcast to go to all started users. |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11684 | users = mStartedUserArray; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11685 | } else { |
| 11686 | // Caller wants broadcast to go to one specific user. |
Amith Yamasani | e98bde0 | 2012-10-01 11:30:47 -0700 | [diff] [blame] | 11687 | users = new int[] {userId}; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11688 | } |
| 11689 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11690 | // Figure out who all will receive this broadcast. |
| 11691 | List receivers = null; |
| 11692 | List<BroadcastFilter> registeredReceivers = null; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11693 | // Need to resolve the intent to interested receivers... |
| 11694 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11695 | == 0) { |
| 11696 | receivers = collectReceiverComponents(intent, resolvedType, users); |
| 11697 | } |
| 11698 | if (intent.getComponent() == null) { |
| 11699 | registeredReceivers = mReceiverResolver.queryIntent(intent, |
| 11700 | resolvedType, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11701 | } |
| 11702 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11703 | final boolean replacePending = |
| 11704 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11705 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11706 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11707 | + " replacePending=" + replacePending); |
| 11708 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11709 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11710 | if (!ordered && NR > 0) { |
| 11711 | // If we are not serializing this broadcast, then send the |
| 11712 | // registered receivers separately so they don't wait for the |
| 11713 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11714 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11715 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 11716 | callerPackage, callingPid, callingUid, requiredPermission, appOp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11717 | registeredReceivers, resultTo, resultCode, resultData, map, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11718 | ordered, sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11719 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11720 | TAG, "Enqueueing parallel broadcast " + r); |
| 11721 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11722 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11723 | queue.enqueueParallelBroadcastLocked(r); |
| 11724 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11725 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11726 | registeredReceivers = null; |
| 11727 | NR = 0; |
| 11728 | } |
| 11729 | |
| 11730 | // Merge into one list. |
| 11731 | int ir = 0; |
| 11732 | if (receivers != null) { |
| 11733 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11734 | // being added to see this broadcast. This prevents them from |
| 11735 | // using this as a back door to get run as soon as they are |
| 11736 | // installed. Maybe in the future we want to have a special install |
| 11737 | // broadcast or such for apps, but we'd like to deliberately make |
| 11738 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11739 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11740 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11741 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11742 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11743 | Uri data = intent.getData(); |
| 11744 | if (data != null) { |
| 11745 | String pkgName = data.getSchemeSpecificPart(); |
| 11746 | if (pkgName != null) { |
| 11747 | skipPackages = new String[] { pkgName }; |
| 11748 | } |
| 11749 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11750 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11751 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11752 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11753 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 11754 | for (String skipPackage : skipPackages) { |
| 11755 | if (skipPackage != null) { |
| 11756 | int NT = receivers.size(); |
| 11757 | for (int it=0; it<NT; it++) { |
| 11758 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 11759 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 11760 | receivers.remove(it); |
| 11761 | it--; |
| 11762 | NT--; |
| 11763 | } |
| 11764 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11765 | } |
| 11766 | } |
| 11767 | } |
| 11768 | |
| 11769 | int NT = receivers != null ? receivers.size() : 0; |
| 11770 | int it = 0; |
| 11771 | ResolveInfo curt = null; |
| 11772 | BroadcastFilter curr = null; |
| 11773 | while (it < NT && ir < NR) { |
| 11774 | if (curt == null) { |
| 11775 | curt = (ResolveInfo)receivers.get(it); |
| 11776 | } |
| 11777 | if (curr == null) { |
| 11778 | curr = registeredReceivers.get(ir); |
| 11779 | } |
| 11780 | if (curr.getPriority() >= curt.priority) { |
| 11781 | // Insert this broadcast record into the final list. |
| 11782 | receivers.add(it, curr); |
| 11783 | ir++; |
| 11784 | curr = null; |
| 11785 | it++; |
| 11786 | NT++; |
| 11787 | } else { |
| 11788 | // Skip to the next ResolveInfo in the final list. |
| 11789 | it++; |
| 11790 | curt = null; |
| 11791 | } |
| 11792 | } |
| 11793 | } |
| 11794 | while (ir < NR) { |
| 11795 | if (receivers == null) { |
| 11796 | receivers = new ArrayList(); |
| 11797 | } |
| 11798 | receivers.add(registeredReceivers.get(ir)); |
| 11799 | ir++; |
| 11800 | } |
| 11801 | |
| 11802 | if ((receivers != null && receivers.size() > 0) |
| 11803 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11804 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11805 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 11806 | callerPackage, callingPid, callingUid, requiredPermission, appOp, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11807 | receivers, resultTo, resultCode, resultData, map, ordered, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11808 | sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11809 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11810 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11811 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11812 | if (DEBUG_BROADCAST) { |
| 11813 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11814 | 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] | 11815 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11816 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11817 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11818 | queue.enqueueOrderedBroadcastLocked(r); |
| 11819 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11820 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11821 | } |
| 11822 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11823 | return ActivityManager.BROADCAST_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11824 | } |
| 11825 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11826 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11827 | // Refuse possible leaked file descriptors |
| 11828 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11829 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11830 | } |
| 11831 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11832 | int flags = intent.getFlags(); |
| 11833 | |
| 11834 | if (!mProcessesReady) { |
| 11835 | // if the caller really truly claims to know what they're doing, go |
| 11836 | // ahead and allow the broadcast without launching any receivers |
| 11837 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 11838 | intent = new Intent(intent); |
| 11839 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 11840 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 11841 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 11842 | + " before boot completion"); |
| 11843 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 11844 | } |
| 11845 | } |
| 11846 | |
| 11847 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 11848 | throw new IllegalArgumentException( |
| 11849 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 11850 | } |
| 11851 | |
| 11852 | return intent; |
| 11853 | } |
| 11854 | |
| 11855 | public final int broadcastIntent(IApplicationThread caller, |
| 11856 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11857 | int resultCode, String resultData, Bundle map, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 11858 | String requiredPermission, int appOp, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11859 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11860 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11861 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11862 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11863 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11864 | final int callingPid = Binder.getCallingPid(); |
| 11865 | final int callingUid = Binder.getCallingUid(); |
| 11866 | final long origId = Binder.clearCallingIdentity(); |
| 11867 | int res = broadcastIntentLocked(callerApp, |
| 11868 | callerApp != null ? callerApp.info.packageName : null, |
| 11869 | intent, resolvedType, resultTo, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 11870 | resultCode, resultData, map, requiredPermission, appOp, serialized, sticky, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11871 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11872 | Binder.restoreCallingIdentity(origId); |
| 11873 | return res; |
| 11874 | } |
| 11875 | } |
| 11876 | |
| 11877 | int broadcastIntentInPackage(String packageName, int uid, |
| 11878 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11879 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11880 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11881 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11882 | intent = verifyBroadcastLocked(intent); |
| 11883 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11884 | final long origId = Binder.clearCallingIdentity(); |
| 11885 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 11886 | resultTo, resultCode, resultData, map, requiredPermission, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 11887 | AppOpsManager.OP_NONE, serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11888 | Binder.restoreCallingIdentity(origId); |
| 11889 | return res; |
| 11890 | } |
| 11891 | } |
| 11892 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11893 | 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] | 11894 | // Refuse possible leaked file descriptors |
| 11895 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11896 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11897 | } |
| 11898 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11899 | userId = handleIncomingUser(Binder.getCallingPid(), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11900 | Binder.getCallingUid(), userId, true, false, "removeStickyBroadcast", null); |
| 11901 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11902 | synchronized(this) { |
| 11903 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 11904 | != PackageManager.PERMISSION_GRANTED) { |
| 11905 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 11906 | + Binder.getCallingPid() |
| 11907 | + ", uid=" + Binder.getCallingUid() |
| 11908 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11909 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11910 | throw new SecurityException(msg); |
| 11911 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11912 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11913 | if (stickies != null) { |
| 11914 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11915 | if (list != null) { |
| 11916 | int N = list.size(); |
| 11917 | int i; |
| 11918 | for (i=0; i<N; i++) { |
| 11919 | if (intent.filterEquals(list.get(i))) { |
| 11920 | list.remove(i); |
| 11921 | break; |
| 11922 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11923 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11924 | if (list.size() <= 0) { |
| 11925 | stickies.remove(intent.getAction()); |
| 11926 | } |
| 11927 | } |
| 11928 | if (stickies.size() <= 0) { |
| 11929 | mStickyBroadcasts.remove(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11930 | } |
| 11931 | } |
| 11932 | } |
| 11933 | } |
| 11934 | |
| 11935 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 11936 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 11937 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11938 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 11939 | if (r == null) { |
| 11940 | 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] | 11941 | return false; |
| 11942 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11943 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11944 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 11945 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11946 | } |
| 11947 | |
| 11948 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 11949 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11950 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11951 | |
| 11952 | // Refuse possible leaked file descriptors |
| 11953 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 11954 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11955 | } |
| 11956 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11957 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11958 | try { |
| 11959 | boolean doNext = false; |
| 11960 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11961 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11962 | synchronized(this) { |
| 11963 | r = broadcastRecordForReceiverLocked(who); |
| 11964 | if (r != null) { |
| 11965 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 11966 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11967 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11968 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11969 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11970 | if (doNext) { |
| 11971 | r.queue.processNextBroadcast(false); |
| 11972 | } |
| 11973 | trimApplications(); |
| 11974 | } finally { |
| 11975 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11976 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11977 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11978 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11979 | // ========================================================= |
| 11980 | // INSTRUMENTATION |
| 11981 | // ========================================================= |
| 11982 | |
| 11983 | public boolean startInstrumentation(ComponentName className, |
| 11984 | String profileFile, int flags, Bundle arguments, |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 11985 | IInstrumentationWatcher watcher, IUiAutomationConnection uiAutomationConnection, |
| 11986 | int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11987 | enforceNotIsolatedCaller("startInstrumentation"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11988 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 11989 | userId, false, true, "startInstrumentation", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11990 | // Refuse possible leaked file descriptors |
| 11991 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 11992 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11993 | } |
| 11994 | |
| 11995 | synchronized(this) { |
| 11996 | InstrumentationInfo ii = null; |
| 11997 | ApplicationInfo ai = null; |
| 11998 | try { |
| 11999 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12000 | className, STOCK_PM_FLAGS); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12001 | ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 12002 | ii.targetPackage, STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12003 | } catch (PackageManager.NameNotFoundException e) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12004 | } catch (RemoteException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12005 | } |
| 12006 | if (ii == null) { |
| 12007 | reportStartInstrumentationFailure(watcher, className, |
| 12008 | "Unable to find instrumentation info for: " + className); |
| 12009 | return false; |
| 12010 | } |
| 12011 | if (ai == null) { |
| 12012 | reportStartInstrumentationFailure(watcher, className, |
| 12013 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12014 | return false; |
| 12015 | } |
| 12016 | |
| 12017 | int match = mContext.getPackageManager().checkSignatures( |
| 12018 | ii.targetPackage, ii.packageName); |
| 12019 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12020 | String msg = "Permission Denial: starting instrumentation " |
| 12021 | + className + " from pid=" |
| 12022 | + Binder.getCallingPid() |
| 12023 | + ", uid=" + Binder.getCallingPid() |
| 12024 | + " not allowed because package " + ii.packageName |
| 12025 | + " does not have a signature matching the target " |
| 12026 | + ii.targetPackage; |
| 12027 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12028 | throw new SecurityException(msg); |
| 12029 | } |
| 12030 | |
| 12031 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12032 | // Instrumentation can kill and relaunch even persistent processes |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12033 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 12034 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12035 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12036 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12037 | app.instrumentationProfileFile = profileFile; |
| 12038 | app.instrumentationArguments = arguments; |
| 12039 | app.instrumentationWatcher = watcher; |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 12040 | app.instrumentationUiAutomationConnection = uiAutomationConnection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12041 | app.instrumentationResultClass = className; |
| 12042 | Binder.restoreCallingIdentity(origId); |
| 12043 | } |
| 12044 | |
| 12045 | return true; |
| 12046 | } |
| 12047 | |
| 12048 | /** |
| 12049 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 12050 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 12051 | * the "am" command to report errors with more information. |
| 12052 | * |
| 12053 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 12054 | * @param cn The component name of the instrumentation. |
| 12055 | * @param report The error report. |
| 12056 | */ |
| 12057 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 12058 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12059 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12060 | try { |
| 12061 | if (watcher != null) { |
| 12062 | Bundle results = new Bundle(); |
| 12063 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 12064 | results.putString("Error", report); |
| 12065 | watcher.instrumentationStatus(cn, -1, results); |
| 12066 | } |
| 12067 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12068 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12069 | } |
| 12070 | } |
| 12071 | |
| 12072 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 12073 | if (app.instrumentationWatcher != null) { |
| 12074 | try { |
| 12075 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 12076 | app.instrumentationWatcher.instrumentationFinished( |
| 12077 | app.instrumentationClass, |
| 12078 | resultCode, |
| 12079 | results); |
| 12080 | } catch (RemoteException e) { |
| 12081 | } |
| 12082 | } |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 12083 | if (app.instrumentationUiAutomationConnection != null) { |
| 12084 | try { |
| 12085 | app.instrumentationUiAutomationConnection.shutdown(); |
| 12086 | } catch (RemoteException re) { |
| 12087 | /* ignore */ |
| 12088 | } |
| 12089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12090 | app.instrumentationWatcher = null; |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 12091 | app.instrumentationUiAutomationConnection = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12092 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12093 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12094 | app.instrumentationProfileFile = null; |
| 12095 | app.instrumentationArguments = null; |
| 12096 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 12097 | 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] | 12098 | } |
| 12099 | |
| 12100 | public void finishInstrumentation(IApplicationThread target, |
| 12101 | int resultCode, Bundle results) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 12102 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12103 | // Refuse possible leaked file descriptors |
| 12104 | if (results != null && results.hasFileDescriptors()) { |
| 12105 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12106 | } |
| 12107 | |
| 12108 | synchronized(this) { |
| 12109 | ProcessRecord app = getRecordForAppLocked(target); |
| 12110 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12111 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12112 | return; |
| 12113 | } |
| 12114 | final long origId = Binder.clearCallingIdentity(); |
| 12115 | finishInstrumentationLocked(app, resultCode, results); |
| 12116 | Binder.restoreCallingIdentity(origId); |
| 12117 | } |
| 12118 | } |
| 12119 | |
| 12120 | // ========================================================= |
| 12121 | // CONFIGURATION |
| 12122 | // ========================================================= |
| 12123 | |
| 12124 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 12125 | ConfigurationInfo config = new ConfigurationInfo(); |
| 12126 | synchronized (this) { |
| 12127 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 12128 | config.reqKeyboardType = mConfiguration.keyboard; |
| 12129 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12130 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 12131 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12132 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 12133 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12134 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 12135 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12136 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 12137 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 12138 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12139 | } |
| 12140 | return config; |
| 12141 | } |
| 12142 | |
| 12143 | public Configuration getConfiguration() { |
| 12144 | Configuration ci; |
| 12145 | synchronized(this) { |
| 12146 | ci = new Configuration(mConfiguration); |
| 12147 | } |
| 12148 | return ci; |
| 12149 | } |
| 12150 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12151 | public void updatePersistentConfiguration(Configuration values) { |
| 12152 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12153 | "updateConfiguration()"); |
| 12154 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 12155 | "updateConfiguration()"); |
| 12156 | if (values == null) { |
| 12157 | throw new NullPointerException("Configuration must not be null"); |
| 12158 | } |
| 12159 | |
| 12160 | synchronized(this) { |
| 12161 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12162 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12163 | Binder.restoreCallingIdentity(origId); |
| 12164 | } |
| 12165 | } |
| 12166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12167 | public void updateConfiguration(Configuration values) { |
| 12168 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12169 | "updateConfiguration()"); |
| 12170 | |
| 12171 | synchronized(this) { |
| 12172 | if (values == null && mWindowManager != null) { |
| 12173 | // sentinel: fetch the current configuration from the window manager |
| 12174 | values = mWindowManager.computeNewConfiguration(); |
| 12175 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12176 | |
| 12177 | if (mWindowManager != null) { |
| 12178 | mProcessList.applyDisplaySize(mWindowManager); |
| 12179 | } |
| 12180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12181 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12182 | if (values != null) { |
| 12183 | Settings.System.clearConfiguration(values); |
| 12184 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12185 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12186 | Binder.restoreCallingIdentity(origId); |
| 12187 | } |
| 12188 | } |
| 12189 | |
| 12190 | /** |
| 12191 | * Do either or both things: (1) change the current configuration, and (2) |
| 12192 | * make sure the given activity is running with the (now) current |
| 12193 | * configuration. Returns true if the activity has been left running, or |
| 12194 | * false if <var>starting</var> is being destroyed to match the new |
| 12195 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12196 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12197 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12198 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12199 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 12200 | // do nothing if we are headless |
| 12201 | if (mHeadless) return true; |
| 12202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12203 | int changes = 0; |
| 12204 | |
| 12205 | boolean kept = true; |
| 12206 | |
| 12207 | if (values != null) { |
| 12208 | Configuration newConfig = new Configuration(mConfiguration); |
| 12209 | changes = newConfig.updateFrom(values); |
| 12210 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12211 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12212 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12213 | } |
| 12214 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12215 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12216 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12217 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12218 | saveLocaleLocked(values.locale, |
| 12219 | !values.locale.equals(mConfiguration.locale), |
| 12220 | values.userSetLocale); |
| 12221 | } |
| 12222 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12223 | mConfigurationSeq++; |
| 12224 | if (mConfigurationSeq <= 0) { |
| 12225 | mConfigurationSeq = 1; |
| 12226 | } |
| 12227 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12228 | mConfiguration = newConfig; |
Craig Mautner | ae44659 | 2012-12-06 19:05:05 -0800 | [diff] [blame] | 12229 | Slog.i(TAG, "Config changes=" + Integer.toHexString(changes) + " " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12230 | |
| 12231 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12232 | |
| 12233 | // TODO: If our config changes, should we auto dismiss any currently |
| 12234 | // showing dialogs? |
| 12235 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12236 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12237 | AttributeCache ac = AttributeCache.instance(); |
| 12238 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12239 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12240 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12241 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12242 | // Make sure all resources in our process are updated |
| 12243 | // right now, so that anyone who is going to retrieve |
| 12244 | // resource values after we return will be sure to get |
| 12245 | // the new ones. This is especially important during |
| 12246 | // boot, where the first config change needs to guarantee |
| 12247 | // all resources have that config before following boot |
| 12248 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12249 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12250 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12251 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12252 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12253 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12254 | mHandler.sendMessage(msg); |
| 12255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12256 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12257 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12258 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12259 | try { |
| 12260 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12261 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12262 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12263 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12264 | } |
| 12265 | } catch (Exception e) { |
| 12266 | } |
| 12267 | } |
| 12268 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12269 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 12270 | | Intent.FLAG_RECEIVER_REPLACE_PENDING |
| 12271 | | Intent.FLAG_RECEIVER_FOREGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12272 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 12273 | null, AppOpsManager.OP_NONE, false, false, MY_PID, |
| 12274 | Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12275 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 12276 | intent = new Intent(Intent.ACTION_LOCALE_CHANGED); |
| 12277 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 12278 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 12279 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 12280 | false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12281 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12282 | } |
| 12283 | } |
| 12284 | |
| 12285 | if (changes != 0 && starting == null) { |
| 12286 | // If the configuration changed, and the caller is not already |
| 12287 | // in the process of starting an activity, then find the top |
| 12288 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12289 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12290 | } |
| 12291 | |
| 12292 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12293 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12294 | // And we need to make sure at this point that all other activities |
| 12295 | // are made visible with the correct configuration. |
| 12296 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12297 | } |
| 12298 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12299 | if (values != null && mWindowManager != null) { |
| 12300 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12301 | } |
| 12302 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12303 | return kept; |
| 12304 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12305 | |
| 12306 | /** |
| 12307 | * Decide based on the configuration whether we should shouw the ANR, |
| 12308 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 12309 | * press the on-screen buttons, we shouldn't show the dialog. |
| 12310 | * |
| 12311 | * A thought: SystemUI might also want to get told about this, the Power |
| 12312 | * dialog / global actions also might want different behaviors. |
| 12313 | */ |
| 12314 | private static final boolean shouldShowDialogs(Configuration config) { |
| 12315 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 12316 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 12317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12318 | |
| 12319 | /** |
| 12320 | * Save the locale. You must be inside a synchronized (this) block. |
| 12321 | */ |
| 12322 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 12323 | if(isDiff) { |
| 12324 | SystemProperties.set("user.language", l.getLanguage()); |
| 12325 | SystemProperties.set("user.region", l.getCountry()); |
| 12326 | } |
| 12327 | |
| 12328 | if(isPersist) { |
| 12329 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 12330 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 12331 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 12332 | } |
| 12333 | } |
| 12334 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12335 | @Override |
| 12336 | public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) { |
| 12337 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12338 | return srec != null && srec.task.affinity != null && |
| 12339 | srec.task.affinity.equals(destAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12340 | } |
| 12341 | |
| 12342 | public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode, |
| 12343 | Intent resultData) { |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12344 | |
| 12345 | synchronized (this) { |
| 12346 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12347 | if (srec == null) { |
| 12348 | return false; |
| 12349 | } |
Craig Mautner | cae015f | 2013-02-08 14:31:27 -0800 | [diff] [blame] | 12350 | ActivityStack stack = srec.stack; |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 12351 | return stack.navigateUpToLocked(srec, destIntent, resultCode, resultData); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12352 | } |
| 12353 | } |
| 12354 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 12355 | public int getLaunchedFromUid(IBinder activityToken) { |
| 12356 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 12357 | if (srec == null) { |
| 12358 | return -1; |
| 12359 | } |
| 12360 | return srec.launchedFromUid; |
| 12361 | } |
| 12362 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 12363 | public String getLaunchedFromPackage(IBinder activityToken) { |
| 12364 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 12365 | if (srec == null) { |
| 12366 | return null; |
| 12367 | } |
| 12368 | return srec.launchedFromPackage; |
| 12369 | } |
| 12370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12371 | // ========================================================= |
| 12372 | // LIFETIME MANAGEMENT |
| 12373 | // ========================================================= |
| 12374 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12375 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 12376 | // on, or 'null' if the app is not an active broadcast recipient. |
| 12377 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 12378 | BroadcastRecord r = app.curReceiver; |
| 12379 | if (r != null) { |
| 12380 | return r.queue; |
| 12381 | } |
| 12382 | |
| 12383 | // It's not the current receiver, but it might be starting up to become one |
| 12384 | synchronized (this) { |
| 12385 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12386 | r = queue.mPendingBroadcast; |
| 12387 | if (r != null && r.curApp == app) { |
| 12388 | // found it; report which queue it's in |
| 12389 | return queue; |
| 12390 | } |
| 12391 | } |
| 12392 | } |
| 12393 | |
| 12394 | return null; |
| 12395 | } |
| 12396 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12397 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, int clientHiddenAdj, |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12398 | int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12399 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12400 | // This adjustment has already been computed. If we are calling |
| 12401 | // from the top, we may have already computed our adjustment with |
| 12402 | // an earlier hidden adjustment that isn't really for us... if |
| 12403 | // so, use the new hidden adjustment. |
| 12404 | if (!recursed && app.hidden) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12405 | if (app.hasActivities) { |
| 12406 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = hiddenAdj; |
| 12407 | } else if (app.hasClientActivities) { |
| 12408 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = clientHiddenAdj; |
| 12409 | } else { |
| 12410 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = emptyAdj; |
| 12411 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12412 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12413 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12414 | } |
| 12415 | |
| 12416 | if (app.thread == null) { |
| 12417 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12418 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12419 | 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] | 12420 | } |
| 12421 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12422 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 12423 | app.adjSource = null; |
| 12424 | app.adjTarget = null; |
| 12425 | app.empty = false; |
| 12426 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12427 | app.hasClientActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12428 | |
| 12429 | final int activitiesSize = app.activities.size(); |
| 12430 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12431 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12432 | // The max adjustment doesn't allow this app to be anything |
| 12433 | // below foreground, so it is not worth doing work for it. |
| 12434 | app.adjType = "fixed"; |
| 12435 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12436 | app.curRawAdj = app.nonStoppingAdj = app.maxAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12437 | app.hasActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12438 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12439 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12440 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12441 | // System process can do UI, and when they do we want to have |
| 12442 | // them trim their memory after the user leaves the UI. To |
| 12443 | // facilitate this, here we need to determine whether or not it |
| 12444 | // is currently showing UI. |
| 12445 | app.systemNoUi = true; |
| 12446 | if (app == TOP_APP) { |
| 12447 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12448 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12449 | } else if (activitiesSize > 0) { |
| 12450 | for (int j = 0; j < activitiesSize; j++) { |
| 12451 | final ActivityRecord r = app.activities.get(j); |
| 12452 | if (r.visible) { |
| 12453 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12454 | } |
| 12455 | if (r.app == app) { |
| 12456 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12457 | } |
| 12458 | } |
| 12459 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12460 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12461 | } |
| 12462 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12463 | app.keeping = false; |
| 12464 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12465 | app.hasActivities = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12466 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12467 | // Determine the importance of the process, starting with most |
| 12468 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12469 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12470 | int schedGroup; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12471 | boolean foregroundActivities = false; |
| 12472 | boolean interesting = false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12473 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12474 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12475 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12476 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12477 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12478 | app.adjType = "top-activity"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12479 | foregroundActivities = true; |
| 12480 | interesting = true; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12481 | app.hasActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12482 | } else if (app.instrumentationClass != null) { |
| 12483 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12484 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12485 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12486 | app.adjType = "instrumentation"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12487 | interesting = true; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12488 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12489 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12490 | // counts as being in the foreground for OOM killer purposes. |
| 12491 | // It's placed in a sched group based on the nature of the |
| 12492 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12493 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12494 | schedGroup = (queue == mFgBroadcastQueue) |
| 12495 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12496 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12497 | } else if (app.executingServices.size() > 0) { |
| 12498 | // An app that is currently executing a service callback also |
| 12499 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12500 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12501 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12502 | app.adjType = "exec-service"; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12503 | } else { |
| 12504 | // Assume process is hidden (has activities); we will correct |
| 12505 | // later if this is not the case. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12506 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12507 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12508 | app.hidden = true; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12509 | app.adjType = "bg-act"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12510 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12511 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12512 | boolean hasStoppingActivities = false; |
| 12513 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12514 | // Examine all activities if not already foreground. |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12515 | if (!foregroundActivities && activitiesSize > 0) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12516 | for (int j = 0; j < activitiesSize; j++) { |
| 12517 | final ActivityRecord r = app.activities.get(j); |
| 12518 | if (r.visible) { |
| 12519 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12520 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12521 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12522 | app.adjType = "visible"; |
| 12523 | } |
| 12524 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12525 | app.hidden = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12526 | app.hasActivities = true; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12527 | foregroundActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12528 | break; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12529 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12530 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12531 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12532 | app.adjType = "pausing"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12533 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 12534 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12535 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12536 | } else if (r.state == ActivityState.STOPPING) { |
| 12537 | // We will apply the actual adjustment later, because |
| 12538 | // we want to allow this process to immediately go through |
| 12539 | // any memory trimming that is in effect. |
| 12540 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12541 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12542 | hasStoppingActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12543 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12544 | if (r.app == app) { |
| 12545 | app.hasActivities = true; |
| 12546 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12547 | } |
| 12548 | } |
| 12549 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12550 | if (adj == hiddenAdj && !app.hasActivities) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12551 | if (app.hasClientActivities) { |
| 12552 | adj = clientHiddenAdj; |
| 12553 | app.adjType = "bg-client-act"; |
| 12554 | } else { |
| 12555 | // Whoops, this process is completely empty as far as we know |
| 12556 | // at this point. |
| 12557 | adj = emptyAdj; |
| 12558 | app.empty = true; |
| 12559 | app.adjType = "bg-empty"; |
| 12560 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12561 | } |
| 12562 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12563 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12564 | if (app.foregroundServices) { |
| 12565 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12566 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12567 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12568 | app.adjType = "fg-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12569 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12570 | } else if (app.forcingToForeground != null) { |
| 12571 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12572 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12573 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12574 | app.adjType = "force-fg"; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12575 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12576 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12577 | } |
| 12578 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12579 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12580 | if (app.foregroundServices) { |
| 12581 | interesting = true; |
| 12582 | } |
| 12583 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12584 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12585 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12586 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12587 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12588 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12589 | app.adjType = "heavy"; |
| 12590 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12591 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12592 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12593 | // This process is hosting what we currently consider to be the |
| 12594 | // 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] | 12595 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12596 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12597 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12598 | app.adjType = "home"; |
| 12599 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12600 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 12601 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 12602 | && app.activities.size() > 0) { |
| 12603 | // This was the previous process that showed UI to the user. |
| 12604 | // We want to try to keep it around more aggressively, to give |
| 12605 | // a good experience around switching between two apps. |
| 12606 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 12607 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12608 | app.hidden = false; |
| 12609 | app.adjType = "previous"; |
| 12610 | } |
| 12611 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12612 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 12613 | + " reason=" + app.adjType); |
| 12614 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12615 | // 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] | 12616 | // there are applications dependent on our services or providers, but |
| 12617 | // this gives us a baseline and makes sure we don't get into an |
| 12618 | // infinite recursion. |
| 12619 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12620 | app.curRawAdj = app.nonStoppingAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12621 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12622 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 12623 | // 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] | 12624 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12625 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12626 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12627 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12628 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12629 | } |
| 12630 | } |
| 12631 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12632 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12633 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12634 | final long now = SystemClock.uptimeMillis(); |
| 12635 | // This process is more important if the top activity is |
| 12636 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12637 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12638 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12639 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12640 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12641 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12642 | // If this process has shown some UI, let it immediately |
| 12643 | // go to the LRU list because it may be pretty heavy with |
| 12644 | // UI stuff. We'll tag it with a label just to help |
| 12645 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12646 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12647 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12648 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12649 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12650 | if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12651 | // This service has seen some activity within |
| 12652 | // recent memory, so we will keep its process ahead |
| 12653 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12654 | if (adj > ProcessList.SERVICE_ADJ) { |
| 12655 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12656 | app.adjType = "started-services"; |
| 12657 | app.hidden = false; |
| 12658 | } |
| 12659 | } |
| 12660 | // If we have let the service slide into the background |
| 12661 | // state, still have some text describing what it is doing |
| 12662 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12663 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12664 | app.adjType = "started-bg-services"; |
| 12665 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12666 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12667 | // Don't kill this process because it is doing work; it |
| 12668 | // has said it is doing work. |
| 12669 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12670 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12671 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12672 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12673 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12674 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12675 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12676 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12677 | 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] | 12678 | // XXX should compute this based on the max of |
| 12679 | // all connected clients. |
| 12680 | ConnectionRecord cr = clist.get(i); |
| 12681 | if (cr.binding.client == app) { |
| 12682 | // Binding to ourself is not interesting. |
| 12683 | continue; |
| 12684 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12685 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12686 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12687 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12688 | int myHiddenAdj = hiddenAdj; |
| 12689 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12690 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12691 | myHiddenAdj = client.hiddenAdj; |
| 12692 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12693 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12694 | } |
| 12695 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12696 | int myClientHiddenAdj = clientHiddenAdj; |
| 12697 | if (myClientHiddenAdj > client.clientHiddenAdj) { |
| 12698 | if (client.clientHiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 12699 | myClientHiddenAdj = client.clientHiddenAdj; |
| 12700 | } else { |
| 12701 | myClientHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
| 12702 | } |
| 12703 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12704 | int myEmptyAdj = emptyAdj; |
| 12705 | if (myEmptyAdj > client.emptyAdj) { |
| 12706 | if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 12707 | myEmptyAdj = client.emptyAdj; |
| 12708 | } else { |
| 12709 | myEmptyAdj = ProcessList.VISIBLE_APP_ADJ; |
| 12710 | } |
| 12711 | } |
| 12712 | clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12713 | myClientHiddenAdj, myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12714 | String adjType = null; |
| 12715 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 12716 | // Not doing bind OOM management, so treat |
| 12717 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12718 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12719 | // If this process has shown some UI, let it immediately |
| 12720 | // go to the LRU list because it may be pretty heavy with |
| 12721 | // UI stuff. We'll tag it with a label just to help |
| 12722 | // debug and understand what is going on. |
| 12723 | if (adj > clientAdj) { |
| 12724 | adjType = "bound-bg-ui-services"; |
| 12725 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12726 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12727 | clientAdj = adj; |
| 12728 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12729 | if (now >= (s.lastActivity |
| 12730 | + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12731 | // This service has not seen activity within |
| 12732 | // recent memory, so allow it to drop to the |
| 12733 | // LRU list if there is no other reason to keep |
| 12734 | // it around. We'll also tag it with a label just |
| 12735 | // to help debug and undertand what is going on. |
| 12736 | if (adj > clientAdj) { |
| 12737 | adjType = "bound-bg-services"; |
| 12738 | } |
| 12739 | clientAdj = adj; |
| 12740 | } |
| 12741 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12742 | } else if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 12743 | if ((cr.flags&Context.BIND_NOT_VISIBLE) == 0) { |
| 12744 | // If this connection is keeping the service |
| 12745 | // created, then we want to try to better follow |
| 12746 | // its memory management semantics for activities. |
| 12747 | // That is, if it is sitting in the background |
| 12748 | // LRU list as a hidden process (with activities), |
| 12749 | // we don't want the service it is connected to |
| 12750 | // to go into the empty LRU and quickly get killed, |
| 12751 | // because I'll we'll do is just end up restarting |
| 12752 | // the service. |
| 12753 | app.hasClientActivities |= client.hasActivities; |
| 12754 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12755 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12756 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12757 | // If this process has recently shown UI, and |
| 12758 | // the process that is binding to it is less |
| 12759 | // important than being visible, then we don't |
| 12760 | // care about the binding as much as we care |
| 12761 | // about letting this process get into the LRU |
| 12762 | // list to be killed and restarted if needed for |
| 12763 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12764 | if (app.hasShownUi && app != mHomeProcess |
| 12765 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12766 | adjType = "bound-bg-ui-services"; |
| 12767 | } else { |
| 12768 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 12769 | |Context.BIND_IMPORTANT)) != 0) { |
| 12770 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12771 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 12772 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 12773 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12774 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12775 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12776 | adj = clientAdj; |
| 12777 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12778 | app.pendingUiClean = true; |
| 12779 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12780 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 12781 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12782 | } |
| 12783 | if (!client.hidden) { |
| 12784 | app.hidden = false; |
| 12785 | } |
| 12786 | if (client.keeping) { |
| 12787 | app.keeping = true; |
| 12788 | } |
| 12789 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12790 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12791 | } |
| 12792 | if (adjType != null) { |
| 12793 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12794 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12795 | .REASON_SERVICE_IN_USE; |
| 12796 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12797 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12798 | app.adjTarget = s.name; |
| 12799 | } |
| 12800 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12801 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12802 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12803 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12804 | } |
| 12805 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12806 | final ActivityRecord a = cr.activity; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12807 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12808 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12809 | (a.visible || a.state == ActivityState.RESUMED |
| 12810 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12811 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12812 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12813 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12814 | } |
| 12815 | app.hidden = false; |
| 12816 | app.adjType = "service"; |
| 12817 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12818 | .REASON_SERVICE_IN_USE; |
| 12819 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12820 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12821 | app.adjTarget = s.name; |
| 12822 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12823 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12824 | } |
| 12825 | } |
| 12826 | } |
| 12827 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12828 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12829 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12830 | // would like to avoid killing it unless it would prevent the current |
| 12831 | // application from running. By default we put the process in |
| 12832 | // with the rest of the background processes; as we scan through |
| 12833 | // its services we may bump it up from there. |
| 12834 | if (adj > hiddenAdj) { |
| 12835 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12836 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12837 | app.adjType = "bg-services"; |
| 12838 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12839 | } |
| 12840 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12841 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12842 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12843 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12844 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12845 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12846 | ContentProviderRecord cpr = jt.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12847 | for (int i = cpr.connections.size()-1; |
| 12848 | i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
| 12849 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE); |
| 12850 | i--) { |
| 12851 | ContentProviderConnection conn = cpr.connections.get(i); |
| 12852 | ProcessRecord client = conn.client; |
| 12853 | if (client == app) { |
| 12854 | // Being our own client is not interesting. |
| 12855 | continue; |
| 12856 | } |
| 12857 | int myHiddenAdj = hiddenAdj; |
| 12858 | if (myHiddenAdj > client.hiddenAdj) { |
| 12859 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12860 | myHiddenAdj = client.hiddenAdj; |
| 12861 | } else { |
| 12862 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12863 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12864 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12865 | int myClientHiddenAdj = clientHiddenAdj; |
| 12866 | if (myClientHiddenAdj > client.clientHiddenAdj) { |
| 12867 | if (client.clientHiddenAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 12868 | myClientHiddenAdj = client.clientHiddenAdj; |
| 12869 | } else { |
| 12870 | myClientHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 12871 | } |
| 12872 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12873 | int myEmptyAdj = emptyAdj; |
| 12874 | if (myEmptyAdj > client.emptyAdj) { |
| 12875 | if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12876 | myEmptyAdj = client.emptyAdj; |
| 12877 | } else { |
| 12878 | myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 12879 | } |
| 12880 | } |
| 12881 | int clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12882 | myClientHiddenAdj, myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12883 | if (adj > clientAdj) { |
| 12884 | if (app.hasShownUi && app != mHomeProcess |
| 12885 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12886 | app.adjType = "bg-ui-provider"; |
| 12887 | } else { |
| 12888 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 12889 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
| 12890 | app.adjType = "provider"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12891 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12892 | if (!client.hidden) { |
| 12893 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12894 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12895 | if (client.keeping) { |
| 12896 | app.keeping = true; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12897 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12898 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12899 | .REASON_PROVIDER_IN_USE; |
| 12900 | app.adjSource = client; |
| 12901 | app.adjSourceOom = clientAdj; |
| 12902 | app.adjTarget = cpr.name; |
| 12903 | } |
| 12904 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12905 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12906 | } |
| 12907 | } |
| 12908 | // If the provider has external (non-framework) process |
| 12909 | // dependencies, ensure that its adjustment is at least |
| 12910 | // FOREGROUND_APP_ADJ. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 12911 | if (cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12912 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12913 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12914 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12915 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12916 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12917 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12918 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12919 | } |
| 12920 | } |
| 12921 | } |
| 12922 | } |
| 12923 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12924 | if (adj == ProcessList.SERVICE_ADJ) { |
| 12925 | if (doingAll) { |
| 12926 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 12927 | mNewNumServiceProcs++; |
| 12928 | } |
| 12929 | if (app.serviceb) { |
| 12930 | adj = ProcessList.SERVICE_B_ADJ; |
| 12931 | } |
| 12932 | } else { |
| 12933 | app.serviceb = false; |
| 12934 | } |
| 12935 | |
| 12936 | app.nonStoppingAdj = adj; |
| 12937 | |
| 12938 | if (hasStoppingActivities) { |
| 12939 | // Only upgrade adjustment. |
| 12940 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12941 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12942 | app.adjType = "stopping"; |
| 12943 | } |
| 12944 | } |
| 12945 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12946 | app.curRawAdj = adj; |
| 12947 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12948 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12949 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 12950 | if (adj > app.maxAdj) { |
| 12951 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12952 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12953 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12954 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12955 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12956 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12957 | app.keeping = true; |
| 12958 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12959 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12960 | if (app.hasAboveClient) { |
| 12961 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 12962 | // then we need to drop its adjustment to be lower than the service's |
| 12963 | // in order to honor the request. We want to drop it by one adjustment |
| 12964 | // level... but there is special meaning applied to various levels so |
| 12965 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12966 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12967 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12968 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 12969 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 12970 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12971 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12972 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 12973 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12974 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12975 | adj++; |
| 12976 | } |
| 12977 | } |
| 12978 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12979 | int importance = app.memImportance; |
| 12980 | if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) { |
| 12981 | app.curAdj = adj; |
| 12982 | app.curSchedGroup = schedGroup; |
| 12983 | if (!interesting) { |
| 12984 | // For this reporting, if there is not something explicitly |
| 12985 | // interesting in this process then we will push it to the |
| 12986 | // background importance. |
| 12987 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12988 | } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 12989 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12990 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 12991 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 12992 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 12993 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12994 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
| 12995 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 12996 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 12997 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 12998 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12999 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 13000 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13001 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 13002 | } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 13003 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 13004 | } else { |
| 13005 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT; |
| 13006 | } |
| 13007 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13008 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13009 | int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0; |
| 13010 | if (foregroundActivities != app.foregroundActivities) { |
| 13011 | changes |= ProcessChangeItem.CHANGE_ACTIVITIES; |
| 13012 | } |
| 13013 | if (changes != 0) { |
| 13014 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes); |
| 13015 | app.memImportance = importance; |
| 13016 | app.foregroundActivities = foregroundActivities; |
| 13017 | int i = mPendingProcessChanges.size()-1; |
| 13018 | ProcessChangeItem item = null; |
| 13019 | while (i >= 0) { |
| 13020 | item = mPendingProcessChanges.get(i); |
| 13021 | if (item.pid == app.pid) { |
| 13022 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item); |
| 13023 | break; |
| 13024 | } |
| 13025 | i--; |
| 13026 | } |
| 13027 | if (i < 0) { |
| 13028 | // No existing item in pending changes; need a new one. |
| 13029 | final int NA = mAvailProcessChanges.size(); |
| 13030 | if (NA > 0) { |
| 13031 | item = mAvailProcessChanges.remove(NA-1); |
| 13032 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item); |
| 13033 | } else { |
| 13034 | item = new ProcessChangeItem(); |
| 13035 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item); |
| 13036 | } |
| 13037 | item.changes = 0; |
| 13038 | item.pid = app.pid; |
| 13039 | item.uid = app.info.uid; |
| 13040 | if (mPendingProcessChanges.size() == 0) { |
| 13041 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, |
| 13042 | "*** Enqueueing dispatch processes changed!"); |
| 13043 | mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget(); |
| 13044 | } |
| 13045 | mPendingProcessChanges.add(item); |
| 13046 | } |
| 13047 | item.changes |= changes; |
| 13048 | item.importance = importance; |
| 13049 | item.foregroundActivities = foregroundActivities; |
| 13050 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item " |
| 13051 | + Integer.toHexString(System.identityHashCode(item)) |
| 13052 | + " " + app.toShortString() + ": changes=" + item.changes |
| 13053 | + " importance=" + item.importance |
| 13054 | + " foreground=" + item.foregroundActivities |
| 13055 | + " type=" + app.adjType + " source=" + app.adjSource |
| 13056 | + " target=" + app.adjTarget); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13057 | } |
| 13058 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13059 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13060 | } |
| 13061 | |
| 13062 | /** |
| 13063 | * Ask a given process to GC right now. |
| 13064 | */ |
| 13065 | final void performAppGcLocked(ProcessRecord app) { |
| 13066 | try { |
| 13067 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13068 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13069 | if (app.reportLowMemory) { |
| 13070 | app.reportLowMemory = false; |
| 13071 | app.thread.scheduleLowMemory(); |
| 13072 | } else { |
| 13073 | app.thread.processInBackground(); |
| 13074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13075 | } |
| 13076 | } catch (Exception e) { |
| 13077 | // whatever. |
| 13078 | } |
| 13079 | } |
| 13080 | |
| 13081 | /** |
| 13082 | * Returns true if things are idle enough to perform GCs. |
| 13083 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13084 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13085 | boolean processingBroadcasts = false; |
| 13086 | for (BroadcastQueue q : mBroadcastQueues) { |
| 13087 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 13088 | processingBroadcasts = true; |
| 13089 | } |
| 13090 | } |
| 13091 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13092 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13093 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13094 | } |
| 13095 | |
| 13096 | /** |
| 13097 | * Perform GCs on all processes that are waiting for it, but only |
| 13098 | * if things are idle. |
| 13099 | */ |
| 13100 | final void performAppGcsLocked() { |
| 13101 | final int N = mProcessesToGc.size(); |
| 13102 | if (N <= 0) { |
| 13103 | return; |
| 13104 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13105 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13106 | while (mProcessesToGc.size() > 0) { |
| 13107 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13108 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13109 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 13110 | <= SystemClock.uptimeMillis()) { |
| 13111 | // To avoid spamming the system, we will GC processes one |
| 13112 | // at a time, waiting a few seconds between each. |
| 13113 | performAppGcLocked(proc); |
| 13114 | scheduleAppGcsLocked(); |
| 13115 | return; |
| 13116 | } else { |
| 13117 | // It hasn't been long enough since we last GCed this |
| 13118 | // process... put it in the list to wait for its time. |
| 13119 | addProcessToGcListLocked(proc); |
| 13120 | break; |
| 13121 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13122 | } |
| 13123 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13124 | |
| 13125 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13126 | } |
| 13127 | } |
| 13128 | |
| 13129 | /** |
| 13130 | * If all looks good, perform GCs on all processes waiting for them. |
| 13131 | */ |
| 13132 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13133 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13134 | performAppGcsLocked(); |
| 13135 | return; |
| 13136 | } |
| 13137 | // Still not idle, wait some more. |
| 13138 | scheduleAppGcsLocked(); |
| 13139 | } |
| 13140 | |
| 13141 | /** |
| 13142 | * Schedule the execution of all pending app GCs. |
| 13143 | */ |
| 13144 | final void scheduleAppGcsLocked() { |
| 13145 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13146 | |
| 13147 | if (mProcessesToGc.size() > 0) { |
| 13148 | // Schedule a GC for the time to the next process. |
| 13149 | ProcessRecord proc = mProcessesToGc.get(0); |
| 13150 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 13151 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13152 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13153 | long now = SystemClock.uptimeMillis(); |
| 13154 | if (when < (now+GC_TIMEOUT)) { |
| 13155 | when = now + GC_TIMEOUT; |
| 13156 | } |
| 13157 | mHandler.sendMessageAtTime(msg, when); |
| 13158 | } |
| 13159 | } |
| 13160 | |
| 13161 | /** |
| 13162 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 13163 | * list in sorted order by the last GC time. The process can't already be |
| 13164 | * on the list. |
| 13165 | */ |
| 13166 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 13167 | boolean added = false; |
| 13168 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 13169 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 13170 | proc.lastRequestedGc) { |
| 13171 | added = true; |
| 13172 | mProcessesToGc.add(i+1, proc); |
| 13173 | break; |
| 13174 | } |
| 13175 | } |
| 13176 | if (!added) { |
| 13177 | mProcessesToGc.add(0, proc); |
| 13178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13179 | } |
| 13180 | |
| 13181 | /** |
| 13182 | * Set up to ask a process to GC itself. This will either do it |
| 13183 | * immediately, or put it on the list of processes to gc the next |
| 13184 | * time things are idle. |
| 13185 | */ |
| 13186 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 13187 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13188 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13189 | return; |
| 13190 | } |
| 13191 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13192 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13193 | scheduleAppGcsLocked(); |
| 13194 | } |
| 13195 | } |
| 13196 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13197 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 13198 | updateCpuStatsNow(); |
| 13199 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13200 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13201 | boolean doWakeKills = doKills; |
| 13202 | boolean doCpuKills = doKills; |
| 13203 | if (mLastPowerCheckRealtime == 0) { |
| 13204 | doWakeKills = false; |
| 13205 | } |
| 13206 | if (mLastPowerCheckUptime == 0) { |
| 13207 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13208 | } |
| 13209 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13210 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13211 | } |
| 13212 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13213 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 13214 | final long curUptime = SystemClock.uptimeMillis(); |
| 13215 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 13216 | mLastPowerCheckRealtime = curRealtime; |
| 13217 | mLastPowerCheckUptime = curUptime; |
| 13218 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 13219 | doWakeKills = false; |
| 13220 | } |
| 13221 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 13222 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13223 | } |
| 13224 | int i = mLruProcesses.size(); |
| 13225 | while (i > 0) { |
| 13226 | i--; |
| 13227 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13228 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13229 | long wtime; |
| 13230 | synchronized (stats) { |
| 13231 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 13232 | app.pid, curRealtime); |
| 13233 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13234 | long wtimeUsed = wtime - app.lastWakeTime; |
| 13235 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13236 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13237 | StringBuilder sb = new StringBuilder(128); |
| 13238 | sb.append("Wake for "); |
| 13239 | app.toShortString(sb); |
| 13240 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13241 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13242 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13243 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13244 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13245 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13246 | sb.append("%)"); |
| 13247 | Slog.i(TAG, sb.toString()); |
| 13248 | sb.setLength(0); |
| 13249 | sb.append("CPU for "); |
| 13250 | app.toShortString(sb); |
| 13251 | sb.append(": over "); |
| 13252 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13253 | sb.append(" used "); |
| 13254 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13255 | sb.append(" ("); |
| 13256 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13257 | sb.append("%)"); |
| 13258 | Slog.i(TAG, sb.toString()); |
| 13259 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13260 | // If a process has held a wake lock for more |
| 13261 | // than 50% of the time during this period, |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13262 | // that sounds bad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13263 | if (doWakeKills && realtimeSince > 0 |
| 13264 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13265 | synchronized (stats) { |
| 13266 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13267 | realtimeSince, wtimeUsed); |
| 13268 | } |
| 13269 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13270 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13271 | + " during " + realtimeSince); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13272 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13273 | app.processName, app.setAdj, "excessive wake lock"); |
| 13274 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13275 | } else if (doCpuKills && uptimeSince > 0 |
| 13276 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13277 | synchronized (stats) { |
| 13278 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13279 | uptimeSince, cputimeUsed); |
| 13280 | } |
| 13281 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13282 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13283 | + " during " + uptimeSince); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13284 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13285 | app.processName, app.setAdj, "excessive cpu"); |
| 13286 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13287 | } else { |
| 13288 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13289 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13290 | } |
| 13291 | } |
| 13292 | } |
| 13293 | } |
| 13294 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13295 | private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13296 | int clientHiddenAdj, int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13297 | app.hiddenAdj = hiddenAdj; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13298 | app.clientHiddenAdj = clientHiddenAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13299 | app.emptyAdj = emptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13300 | |
| 13301 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13302 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13303 | } |
| 13304 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13305 | final boolean wasKeeping = app.keeping; |
| 13306 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13307 | boolean success = true; |
| 13308 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13309 | computeOomAdjLocked(app, hiddenAdj, clientHiddenAdj, emptyAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13310 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13311 | if (app.curRawAdj != app.setRawAdj) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13312 | if (wasKeeping && !app.keeping) { |
| 13313 | // This app is no longer something we want to keep. Note |
| 13314 | // its current wake lock time to later know to kill it if |
| 13315 | // it is not behaving well. |
| 13316 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13317 | synchronized (stats) { |
| 13318 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13319 | app.pid, SystemClock.elapsedRealtime()); |
| 13320 | } |
| 13321 | app.lastCpuTime = app.curCpuTime; |
| 13322 | } |
| 13323 | |
| 13324 | app.setRawAdj = app.curRawAdj; |
| 13325 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13326 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13327 | if (app.curAdj != app.setAdj) { |
| 13328 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 13329 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13330 | TAG, "Set " + app.pid + " " + app.processName + |
| 13331 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13332 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13333 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13334 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13335 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13336 | } |
| 13337 | } |
| 13338 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13339 | app.setSchedGroup = app.curSchedGroup; |
| 13340 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 13341 | "Setting process group of " + app.processName |
| 13342 | + " to " + app.curSchedGroup); |
| 13343 | if (app.waitingToKill != null && |
| 13344 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13345 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13346 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13347 | app.processName, app.setAdj, app.waitingToKill); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 13348 | app.killedBackground = true; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13349 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13350 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13351 | } else { |
| 13352 | if (true) { |
| 13353 | long oldId = Binder.clearCallingIdentity(); |
| 13354 | try { |
| 13355 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13356 | } catch (Exception e) { |
| 13357 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13358 | + " to " + app.curSchedGroup); |
| 13359 | e.printStackTrace(); |
| 13360 | } finally { |
| 13361 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13362 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13363 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13364 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13365 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13366 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13367 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13368 | } |
| 13369 | } |
| 13370 | } |
| 13371 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13372 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13373 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13374 | } |
| 13375 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13376 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13377 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13378 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 13379 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13380 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13381 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13382 | } |
| 13383 | } |
| 13384 | return resumedActivity; |
| 13385 | } |
| 13386 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13387 | final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13388 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13389 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13390 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13391 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13392 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13393 | |
| 13394 | mAdjSeq++; |
| 13395 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13396 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.clientHiddenAdj, |
| 13397 | app.emptyAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13398 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13399 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13400 | if (nowHidden != wasHidden) { |
| 13401 | // Changed to/from hidden state, so apps after it in the LRU |
| 13402 | // list may also be changed. |
| 13403 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13404 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13405 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13406 | } |
| 13407 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13408 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13409 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13410 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13411 | final long oldTime = SystemClock.uptimeMillis() - ProcessList.MAX_EMPTY_TIME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13412 | |
| 13413 | if (false) { |
| 13414 | RuntimeException e = new RuntimeException(); |
| 13415 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13416 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13417 | } |
| 13418 | |
| 13419 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13420 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13421 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13422 | final int emptyProcessLimit; |
| 13423 | final int hiddenProcessLimit; |
| 13424 | if (mProcessLimit <= 0) { |
| 13425 | emptyProcessLimit = hiddenProcessLimit = 0; |
| 13426 | } else if (mProcessLimit == 1) { |
| 13427 | emptyProcessLimit = 1; |
| 13428 | hiddenProcessLimit = 0; |
| 13429 | } else { |
| 13430 | emptyProcessLimit = (mProcessLimit*2)/3; |
| 13431 | hiddenProcessLimit = mProcessLimit - emptyProcessLimit; |
| 13432 | } |
| 13433 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13434 | // Let's determine how many processes we have running vs. |
| 13435 | // how many slots we have for background processes; we may want |
| 13436 | // to put multiple processes in a slot of there are enough of |
| 13437 | // them. |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13438 | int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ |
| 13439 | - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13440 | int numEmptyProcs = mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs; |
| 13441 | if (numEmptyProcs > hiddenProcessLimit) { |
| 13442 | // If there are more empty processes than our limit on hidden |
| 13443 | // processes, then use the hidden process limit for the factor. |
| 13444 | // This ensures that the really old empty processes get pushed |
| 13445 | // down to the bottom, so if we are running low on memory we will |
| 13446 | // have a better chance at keeping around more hidden processes |
| 13447 | // instead of a gazillion empty processes. |
| 13448 | numEmptyProcs = hiddenProcessLimit; |
| 13449 | } |
| 13450 | int emptyFactor = numEmptyProcs/numSlots; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13451 | if (emptyFactor < 1) emptyFactor = 1; |
| 13452 | int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots; |
| 13453 | if (hiddenFactor < 1) hiddenFactor = 1; |
| 13454 | int stepHidden = 0; |
| 13455 | int stepEmpty = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13456 | int numHidden = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13457 | int numEmpty = 0; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13458 | int numTrimming = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13459 | |
| 13460 | mNumNonHiddenProcs = 0; |
| 13461 | mNumHiddenProcs = 0; |
| 13462 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13463 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13464 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13465 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13466 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13467 | int nextHiddenAdj = curHiddenAdj+1; |
| 13468 | int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
| 13469 | int nextEmptyAdj = curEmptyAdj+2; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13470 | int curClientHiddenAdj = curEmptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13471 | while (i > 0) { |
| 13472 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13473 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13474 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13475 | updateOomAdjLocked(app, curHiddenAdj, curClientHiddenAdj, curEmptyAdj, TOP_APP, true); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13476 | if (!app.killedBackground) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13477 | if (app.curRawAdj == curHiddenAdj && app.hasActivities) { |
| 13478 | // This process was assigned as a hidden process... step the |
| 13479 | // hidden level. |
| 13480 | mNumHiddenProcs++; |
| 13481 | if (curHiddenAdj != nextHiddenAdj) { |
| 13482 | stepHidden++; |
| 13483 | if (stepHidden >= hiddenFactor) { |
| 13484 | stepHidden = 0; |
| 13485 | curHiddenAdj = nextHiddenAdj; |
| 13486 | nextHiddenAdj += 2; |
| 13487 | if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13488 | nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13489 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13490 | if (curClientHiddenAdj <= curHiddenAdj) { |
| 13491 | curClientHiddenAdj = curHiddenAdj + 1; |
| 13492 | if (curClientHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13493 | curClientHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13494 | } |
| 13495 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13496 | } |
| 13497 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13498 | numHidden++; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13499 | if (numHidden > hiddenProcessLimit) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13500 | Slog.i(TAG, "No longer want " + app.processName |
| 13501 | + " (pid " + app.pid + "): hidden #" + numHidden); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13502 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13503 | app.processName, app.setAdj, "too many background"); |
| 13504 | app.killedBackground = true; |
| 13505 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13506 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13507 | } else if (app.curRawAdj == curHiddenAdj && app.hasClientActivities) { |
| 13508 | // This process has a client that has activities. We will have |
| 13509 | // given it the current hidden adj; here we will just leave it |
| 13510 | // without stepping the hidden adj. |
| 13511 | curClientHiddenAdj++; |
| 13512 | if (curClientHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13513 | curClientHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13514 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13515 | } else { |
| 13516 | if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) { |
| 13517 | // This process was assigned as an empty process... step the |
| 13518 | // empty level. |
| 13519 | if (curEmptyAdj != nextEmptyAdj) { |
| 13520 | stepEmpty++; |
| 13521 | if (stepEmpty >= emptyFactor) { |
| 13522 | stepEmpty = 0; |
| 13523 | curEmptyAdj = nextEmptyAdj; |
| 13524 | nextEmptyAdj += 2; |
| 13525 | if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13526 | nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13527 | } |
| 13528 | } |
| 13529 | } |
| 13530 | } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13531 | mNumNonHiddenProcs++; |
| 13532 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13533 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13534 | && !app.hasClientActivities) { |
| 13535 | if (numEmpty > ProcessList.TRIM_EMPTY_APPS |
| 13536 | && app.lastActivityTime < oldTime) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13537 | Slog.i(TAG, "No longer want " + app.processName |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13538 | + " (pid " + app.pid + "): empty for " |
| 13539 | + ((oldTime+ProcessList.MAX_EMPTY_TIME-app.lastActivityTime) |
| 13540 | / 1000) + "s"); |
| 13541 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
| 13542 | app.processName, app.setAdj, "old background process"); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13543 | app.killedBackground = true; |
| 13544 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13545 | } else { |
| 13546 | numEmpty++; |
| 13547 | if (numEmpty > emptyProcessLimit) { |
| 13548 | Slog.i(TAG, "No longer want " + app.processName |
| 13549 | + " (pid " + app.pid + "): empty #" + numEmpty); |
| 13550 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
| 13551 | app.processName, app.setAdj, "too many background"); |
| 13552 | app.killedBackground = true; |
| 13553 | Process.killProcessQuiet(app.pid); |
| 13554 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13555 | } |
| 13556 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13557 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13558 | if (app.isolated && app.services.size() <= 0) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13559 | // If this is an isolated process, and there are no |
| 13560 | // services running in it, then the process is no longer |
| 13561 | // needed. We agressively kill these because we can by |
| 13562 | // definition not re-use the same process again, and it is |
| 13563 | // good to avoid having whatever code was running in them |
| 13564 | // left sitting around after no longer needed. |
| 13565 | Slog.i(TAG, "Isolated process " + app.processName |
| 13566 | + " (pid " + app.pid + ") no longer needed"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13567 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13568 | app.processName, app.setAdj, "isolated not needed"); |
| 13569 | app.killedBackground = true; |
| 13570 | Process.killProcessQuiet(app.pid); |
| 13571 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13572 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13573 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
| 13574 | && !app.killedBackground) { |
| 13575 | numTrimming++; |
| 13576 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13577 | } |
| 13578 | } |
| 13579 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13580 | mNumServiceProcs = mNewNumServiceProcs; |
| 13581 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13582 | // Now determine the memory trimming level of background processes. |
| 13583 | // Unfortunately we need to start at the back of the list to do this |
| 13584 | // properly. We only do this if the number of background apps we |
| 13585 | // are managing to keep around is less than half the maximum we desire; |
| 13586 | // if we are keeping a good number around, we'll let them use whatever |
| 13587 | // memory they want. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13588 | if (numHidden <= ProcessList.TRIM_HIDDEN_APPS |
| 13589 | && numEmpty <= ProcessList.TRIM_EMPTY_APPS) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13590 | final int numHiddenAndEmpty = numHidden + numEmpty; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13591 | final int N = mLruProcesses.size(); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13592 | int factor = numTrimming/3; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13593 | int minFactor = 2; |
| 13594 | if (mHomeProcess != null) minFactor++; |
| 13595 | if (mPreviousProcess != null) minFactor++; |
| 13596 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13597 | int step = 0; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13598 | int fgTrimLevel; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13599 | if (numHiddenAndEmpty <= ProcessList.TRIM_CRITICAL_THRESHOLD) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13600 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13601 | } else if (numHiddenAndEmpty <= ProcessList.TRIM_LOW_THRESHOLD) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13602 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
| 13603 | } else { |
| 13604 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
| 13605 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13606 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13607 | for (i=0; i<N; i++) { |
| 13608 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13609 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13610 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13611 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13612 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 13613 | try { |
| 13614 | app.thread.scheduleTrimMemory(curLevel); |
| 13615 | } catch (RemoteException e) { |
| 13616 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13617 | if (false) { |
| 13618 | // For now we won't do this; our memory trimming seems |
| 13619 | // to be good enough at this point that destroying |
| 13620 | // activities causes more harm than good. |
| 13621 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 13622 | && app != mHomeProcess && app != mPreviousProcess) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13623 | // Need to do this on its own message because the stack may not |
| 13624 | // be in a consistent state at this point. |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13625 | // For these apps we will also finish their activities |
| 13626 | // to help them free memory. |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13627 | mMainStack.scheduleDestroyActivities(app, false, "trim"); |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13628 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13629 | } |
| 13630 | } |
| 13631 | app.trimMemoryLevel = curLevel; |
| 13632 | step++; |
| 13633 | if (step >= factor) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13634 | step = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13635 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13636 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 13637 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13638 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13639 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 13640 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13641 | break; |
| 13642 | } |
| 13643 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13644 | } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13645 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13646 | && app.thread != null) { |
| 13647 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13648 | app.thread.scheduleTrimMemory( |
| 13649 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13650 | } catch (RemoteException e) { |
| 13651 | } |
| 13652 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13653 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13654 | } else { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13655 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13656 | && app.pendingUiClean) { |
| 13657 | // If this application is now in the background and it |
| 13658 | // had done UI, then give it the special trim level to |
| 13659 | // have it free UI resources. |
| 13660 | final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13661 | if (app.trimMemoryLevel < level && app.thread != null) { |
| 13662 | try { |
| 13663 | app.thread.scheduleTrimMemory(level); |
| 13664 | } catch (RemoteException e) { |
| 13665 | } |
| 13666 | } |
| 13667 | app.pendingUiClean = false; |
| 13668 | } |
| 13669 | if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13670 | try { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13671 | app.thread.scheduleTrimMemory(fgTrimLevel); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13672 | } catch (RemoteException e) { |
| 13673 | } |
| 13674 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13675 | app.trimMemoryLevel = fgTrimLevel; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13676 | } |
| 13677 | } |
| 13678 | } else { |
| 13679 | final int N = mLruProcesses.size(); |
| 13680 | for (i=0; i<N; i++) { |
| 13681 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13682 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13683 | && app.pendingUiClean) { |
| 13684 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13685 | && app.thread != null) { |
| 13686 | try { |
| 13687 | app.thread.scheduleTrimMemory( |
| 13688 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 13689 | } catch (RemoteException e) { |
| 13690 | } |
| 13691 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13692 | app.pendingUiClean = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13693 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13694 | app.trimMemoryLevel = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13695 | } |
| 13696 | } |
| 13697 | |
| 13698 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13699 | // Need to do this on its own message because the stack may not |
| 13700 | // be in a consistent state at this point. |
| 13701 | mMainStack.scheduleDestroyActivities(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13702 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13703 | } |
| 13704 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13705 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13706 | synchronized (this) { |
| 13707 | int i; |
| 13708 | |
| 13709 | // First remove any unused application processes whose package |
| 13710 | // has been removed. |
| 13711 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 13712 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 13713 | if (app.activities.size() == 0 |
| 13714 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13715 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13716 | TAG, "Exiting empty application process " |
| 13717 | + app.processName + " (" |
| 13718 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13719 | + ")\n"); |
| 13720 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13721 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13722 | app.processName, app.setAdj, "empty"); |
| 13723 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13724 | } else { |
| 13725 | try { |
| 13726 | app.thread.scheduleExit(); |
| 13727 | } catch (Exception e) { |
| 13728 | // Ignore exceptions. |
| 13729 | } |
| 13730 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13731 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13732 | mRemovedProcesses.remove(i); |
| 13733 | |
| 13734 | if (app.persistent) { |
| 13735 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13736 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13737 | } |
| 13738 | } |
| 13739 | } |
| 13740 | } |
| 13741 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13742 | // Now update the oom adj for all processes. |
| 13743 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13744 | } |
| 13745 | } |
| 13746 | |
| 13747 | /** This method sends the specified signal to each of the persistent apps */ |
| 13748 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 13749 | if (sig != Process.SIGNAL_USR1) { |
| 13750 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 13751 | } |
| 13752 | |
| 13753 | synchronized (this) { |
| 13754 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 13755 | != PackageManager.PERMISSION_GRANTED) { |
| 13756 | throw new SecurityException("Requires permission " |
| 13757 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 13758 | } |
| 13759 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13760 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 13761 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13762 | if (r.thread != null && r.persistent) { |
| 13763 | Process.sendSignal(r.pid, sig); |
| 13764 | } |
| 13765 | } |
| 13766 | } |
| 13767 | } |
| 13768 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13769 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 13770 | if (proc == null || proc == mProfileProc) { |
| 13771 | proc = mProfileProc; |
| 13772 | path = mProfileFile; |
| 13773 | profileType = mProfileType; |
| 13774 | clearProfilerLocked(); |
| 13775 | } |
| 13776 | if (proc == null) { |
| 13777 | return; |
| 13778 | } |
| 13779 | try { |
| 13780 | proc.thread.profilerControl(false, path, null, profileType); |
| 13781 | } catch (RemoteException e) { |
| 13782 | throw new IllegalStateException("Process disappeared"); |
| 13783 | } |
| 13784 | } |
| 13785 | |
| 13786 | private void clearProfilerLocked() { |
| 13787 | if (mProfileFd != null) { |
| 13788 | try { |
| 13789 | mProfileFd.close(); |
| 13790 | } catch (IOException e) { |
| 13791 | } |
| 13792 | } |
| 13793 | mProfileApp = null; |
| 13794 | mProfileProc = null; |
| 13795 | mProfileFile = null; |
| 13796 | mProfileType = 0; |
| 13797 | mAutoStopProfiler = false; |
| 13798 | } |
| 13799 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13800 | public boolean profileControl(String process, int userId, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 13801 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13802 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13803 | try { |
| 13804 | synchronized (this) { |
| 13805 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13806 | // its own permission. |
| 13807 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13808 | != PackageManager.PERMISSION_GRANTED) { |
| 13809 | throw new SecurityException("Requires permission " |
| 13810 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13811 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13812 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13813 | if (start && fd == null) { |
| 13814 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13815 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13816 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13817 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13818 | if (process != null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13819 | proc = findProcessLocked(process, userId, "profileControl"); |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13820 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13821 | |
| 13822 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13823 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13824 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13825 | |
| 13826 | if (start) { |
| 13827 | stopProfilerLocked(null, null, 0); |
| 13828 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 13829 | mProfileProc = proc; |
| 13830 | mProfileType = profileType; |
| 13831 | try { |
| 13832 | fd = fd.dup(); |
| 13833 | } catch (IOException e) { |
| 13834 | fd = null; |
| 13835 | } |
| 13836 | proc.thread.profilerControl(start, path, fd, profileType); |
| 13837 | fd = null; |
| 13838 | mProfileFd = null; |
| 13839 | } else { |
| 13840 | stopProfilerLocked(proc, path, profileType); |
| 13841 | if (fd != null) { |
| 13842 | try { |
| 13843 | fd.close(); |
| 13844 | } catch (IOException e) { |
| 13845 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13846 | } |
| 13847 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13848 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13849 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13850 | } |
| 13851 | } catch (RemoteException e) { |
| 13852 | throw new IllegalStateException("Process disappeared"); |
| 13853 | } finally { |
| 13854 | if (fd != null) { |
| 13855 | try { |
| 13856 | fd.close(); |
| 13857 | } catch (IOException e) { |
| 13858 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13859 | } |
| 13860 | } |
| 13861 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13862 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13863 | private ProcessRecord findProcessLocked(String process, int userId, String callName) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 13864 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13865 | userId, true, true, callName, null); |
| 13866 | ProcessRecord proc = null; |
| 13867 | try { |
| 13868 | int pid = Integer.parseInt(process); |
| 13869 | synchronized (mPidsSelfLocked) { |
| 13870 | proc = mPidsSelfLocked.get(pid); |
| 13871 | } |
| 13872 | } catch (NumberFormatException e) { |
| 13873 | } |
| 13874 | |
| 13875 | if (proc == null) { |
| 13876 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13877 | = mProcessNames.getMap(); |
| 13878 | SparseArray<ProcessRecord> procs = all.get(process); |
| 13879 | if (procs != null && procs.size() > 0) { |
| 13880 | proc = procs.valueAt(0); |
| 13881 | if (userId != UserHandle.USER_ALL && proc.userId != userId) { |
| 13882 | for (int i=1; i<procs.size(); i++) { |
| 13883 | ProcessRecord thisProc = procs.valueAt(i); |
| 13884 | if (thisProc.userId == userId) { |
| 13885 | proc = thisProc; |
| 13886 | break; |
| 13887 | } |
| 13888 | } |
| 13889 | } |
| 13890 | } |
| 13891 | } |
| 13892 | |
| 13893 | return proc; |
| 13894 | } |
| 13895 | |
| 13896 | public boolean dumpHeap(String process, int userId, boolean managed, |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13897 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 13898 | |
| 13899 | try { |
| 13900 | synchronized (this) { |
| 13901 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13902 | // its own permission (same as profileControl). |
| 13903 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13904 | != PackageManager.PERMISSION_GRANTED) { |
| 13905 | throw new SecurityException("Requires permission " |
| 13906 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 13907 | } |
| 13908 | |
| 13909 | if (fd == null) { |
| 13910 | throw new IllegalArgumentException("null fd"); |
| 13911 | } |
| 13912 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13913 | ProcessRecord proc = findProcessLocked(process, userId, "dumpHeap"); |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13914 | if (proc == null || proc.thread == null) { |
| 13915 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13916 | } |
| 13917 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 13918 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 13919 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13920 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 13921 | throw new SecurityException("Process not debuggable: " + proc); |
| 13922 | } |
| 13923 | } |
| 13924 | |
| 13925 | proc.thread.dumpHeap(managed, path, fd); |
| 13926 | fd = null; |
| 13927 | return true; |
| 13928 | } |
| 13929 | } catch (RemoteException e) { |
| 13930 | throw new IllegalStateException("Process disappeared"); |
| 13931 | } finally { |
| 13932 | if (fd != null) { |
| 13933 | try { |
| 13934 | fd.close(); |
| 13935 | } catch (IOException e) { |
| 13936 | } |
| 13937 | } |
| 13938 | } |
| 13939 | } |
| 13940 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13941 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 13942 | public void monitor() { |
| 13943 | synchronized (this) { } |
| 13944 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13945 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 13946 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13947 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 13948 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 13949 | try { |
| 13950 | if (processRecord.thread != null) { |
| 13951 | processRecord.thread.setCoreSettings(settings); |
| 13952 | } |
| 13953 | } catch (RemoteException re) { |
| 13954 | /* ignore */ |
| 13955 | } |
| 13956 | } |
| 13957 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 13958 | |
| 13959 | // Multi-user methods |
| 13960 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13961 | @Override |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 13962 | public boolean switchUser(final int userId) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13963 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 13964 | != PackageManager.PERMISSION_GRANTED) { |
| 13965 | String msg = "Permission Denial: switchUser() from pid=" |
| 13966 | + Binder.getCallingPid() |
| 13967 | + ", uid=" + Binder.getCallingUid() |
| 13968 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 13969 | Slog.w(TAG, msg); |
| 13970 | throw new SecurityException(msg); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13971 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13972 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13973 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13974 | try { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13975 | synchronized (this) { |
| 13976 | final int oldUserId = mCurrentUserId; |
| 13977 | if (oldUserId == userId) { |
| 13978 | return true; |
| 13979 | } |
| 13980 | |
| 13981 | final UserInfo userInfo = getUserManagerLocked().getUserInfo(userId); |
| 13982 | if (userInfo == null) { |
| 13983 | Slog.w(TAG, "No user info for user #" + userId); |
| 13984 | return false; |
| 13985 | } |
| 13986 | |
| 13987 | mWindowManager.startFreezingScreen(R.anim.screen_user_exit, |
| 13988 | R.anim.screen_user_enter); |
| 13989 | |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 13990 | boolean needStart = false; |
| 13991 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13992 | // If the user we are switching to is not currently started, then |
| 13993 | // we need to start it now. |
| 13994 | if (mStartedUsers.get(userId) == null) { |
| 13995 | mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 13996 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 13997 | needStart = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 13998 | } |
| 13999 | |
| 14000 | mCurrentUserId = userId; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14001 | mCurrentUserArray = new int[] { userId }; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14002 | final Integer userIdInt = Integer.valueOf(userId); |
| 14003 | mUserLru.remove(userIdInt); |
| 14004 | mUserLru.add(userIdInt); |
| 14005 | |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14006 | mWindowManager.setCurrentUser(userId); |
| 14007 | |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 14008 | // Once the internal notion of the active user has switched, we lock the device |
| 14009 | // with the option to show the user switcher on the keyguard. |
Winson Chung | f7614fc | 2012-11-26 14:43:24 -0800 | [diff] [blame] | 14010 | mWindowManager.lockNow(null); |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 14011 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14012 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14013 | |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14014 | // Make sure user is in the started state. If it is currently |
| 14015 | // stopping, we need to knock that off. |
| 14016 | if (uss.mState == UserStartedState.STATE_STOPPING) { |
| 14017 | // If we are stopping, we haven't sent ACTION_SHUTDOWN, |
| 14018 | // so we can just fairly silently bring the user back from |
| 14019 | // the almost-dead. |
| 14020 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14021 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14022 | needStart = true; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14023 | } else if (uss.mState == UserStartedState.STATE_SHUTDOWN) { |
| 14024 | // This means ACTION_SHUTDOWN has been sent, so we will |
| 14025 | // need to treat this as a new boot of the user. |
| 14026 | uss.mState = UserStartedState.STATE_BOOTING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14027 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14028 | needStart = true; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14029 | } |
| 14030 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14031 | mHandler.removeMessages(REPORT_USER_SWITCH_MSG); |
| 14032 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14033 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG, |
| 14034 | oldUserId, userId, uss)); |
| 14035 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG, |
| 14036 | oldUserId, userId, uss), USER_SWITCH_TIMEOUT); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14037 | if (needStart) { |
| 14038 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
| 14039 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14040 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 14041 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14042 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14043 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14044 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14045 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14046 | |
| 14047 | if ((userInfo.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
| 14048 | if (userId != 0) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14049 | Intent intent = new Intent(Intent.ACTION_USER_INITIALIZE); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14050 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14051 | broadcastIntentLocked(null, null, intent, null, |
| 14052 | new IIntentReceiver.Stub() { |
| 14053 | public void performReceive(Intent intent, int resultCode, |
| 14054 | String data, Bundle extras, boolean ordered, |
| 14055 | boolean sticky, int sendingUser) { |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14056 | userInitialized(uss, userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14057 | } |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14058 | }, 0, null, null, null, AppOpsManager.OP_NONE, |
| 14059 | true, false, MY_PID, Process.SYSTEM_UID, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14060 | userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14061 | uss.initializing = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14062 | } else { |
| 14063 | getUserManagerLocked().makeInitialized(userInfo.id); |
| 14064 | } |
| 14065 | } |
| 14066 | |
| 14067 | boolean haveActivities = mMainStack.switchUserLocked(userId, uss); |
| 14068 | if (!haveActivities) { |
| 14069 | startHomeActivityLocked(userId); |
| 14070 | } |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14071 | |
Jeff Sharkey | 86597df | 2012-11-09 15:00:31 -0800 | [diff] [blame] | 14072 | EventLogTags.writeAmSwitchUser(userId); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 14073 | getUserManagerLocked().userForeground(userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14074 | sendUserSwitchBroadcastsLocked(oldUserId, userId); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14075 | if (needStart) { |
| 14076 | Intent intent = new Intent(Intent.ACTION_USER_STARTING); |
| 14077 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14078 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14079 | broadcastIntentLocked(null, null, intent, |
| 14080 | null, new IIntentReceiver.Stub() { |
| 14081 | @Override |
| 14082 | public void performReceive(Intent intent, int resultCode, String data, |
| 14083 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) |
| 14084 | throws RemoteException { |
| 14085 | } |
| 14086 | }, 0, null, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14087 | android.Manifest.permission.INTERACT_ACROSS_USERS, AppOpsManager.OP_NONE, |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 14088 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14089 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14090 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14091 | } finally { |
| 14092 | Binder.restoreCallingIdentity(ident); |
| 14093 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14094 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14095 | return true; |
| 14096 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14097 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14098 | void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) { |
| 14099 | long ident = Binder.clearCallingIdentity(); |
| 14100 | try { |
| 14101 | Intent intent; |
| 14102 | if (oldUserId >= 0) { |
| 14103 | intent = new Intent(Intent.ACTION_USER_BACKGROUND); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14104 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14105 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14106 | intent.putExtra(Intent.EXTRA_USER_HANDLE, oldUserId); |
| 14107 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14108 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14109 | false, false, MY_PID, Process.SYSTEM_UID, oldUserId); |
| 14110 | } |
| 14111 | if (newUserId >= 0) { |
| 14112 | intent = new Intent(Intent.ACTION_USER_FOREGROUND); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14113 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14114 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14115 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14116 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14117 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14118 | false, false, MY_PID, Process.SYSTEM_UID, newUserId); |
| 14119 | intent = new Intent(Intent.ACTION_USER_SWITCHED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14120 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14121 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14122 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14123 | broadcastIntentLocked(null, null, intent, |
| 14124 | null, null, 0, null, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14125 | android.Manifest.permission.MANAGE_USERS, AppOpsManager.OP_NONE, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14126 | false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
| 14127 | } |
| 14128 | } finally { |
| 14129 | Binder.restoreCallingIdentity(ident); |
| 14130 | } |
| 14131 | } |
| 14132 | |
| 14133 | void dispatchUserSwitch(final UserStartedState uss, final int oldUserId, |
| 14134 | final int newUserId) { |
| 14135 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14136 | if (N > 0) { |
| 14137 | final IRemoteCallback callback = new IRemoteCallback.Stub() { |
| 14138 | int mCount = 0; |
| 14139 | @Override |
| 14140 | public void sendResult(Bundle data) throws RemoteException { |
| 14141 | synchronized (ActivityManagerService.this) { |
| 14142 | if (mCurUserSwitchCallback == this) { |
| 14143 | mCount++; |
| 14144 | if (mCount == N) { |
| 14145 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14146 | } |
| 14147 | } |
| 14148 | } |
| 14149 | } |
| 14150 | }; |
| 14151 | synchronized (this) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14152 | uss.switching = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14153 | mCurUserSwitchCallback = callback; |
| 14154 | } |
| 14155 | for (int i=0; i<N; i++) { |
| 14156 | try { |
| 14157 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitching( |
| 14158 | newUserId, callback); |
| 14159 | } catch (RemoteException e) { |
| 14160 | } |
| 14161 | } |
| 14162 | } else { |
| 14163 | synchronized (this) { |
| 14164 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14165 | } |
| 14166 | } |
| 14167 | mUserSwitchObservers.finishBroadcast(); |
| 14168 | } |
| 14169 | |
| 14170 | void timeoutUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
| 14171 | synchronized (this) { |
| 14172 | Slog.w(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId); |
| 14173 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14174 | } |
| 14175 | } |
| 14176 | |
| 14177 | void sendContinueUserSwitchLocked(UserStartedState uss, int oldUserId, int newUserId) { |
| 14178 | mCurUserSwitchCallback = null; |
| 14179 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14180 | mHandler.sendMessage(mHandler.obtainMessage(CONTINUE_USER_SWITCH_MSG, |
| 14181 | oldUserId, newUserId, uss)); |
| 14182 | } |
| 14183 | |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14184 | void userInitialized(UserStartedState uss, int newUserId) { |
| 14185 | completeSwitchAndInitalize(uss, newUserId, true, false); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14186 | } |
| 14187 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14188 | void continueUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14189 | completeSwitchAndInitalize(uss, newUserId, false, true); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14190 | } |
| 14191 | |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14192 | void completeSwitchAndInitalize(UserStartedState uss, int newUserId, |
| 14193 | boolean clearInitializing, boolean clearSwitching) { |
| 14194 | boolean unfrozen = false; |
| 14195 | synchronized (this) { |
| 14196 | if (clearInitializing) { |
| 14197 | uss.initializing = false; |
| 14198 | getUserManagerLocked().makeInitialized(uss.mHandle.getIdentifier()); |
| 14199 | } |
| 14200 | if (clearSwitching) { |
| 14201 | uss.switching = false; |
| 14202 | } |
| 14203 | if (!uss.switching && !uss.initializing) { |
| 14204 | mWindowManager.stopFreezingScreen(); |
| 14205 | unfrozen = true; |
| 14206 | } |
| 14207 | } |
| 14208 | if (unfrozen) { |
| 14209 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14210 | for (int i=0; i<N; i++) { |
| 14211 | try { |
| 14212 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(newUserId); |
| 14213 | } catch (RemoteException e) { |
| 14214 | } |
| 14215 | } |
| 14216 | mUserSwitchObservers.finishBroadcast(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14217 | } |
| 14218 | } |
| 14219 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14220 | void finishUserSwitch(UserStartedState uss) { |
| 14221 | synchronized (this) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14222 | if (uss.mState == UserStartedState.STATE_BOOTING |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14223 | && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) { |
| 14224 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14225 | final int userId = uss.mHandle.getIdentifier(); |
| 14226 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 14227 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14228 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14229 | null, null, 0, null, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14230 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, AppOpsManager.OP_NONE, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14231 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14232 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14233 | int num = mUserLru.size(); |
| 14234 | int i = 0; |
| 14235 | while (num > MAX_RUNNING_USERS && i < mUserLru.size()) { |
| 14236 | Integer oldUserId = mUserLru.get(i); |
| 14237 | UserStartedState oldUss = mStartedUsers.get(oldUserId); |
| 14238 | if (oldUss == null) { |
| 14239 | // Shouldn't happen, but be sane if it does. |
| 14240 | mUserLru.remove(i); |
| 14241 | num--; |
| 14242 | continue; |
| 14243 | } |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14244 | if (oldUss.mState == UserStartedState.STATE_STOPPING |
| 14245 | || oldUss.mState == UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14246 | // This user is already stopping, doesn't count. |
| 14247 | num--; |
| 14248 | i++; |
| 14249 | continue; |
| 14250 | } |
| 14251 | if (oldUserId == UserHandle.USER_OWNER || oldUserId == mCurrentUserId) { |
| 14252 | // Owner and current can't be stopped, but count as running. |
| 14253 | i++; |
| 14254 | continue; |
| 14255 | } |
| 14256 | // This is a user to be stopped. |
| 14257 | stopUserLocked(oldUserId, null); |
| 14258 | num--; |
| 14259 | i++; |
| 14260 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14261 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14262 | } |
| 14263 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14264 | @Override |
| 14265 | public int stopUser(final int userId, final IStopUserCallback callback) { |
| 14266 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14267 | != PackageManager.PERMISSION_GRANTED) { |
| 14268 | String msg = "Permission Denial: switchUser() from pid=" |
| 14269 | + Binder.getCallingPid() |
| 14270 | + ", uid=" + Binder.getCallingUid() |
| 14271 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14272 | Slog.w(TAG, msg); |
| 14273 | throw new SecurityException(msg); |
| 14274 | } |
| 14275 | if (userId <= 0) { |
| 14276 | throw new IllegalArgumentException("Can't stop primary user " + userId); |
| 14277 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14278 | synchronized (this) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14279 | return stopUserLocked(userId, callback); |
| 14280 | } |
| 14281 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14282 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14283 | private int stopUserLocked(final int userId, final IStopUserCallback callback) { |
| 14284 | if (mCurrentUserId == userId) { |
| 14285 | return ActivityManager.USER_OP_IS_CURRENT; |
| 14286 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14287 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14288 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14289 | if (uss == null) { |
| 14290 | // User is not started, nothing to do... but we do need to |
| 14291 | // callback if requested. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14292 | if (callback != null) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14293 | mHandler.post(new Runnable() { |
| 14294 | @Override |
| 14295 | public void run() { |
| 14296 | try { |
| 14297 | callback.userStopped(userId); |
| 14298 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14299 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14300 | } |
| 14301 | }); |
| 14302 | } |
| 14303 | return ActivityManager.USER_OP_SUCCESS; |
| 14304 | } |
| 14305 | |
| 14306 | if (callback != null) { |
| 14307 | uss.mStopCallbacks.add(callback); |
| 14308 | } |
| 14309 | |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14310 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14311 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14312 | uss.mState = UserStartedState.STATE_STOPPING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14313 | updateStartedUserArrayLocked(); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14314 | |
| 14315 | long ident = Binder.clearCallingIdentity(); |
| 14316 | try { |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14317 | // We are going to broadcast ACTION_USER_STOPPING and then |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 14318 | // once that is done send a final ACTION_SHUTDOWN and then |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14319 | // stop the user. |
| 14320 | final Intent stoppingIntent = new Intent(Intent.ACTION_USER_STOPPING); |
| 14321 | stoppingIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14322 | stoppingIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14323 | final Intent shutdownIntent = new Intent(Intent.ACTION_SHUTDOWN); |
| 14324 | // This is the result receiver for the final shutdown broadcast. |
| 14325 | final IIntentReceiver shutdownReceiver = new IIntentReceiver.Stub() { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14326 | @Override |
| 14327 | public void performReceive(Intent intent, int resultCode, String data, |
| 14328 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 14329 | finishUserStop(uss); |
| 14330 | } |
| 14331 | }; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14332 | // This is the result receiver for the initial stopping broadcast. |
| 14333 | final IIntentReceiver stoppingReceiver = new IIntentReceiver.Stub() { |
| 14334 | @Override |
| 14335 | public void performReceive(Intent intent, int resultCode, String data, |
| 14336 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 14337 | // On to the next. |
| 14338 | synchronized (ActivityManagerService.this) { |
| 14339 | if (uss.mState != UserStartedState.STATE_STOPPING) { |
| 14340 | // Whoops, we are being started back up. Abort, abort! |
| 14341 | return; |
| 14342 | } |
| 14343 | uss.mState = UserStartedState.STATE_SHUTDOWN; |
| 14344 | } |
| 14345 | broadcastIntentLocked(null, null, shutdownIntent, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14346 | null, shutdownReceiver, 0, null, null, null, AppOpsManager.OP_NONE, |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14347 | true, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14348 | } |
| 14349 | }; |
| 14350 | // Kick things off. |
| 14351 | broadcastIntentLocked(null, null, stoppingIntent, |
| 14352 | null, stoppingReceiver, 0, null, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 14353 | android.Manifest.permission.INTERACT_ACROSS_USERS, AppOpsManager.OP_NONE, |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14354 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14355 | } finally { |
| 14356 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14357 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14358 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14359 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14360 | return ActivityManager.USER_OP_SUCCESS; |
| 14361 | } |
| 14362 | |
| 14363 | void finishUserStop(UserStartedState uss) { |
| 14364 | final int userId = uss.mHandle.getIdentifier(); |
| 14365 | boolean stopped; |
| 14366 | ArrayList<IStopUserCallback> callbacks; |
| 14367 | synchronized (this) { |
| 14368 | callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks); |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14369 | if (mStartedUsers.get(userId) != uss) { |
| 14370 | stopped = false; |
| 14371 | } else if (uss.mState != UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14372 | stopped = false; |
| 14373 | } else { |
| 14374 | stopped = true; |
| 14375 | // User can no longer run. |
| 14376 | mStartedUsers.remove(userId); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14377 | mUserLru.remove(Integer.valueOf(userId)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14378 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14379 | |
| 14380 | // Clean up all state and processes associated with the user. |
| 14381 | // Kill all the processes for the user. |
| 14382 | forceStopUserLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14383 | } |
| 14384 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14385 | |
| 14386 | for (int i=0; i<callbacks.size(); i++) { |
| 14387 | try { |
| 14388 | if (stopped) callbacks.get(i).userStopped(userId); |
| 14389 | else callbacks.get(i).userStopAborted(userId); |
| 14390 | } catch (RemoteException e) { |
| 14391 | } |
| 14392 | } |
| 14393 | } |
| 14394 | |
| 14395 | @Override |
| 14396 | public UserInfo getCurrentUser() { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14397 | if ((checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14398 | != PackageManager.PERMISSION_GRANTED) && ( |
| 14399 | checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14400 | != PackageManager.PERMISSION_GRANTED)) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14401 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 14402 | + Binder.getCallingPid() |
| 14403 | + ", uid=" + Binder.getCallingUid() |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14404 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14405 | Slog.w(TAG, msg); |
| 14406 | throw new SecurityException(msg); |
| 14407 | } |
| 14408 | synchronized (this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14409 | return getUserManagerLocked().getUserInfo(mCurrentUserId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14410 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14411 | } |
| 14412 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 14413 | int getCurrentUserIdLocked() { |
| 14414 | return mCurrentUserId; |
| 14415 | } |
| 14416 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14417 | @Override |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14418 | public boolean isUserRunning(int userId, boolean orStopped) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14419 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14420 | != PackageManager.PERMISSION_GRANTED) { |
| 14421 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14422 | + Binder.getCallingPid() |
| 14423 | + ", uid=" + Binder.getCallingUid() |
| 14424 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14425 | Slog.w(TAG, msg); |
| 14426 | throw new SecurityException(msg); |
| 14427 | } |
| 14428 | synchronized (this) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14429 | return isUserRunningLocked(userId, orStopped); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14430 | } |
| 14431 | } |
| 14432 | |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14433 | boolean isUserRunningLocked(int userId, boolean orStopped) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14434 | UserStartedState state = mStartedUsers.get(userId); |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14435 | if (state == null) { |
| 14436 | return false; |
| 14437 | } |
| 14438 | if (orStopped) { |
| 14439 | return true; |
| 14440 | } |
| 14441 | return state.mState != UserStartedState.STATE_STOPPING |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14442 | && state.mState != UserStartedState.STATE_SHUTDOWN; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14443 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14444 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14445 | @Override |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14446 | public int[] getRunningUserIds() { |
| 14447 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14448 | != PackageManager.PERMISSION_GRANTED) { |
| 14449 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14450 | + Binder.getCallingPid() |
| 14451 | + ", uid=" + Binder.getCallingUid() |
| 14452 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14453 | Slog.w(TAG, msg); |
| 14454 | throw new SecurityException(msg); |
| 14455 | } |
| 14456 | synchronized (this) { |
| 14457 | return mStartedUserArray; |
| 14458 | } |
| 14459 | } |
| 14460 | |
| 14461 | private void updateStartedUserArrayLocked() { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14462 | int num = 0; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14463 | for (int i=0; i<mStartedUsers.size(); i++) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14464 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 14465 | // This list does not include stopping users. |
| 14466 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14467 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
| 14468 | num++; |
| 14469 | } |
| 14470 | } |
| 14471 | mStartedUserArray = new int[num]; |
| 14472 | num = 0; |
| 14473 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 14474 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 14475 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14476 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
| 14477 | mStartedUserArray[num] = mStartedUsers.keyAt(i); |
| 14478 | num++; |
| 14479 | } |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14480 | } |
| 14481 | } |
| 14482 | |
| 14483 | @Override |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14484 | public void registerUserSwitchObserver(IUserSwitchObserver observer) { |
| 14485 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14486 | != PackageManager.PERMISSION_GRANTED) { |
| 14487 | String msg = "Permission Denial: registerUserSwitchObserver() from pid=" |
| 14488 | + Binder.getCallingPid() |
| 14489 | + ", uid=" + Binder.getCallingUid() |
| 14490 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14491 | Slog.w(TAG, msg); |
| 14492 | throw new SecurityException(msg); |
| 14493 | } |
| 14494 | |
| 14495 | mUserSwitchObservers.register(observer); |
| 14496 | } |
| 14497 | |
| 14498 | @Override |
| 14499 | public void unregisterUserSwitchObserver(IUserSwitchObserver observer) { |
| 14500 | mUserSwitchObservers.unregister(observer); |
| 14501 | } |
| 14502 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14503 | private boolean userExists(int userId) { |
| 14504 | if (userId == 0) { |
| 14505 | return true; |
| 14506 | } |
| 14507 | UserManagerService ums = getUserManagerLocked(); |
| 14508 | return ums != null ? (ums.getUserInfo(userId) != null) : false; |
| 14509 | } |
| 14510 | |
| 14511 | int[] getUsersLocked() { |
| 14512 | UserManagerService ums = getUserManagerLocked(); |
| 14513 | return ums != null ? ums.getUserIds() : new int[] { 0 }; |
| 14514 | } |
| 14515 | |
| 14516 | UserManagerService getUserManagerLocked() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14517 | if (mUserManager == null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14518 | IBinder b = ServiceManager.getService(Context.USER_SERVICE); |
| 14519 | mUserManager = (UserManagerService)IUserManager.Stub.asInterface(b); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14520 | } |
| 14521 | return mUserManager; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14522 | } |
| 14523 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14524 | private void checkValidCaller(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14525 | if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14526 | |
| 14527 | throw new SecurityException("Caller uid=" + uid |
| 14528 | + " is not privileged to communicate with user=" + userId); |
| 14529 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14530 | |
| 14531 | private int applyUserId(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14532 | return UserHandle.getUid(userId, uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14533 | } |
| 14534 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 14535 | ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14536 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14537 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 14538 | newInfo.uid = applyUserId(info.uid, userId); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14539 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 14540 | + info.packageName; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14541 | return newInfo; |
| 14542 | } |
| 14543 | |
| 14544 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14545 | if (aInfo == null |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14546 | || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14547 | return aInfo; |
| 14548 | } |
| 14549 | |
| 14550 | ActivityInfo info = new ActivityInfo(aInfo); |
| 14551 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 14552 | return info; |
| 14553 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14554 | } |