The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 20 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 21 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | 45ce864 | 2011-07-14 16:10:16 -0700 | [diff] [blame] | 23 | import com.android.internal.os.ProcessStats; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 24 | import com.android.server.AppOpsService; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 25 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import com.android.server.IntentResolver; |
| 27 | import com.android.server.ProcessMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import com.android.server.SystemServer; |
| 29 | import com.android.server.Watchdog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 30 | import com.android.server.am.ActivityStack.ActivityState; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 31 | import com.android.server.pm.UserManagerService; |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 32 | import com.android.server.wm.AppTransition; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 33 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 35 | import dalvik.system.Zygote; |
| 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.app.Activity; |
| 38 | import android.app.ActivityManager; |
| 39 | import android.app.ActivityManagerNative; |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 40 | import android.app.ActivityOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.app.ActivityThread; |
| 42 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 43 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 44 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 46 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.app.IApplicationThread; |
| 48 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 49 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 50 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.app.IServiceConnection; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 52 | import android.app.IStopUserCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.app.IThumbnailReceiver; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 54 | import android.app.IUserSwitchObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 56 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 57 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.app.PendingIntent; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 59 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 60 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 61 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 62 | import android.content.ClipData; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 63 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.content.ComponentName; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 65 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.content.ContentResolver; |
| 67 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 68 | import android.content.DialogInterface; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 69 | import android.content.IContentProvider; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 70 | import android.content.IIntentReceiver; |
| 71 | import android.content.IIntentSender; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 72 | import android.content.Intent; |
| 73 | import android.content.IntentFilter; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 74 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.content.pm.ActivityInfo; |
| 76 | import android.content.pm.ApplicationInfo; |
| 77 | import android.content.pm.ConfigurationInfo; |
| 78 | import android.content.pm.IPackageDataObserver; |
| 79 | import android.content.pm.IPackageManager; |
| 80 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 81 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.content.pm.PackageManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 83 | import android.content.pm.UserInfo; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 84 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 85 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.content.pm.ProviderInfo; |
| 87 | import android.content.pm.ResolveInfo; |
| 88 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 89 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.content.res.Configuration; |
| 91 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 92 | import android.net.Proxy; |
| 93 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import android.net.Uri; |
| 95 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 96 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 97 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 98 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 99 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 101 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import android.os.FileUtils; |
| 103 | import android.os.Handler; |
| 104 | import android.os.IBinder; |
| 105 | import android.os.IPermissionController; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 106 | import android.os.IRemoteCallback; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 107 | import android.os.IUserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.os.Looper; |
| 109 | import android.os.Message; |
| 110 | import android.os.Parcel; |
| 111 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 113 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | import android.os.RemoteException; |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 115 | import android.os.SELinux; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 117 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | import android.os.SystemClock; |
| 119 | import android.os.SystemProperties; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 120 | import android.os.UpdateLock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 121 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | import android.provider.Settings; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 123 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | import android.util.EventLog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 125 | import android.util.Log; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 126 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | import android.util.PrintWriterPrinter; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 128 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | import android.util.SparseArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 130 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | import android.view.Gravity; |
| 132 | import android.view.LayoutInflater; |
| 133 | import android.view.View; |
| 134 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 136 | import java.io.BufferedInputStream; |
| 137 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 138 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 139 | import java.io.DataInputStream; |
| 140 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | import java.io.File; |
| 142 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 143 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 145 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 146 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 147 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 149 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | import java.lang.ref.WeakReference; |
| 151 | import java.util.ArrayList; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 152 | import java.util.Arrays; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 153 | import java.util.Collections; |
| 154 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | import java.util.HashMap; |
| 156 | import java.util.HashSet; |
| 157 | import java.util.Iterator; |
| 158 | import java.util.List; |
| 159 | import java.util.Locale; |
| 160 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 161 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 162 | import java.util.concurrent.atomic.AtomicBoolean; |
| 163 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 165 | public final class ActivityManagerService extends ActivityManagerNative |
| 166 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 167 | private static final String USER_DATA_DIR = "/data/user/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | static final String TAG = "ActivityManager"; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 169 | static final String TAG_MU = "ActivityManagerServiceMU"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 171 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 173 | static final boolean DEBUG_TASKS = localLOGV || false; |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 174 | static final boolean DEBUG_THUMBNAILS = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 176 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 177 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 178 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 179 | static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 180 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 182 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 184 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 185 | static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false; |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 186 | static final boolean DEBUG_CLEANUP = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 187 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 188 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 190 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 191 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 192 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 193 | static final boolean DEBUG_POWER = localLOGV || false; |
| 194 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 195 | static final boolean DEBUG_MU = localLOGV || false; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 196 | static final boolean DEBUG_IMMERSIVE = localLOGV || false; |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame^] | 197 | static final boolean VALIDATE_TOKENS = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 199 | |
| 200 | // Control over CPU and battery monitoring. |
| 201 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 202 | static final boolean MONITOR_CPU_USAGE = true; |
| 203 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 204 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 205 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 206 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 207 | // 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] | 208 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 209 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 210 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 212 | static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); |
| 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | // Maximum number of recent tasks that we can remember. |
| 215 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 216 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 217 | // Amount of time after a call to stopAppSwitches() during which we will |
| 218 | // prevent further untrusted switches from happening. |
| 219 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | |
| 221 | // How long we wait for a launched process to attach to the activity manager |
| 222 | // before we decide it's never going to come up for real. |
| 223 | static final int PROC_START_TIMEOUT = 10*1000; |
| 224 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 225 | // How long we wait for a launched process to attach to the activity manager |
| 226 | // before we decide it's never going to come up for real, when the process was |
| 227 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 228 | // could take much longer than usual. |
| 229 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | // How long to wait after going idle before forcing apps to GC. |
| 232 | static final int GC_TIMEOUT = 5*1000; |
| 233 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 234 | // The minimum amount of time between successive GC requests for a process. |
| 235 | static final int GC_MIN_INTERVAL = 60*1000; |
| 236 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 237 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 238 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 239 | |
| 240 | // The minimum sample duration we will allow before deciding we have |
| 241 | // enough data on wake locks to start killing things. |
| 242 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 243 | |
| 244 | // The minimum sample duration we will allow before deciding we have |
| 245 | // enough data on CPU usage to start killing things. |
| 246 | 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] | 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | // 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] | 249 | static final int BROADCAST_FG_TIMEOUT = 10*1000; |
| 250 | static final int BROADCAST_BG_TIMEOUT = 60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | // How long we wait until we timeout on key dispatching. |
| 253 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 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 during instrumentation. |
| 256 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 257 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 258 | // Amount of time we wait for observers to handle a user switch before |
| 259 | // giving up on them and unfreezing the screen. |
| 260 | static final int USER_SWITCH_TIMEOUT = 2*1000; |
| 261 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 262 | // Maximum number of users we allow to be running at a time. |
| 263 | static final int MAX_RUNNING_USERS = 3; |
| 264 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 265 | // How long to wait in getTopActivityExtras for the activity to respond with the result. |
| 266 | static final int PENDING_ACTIVITY_RESULT_TIMEOUT = 2*2000; |
| 267 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 268 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | |
| 270 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 271 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 272 | public ActivityStack mMainStack; |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 273 | |
| 274 | private final boolean mHeadless; |
| 275 | |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 276 | // Whether we should show our dialogs (ANR, crash, etc) or just perform their |
| 277 | // default actuion automatically. Important for devices without direct input |
| 278 | // devices. |
| 279 | private boolean mShowDialogs = true; |
| 280 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 282 | * Description of a request to start a new activity, which has been held |
| 283 | * due to app switches being disabled. |
| 284 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 285 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 286 | ActivityRecord r; |
| 287 | ActivityRecord sourceRecord; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 288 | int startFlags; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 292 | = new ArrayList<PendingActivityLaunch>(); |
| 293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 295 | BroadcastQueue mFgBroadcastQueue; |
| 296 | BroadcastQueue mBgBroadcastQueue; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 297 | // Convenient for easy iteration over the queues. Foreground is first |
| 298 | // so that dispatch of foreground broadcasts gets precedence. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 299 | final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 300 | |
| 301 | BroadcastQueue broadcastQueueForIntent(Intent intent) { |
| 302 | final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; |
| 303 | if (DEBUG_BACKGROUND_BROADCAST) { |
| 304 | Slog.i(TAG, "Broadcast intent " + intent + " on " |
| 305 | + (isFg ? "foreground" : "background") |
| 306 | + " queue"); |
| 307 | } |
| 308 | return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; |
| 309 | } |
| 310 | |
| 311 | BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) { |
| 312 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 313 | BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver); |
| 314 | if (r != null) { |
| 315 | return r; |
| 316 | } |
| 317 | } |
| 318 | return null; |
| 319 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | |
| 321 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | * Activity we have told the window manager to have key focus. |
| 323 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 324 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 325 | |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 326 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | * List of intents that were used to start the most recent tasks. |
| 328 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 329 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 331 | public class PendingActivityExtras extends Binder implements Runnable { |
| 332 | public final ActivityRecord activity; |
| 333 | public boolean haveResult = false; |
| 334 | public Bundle result = null; |
| 335 | public PendingActivityExtras(ActivityRecord _activity) { |
| 336 | activity = _activity; |
| 337 | } |
| 338 | @Override |
| 339 | public void run() { |
| 340 | Slog.w(TAG, "getTopActivityExtras failed: timeout retrieving from " + activity); |
| 341 | synchronized (this) { |
| 342 | haveResult = true; |
| 343 | notifyAll(); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | final ArrayList<PendingActivityExtras> mPendingActivityExtras |
| 349 | = new ArrayList<PendingActivityExtras>(); |
| 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 352 | * Process management. |
| 353 | */ |
| 354 | final ProcessList mProcessList = new ProcessList(); |
| 355 | |
| 356 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | * All of the applications we currently have running organized by name. |
| 358 | * The keys are strings of the application package name (as |
| 359 | * returned by the package manager), and the keys are ApplicationRecord |
| 360 | * objects. |
| 361 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 362 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | |
| 364 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 365 | * The currently running isolated processes. |
| 366 | */ |
| 367 | final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>(); |
| 368 | |
| 369 | /** |
| 370 | * Counter for assigning isolated process uids, to avoid frequently reusing the |
| 371 | * same ones. |
| 372 | */ |
| 373 | int mNextIsolatedProcessUid = 0; |
| 374 | |
| 375 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 376 | * The currently running heavy-weight process, if any. |
| 377 | */ |
| 378 | ProcessRecord mHeavyWeightProcess = null; |
| 379 | |
| 380 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | * The last time that various processes have crashed. |
| 382 | */ |
| 383 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 384 | |
| 385 | /** |
| 386 | * Set of applications that we consider to be bad, and will reject |
| 387 | * incoming broadcasts from (which the user has no control over). |
| 388 | * Processes are added to this set when they have crashed twice within |
| 389 | * a minimum amount of time; they are removed from it when they are |
| 390 | * later restarted (hopefully due to some user action). The value is the |
| 391 | * time it was added to the list. |
| 392 | */ |
| 393 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 394 | |
| 395 | /** |
| 396 | * All of the processes we currently have running organized by pid. |
| 397 | * The keys are the pid running the application. |
| 398 | * |
| 399 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 400 | * activity manager lock! |
| 401 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 402 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * All of the processes that have been forced to be foreground. The key |
| 406 | * is the pid of the caller who requested it (we hold a death |
| 407 | * link on it). |
| 408 | */ |
| 409 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 410 | int pid; |
| 411 | IBinder token; |
| 412 | } |
| 413 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 414 | = new SparseArray<ForegroundToken>(); |
| 415 | |
| 416 | /** |
| 417 | * List of records for processes that someone had tried to start before the |
| 418 | * system was ready. We don't start them at that point, but ensure they |
| 419 | * are started by the time booting is complete. |
| 420 | */ |
| 421 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 422 | = new ArrayList<ProcessRecord>(); |
| 423 | |
| 424 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | * List of persistent applications that are in the process |
| 426 | * of being started. |
| 427 | */ |
| 428 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 429 | = new ArrayList<ProcessRecord>(); |
| 430 | |
| 431 | /** |
| 432 | * Processes that are being forcibly torn down. |
| 433 | */ |
| 434 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 435 | = new ArrayList<ProcessRecord>(); |
| 436 | |
| 437 | /** |
| 438 | * List of running applications, sorted by recent usage. |
| 439 | * The first entry in the list is the least recently used. |
| 440 | * It contains ApplicationRecord objects. This list does NOT include |
| 441 | * any persistent application records (since we never want to exit them). |
| 442 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 443 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | = new ArrayList<ProcessRecord>(); |
| 445 | |
| 446 | /** |
| 447 | * List of processes that should gc as soon as things are idle. |
| 448 | */ |
| 449 | final ArrayList<ProcessRecord> mProcessesToGc |
| 450 | = new ArrayList<ProcessRecord>(); |
| 451 | |
| 452 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 453 | * This is the process holding what we currently consider to be |
| 454 | * the "home" activity. |
| 455 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 456 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 457 | |
| 458 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 459 | * This is the process holding the activity the user last visited that |
| 460 | * is in a different process from the one they are currently in. |
| 461 | */ |
| 462 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * The time at which the previous process was last visible. |
| 466 | */ |
| 467 | long mPreviousProcessVisibleTime; |
| 468 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 469 | /** |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 470 | * Which uses have been started, so are allowed to run code. |
| 471 | */ |
| 472 | final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>(); |
| 473 | |
| 474 | /** |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 475 | * LRU list of history of current users. Most recently current is at the end. |
| 476 | */ |
| 477 | final ArrayList<Integer> mUserLru = new ArrayList<Integer>(); |
| 478 | |
| 479 | /** |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 480 | * Constant array of the users that are currently started. |
| 481 | */ |
| 482 | int[] mStartedUserArray = new int[] { 0 }; |
| 483 | |
| 484 | /** |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 485 | * Registered observers of the user switching mechanics. |
| 486 | */ |
| 487 | final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers |
| 488 | = new RemoteCallbackList<IUserSwitchObserver>(); |
| 489 | |
| 490 | /** |
| 491 | * Currently active user switch. |
| 492 | */ |
| 493 | Object mCurUserSwitchCallback; |
| 494 | |
| 495 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 496 | * Packages that the user has asked to have run in screen size |
| 497 | * compatibility mode instead of filling the screen. |
| 498 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 499 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 500 | |
| 501 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | * Set of PendingResultRecord objects that are currently active. |
| 503 | */ |
| 504 | final HashSet mPendingResultRecords = new HashSet(); |
| 505 | |
| 506 | /** |
| 507 | * Set of IntentSenderRecord objects that are currently active. |
| 508 | */ |
| 509 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 510 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 511 | |
| 512 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 513 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 514 | * already logged DropBox entries for. Guarded by itself. If |
| 515 | * something (rogue user app) forces this over |
| 516 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 517 | */ |
| 518 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 519 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 520 | |
| 521 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 522 | * Strict Mode background batched logging state. |
| 523 | * |
| 524 | * The string buffer is guarded by itself, and its lock is also |
| 525 | * used to determine if another batched write is already |
| 526 | * in-flight. |
| 527 | */ |
| 528 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 529 | |
| 530 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | * Keeps track of all IIntentReceivers that have been registered for |
| 532 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 533 | * a ReceiverList. |
| 534 | */ |
| 535 | final HashMap mRegisteredReceivers = new HashMap(); |
| 536 | |
| 537 | /** |
| 538 | * Resolver for broadcast intents to registered receivers. |
| 539 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 540 | */ |
| 541 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 542 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 543 | @Override |
| 544 | protected boolean allowFilterResult( |
| 545 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 546 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 547 | for (int i=dest.size()-1; i>=0; i--) { |
| 548 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 549 | return false; |
| 550 | } |
| 551 | } |
| 552 | return true; |
| 553 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 554 | |
| 555 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 556 | protected BroadcastFilter newResult(BroadcastFilter filter, int match, int userId) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 557 | if (userId == UserHandle.USER_ALL || filter.owningUserId == UserHandle.USER_ALL |
| 558 | || userId == filter.owningUserId) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 559 | return super.newResult(filter, match, userId); |
| 560 | } |
| 561 | return null; |
| 562 | } |
| 563 | |
| 564 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 565 | protected BroadcastFilter[] newArray(int size) { |
| 566 | return new BroadcastFilter[size]; |
| 567 | } |
| 568 | |
| 569 | @Override |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 570 | protected String packageForFilter(BroadcastFilter filter) { |
| 571 | return filter.packageName; |
| 572 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | }; |
| 574 | |
| 575 | /** |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 576 | * 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] | 577 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 578 | * that action (which should usually be one). The SparseArray is keyed |
| 579 | * by the user ID the sticky is for, and can include UserHandle.USER_ALL |
| 580 | * for stickies that are sent to all users. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | */ |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 582 | final SparseArray<HashMap<String, ArrayList<Intent>>> mStickyBroadcasts = |
| 583 | new SparseArray<HashMap<String, ArrayList<Intent>>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 585 | final ActiveServices mServices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | |
| 587 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 588 | * Backup/restore process management |
| 589 | */ |
| 590 | String mBackupAppName = null; |
| 591 | BackupRecord mBackupTarget = null; |
| 592 | |
| 593 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | * List of PendingThumbnailsRecord objects of clients who are still |
| 595 | * waiting to receive all of the thumbnails for a task. |
| 596 | */ |
| 597 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 598 | |
| 599 | /** |
| 600 | * List of HistoryRecord objects that have been finished and must |
| 601 | * still report back to a pending thumbnail receiver. |
| 602 | */ |
| 603 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 604 | |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 605 | final ProviderMap mProviderMap; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | /** |
| 608 | * List of content providers who have clients waiting for them. The |
| 609 | * application is currently being launched and the provider will be |
| 610 | * removed from this list once it is published. |
| 611 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 612 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 613 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | |
| 615 | /** |
| 616 | * Global set of specific Uri permissions that have been granted. |
| 617 | */ |
| 618 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 619 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 620 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 621 | CoreSettingsObserver mCoreSettingsObserver; |
| 622 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | /** |
| 624 | * Thread-local storage used to carry caller permissions over through |
| 625 | * indirect content-provider access. |
| 626 | * @see #ActivityManagerService.openContentUri() |
| 627 | */ |
| 628 | private class Identity { |
| 629 | public int pid; |
| 630 | public int uid; |
| 631 | |
| 632 | Identity(int _pid, int _uid) { |
| 633 | pid = _pid; |
| 634 | uid = _uid; |
| 635 | } |
| 636 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 637 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 639 | |
| 640 | /** |
| 641 | * All information we have collected about the runtime performance of |
| 642 | * any user id that can impact battery performance. |
| 643 | */ |
| 644 | final BatteryStatsService mBatteryStatsService; |
| 645 | |
| 646 | /** |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 647 | * Information about component usage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | */ |
| 649 | final UsageStatsService mUsageStatsService; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 650 | |
| 651 | /** |
| 652 | * Information about and control over application operations |
| 653 | */ |
| 654 | final AppOpsService mAppOpsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | |
| 656 | /** |
| 657 | * Current configuration information. HistoryRecord objects are given |
| 658 | * a reference to this object to indicate which configuration they are |
| 659 | * currently running in, so this object must be kept immutable. |
| 660 | */ |
| 661 | Configuration mConfiguration = new Configuration(); |
| 662 | |
| 663 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 664 | * Current sequencing integer of the configuration, for skipping old |
| 665 | * configurations. |
| 666 | */ |
| 667 | int mConfigurationSeq = 0; |
| 668 | |
| 669 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 670 | * Hardware-reported OpenGLES version. |
| 671 | */ |
| 672 | final int GL_ES_VERSION; |
| 673 | |
| 674 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 676 | * For example, references to the commonly used services. |
| 677 | */ |
| 678 | HashMap<String, IBinder> mAppBindArgs; |
| 679 | |
| 680 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 681 | * Temporary to avoid allocations. Protected by main lock. |
| 682 | */ |
| 683 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 684 | |
| 685 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | * Used to control how we initialize the service. |
| 687 | */ |
| 688 | boolean mStartRunning = false; |
| 689 | ComponentName mTopComponent; |
| 690 | String mTopAction; |
| 691 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 692 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | boolean mSystemReady = false; |
| 694 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 695 | boolean mWaitingUpdate = false; |
| 696 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 697 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 698 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | |
| 700 | Context mContext; |
| 701 | |
| 702 | int mFactoryTest; |
| 703 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 704 | boolean mCheckedForSetup; |
| 705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 707 | * The time at which we will allow normal application switches again, |
| 708 | * after a call to {@link #stopAppSwitches()}. |
| 709 | */ |
| 710 | long mAppSwitchesAllowedTime; |
| 711 | |
| 712 | /** |
| 713 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 714 | * is set; any switches after that will clear the time. |
| 715 | */ |
| 716 | boolean mDidAppSwitch; |
| 717 | |
| 718 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 719 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 720 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 721 | long mLastPowerCheckRealtime; |
| 722 | |
| 723 | /** |
| 724 | * Last time (in uptime) at which we checked for power usage. |
| 725 | */ |
| 726 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 727 | |
| 728 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | * Set while we are wanting to sleep, to prevent any |
| 730 | * activities from being started/resumed. |
| 731 | */ |
| 732 | boolean mSleeping = false; |
| 733 | |
| 734 | /** |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 735 | * State of external calls telling us if the device is asleep. |
| 736 | */ |
| 737 | boolean mWentToSleep = false; |
| 738 | |
| 739 | /** |
| 740 | * State of external call telling us if the lock screen is shown. |
| 741 | */ |
| 742 | boolean mLockScreenShown = false; |
| 743 | |
| 744 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 745 | * Set if we are shutting down the system, similar to sleeping. |
| 746 | */ |
| 747 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | |
| 749 | /** |
| 750 | * Task identifier that activities are currently being started |
| 751 | * in. Incremented each time a new task is created. |
| 752 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 753 | * integers that won't wrap. |
| 754 | */ |
| 755 | int mCurTask = 1; |
| 756 | |
| 757 | /** |
| 758 | * Current sequence id for oom_adj computation traversal. |
| 759 | */ |
| 760 | int mAdjSeq = 0; |
| 761 | |
| 762 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 763 | * Current sequence id for process LRU updating. |
| 764 | */ |
| 765 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | |
| 767 | /** |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 768 | * Keep track of the non-hidden/empty process we last found, to help |
| 769 | * determine how to distribute hidden/empty processes next time. |
| 770 | */ |
| 771 | int mNumNonHiddenProcs = 0; |
| 772 | |
| 773 | /** |
| 774 | * Keep track of the number of hidden procs, to balance oom adj |
| 775 | * distribution between those and empty procs. |
| 776 | */ |
| 777 | int mNumHiddenProcs = 0; |
| 778 | |
| 779 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 780 | * Keep track of the number of service processes we last found, to |
| 781 | * determine on the next iteration which should be B services. |
| 782 | */ |
| 783 | int mNumServiceProcs = 0; |
| 784 | int mNewNumServiceProcs = 0; |
| 785 | |
| 786 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | * System monitoring: number of processes that died since the last |
| 788 | * N procs were started. |
| 789 | */ |
| 790 | int[] mProcDeaths = new int[20]; |
| 791 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 792 | /** |
| 793 | * This is set if we had to do a delayed dexopt of an app before launching |
| 794 | * it, to increasing the ANR timeouts in that case. |
| 795 | */ |
| 796 | boolean mDidDexOpt; |
| 797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | String mDebugApp = null; |
| 799 | boolean mWaitForDebugger = false; |
| 800 | boolean mDebugTransient = false; |
| 801 | String mOrigDebugApp = null; |
| 802 | boolean mOrigWaitForDebugger = false; |
| 803 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 804 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 805 | String mProfileApp = null; |
| 806 | ProcessRecord mProfileProc = null; |
| 807 | String mProfileFile; |
| 808 | ParcelFileDescriptor mProfileFd; |
| 809 | int mProfileType = 0; |
| 810 | boolean mAutoStopProfiler = false; |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 811 | String mOpenGlTraceApp = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 813 | static class ProcessChangeItem { |
| 814 | static final int CHANGE_ACTIVITIES = 1<<0; |
| 815 | static final int CHANGE_IMPORTANCE= 1<<1; |
| 816 | int changes; |
| 817 | int uid; |
| 818 | int pid; |
| 819 | int importance; |
| 820 | boolean foregroundActivities; |
| 821 | } |
| 822 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 823 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 824 | = new RemoteCallbackList<IProcessObserver>(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 825 | ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5]; |
| 826 | |
| 827 | final ArrayList<ProcessChangeItem> mPendingProcessChanges |
| 828 | = new ArrayList<ProcessChangeItem>(); |
| 829 | final ArrayList<ProcessChangeItem> mAvailProcessChanges |
| 830 | = new ArrayList<ProcessChangeItem>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 831 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | /** |
| 833 | * Callback of last caller to {@link #requestPss}. |
| 834 | */ |
| 835 | Runnable mRequestPssCallback; |
| 836 | |
| 837 | /** |
| 838 | * Remaining processes for which we are waiting results from the last |
| 839 | * call to {@link #requestPss}. |
| 840 | */ |
| 841 | final ArrayList<ProcessRecord> mRequestPssList |
| 842 | = new ArrayList<ProcessRecord>(); |
| 843 | |
| 844 | /** |
| 845 | * Runtime statistics collection thread. This object's lock is used to |
| 846 | * protect all related state. |
| 847 | */ |
| 848 | final Thread mProcessStatsThread; |
| 849 | |
| 850 | /** |
| 851 | * Used to collect process stats when showing not responding dialog. |
| 852 | * Protected by mProcessStatsThread. |
| 853 | */ |
| 854 | final ProcessStats mProcessStats = new ProcessStats( |
| 855 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 856 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 857 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 858 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | long mLastWriteTime = 0; |
| 860 | |
| 861 | /** |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 862 | * Used to retain an update lock when the foreground activity is in |
| 863 | * immersive mode. |
| 864 | */ |
| 865 | final UpdateLock mUpdateLock = new UpdateLock("immersive"); |
| 866 | |
| 867 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | * Set to true after the system has finished booting. |
| 869 | */ |
| 870 | boolean mBooted = false; |
| 871 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 872 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 873 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | |
| 875 | WindowManagerService mWindowManager; |
| 876 | |
| 877 | static ActivityManagerService mSelf; |
| 878 | static ActivityThread mSystemThread; |
| 879 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 880 | private int mCurrentUserId = 0; |
| 881 | private int[] mCurrentUserArray = new int[] { 0 }; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 882 | private UserManagerService mUserManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 883 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 885 | final ProcessRecord mApp; |
| 886 | final int mPid; |
| 887 | final IApplicationThread mAppThread; |
| 888 | |
| 889 | AppDeathRecipient(ProcessRecord app, int pid, |
| 890 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 891 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | TAG, "New death recipient " + this |
| 893 | + " for thread " + thread.asBinder()); |
| 894 | mApp = app; |
| 895 | mPid = pid; |
| 896 | mAppThread = thread; |
| 897 | } |
| 898 | |
| 899 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 900 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | TAG, "Death received in " + this |
| 902 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | synchronized(ActivityManagerService.this) { |
| 904 | appDiedLocked(mApp, mPid, mAppThread); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | static final int SHOW_ERROR_MSG = 1; |
| 910 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 911 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 912 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 913 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 914 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 916 | static final int UPDATE_TIME_ZONE = 13; |
| 917 | static final int SHOW_UID_ERROR_MSG = 14; |
| 918 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 920 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 921 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 922 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 923 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 924 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 925 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 926 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 927 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 928 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 929 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 930 | static final int DISPATCH_PROCESSES_CHANGED = 31; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 931 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 932 | static final int REPORT_MEM_USAGE = 33; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 933 | static final int REPORT_USER_SWITCH_MSG = 34; |
| 934 | static final int CONTINUE_USER_SWITCH_MSG = 35; |
| 935 | static final int USER_SWITCH_TIMEOUT_MSG = 36; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 936 | static final int IMMERSIVE_MODE_LOCK_MSG = 37; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 938 | static final int FIRST_ACTIVITY_STACK_MSG = 100; |
| 939 | static final int FIRST_BROADCAST_QUEUE_MSG = 200; |
| 940 | static final int FIRST_COMPAT_MODE_MSG = 300; |
| 941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 943 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 944 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | |
| 946 | final Handler mHandler = new Handler() { |
| 947 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 948 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | //} |
| 950 | |
| 951 | public void handleMessage(Message msg) { |
| 952 | switch (msg.what) { |
| 953 | case SHOW_ERROR_MSG: { |
| 954 | HashMap data = (HashMap) msg.obj; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 955 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 956 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | synchronized (ActivityManagerService.this) { |
| 958 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 959 | AppErrorResult res = (AppErrorResult) data.get("result"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 961 | Slog.e(TAG, "App already has crash dialog: " + proc); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 962 | if (res != null) { |
| 963 | res.set(0); |
| 964 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | return; |
| 966 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 967 | if (!showBackground && UserHandle.getAppId(proc.uid) |
| 968 | >= Process.FIRST_APPLICATION_UID && proc.userId != mCurrentUserId |
| 969 | && proc.pid != MY_PID) { |
| 970 | Slog.w(TAG, "Skipping crash dialog of " + proc + ": background"); |
| 971 | if (res != null) { |
| 972 | res.set(0); |
| 973 | } |
| 974 | return; |
| 975 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 976 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 977 | Dialog d = new AppErrorDialog(mContext, |
| 978 | ActivityManagerService.this, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | d.show(); |
| 980 | proc.crashDialog = d; |
| 981 | } else { |
| 982 | // The device is asleep, so just pretend that the user |
| 983 | // saw a crash dialog and hit "force quit". |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 984 | if (res != null) { |
| 985 | res.set(0); |
| 986 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
| 988 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 989 | |
| 990 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | } break; |
| 992 | case SHOW_NOT_RESPONDING_MSG: { |
| 993 | synchronized (ActivityManagerService.this) { |
| 994 | HashMap data = (HashMap) msg.obj; |
| 995 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 996 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 997 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | return; |
| 999 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1000 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1001 | Intent intent = new Intent("android.intent.action.ANR"); |
| 1002 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 1003 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1004 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1005 | } |
| 1006 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1007 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1008 | 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] | 1009 | |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 1010 | if (mShowDialogs) { |
| 1011 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 1012 | mContext, proc, (ActivityRecord)data.get("activity"), |
| 1013 | msg.arg1 != 0); |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 1014 | d.show(); |
| 1015 | proc.anrDialog = d; |
| 1016 | } else { |
| 1017 | // Just kill the app if there is no dialog to be shown. |
| 1018 | killAppAtUsersRequest(proc, null); |
| 1019 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1021 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1022 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1024 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 1025 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 1026 | synchronized (ActivityManagerService.this) { |
| 1027 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 1028 | if (proc == null) { |
| 1029 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 1030 | break; |
| 1031 | } |
| 1032 | if (proc.crashDialog != null) { |
| 1033 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 1034 | return; |
| 1035 | } |
| 1036 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1037 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1038 | Dialog d = new StrictModeViolationDialog(mContext, |
| 1039 | ActivityManagerService.this, res, proc); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1040 | d.show(); |
| 1041 | proc.crashDialog = d; |
| 1042 | } else { |
| 1043 | // The device is asleep, so just pretend that the user |
| 1044 | // saw a crash dialog and hit "force quit". |
| 1045 | res.set(0); |
| 1046 | } |
| 1047 | } |
| 1048 | ensureBootCompleted(); |
| 1049 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | case SHOW_FACTORY_ERROR_MSG: { |
| 1051 | Dialog d = new FactoryErrorDialog( |
| 1052 | mContext, msg.getData().getCharSequence("msg")); |
| 1053 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1054 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | } break; |
| 1056 | case UPDATE_CONFIGURATION_MSG: { |
| 1057 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1058 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 1059 | } break; |
| 1060 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 1061 | synchronized (ActivityManagerService.this) { |
| 1062 | performAppGcsIfAppropriateLocked(); |
| 1063 | } |
| 1064 | } break; |
| 1065 | case WAIT_FOR_DEBUGGER_MSG: { |
| 1066 | synchronized (ActivityManagerService.this) { |
| 1067 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1068 | if (msg.arg1 != 0) { |
| 1069 | if (!app.waitedForDebugger) { |
| 1070 | Dialog d = new AppWaitingForDebuggerDialog( |
| 1071 | ActivityManagerService.this, |
| 1072 | mContext, app); |
| 1073 | app.waitDialog = d; |
| 1074 | app.waitedForDebugger = true; |
| 1075 | d.show(); |
| 1076 | } |
| 1077 | } else { |
| 1078 | if (app.waitDialog != null) { |
| 1079 | app.waitDialog.dismiss(); |
| 1080 | app.waitDialog = null; |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1086 | if (mDidDexOpt) { |
| 1087 | mDidDexOpt = false; |
| 1088 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 1089 | nmsg.obj = msg.obj; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1090 | mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1091 | return; |
| 1092 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1093 | mServices.serviceTimeout((ProcessRecord)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | } break; |
| 1095 | case UPDATE_TIME_ZONE: { |
| 1096 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1097 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1098 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | if (r.thread != null) { |
| 1100 | try { |
| 1101 | r.thread.updateTimeZone(); |
| 1102 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1103 | 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] | 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1108 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1109 | case CLEAR_DNS_CACHE: { |
| 1110 | synchronized (ActivityManagerService.this) { |
| 1111 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1112 | ProcessRecord r = mLruProcesses.get(i); |
| 1113 | if (r.thread != null) { |
| 1114 | try { |
| 1115 | r.thread.clearDnsCache(); |
| 1116 | } catch (RemoteException ex) { |
| 1117 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1123 | case UPDATE_HTTP_PROXY: { |
| 1124 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1125 | String host = ""; |
| 1126 | String port = ""; |
| 1127 | String exclList = ""; |
| 1128 | if (proxy != null) { |
| 1129 | host = proxy.getHost(); |
| 1130 | port = Integer.toString(proxy.getPort()); |
| 1131 | exclList = proxy.getExclusionList(); |
| 1132 | } |
| 1133 | synchronized (ActivityManagerService.this) { |
| 1134 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1135 | ProcessRecord r = mLruProcesses.get(i); |
| 1136 | if (r.thread != null) { |
| 1137 | try { |
| 1138 | r.thread.setHttpProxy(host, port, exclList); |
| 1139 | } catch (RemoteException ex) { |
| 1140 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1141 | r.info.processName); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1148 | String title = "System UIDs Inconsistent"; |
| 1149 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1150 | + " data partition or your device will be unstable."; |
| 1151 | Log.e(TAG, title + ": " + text); |
| 1152 | if (mShowDialogs) { |
| 1153 | // XXX This is a temporary dialog, no need to localize. |
| 1154 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1155 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1156 | d.setCancelable(false); |
| 1157 | d.setTitle(title); |
| 1158 | d.setMessage(text); |
| 1159 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1160 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1161 | mUidAlert = d; |
| 1162 | d.show(); |
| 1163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | } break; |
| 1165 | case IM_FEELING_LUCKY_MSG: { |
| 1166 | if (mUidAlert != null) { |
| 1167 | mUidAlert.dismiss(); |
| 1168 | mUidAlert = null; |
| 1169 | } |
| 1170 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1172 | if (mDidDexOpt) { |
| 1173 | mDidDexOpt = false; |
| 1174 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1175 | nmsg.obj = msg.obj; |
| 1176 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1177 | return; |
| 1178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1180 | synchronized (ActivityManagerService.this) { |
| 1181 | processStartTimedOutLocked(app); |
| 1182 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1183 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1184 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1185 | synchronized (ActivityManagerService.this) { |
| 1186 | doPendingActivityLaunchesLocked(true); |
| 1187 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1188 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1189 | case KILL_APPLICATION_MSG: { |
| 1190 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1191 | int appid = msg.arg1; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1192 | boolean restart = (msg.arg2 == 1); |
| 1193 | String pkg = (String) msg.obj; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1194 | forceStopPackageLocked(pkg, appid, restart, false, true, false, |
| 1195 | UserHandle.USER_ALL); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1196 | } |
| 1197 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1198 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1199 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1200 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1201 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1202 | INotificationManager inm = NotificationManager.getService(); |
| 1203 | if (inm == null) { |
| 1204 | return; |
| 1205 | } |
| 1206 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1207 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1208 | ProcessRecord process = root.app; |
| 1209 | if (process == null) { |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | try { |
| 1214 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1215 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1216 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1217 | Notification notification = new Notification(); |
| 1218 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1219 | notification.when = 0; |
| 1220 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1221 | notification.tickerText = text; |
| 1222 | notification.defaults = 0; // please be quiet |
| 1223 | notification.sound = null; |
| 1224 | notification.vibrate = null; |
| 1225 | notification.setLatestEventInfo(context, text, |
| 1226 | mContext.getText(R.string.heavy_weight_notification_detail), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1227 | PendingIntent.getActivityAsUser(mContext, 0, root.intent, |
| 1228 | PendingIntent.FLAG_CANCEL_CURRENT, null, |
| 1229 | new UserHandle(root.userId))); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1230 | |
| 1231 | try { |
| 1232 | int[] outId = new int[1]; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1233 | inm.enqueueNotificationWithTag("android", null, |
| 1234 | R.string.heavy_weight_notification, |
| 1235 | notification, outId, root.userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1236 | } catch (RuntimeException e) { |
| 1237 | Slog.w(ActivityManagerService.TAG, |
| 1238 | "Error showing notification for heavy-weight app", e); |
| 1239 | } catch (RemoteException e) { |
| 1240 | } |
| 1241 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1242 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1243 | } |
| 1244 | } break; |
| 1245 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1246 | INotificationManager inm = NotificationManager.getService(); |
| 1247 | if (inm == null) { |
| 1248 | return; |
| 1249 | } |
| 1250 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1251 | inm.cancelNotificationWithTag("android", null, |
| 1252 | R.string.heavy_weight_notification, msg.arg1); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1253 | } catch (RuntimeException e) { |
| 1254 | Slog.w(ActivityManagerService.TAG, |
| 1255 | "Error canceling notification for service", e); |
| 1256 | } catch (RemoteException e) { |
| 1257 | } |
| 1258 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1259 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1260 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1261 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1262 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1263 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1264 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1265 | } |
| 1266 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1267 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1268 | synchronized (ActivityManagerService.this) { |
| 1269 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1270 | if (mCompatModeDialog != null) { |
| 1271 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1272 | ar.info.applicationInfo.packageName)) { |
| 1273 | return; |
| 1274 | } |
| 1275 | mCompatModeDialog.dismiss(); |
| 1276 | mCompatModeDialog = null; |
| 1277 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1278 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1279 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1280 | ar.packageName)) { |
| 1281 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1282 | ar.info.applicationInfo); |
| 1283 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1284 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1285 | mCompatModeDialog = new CompatModeDialog( |
| 1286 | ActivityManagerService.this, mContext, |
| 1287 | ar.info.applicationInfo); |
| 1288 | mCompatModeDialog.show(); |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1293 | break; |
| 1294 | } |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 1295 | case DISPATCH_PROCESSES_CHANGED: { |
| 1296 | dispatchProcessesChanged(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1297 | break; |
| 1298 | } |
| 1299 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1300 | final int pid = msg.arg1; |
| 1301 | final int uid = msg.arg2; |
| 1302 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1303 | break; |
| 1304 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1305 | case REPORT_MEM_USAGE: { |
| 1306 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1307 | if (!isDebuggable) { |
| 1308 | return; |
| 1309 | } |
| 1310 | synchronized (ActivityManagerService.this) { |
| 1311 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1312 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1313 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1314 | // avoid spamming. |
| 1315 | return; |
| 1316 | } |
| 1317 | mLastMemUsageReportTime = now; |
| 1318 | } |
| 1319 | Thread thread = new Thread() { |
| 1320 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1321 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1322 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1323 | StringWriter oomSw = new StringWriter(); |
| 1324 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1325 | StringWriter catSw = new StringWriter(); |
| 1326 | PrintWriter catPw = new PrintWriter(catSw); |
| 1327 | String[] emptyArgs = new String[] { }; |
| 1328 | StringBuilder tag = new StringBuilder(128); |
| 1329 | StringBuilder stack = new StringBuilder(128); |
| 1330 | tag.append("Low on memory -- "); |
| 1331 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1332 | tag, stack); |
| 1333 | dropBuilder.append(stack); |
| 1334 | dropBuilder.append('\n'); |
| 1335 | dropBuilder.append('\n'); |
| 1336 | String oomString = oomSw.toString(); |
| 1337 | dropBuilder.append(oomString); |
| 1338 | dropBuilder.append('\n'); |
| 1339 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1340 | try { |
| 1341 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1342 | "procrank", }); |
| 1343 | final InputStreamReader converter = new InputStreamReader( |
| 1344 | proc.getInputStream()); |
| 1345 | BufferedReader in = new BufferedReader(converter); |
| 1346 | String line; |
| 1347 | while (true) { |
| 1348 | line = in.readLine(); |
| 1349 | if (line == null) { |
| 1350 | break; |
| 1351 | } |
| 1352 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1353 | logBuilder.append(line); |
| 1354 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1355 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1356 | dropBuilder.append(line); |
| 1357 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1358 | } |
| 1359 | converter.close(); |
| 1360 | } catch (IOException e) { |
| 1361 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1362 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1363 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1364 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1365 | catPw.println(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1366 | mServices.dumpServicesLocked(null, catPw, emptyArgs, 0, |
| 1367 | false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1368 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1369 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1370 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1371 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1372 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1373 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1374 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1375 | synchronized (ActivityManagerService.this) { |
| 1376 | long now = SystemClock.uptimeMillis(); |
| 1377 | if (mLastMemUsageReportTime < now) { |
| 1378 | mLastMemUsageReportTime = now; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | }; |
| 1383 | thread.start(); |
| 1384 | break; |
| 1385 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1386 | case REPORT_USER_SWITCH_MSG: { |
| 1387 | dispatchUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1388 | break; |
| 1389 | } |
| 1390 | case CONTINUE_USER_SWITCH_MSG: { |
| 1391 | continueUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1392 | break; |
| 1393 | } |
| 1394 | case USER_SWITCH_TIMEOUT_MSG: { |
| 1395 | timeoutUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1396 | break; |
| 1397 | } |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1398 | case IMMERSIVE_MODE_LOCK_MSG: { |
| 1399 | final boolean nextState = (msg.arg1 != 0); |
| 1400 | if (mUpdateLock.isHeld() != nextState) { |
| 1401 | if (DEBUG_IMMERSIVE) { |
| 1402 | final ActivityRecord r = (ActivityRecord) msg.obj; |
| 1403 | Slog.d(TAG, "Applying new update lock state '" + nextState + "' for " + r); |
| 1404 | } |
| 1405 | if (nextState) { |
| 1406 | mUpdateLock.acquire(); |
| 1407 | } else { |
| 1408 | mUpdateLock.release(); |
| 1409 | } |
| 1410 | } |
| 1411 | break; |
| 1412 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | }; |
| 1416 | |
| 1417 | public static void setSystemProcess() { |
| 1418 | try { |
| 1419 | ActivityManagerService m = mSelf; |
| 1420 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1421 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1423 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1424 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | if (MONITOR_CPU_USAGE) { |
| 1426 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1429 | |
| 1430 | ApplicationInfo info = |
| 1431 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1432 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1433 | mSystemThread.installSystemApplicationInfo(info); |
| 1434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | synchronized (mSelf) { |
| 1436 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1437 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1438 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1440 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1441 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1442 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | synchronized (mSelf.mPidsSelfLocked) { |
| 1444 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1445 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1446 | mSelf.updateLruProcessLocked(app, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1447 | } |
| 1448 | } catch (PackageManager.NameNotFoundException e) { |
| 1449 | throw new RuntimeException( |
| 1450 | "Unable to find android system package", e); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | public void setWindowManager(WindowManagerService wm) { |
| 1455 | mWindowManager = wm; |
| 1456 | } |
| 1457 | |
| 1458 | public static final Context main(int factoryTest) { |
| 1459 | AThread thr = new AThread(); |
| 1460 | thr.start(); |
| 1461 | |
| 1462 | synchronized (thr) { |
| 1463 | while (thr.mService == null) { |
| 1464 | try { |
| 1465 | thr.wait(); |
| 1466 | } catch (InterruptedException e) { |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | ActivityManagerService m = thr.mService; |
| 1472 | mSelf = m; |
| 1473 | ActivityThread at = ActivityThread.systemMain(); |
| 1474 | mSystemThread = at; |
| 1475 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1476 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | m.mContext = context; |
| 1478 | m.mFactoryTest = factoryTest; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1479 | m.mMainStack = new ActivityStack(m, context, true, thr.mLooper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | |
| 1481 | m.mBatteryStatsService.publish(context); |
| 1482 | m.mUsageStatsService.publish(context); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1483 | m.mAppOpsService.publish(context); |
| 1484 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | synchronized (thr) { |
| 1486 | thr.mReady = true; |
| 1487 | thr.notifyAll(); |
| 1488 | } |
| 1489 | |
| 1490 | m.startRunning(null, null, null, null); |
| 1491 | |
| 1492 | return context; |
| 1493 | } |
| 1494 | |
| 1495 | public static ActivityManagerService self() { |
| 1496 | return mSelf; |
| 1497 | } |
| 1498 | |
| 1499 | static class AThread extends Thread { |
| 1500 | ActivityManagerService mService; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1501 | Looper mLooper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | boolean mReady = false; |
| 1503 | |
| 1504 | public AThread() { |
| 1505 | super("ActivityManager"); |
| 1506 | } |
| 1507 | |
| 1508 | public void run() { |
| 1509 | Looper.prepare(); |
| 1510 | |
| 1511 | android.os.Process.setThreadPriority( |
| 1512 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1513 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1514 | |
| 1515 | ActivityManagerService m = new ActivityManagerService(); |
| 1516 | |
| 1517 | synchronized (this) { |
| 1518 | mService = m; |
Zoran Marceta | f958b32 | 2012-08-09 20:27:12 +0900 | [diff] [blame] | 1519 | mLooper = Looper.myLooper(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | notifyAll(); |
| 1521 | } |
| 1522 | |
| 1523 | synchronized (this) { |
| 1524 | while (!mReady) { |
| 1525 | try { |
| 1526 | wait(); |
| 1527 | } catch (InterruptedException e) { |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1532 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1533 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1534 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1535 | } |
| 1536 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1537 | Looper.loop(); |
| 1538 | } |
| 1539 | } |
| 1540 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | static class MemBinder extends Binder { |
| 1542 | ActivityManagerService mActivityManagerService; |
| 1543 | MemBinder(ActivityManagerService activityManagerService) { |
| 1544 | mActivityManagerService = activityManagerService; |
| 1545 | } |
| 1546 | |
| 1547 | @Override |
| 1548 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1549 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1550 | != PackageManager.PERMISSION_GRANTED) { |
| 1551 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1552 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1553 | + " without permission " + android.Manifest.permission.DUMP); |
| 1554 | return; |
| 1555 | } |
| 1556 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1557 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1558 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1562 | static class GraphicsBinder extends Binder { |
| 1563 | ActivityManagerService mActivityManagerService; |
| 1564 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1565 | mActivityManagerService = activityManagerService; |
| 1566 | } |
| 1567 | |
| 1568 | @Override |
| 1569 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1570 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1571 | != PackageManager.PERMISSION_GRANTED) { |
| 1572 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1573 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1574 | + " without permission " + android.Manifest.permission.DUMP); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1578 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1582 | static class DbBinder extends Binder { |
| 1583 | ActivityManagerService mActivityManagerService; |
| 1584 | DbBinder(ActivityManagerService activityManagerService) { |
| 1585 | mActivityManagerService = activityManagerService; |
| 1586 | } |
| 1587 | |
| 1588 | @Override |
| 1589 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1590 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1591 | != PackageManager.PERMISSION_GRANTED) { |
| 1592 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1593 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1594 | + " without permission " + android.Manifest.permission.DUMP); |
| 1595 | return; |
| 1596 | } |
| 1597 | |
| 1598 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1599 | } |
| 1600 | } |
| 1601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1602 | static class CpuBinder extends Binder { |
| 1603 | ActivityManagerService mActivityManagerService; |
| 1604 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1605 | mActivityManagerService = activityManagerService; |
| 1606 | } |
| 1607 | |
| 1608 | @Override |
| 1609 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1610 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1611 | != PackageManager.PERMISSION_GRANTED) { |
| 1612 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1613 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1614 | + " without permission " + android.Manifest.permission.DUMP); |
| 1615 | return; |
| 1616 | } |
| 1617 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1619 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1620 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1621 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1627 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1628 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1629 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1630 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1631 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1632 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 1633 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1634 | mServices = new ActiveServices(this); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1635 | mProviderMap = new ProviderMap(this); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1636 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | File dataDir = Environment.getDataDirectory(); |
| 1638 | File systemDir = new File(dataDir, "system"); |
| 1639 | systemDir.mkdirs(); |
| 1640 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1641 | systemDir, "batterystats.bin").toString()); |
| 1642 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1643 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1644 | mOnBattery = DEBUG_POWER ? true |
| 1645 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1646 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1647 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1648 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1649 | systemDir, "usagestats").toString()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1650 | mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml")); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1651 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1652 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1653 | // User 0 is the first and only user that runs at boot. |
| 1654 | mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true)); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1655 | mUserLru.add(Integer.valueOf(0)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1656 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1657 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1658 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1659 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1660 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1661 | mConfiguration.setToDefaults(); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1662 | mConfiguration.setLocale(Locale.getDefault()); |
| 1663 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1664 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | mProcessStats.init(); |
| 1666 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1667 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1668 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1669 | // Add ourself to the Watchdog monitors. |
| 1670 | Watchdog.getInstance().addMonitor(this); |
| 1671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1673 | public void run() { |
| 1674 | while (true) { |
| 1675 | try { |
| 1676 | try { |
| 1677 | synchronized(this) { |
| 1678 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1679 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1680 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1681 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | // + ", write delay=" + nextWriteDelay); |
| 1683 | if (nextWriteDelay < nextCpuDelay) { |
| 1684 | nextCpuDelay = nextWriteDelay; |
| 1685 | } |
| 1686 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1687 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | this.wait(nextCpuDelay); |
| 1689 | } |
| 1690 | } |
| 1691 | } catch (InterruptedException e) { |
| 1692 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | updateCpuStatsNow(); |
| 1694 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1695 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | }; |
| 1700 | mProcessStatsThread.start(); |
| 1701 | } |
| 1702 | |
| 1703 | @Override |
| 1704 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1705 | throws RemoteException { |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 1706 | if (code == SYSPROPS_TRANSACTION) { |
| 1707 | // We need to tell all apps about the system property change. |
| 1708 | ArrayList<IBinder> procs = new ArrayList<IBinder>(); |
| 1709 | synchronized(this) { |
| 1710 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 1711 | final int NA = apps.size(); |
| 1712 | for (int ia=0; ia<NA; ia++) { |
| 1713 | ProcessRecord app = apps.valueAt(ia); |
| 1714 | if (app.thread != null) { |
| 1715 | procs.add(app.thread.asBinder()); |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | int N = procs.size(); |
| 1722 | for (int i=0; i<N; i++) { |
| 1723 | Parcel data2 = Parcel.obtain(); |
| 1724 | try { |
| 1725 | procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0); |
| 1726 | } catch (RemoteException e) { |
| 1727 | } |
| 1728 | data2.recycle(); |
| 1729 | } |
| 1730 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | try { |
| 1732 | return super.onTransact(code, data, reply, flags); |
| 1733 | } catch (RuntimeException e) { |
| 1734 | // The activity manager only throws security exceptions, so let's |
| 1735 | // log all others. |
| 1736 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1737 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | } |
| 1739 | throw e; |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1744 | final long now = SystemClock.uptimeMillis(); |
| 1745 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1746 | return; |
| 1747 | } |
| 1748 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1749 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1750 | mProcessStatsThread.notify(); |
| 1751 | } |
| 1752 | } |
| 1753 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1754 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | void updateCpuStatsNow() { |
| 1756 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1757 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | final long now = SystemClock.uptimeMillis(); |
| 1759 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1760 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1761 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1762 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1763 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1764 | haveNewCpuStats = true; |
| 1765 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1766 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1767 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1769 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1770 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1772 | int user = mProcessStats.getLastUserTime(); |
| 1773 | int system = mProcessStats.getLastSystemTime(); |
| 1774 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1775 | int irq = mProcessStats.getLastIrqTime(); |
| 1776 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1777 | int idle = mProcessStats.getLastIdleTime(); |
| 1778 | |
| 1779 | int total = user + system + iowait + irq + softIrq + idle; |
| 1780 | if (total == 0) total = 1; |
| 1781 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1782 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1784 | (user * 100) / total, |
| 1785 | (system * 100) / total, |
| 1786 | (iowait * 100) / total, |
| 1787 | (irq * 100) / total, |
| 1788 | (softIrq * 100) / total); |
| 1789 | } |
| 1790 | } |
| 1791 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1792 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1793 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1794 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | synchronized(mPidsSelfLocked) { |
| 1796 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1797 | if (mOnBattery) { |
| 1798 | int perc = bstats.startAddingCpuLocked(); |
| 1799 | int totalUTime = 0; |
| 1800 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1801 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1803 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1804 | if (!st.working) { |
| 1805 | continue; |
| 1806 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1808 | int otherUTime = (st.rel_utime*perc)/100; |
| 1809 | int otherSTime = (st.rel_stime*perc)/100; |
| 1810 | totalUTime += otherUTime; |
| 1811 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1812 | if (pr != null) { |
| 1813 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1814 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1815 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1816 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1817 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1818 | } else { |
| 1819 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1820 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1821 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1822 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1823 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1824 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1825 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1826 | } |
| 1827 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1828 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1829 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1833 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1834 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1835 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1836 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1842 | @Override |
| 1843 | public void batteryNeedsCpuUpdate() { |
| 1844 | updateCpuStatsNow(); |
| 1845 | } |
| 1846 | |
| 1847 | @Override |
| 1848 | public void batteryPowerChanged(boolean onBattery) { |
| 1849 | // When plugging in, update the CPU stats first before changing |
| 1850 | // the plug state. |
| 1851 | updateCpuStatsNow(); |
| 1852 | synchronized (this) { |
| 1853 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1854 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1855 | } |
| 1856 | } |
| 1857 | } |
| 1858 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | /** |
| 1860 | * Initialize the application bind args. These are passed to each |
| 1861 | * process when the bindApplication() IPC is sent to the process. They're |
| 1862 | * lazily setup to make sure the services are running when they're asked for. |
| 1863 | */ |
| 1864 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1865 | if (mAppBindArgs == null) { |
| 1866 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1867 | |
| 1868 | // Setup the application init args |
| 1869 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1870 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1871 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1872 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1873 | } |
| 1874 | return mAppBindArgs; |
| 1875 | } |
| 1876 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1877 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1878 | if (mFocusedActivity != r) { |
| 1879 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1880 | if (r != null) { |
| 1881 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1882 | } |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1883 | applyUpdateLockStateLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 1887 | final void applyUpdateLockStateLocked(ActivityRecord r) { |
| 1888 | // Modifications to the UpdateLock state are done on our handler, outside |
| 1889 | // the activity manager's locks. The new state is determined based on the |
| 1890 | // state *now* of the relevant activity record. The object is passed to |
| 1891 | // the handler solely for logging detail, not to be consulted/modified. |
| 1892 | final boolean nextState = r != null && r.immersive; |
| 1893 | mHandler.sendMessage( |
| 1894 | mHandler.obtainMessage(IMMERSIVE_MODE_LOCK_MSG, (nextState) ? 1 : 0, 0, r)); |
| 1895 | } |
| 1896 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1897 | private final void updateLruProcessInternalLocked(ProcessRecord app, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1898 | // 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] | 1899 | int lrui = mLruProcesses.indexOf(app); |
| 1900 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1901 | |
| 1902 | int i = mLruProcesses.size()-1; |
| 1903 | int skipTop = 0; |
| 1904 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1905 | app.lruSeq = mLruSeq; |
| 1906 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1907 | // compute the new weight for this process. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1908 | app.lastActivityTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1909 | if (app.activities.size() > 0) { |
| 1910 | // If this process has activities, we more strongly want to keep |
| 1911 | // it around. |
| 1912 | app.lruWeight = app.lastActivityTime; |
| 1913 | } else if (app.pubProviders.size() > 0) { |
| 1914 | // If this process contains content providers, we want to keep |
| 1915 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1916 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1917 | // 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] | 1918 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1919 | } else { |
| 1920 | // If this process doesn't have activities, we less strongly |
| 1921 | // want to keep it around, and generally want to avoid getting |
| 1922 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1923 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1924 | // 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] | 1925 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1926 | } |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1927 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1928 | while (i >= 0) { |
| 1929 | ProcessRecord p = mLruProcesses.get(i); |
| 1930 | // If this app shouldn't be in front of the first N background |
| 1931 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1932 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1933 | skipTop--; |
| 1934 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1935 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1936 | mLruProcesses.add(i+1, app); |
| 1937 | break; |
| 1938 | } |
| 1939 | i--; |
| 1940 | } |
| 1941 | if (i < 0) { |
| 1942 | mLruProcesses.add(0, app); |
| 1943 | } |
| 1944 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1945 | // If the app is currently using a content provider or service, |
| 1946 | // bump those processes as well. |
| 1947 | if (app.connections.size() > 0) { |
| 1948 | for (ConnectionRecord cr : app.connections) { |
| 1949 | if (cr.binding != null && cr.binding.service != null |
| 1950 | && cr.binding.service.app != null |
| 1951 | && cr.binding.service.app.lruSeq != mLruSeq) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1952 | updateLruProcessInternalLocked(cr.binding.service.app, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1953 | } |
| 1954 | } |
| 1955 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1956 | for (int j=app.conProviders.size()-1; j>=0; j--) { |
| 1957 | ContentProviderRecord cpr = app.conProviders.get(j).provider; |
| 1958 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1959 | updateLruProcessInternalLocked(cpr.proc, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1960 | } |
| 1961 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1962 | } |
| 1963 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1964 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1965 | boolean oomAdj) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1966 | mLruSeq++; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1967 | updateLruProcessInternalLocked(app, 0); |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1968 | |
| 1969 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
| 1970 | if (oomAdj) { |
| 1971 | updateOomAdjLocked(); |
| 1972 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1973 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1974 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1975 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | String processName, int uid) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1977 | if (uid == Process.SYSTEM_UID) { |
Amith Yamasani | 0184ce9 | 2012-03-28 22:41:41 -0700 | [diff] [blame] | 1978 | // The system gets to run in any process. If there are multiple |
| 1979 | // processes with the same uid, just pick the first (this |
| 1980 | // should never happen). |
| 1981 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1982 | processName); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1983 | if (procs == null) return null; |
| 1984 | final int N = procs.size(); |
| 1985 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1986 | if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1987 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1988 | } |
| 1989 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1990 | return proc; |
| 1991 | } |
| 1992 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1993 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1994 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1995 | try { |
| 1996 | if (pm.performDexOpt(packageName)) { |
| 1997 | mDidDexOpt = true; |
| 1998 | } |
| 1999 | } catch (RemoteException e) { |
| 2000 | } |
| 2001 | } |
| 2002 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2003 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2004 | int transit = mWindowManager.getPendingAppTransition(); |
Craig Mautner | 4b71aa1 | 2012-12-27 17:20:01 -0800 | [diff] [blame] | 2005 | return transit == AppTransition.TRANSIT_ACTIVITY_OPEN |
| 2006 | || transit == AppTransition.TRANSIT_TASK_OPEN |
| 2007 | || transit == AppTransition.TRANSIT_TASK_TO_FRONT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2010 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2011 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2012 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 2013 | boolean isolated) { |
| 2014 | ProcessRecord app; |
| 2015 | if (!isolated) { |
| 2016 | app = getProcessRecordLocked(processName, info.uid); |
| 2017 | } else { |
| 2018 | // If this is an isolated process, it can't re-use an existing process. |
| 2019 | app = null; |
| 2020 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2021 | // We don't have to do anything more if: |
| 2022 | // (1) There is an existing application record; and |
| 2023 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 2024 | // object attached to it so we know it couldn't have crashed; and |
| 2025 | // (3) There is a pid assigned to it, so it is either starting or |
| 2026 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2027 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2028 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 2029 | + " thread=" + (app != null ? app.thread : null) |
| 2030 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2031 | if (app != null && app.pid > 0) { |
| 2032 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 2033 | // We already have the app running, or are waiting for it to |
| 2034 | // 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] | 2035 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 2036 | // If this is a new package in the process, add the package to the list |
| 2037 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2038 | return app; |
| 2039 | } else { |
| 2040 | // An application record is attached to a previous process, |
| 2041 | // clean it up now. |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 2042 | if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2043 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2044 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2045 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2046 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2047 | String hostingNameStr = hostingName != null |
| 2048 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2049 | |
| 2050 | if (!isolated) { |
| 2051 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 2052 | // If we are in the background, then check to see if this process |
| 2053 | // is bad. If so, we will just silently fail. |
| 2054 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 2055 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 2056 | + "/" + info.processName); |
| 2057 | return null; |
| 2058 | } |
| 2059 | } else { |
| 2060 | // When the user is explicitly starting a process, then clear its |
| 2061 | // crash count so that we won't make it bad until they see at |
| 2062 | // least one crash dialog again, and make the process good again |
| 2063 | // if it had been bad. |
| 2064 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2065 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2066 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 2067 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2068 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, |
| 2069 | UserHandle.getUserId(info.uid), info.uid, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2070 | info.processName); |
| 2071 | mBadProcesses.remove(info.processName, info.uid); |
| 2072 | if (app != null) { |
| 2073 | app.bad = false; |
| 2074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2078 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2080 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 2081 | if (app == null) { |
| 2082 | Slog.w(TAG, "Failed making new process record for " |
| 2083 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 2084 | return null; |
| 2085 | } |
| 2086 | mProcessNames.put(processName, app.uid, app); |
| 2087 | if (isolated) { |
| 2088 | mIsolatedProcesses.put(app.uid, app); |
| 2089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2090 | } else { |
| 2091 | // If this is a new package in the process, add the package to the list |
| 2092 | app.addPackage(info.packageName); |
| 2093 | } |
| 2094 | |
| 2095 | // If the system is not ready yet, then hold off on starting this |
| 2096 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2097 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2098 | && !isAllowedWhileBooting(info) |
| 2099 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2100 | if (!mProcessesOnHold.contains(app)) { |
| 2101 | mProcessesOnHold.add(app); |
| 2102 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2103 | 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] | 2104 | return app; |
| 2105 | } |
| 2106 | |
| 2107 | startProcessLocked(app, hostingType, hostingNameStr); |
| 2108 | return (app.pid != 0) ? app : null; |
| 2109 | } |
| 2110 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2111 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 2112 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 2113 | } |
| 2114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2115 | private final void startProcessLocked(ProcessRecord app, |
| 2116 | String hostingType, String hostingNameStr) { |
| 2117 | if (app.pid > 0 && app.pid != MY_PID) { |
| 2118 | synchronized (mPidsSelfLocked) { |
| 2119 | mPidsSelfLocked.remove(app.pid); |
| 2120 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 2121 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2122 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2123 | } |
| 2124 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2125 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 2126 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2127 | mProcessesOnHold.remove(app); |
| 2128 | |
| 2129 | updateCpuStats(); |
| 2130 | |
| 2131 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 2132 | mProcDeaths[0] = 0; |
| 2133 | |
| 2134 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2135 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2137 | int[] gids = null; |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2138 | int mountExternal = Zygote.MOUNT_EXTERNAL_NONE; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2139 | if (!app.isolated) { |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2140 | int[] permGids = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2141 | try { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2142 | final PackageManager pm = mContext.getPackageManager(); |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2143 | permGids = pm.getPackageGids(app.info.packageName); |
Jeff Sharkey | e217ee4 | 2012-08-28 16:23:01 -0700 | [diff] [blame] | 2144 | |
| 2145 | if (Environment.isExternalStorageEmulated()) { |
| 2146 | if (pm.checkPermission( |
| 2147 | android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE, |
| 2148 | app.info.packageName) == PERMISSION_GRANTED) { |
| 2149 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL; |
| 2150 | } else { |
| 2151 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER; |
| 2152 | } |
| 2153 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2154 | } catch (PackageManager.NameNotFoundException e) { |
| 2155 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 2156 | } |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2157 | |
| 2158 | /* |
| 2159 | * Add shared application GID so applications can share some |
| 2160 | * resources like shared libraries |
| 2161 | */ |
| 2162 | if (permGids == null) { |
| 2163 | gids = new int[1]; |
| 2164 | } else { |
| 2165 | gids = new int[permGids.length + 1]; |
| 2166 | System.arraycopy(permGids, 0, gids, 1, permGids.length); |
| 2167 | } |
| 2168 | gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2169 | } |
| 2170 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 2171 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2172 | && mTopComponent != null |
| 2173 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 2174 | uid = 0; |
| 2175 | } |
| 2176 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 2177 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 2178 | uid = 0; |
| 2179 | } |
| 2180 | } |
| 2181 | int debugFlags = 0; |
| 2182 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 2183 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 2184 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 2185 | // awkward to turn on otherwise. |
| 2186 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2187 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 2188 | // Run the app in safe mode if its manifest requests so or the |
| 2189 | // system is booted in safe mode. |
| 2190 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 2191 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 2192 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 2193 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2194 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 2195 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 2196 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 2197 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 2198 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 2199 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2200 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 2201 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 2202 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2203 | |
| 2204 | // Start the process. It will either succeed and return a result containing |
| 2205 | // the PID of the new process, or else throw a RuntimeException. |
| 2206 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2207 | app.processName, uid, uid, gids, debugFlags, mountExternal, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 2208 | app.info.targetSdkVersion, null, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2210 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2211 | synchronized (bs) { |
| 2212 | if (bs.isOnBattery()) { |
| 2213 | app.batteryStats.incStartsLocked(); |
| 2214 | } |
| 2215 | } |
| 2216 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2217 | EventLog.writeEvent(EventLogTags.AM_PROC_START, |
| 2218 | UserHandle.getUserId(uid), startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2219 | app.processName, hostingType, |
| 2220 | hostingNameStr != null ? hostingNameStr : ""); |
| 2221 | |
| 2222 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2223 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2224 | } |
| 2225 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2226 | StringBuilder buf = mStringBuilder; |
| 2227 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2228 | buf.append("Start proc "); |
| 2229 | buf.append(app.processName); |
| 2230 | buf.append(" for "); |
| 2231 | buf.append(hostingType); |
| 2232 | if (hostingNameStr != null) { |
| 2233 | buf.append(" "); |
| 2234 | buf.append(hostingNameStr); |
| 2235 | } |
| 2236 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2237 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2238 | buf.append(" uid="); |
| 2239 | buf.append(uid); |
| 2240 | buf.append(" gids={"); |
| 2241 | if (gids != null) { |
| 2242 | for (int gi=0; gi<gids.length; gi++) { |
| 2243 | if (gi != 0) buf.append(", "); |
| 2244 | buf.append(gids[gi]); |
| 2245 | |
| 2246 | } |
| 2247 | } |
| 2248 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2249 | Slog.i(TAG, buf.toString()); |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2250 | app.setPid(startResult.pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2251 | app.usingWrapper = startResult.usingWrapper; |
| 2252 | app.removed = false; |
| 2253 | synchronized (mPidsSelfLocked) { |
| 2254 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2255 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2256 | msg.obj = app; |
| 2257 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2258 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2259 | } |
| 2260 | } catch (RuntimeException e) { |
| 2261 | // XXX do better error recovery. |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2262 | app.setPid(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2263 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2267 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2268 | if (resumed) { |
| 2269 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2270 | } else { |
| 2271 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2272 | } |
| 2273 | } |
| 2274 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2275 | boolean startHomeActivityLocked(int userId) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2276 | if (mHeadless) { |
| 2277 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2278 | // when running headless. |
| 2279 | ensureBootCompleted(); |
| 2280 | return false; |
| 2281 | } |
| 2282 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2283 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2284 | && mTopAction == null) { |
| 2285 | // We are running in factory test mode, but unable to find |
| 2286 | // the factory test app, so just sit around displaying the |
| 2287 | // error message and don't try to start anything. |
| 2288 | return false; |
| 2289 | } |
| 2290 | Intent intent = new Intent( |
| 2291 | mTopAction, |
| 2292 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2293 | intent.setComponent(mTopComponent); |
| 2294 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2295 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2296 | } |
| 2297 | ActivityInfo aInfo = |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2298 | resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2299 | if (aInfo != null) { |
| 2300 | intent.setComponent(new ComponentName( |
| 2301 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2302 | // Don't do this if the home app is currently being |
| 2303 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2304 | aInfo = new ActivityInfo(aInfo); |
| 2305 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2306 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2307 | aInfo.applicationInfo.uid); |
| 2308 | if (app == null || app.instrumentationClass == null) { |
| 2309 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2310 | mMainStack.startActivityLocked(null, intent, null, aInfo, |
| 2311 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2312 | } |
| 2313 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2314 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2315 | return true; |
| 2316 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2317 | |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2318 | private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) { |
| 2319 | ActivityInfo ai = null; |
| 2320 | ComponentName comp = intent.getComponent(); |
| 2321 | try { |
| 2322 | if (comp != null) { |
| 2323 | ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId); |
| 2324 | } else { |
| 2325 | ResolveInfo info = AppGlobals.getPackageManager().resolveIntent( |
| 2326 | intent, |
| 2327 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
| 2328 | flags, userId); |
| 2329 | |
| 2330 | if (info != null) { |
| 2331 | ai = info.activityInfo; |
| 2332 | } |
| 2333 | } |
| 2334 | } catch (RemoteException e) { |
| 2335 | // ignore |
| 2336 | } |
| 2337 | |
| 2338 | return ai; |
| 2339 | } |
| 2340 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2341 | /** |
| 2342 | * Starts the "new version setup screen" if appropriate. |
| 2343 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2344 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2345 | // Only do this once per boot. |
| 2346 | if (mCheckedForSetup) { |
| 2347 | return; |
| 2348 | } |
| 2349 | |
| 2350 | // We will show this screen if the current one is a different |
| 2351 | // version than the last one shown, and we are not running in |
| 2352 | // low-level factory test mode. |
| 2353 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2354 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2355 | Settings.Global.getInt(resolver, |
| 2356 | Settings.Global.DEVICE_PROVISIONED, 0) != 0) { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2357 | mCheckedForSetup = true; |
| 2358 | |
| 2359 | // See if we should be showing the platform update setup UI. |
| 2360 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2361 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2362 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2363 | |
| 2364 | // We don't allow third party apps to replace this. |
| 2365 | ResolveInfo ri = null; |
| 2366 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2367 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2368 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2369 | ri = ris.get(i); |
| 2370 | break; |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | if (ri != null) { |
| 2375 | String vers = ri.activityInfo.metaData != null |
| 2376 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2377 | : null; |
| 2378 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2379 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2380 | Intent.METADATA_SETUP_VERSION); |
| 2381 | } |
| 2382 | String lastVers = Settings.Secure.getString( |
| 2383 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2384 | if (vers != null && !vers.equals(lastVers)) { |
| 2385 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2386 | intent.setComponent(new ComponentName( |
| 2387 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2388 | mMainStack.startActivityLocked(null, intent, null, ri.activityInfo, |
| 2389 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | } |
| 2394 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2395 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2396 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2397 | } |
| 2398 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2399 | void enforceNotIsolatedCaller(String caller) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2400 | if (UserHandle.isIsolated(Binder.getCallingUid())) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2401 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2402 | } |
| 2403 | } |
| 2404 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2405 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2406 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2407 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2408 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 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 setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2413 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2414 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2415 | synchronized (this) { |
| 2416 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2417 | } |
| 2418 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2419 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2420 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2421 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2422 | synchronized (this) { |
| 2423 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2424 | } |
| 2425 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2426 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2427 | public void setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2428 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2429 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2430 | synchronized (this) { |
| 2431 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2435 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2436 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2437 | synchronized (this) { |
| 2438 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2443 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2444 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2445 | synchronized (this) { |
| 2446 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2447 | } |
| 2448 | } |
| 2449 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2450 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2451 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2452 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2453 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2454 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2455 | private void dispatchProcessesChanged() { |
| 2456 | int N; |
| 2457 | synchronized (this) { |
| 2458 | N = mPendingProcessChanges.size(); |
| 2459 | if (mActiveProcessChanges.length < N) { |
| 2460 | mActiveProcessChanges = new ProcessChangeItem[N]; |
| 2461 | } |
| 2462 | mPendingProcessChanges.toArray(mActiveProcessChanges); |
| 2463 | mAvailProcessChanges.addAll(mPendingProcessChanges); |
| 2464 | mPendingProcessChanges.clear(); |
| 2465 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes"); |
| 2466 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2467 | int i = mProcessObservers.beginBroadcast(); |
| 2468 | while (i > 0) { |
| 2469 | i--; |
| 2470 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2471 | if (observer != null) { |
| 2472 | try { |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2473 | for (int j=0; j<N; j++) { |
| 2474 | ProcessChangeItem item = mActiveProcessChanges[j]; |
| 2475 | if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) { |
| 2476 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid=" |
| 2477 | + item.pid + " uid=" + item.uid + ": " |
| 2478 | + item.foregroundActivities); |
| 2479 | observer.onForegroundActivitiesChanged(item.pid, item.uid, |
| 2480 | item.foregroundActivities); |
| 2481 | } |
| 2482 | if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) { |
| 2483 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid=" |
| 2484 | + item.pid + " uid=" + item.uid + ": " + item.importance); |
| 2485 | observer.onImportanceChanged(item.pid, item.uid, |
| 2486 | item.importance); |
| 2487 | } |
| 2488 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2489 | } catch (RemoteException e) { |
| 2490 | } |
| 2491 | } |
| 2492 | } |
| 2493 | mProcessObservers.finishBroadcast(); |
| 2494 | } |
| 2495 | |
| 2496 | private void dispatchProcessDied(int pid, int uid) { |
| 2497 | int i = mProcessObservers.beginBroadcast(); |
| 2498 | while (i > 0) { |
| 2499 | i--; |
| 2500 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2501 | if (observer != null) { |
| 2502 | try { |
| 2503 | observer.onProcessDied(pid, uid); |
| 2504 | } catch (RemoteException e) { |
| 2505 | } |
| 2506 | } |
| 2507 | } |
| 2508 | mProcessObservers.finishBroadcast(); |
| 2509 | } |
| 2510 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2511 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2512 | final int N = mPendingActivityLaunches.size(); |
| 2513 | if (N <= 0) { |
| 2514 | return; |
| 2515 | } |
| 2516 | for (int i=0; i<N; i++) { |
| 2517 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2518 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2519 | pal.startFlags, doResume && i == (N-1), null); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2520 | } |
| 2521 | mPendingActivityLaunches.clear(); |
| 2522 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2523 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2524 | public final int startActivity(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2525 | Intent intent, String resolvedType, IBinder resultTo, |
| 2526 | String resultWho, int requestCode, int startFlags, |
| 2527 | String profileFile, ParcelFileDescriptor profileFd, Bundle options) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2528 | return startActivityAsUser(caller, intent, resolvedType, resultTo, resultWho, requestCode, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2529 | startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId()); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | public final int startActivityAsUser(IApplicationThread caller, |
| 2533 | Intent intent, String resolvedType, IBinder resultTo, |
| 2534 | String resultWho, int requestCode, int startFlags, |
| 2535 | String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2536 | enforceNotIsolatedCaller("startActivity"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2537 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | d888399 | 2012-09-07 15:58:52 -0700 | [diff] [blame] | 2538 | false, true, "startActivity", null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2539 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2540 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2541 | null, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
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, String profileFile, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2547 | ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2548 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2549 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2550 | false, true, "startActivityAndWait", null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2551 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2552 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2553 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2554 | res, null, options, UserHandle.getCallingUserId()); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2555 | return res; |
| 2556 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 2557 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2558 | public final int startActivityWithConfig(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2559 | Intent intent, String resolvedType, IBinder resultTo, |
| 2560 | String resultWho, int requestCode, int startFlags, Configuration config, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2561 | Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2562 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2563 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2564 | false, true, "startActivityWithConfig", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2565 | int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2566 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2567 | null, null, null, config, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2568 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2569 | } |
| 2570 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2571 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2572 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2573 | IBinder resultTo, String resultWho, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2574 | int flagsMask, int flagsValues, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2575 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2576 | // Refuse possible leaked file descriptors |
| 2577 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2578 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2579 | } |
| 2580 | |
| 2581 | IIntentSender sender = intent.getTarget(); |
| 2582 | if (!(sender instanceof PendingIntentRecord)) { |
| 2583 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2584 | } |
| 2585 | |
| 2586 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2587 | |
| 2588 | synchronized (this) { |
| 2589 | // If this is coming from the currently resumed activity, it is |
| 2590 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2591 | if (mMainStack.mResumedActivity != null |
| 2592 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2593 | Binder.getCallingUid()) { |
| 2594 | mAppSwitchesAllowedTime = 0; |
| 2595 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2596 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2597 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null, |
| 2598 | resultTo, resultWho, requestCode, flagsMask, flagsValues, options); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2599 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2600 | } |
| 2601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | public boolean startNextMatchingActivity(IBinder callingActivity, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2603 | Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | // Refuse possible leaked file descriptors |
| 2605 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2606 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2607 | } |
| 2608 | |
| 2609 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2610 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2611 | if (r == null) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2612 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2613 | return false; |
| 2614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | if (r.app == null || r.app.thread == null) { |
| 2616 | // The caller is not running... d'oh! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2617 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2618 | return false; |
| 2619 | } |
| 2620 | intent = new Intent(intent); |
| 2621 | // The caller is not allowed to change the data. |
| 2622 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2623 | // And we are resetting to find the next component... |
| 2624 | intent.setComponent(null); |
| 2625 | |
| 2626 | ActivityInfo aInfo = null; |
| 2627 | try { |
| 2628 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2629 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2630 | intent, r.resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2631 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2632 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2633 | |
| 2634 | // Look for the original activity in the list... |
| 2635 | final int N = resolves != null ? resolves.size() : 0; |
| 2636 | for (int i=0; i<N; i++) { |
| 2637 | ResolveInfo rInfo = resolves.get(i); |
| 2638 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2639 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2640 | // We found the current one... the next matching is |
| 2641 | // after it. |
| 2642 | i++; |
| 2643 | if (i<N) { |
| 2644 | aInfo = resolves.get(i).activityInfo; |
| 2645 | } |
| 2646 | break; |
| 2647 | } |
| 2648 | } |
| 2649 | } catch (RemoteException e) { |
| 2650 | } |
| 2651 | |
| 2652 | if (aInfo == null) { |
| 2653 | // Nobody who is next! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2654 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2655 | return false; |
| 2656 | } |
| 2657 | |
| 2658 | intent.setComponent(new ComponentName( |
| 2659 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2660 | intent.setFlags(intent.getFlags()&~( |
| 2661 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2662 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2663 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2664 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2665 | |
| 2666 | // Okay now we need to start the new activity, replacing the |
| 2667 | // currently running activity. This is a little tricky because |
| 2668 | // we want to start the new one as if the current one is finished, |
| 2669 | // but not finish the current one first so that there is no flicker. |
| 2670 | // And thus... |
| 2671 | final boolean wasFinishing = r.finishing; |
| 2672 | r.finishing = true; |
| 2673 | |
| 2674 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2675 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2676 | final String resultWho = r.resultWho; |
| 2677 | final int requestCode = r.requestCode; |
| 2678 | r.resultTo = null; |
| 2679 | if (resultTo != null) { |
| 2680 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2681 | } |
| 2682 | |
| 2683 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2684 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2685 | r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null, |
| 2686 | resultWho, requestCode, -1, r.launchedFromUid, 0, |
| 2687 | options, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2688 | Binder.restoreCallingIdentity(origId); |
| 2689 | |
| 2690 | r.finishing = wasFinishing; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2691 | if (res != ActivityManager.START_SUCCESS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2692 | return false; |
| 2693 | } |
| 2694 | return true; |
| 2695 | } |
| 2696 | } |
| 2697 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2698 | final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2699 | Intent intent, String resolvedType, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2700 | String resultWho, int requestCode, int startFlags, Bundle options, int userId) { |
| 2701 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2702 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2703 | false, true, "startActivityInPackage", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2704 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2705 | int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2706 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2707 | null, null, null, null, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2708 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | public final int startActivities(IApplicationThread caller, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2712 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options, |
| 2713 | int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2714 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 16979ea | 2012-09-24 18:14:47 -0700 | [diff] [blame] | 2715 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2716 | false, true, "startActivity", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2717 | int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2718 | options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2719 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2720 | } |
| 2721 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2722 | final int startActivitiesInPackage(int uid, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2723 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2724 | Bundle options, int userId) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2725 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2726 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2727 | false, true, "startActivityInPackage", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2728 | int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2729 | options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2730 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | } |
| 2732 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2733 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2734 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2735 | // Quick case: check if the top-most recent task is the same. |
| 2736 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2737 | return; |
| 2738 | } |
| 2739 | // 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] | 2740 | for (int i=0; i<N; i++) { |
| 2741 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2742 | if (task.userId == tr.userId |
| 2743 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2744 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | mRecentTasks.remove(i); |
| 2746 | i--; |
| 2747 | N--; |
| 2748 | if (task.intent == null) { |
| 2749 | // If the new recent task we are adding is not fully |
| 2750 | // specified, then replace it with the existing recent task. |
| 2751 | task = tr; |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | if (N >= MAX_RECENT_TASKS) { |
| 2756 | mRecentTasks.remove(N-1); |
| 2757 | } |
| 2758 | mRecentTasks.add(0, task); |
| 2759 | } |
| 2760 | |
| 2761 | public void setRequestedOrientation(IBinder token, |
| 2762 | int requestedOrientation) { |
| 2763 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2764 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2765 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2766 | return; |
| 2767 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2768 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2769 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2770 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2771 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2772 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2773 | if (config != null) { |
| 2774 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2775 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2776 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | Binder.restoreCallingIdentity(origId); |
| 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | public int getRequestedOrientation(IBinder token) { |
| 2784 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2785 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2786 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2787 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2788 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2789 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2790 | } |
| 2791 | } |
| 2792 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2793 | /** |
| 2794 | * This is the internal entry point for handling Activity.finish(). |
| 2795 | * |
| 2796 | * @param token The Binder token referencing the Activity we want to finish. |
| 2797 | * @param resultCode Result code, if any, from this Activity. |
| 2798 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2799 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2800 | * @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] | 2801 | */ |
| 2802 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2803 | // Refuse possible leaked file descriptors |
| 2804 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2805 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2806 | } |
| 2807 | |
| 2808 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2809 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2810 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2811 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2812 | if (next != null) { |
| 2813 | // ask watcher if this is allowed |
| 2814 | boolean resumeOK = true; |
| 2815 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2816 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2817 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2818 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | if (!resumeOK) { |
| 2822 | return false; |
| 2823 | } |
| 2824 | } |
| 2825 | } |
| 2826 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2827 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2828 | resultData, "app-request", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2829 | Binder.restoreCallingIdentity(origId); |
| 2830 | return res; |
| 2831 | } |
| 2832 | } |
| 2833 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2834 | public final void finishHeavyWeightApp() { |
| 2835 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2836 | != PackageManager.PERMISSION_GRANTED) { |
| 2837 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2838 | + Binder.getCallingPid() |
| 2839 | + ", uid=" + Binder.getCallingUid() |
| 2840 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2841 | Slog.w(TAG, msg); |
| 2842 | throw new SecurityException(msg); |
| 2843 | } |
| 2844 | |
| 2845 | synchronized(this) { |
| 2846 | if (mHeavyWeightProcess == null) { |
| 2847 | return; |
| 2848 | } |
| 2849 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2850 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2851 | mHeavyWeightProcess.activities); |
| 2852 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2853 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2854 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2855 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2856 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2857 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2858 | null, "finish-heavy", true); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | } |
| 2862 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2863 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 2864 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2865 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2866 | } |
| 2867 | } |
| 2868 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2869 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2870 | String message) { |
| 2871 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2872 | != PackageManager.PERMISSION_GRANTED) { |
| 2873 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2874 | + Binder.getCallingPid() |
| 2875 | + ", uid=" + Binder.getCallingUid() |
| 2876 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2877 | Slog.w(TAG, msg); |
| 2878 | throw new SecurityException(msg); |
| 2879 | } |
| 2880 | |
| 2881 | synchronized(this) { |
| 2882 | ProcessRecord proc = null; |
| 2883 | |
| 2884 | // Figure out which process to kill. We don't trust that initialPid |
| 2885 | // still has any relation to current pids, so must scan through the |
| 2886 | // list. |
| 2887 | synchronized (mPidsSelfLocked) { |
| 2888 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2889 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2890 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2891 | continue; |
| 2892 | } |
| 2893 | if (p.pid == initialPid) { |
| 2894 | proc = p; |
| 2895 | break; |
| 2896 | } |
| 2897 | for (String str : p.pkgList) { |
| 2898 | if (str.equals(packageName)) { |
| 2899 | proc = p; |
| 2900 | } |
| 2901 | } |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2906 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2907 | + " initialPid=" + initialPid |
| 2908 | + " packageName=" + packageName); |
| 2909 | return; |
| 2910 | } |
| 2911 | |
| 2912 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2913 | if (proc.pid == Process.myPid()) { |
| 2914 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2915 | return; |
| 2916 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2917 | long ident = Binder.clearCallingIdentity(); |
| 2918 | try { |
| 2919 | proc.thread.scheduleCrash(message); |
| 2920 | } catch (RemoteException e) { |
| 2921 | } |
| 2922 | Binder.restoreCallingIdentity(ident); |
| 2923 | } |
| 2924 | } |
| 2925 | } |
| 2926 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2927 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2928 | int requestCode) { |
| 2929 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2930 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2931 | mMainStack.finishSubActivityLocked(token, resultWho, requestCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2932 | Binder.restoreCallingIdentity(origId); |
| 2933 | } |
| 2934 | } |
| 2935 | |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2936 | public boolean finishActivityAffinity(IBinder token) { |
| 2937 | synchronized(this) { |
| 2938 | final long origId = Binder.clearCallingIdentity(); |
| 2939 | boolean res = mMainStack.finishActivityAffinityLocked(token); |
| 2940 | Binder.restoreCallingIdentity(origId); |
| 2941 | return res; |
| 2942 | } |
| 2943 | } |
| 2944 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2945 | public boolean willActivityBeVisible(IBinder token) { |
| 2946 | synchronized(this) { |
| 2947 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2948 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2949 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2950 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2951 | return true; |
| 2952 | } |
| 2953 | if (r.fullscreen && !r.finishing) { |
| 2954 | return false; |
| 2955 | } |
| 2956 | } |
| 2957 | return true; |
| 2958 | } |
| 2959 | } |
| 2960 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2961 | public void overridePendingTransition(IBinder token, String packageName, |
| 2962 | int enterAnim, int exitAnim) { |
| 2963 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2964 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2965 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2966 | return; |
| 2967 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2968 | |
| 2969 | final long origId = Binder.clearCallingIdentity(); |
| 2970 | |
| 2971 | if (self.state == ActivityState.RESUMED |
| 2972 | || self.state == ActivityState.PAUSING) { |
| 2973 | mWindowManager.overridePendingAppTransition(packageName, |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2974 | enterAnim, exitAnim, null); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | Binder.restoreCallingIdentity(origId); |
| 2978 | } |
| 2979 | } |
| 2980 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2981 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2982 | * Main function for removing an existing process from the activity manager |
| 2983 | * as a result of that process going away. Clears out all connections |
| 2984 | * to the process. |
| 2985 | */ |
| 2986 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2987 | boolean restarting, boolean allowRestart) { |
| 2988 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2989 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2990 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2991 | } |
| 2992 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2993 | if (mProfileProc == app) { |
| 2994 | clearProfilerLocked(); |
| 2995 | } |
| 2996 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2997 | // Just in case... |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2998 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 2999 | if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG, |
| 3000 | "App died while pausing: " + mMainStack.mPausingActivity); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 3001 | mMainStack.mPausingActivity = null; |
| 3002 | } |
| 3003 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 3004 | mMainStack.mLastPausedActivity = null; |
| 3005 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3006 | |
| 3007 | // Remove this application's activities from active lists. |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3008 | boolean hasVisibleActivities = mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3009 | |
| 3010 | app.activities.clear(); |
| 3011 | |
| 3012 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3013 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3014 | + " running instrumentation " + app.instrumentationClass); |
| 3015 | Bundle info = new Bundle(); |
| 3016 | info.putString("shortMsg", "Process crashed."); |
| 3017 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 3018 | } |
| 3019 | |
| 3020 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3021 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3022 | // If there was nothing to resume, and we are not already |
| 3023 | // restarting this process, but there is a visible activity that |
| 3024 | // is hosted by the process... then make sure all visible |
| 3025 | // activities are running, taking care of restarting this |
| 3026 | // process. |
| 3027 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3028 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 3035 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3036 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3037 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3038 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3039 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 3040 | return i; |
| 3041 | } |
| 3042 | } |
| 3043 | return -1; |
| 3044 | } |
| 3045 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3046 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3047 | IApplicationThread thread) { |
| 3048 | if (thread == null) { |
| 3049 | return null; |
| 3050 | } |
| 3051 | |
| 3052 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3053 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3054 | } |
| 3055 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3056 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3057 | IApplicationThread thread) { |
| 3058 | |
| 3059 | mProcDeaths[0]++; |
| 3060 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3061 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 3062 | synchronized (stats) { |
| 3063 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 3064 | } |
| 3065 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3066 | // Clean up already done if the process has been re-started. |
| 3067 | if (app.pid == pid && app.thread != null && |
| 3068 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 3069 | if (!app.killedBackground) { |
| 3070 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 3071 | + ") has died."); |
| 3072 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3073 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName); |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 3074 | if (DEBUG_CLEANUP) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3075 | TAG, "Dying app: " + app + ", pid: " + pid |
| 3076 | + ", thread: " + thread.asBinder()); |
| 3077 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3078 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3079 | |
| 3080 | if (doLowMem) { |
| 3081 | // If there are no longer any background processes running, |
| 3082 | // and the app that died was not running instrumentation, |
| 3083 | // then tell everyone we are now low on memory. |
| 3084 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3085 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3086 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3087 | 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] | 3088 | haveBg = true; |
| 3089 | break; |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3094 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3095 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3096 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3097 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 3098 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3099 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 3100 | // The low memory report is overriding any current |
| 3101 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 3102 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3103 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3104 | rec.lastRequestedGc = 0; |
| 3105 | } else { |
| 3106 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3107 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3108 | rec.reportLowMemory = true; |
| 3109 | rec.lastLowMemory = now; |
| 3110 | mProcessesToGc.remove(rec); |
| 3111 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3112 | } |
| 3113 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 3114 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3115 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3116 | } |
| 3117 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3118 | } else if (app.pid != pid) { |
| 3119 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3120 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3121 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3122 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3123 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3124 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3125 | + thread.asBinder()); |
| 3126 | } |
| 3127 | } |
| 3128 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3129 | /** |
| 3130 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3131 | * @param clearTraces causes the dump file to be erased prior to the new |
| 3132 | * traces being written, if true; when false, the new traces will be |
| 3133 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3134 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 3135 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3136 | * @param nativeProcs optional list of native process names to dump stack crawls |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3137 | * @return file containing stack traces, or null if no dump file is configured |
| 3138 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3139 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3140 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3141 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3142 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3143 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3144 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3145 | |
| 3146 | File tracesFile = new File(tracesPath); |
| 3147 | try { |
| 3148 | File tracesDir = tracesFile.getParentFile(); |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3149 | if (!tracesDir.exists()) { |
| 3150 | tracesFile.mkdirs(); |
| 3151 | if (!SELinux.restorecon(tracesDir)) { |
| 3152 | return null; |
| 3153 | } |
| 3154 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3155 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3156 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3157 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3158 | tracesFile.createNewFile(); |
| 3159 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3160 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3161 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3162 | return null; |
| 3163 | } |
| 3164 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3165 | dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3166 | return tracesFile; |
| 3167 | } |
| 3168 | |
| 3169 | private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3170 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3171 | // Use a FileObserver to detect when traces finish writing. |
| 3172 | // The order of traces is considered important to maintain for legibility. |
| 3173 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 3174 | public synchronized void onEvent(int event, String path) { notify(); } |
| 3175 | }; |
| 3176 | |
| 3177 | try { |
| 3178 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3179 | |
| 3180 | // First collect all of the stacks of the most important pids. |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3181 | if (firstPids != null) { |
| 3182 | try { |
| 3183 | int num = firstPids.size(); |
| 3184 | for (int i = 0; i < num; i++) { |
| 3185 | synchronized (observer) { |
| 3186 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3187 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3188 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3189 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3190 | } catch (InterruptedException e) { |
| 3191 | Log.wtf(TAG, e); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3192 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | // Next measure CPU usage. |
| 3196 | if (processStats != null) { |
| 3197 | processStats.init(); |
| 3198 | System.gc(); |
| 3199 | processStats.update(); |
| 3200 | try { |
| 3201 | synchronized (processStats) { |
| 3202 | processStats.wait(500); // measure over 1/2 second. |
| 3203 | } |
| 3204 | } catch (InterruptedException e) { |
| 3205 | } |
| 3206 | processStats.update(); |
| 3207 | |
| 3208 | // We'll take the stack crawls of just the top apps using CPU. |
| 3209 | final int N = processStats.countWorkingStats(); |
| 3210 | int numProcs = 0; |
| 3211 | for (int i=0; i<N && numProcs<5; i++) { |
| 3212 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3213 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3214 | numProcs++; |
| 3215 | try { |
| 3216 | synchronized (observer) { |
| 3217 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3218 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3219 | } |
| 3220 | } catch (InterruptedException e) { |
| 3221 | Log.wtf(TAG, e); |
| 3222 | } |
| 3223 | |
| 3224 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3225 | } |
| 3226 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3227 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3228 | } finally { |
| 3229 | observer.stopWatching(); |
| 3230 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3231 | |
| 3232 | if (nativeProcs != null) { |
| 3233 | int[] pids = Process.getPidsForCommands(nativeProcs); |
| 3234 | if (pids != null) { |
| 3235 | for (int pid : pids) { |
| 3236 | Debug.dumpNativeBacktraceToFile(pid, tracesPath); |
| 3237 | } |
| 3238 | } |
| 3239 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3240 | } |
| 3241 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3242 | final void logAppTooSlow(ProcessRecord app, long startTime, String msg) { |
Dianne Hackborn | 69dc66e | 2012-03-26 10:50:54 -0700 | [diff] [blame] | 3243 | if (true || IS_USER_BUILD) { |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3244 | return; |
| 3245 | } |
| 3246 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3247 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3248 | return; |
| 3249 | } |
| 3250 | |
| 3251 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 3252 | StrictMode.allowThreadDiskWrites(); |
| 3253 | try { |
| 3254 | final File tracesFile = new File(tracesPath); |
| 3255 | final File tracesDir = tracesFile.getParentFile(); |
| 3256 | final File tracesTmp = new File(tracesDir, "__tmp__"); |
| 3257 | try { |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3258 | if (!tracesDir.exists()) { |
| 3259 | tracesFile.mkdirs(); |
| 3260 | if (!SELinux.restorecon(tracesDir.getPath())) { |
| 3261 | return; |
| 3262 | } |
| 3263 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3264 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3265 | |
| 3266 | if (tracesFile.exists()) { |
| 3267 | tracesTmp.delete(); |
| 3268 | tracesFile.renameTo(tracesTmp); |
| 3269 | } |
| 3270 | StringBuilder sb = new StringBuilder(); |
| 3271 | Time tobj = new Time(); |
| 3272 | tobj.set(System.currentTimeMillis()); |
| 3273 | sb.append(tobj.format("%Y-%m-%d %H:%M:%S")); |
| 3274 | sb.append(": "); |
| 3275 | TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb); |
| 3276 | sb.append(" since "); |
| 3277 | sb.append(msg); |
| 3278 | FileOutputStream fos = new FileOutputStream(tracesFile); |
| 3279 | fos.write(sb.toString().getBytes()); |
| 3280 | if (app == null) { |
| 3281 | fos.write("\n*** No application process!".getBytes()); |
| 3282 | } |
| 3283 | fos.close(); |
| 3284 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3285 | } catch (IOException e) { |
| 3286 | Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e); |
| 3287 | return; |
| 3288 | } |
| 3289 | |
| 3290 | if (app != null) { |
| 3291 | ArrayList<Integer> firstPids = new ArrayList<Integer>(); |
| 3292 | firstPids.add(app.pid); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3293 | dumpStackTraces(tracesPath, firstPids, null, null, null); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | File lastTracesFile = null; |
| 3297 | File curTracesFile = null; |
| 3298 | for (int i=9; i>=0; i--) { |
| 3299 | String name = String.format("slow%02d.txt", i); |
| 3300 | curTracesFile = new File(tracesDir, name); |
| 3301 | if (curTracesFile.exists()) { |
| 3302 | if (lastTracesFile != null) { |
| 3303 | curTracesFile.renameTo(lastTracesFile); |
| 3304 | } else { |
| 3305 | curTracesFile.delete(); |
| 3306 | } |
| 3307 | } |
| 3308 | lastTracesFile = curTracesFile; |
| 3309 | } |
| 3310 | tracesFile.renameTo(curTracesFile); |
| 3311 | if (tracesTmp.exists()) { |
| 3312 | tracesTmp.renameTo(tracesFile); |
| 3313 | } |
| 3314 | } finally { |
| 3315 | StrictMode.setThreadPolicy(oldPolicy); |
| 3316 | } |
| 3317 | } |
| 3318 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3319 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 3320 | ActivityRecord parent, boolean aboveSystem, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3321 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3322 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3323 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3324 | if (mController != null) { |
| 3325 | try { |
| 3326 | // 0 == continue, -1 = kill process immediately |
| 3327 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3328 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3329 | } catch (RemoteException e) { |
| 3330 | mController = null; |
| 3331 | } |
| 3332 | } |
| 3333 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3334 | long anrTime = SystemClock.uptimeMillis(); |
| 3335 | if (MONITOR_CPU_USAGE) { |
| 3336 | updateCpuStatsNow(); |
| 3337 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3338 | |
| 3339 | synchronized (this) { |
| 3340 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3341 | if (mShuttingDown) { |
| 3342 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3343 | return; |
| 3344 | } else if (app.notResponding) { |
| 3345 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3346 | return; |
| 3347 | } else if (app.crashing) { |
| 3348 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3349 | return; |
| 3350 | } |
| 3351 | |
| 3352 | // In case we come through here for the same app before completing |
| 3353 | // this one, mark as anring now so we will bail out. |
| 3354 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3355 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3356 | // Log the ANR to the event log. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3357 | EventLog.writeEvent(EventLogTags.AM_ANR, app.userId, app.pid, |
| 3358 | app.processName, app.info.flags, annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3359 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3360 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3361 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3362 | |
| 3363 | int parentPid = app.pid; |
| 3364 | 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] | 3365 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3366 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3367 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3368 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3369 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3370 | ProcessRecord r = mLruProcesses.get(i); |
| 3371 | if (r != null && r.thread != null) { |
| 3372 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3373 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3374 | if (r.persistent) { |
| 3375 | firstPids.add(pid); |
| 3376 | } else { |
| 3377 | lastPids.put(pid, Boolean.TRUE); |
| 3378 | } |
| 3379 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3380 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3384 | // Log the ANR to the main log. |
Jeff Brown | deb6ed8 | 2012-04-10 14:26:26 -0700 | [diff] [blame] | 3385 | StringBuilder info = new StringBuilder(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3386 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3387 | info.append("ANR in ").append(app.processName); |
| 3388 | if (activity != null && activity.shortComponentName != null) { |
| 3389 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3390 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3391 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3392 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3393 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3394 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3395 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3396 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3398 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3399 | final ProcessStats processStats = new ProcessStats(true); |
| 3400 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3401 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3402 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3403 | String cpuInfo = null; |
| 3404 | if (MONITOR_CPU_USAGE) { |
| 3405 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3406 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3407 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3408 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3409 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3410 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3411 | } |
| 3412 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3413 | info.append(processStats.printCurrentState(anrTime)); |
| 3414 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3415 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3416 | if (tracesFile == null) { |
| 3417 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3418 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3419 | } |
| 3420 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3421 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3422 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3423 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3424 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3425 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3426 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3427 | 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] | 3428 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3429 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3430 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3431 | } |
| 3432 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3433 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3434 | } |
| 3435 | } |
| 3436 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3437 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3438 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3439 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3440 | |
| 3441 | synchronized (this) { |
| 3442 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3443 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3444 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3445 | app.processName, app.setAdj, "background ANR"); |
| 3446 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3447 | return; |
| 3448 | } |
| 3449 | |
| 3450 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3451 | makeAppNotRespondingLocked(app, |
| 3452 | activity != null ? activity.shortComponentName : null, |
| 3453 | annotation != null ? "ANR " + annotation : "ANR", |
| 3454 | info.toString()); |
| 3455 | |
| 3456 | // Bring up the infamous App Not Responding dialog |
| 3457 | Message msg = Message.obtain(); |
| 3458 | HashMap map = new HashMap(); |
| 3459 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3460 | msg.obj = map; |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 3461 | msg.arg1 = aboveSystem ? 1 : 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3462 | map.put("app", app); |
| 3463 | if (activity != null) { |
| 3464 | map.put("activity", activity); |
| 3465 | } |
| 3466 | |
| 3467 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3469 | } |
| 3470 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3471 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3472 | if (!mLaunchWarningShown) { |
| 3473 | mLaunchWarningShown = true; |
| 3474 | mHandler.post(new Runnable() { |
| 3475 | @Override |
| 3476 | public void run() { |
| 3477 | synchronized (ActivityManagerService.this) { |
| 3478 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3479 | d.show(); |
| 3480 | mHandler.postDelayed(new Runnable() { |
| 3481 | @Override |
| 3482 | public void run() { |
| 3483 | synchronized (ActivityManagerService.this) { |
| 3484 | d.dismiss(); |
| 3485 | mLaunchWarningShown = false; |
| 3486 | } |
| 3487 | } |
| 3488 | }, 4000); |
| 3489 | } |
| 3490 | } |
| 3491 | }); |
| 3492 | } |
| 3493 | } |
| 3494 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3495 | public boolean clearApplicationUserData(final String packageName, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3496 | final IPackageDataObserver observer, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3497 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3498 | int uid = Binder.getCallingUid(); |
| 3499 | int pid = Binder.getCallingPid(); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3500 | userId = handleIncomingUser(pid, uid, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3501 | userId, false, true, "clearApplicationUserData", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3502 | long callingId = Binder.clearCallingIdentity(); |
| 3503 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3504 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3505 | int pkgUid = -1; |
| 3506 | synchronized(this) { |
| 3507 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3508 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3509 | } catch (RemoteException e) { |
| 3510 | } |
| 3511 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3512 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3513 | return false; |
| 3514 | } |
| 3515 | if (uid == pkgUid || checkComponentPermission( |
| 3516 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3517 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3518 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3519 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3520 | } else { |
| 3521 | throw new SecurityException(pid+" does not have permission:"+ |
| 3522 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3523 | "for process:"+packageName); |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | try { |
| 3528 | //clear application user data |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3529 | pm.clearApplicationUserData(packageName, observer, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3530 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3531 | Uri.fromParts("package", packageName, null)); |
| 3532 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3533 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3534 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3535 | } catch (RemoteException e) { |
| 3536 | } |
| 3537 | } finally { |
| 3538 | Binder.restoreCallingIdentity(callingId); |
| 3539 | } |
| 3540 | return true; |
| 3541 | } |
| 3542 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3543 | public void killBackgroundProcesses(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3544 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3545 | != PackageManager.PERMISSION_GRANTED && |
| 3546 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3547 | != PackageManager.PERMISSION_GRANTED) { |
| 3548 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3549 | + Binder.getCallingPid() |
| 3550 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3551 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3552 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3553 | throw new SecurityException(msg); |
| 3554 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3555 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3556 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3557 | userId, true, true, "killBackgroundProcesses", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3558 | long callingId = Binder.clearCallingIdentity(); |
| 3559 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3560 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3561 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3562 | int appId = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3563 | try { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3564 | appId = UserHandle.getAppId(pm.getPackageUid(packageName, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3565 | } catch (RemoteException e) { |
| 3566 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3567 | if (appId == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3568 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3569 | return; |
| 3570 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3571 | killPackageProcessesLocked(packageName, appId, userId, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3572 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3573 | } |
| 3574 | } finally { |
| 3575 | Binder.restoreCallingIdentity(callingId); |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | public void killAllBackgroundProcesses() { |
| 3580 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3581 | != PackageManager.PERMISSION_GRANTED) { |
| 3582 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3583 | + Binder.getCallingPid() |
| 3584 | + ", uid=" + Binder.getCallingUid() |
| 3585 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3586 | Slog.w(TAG, msg); |
| 3587 | throw new SecurityException(msg); |
| 3588 | } |
| 3589 | |
| 3590 | long callingId = Binder.clearCallingIdentity(); |
| 3591 | try { |
| 3592 | synchronized(this) { |
| 3593 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3594 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3595 | final int NA = apps.size(); |
| 3596 | for (int ia=0; ia<NA; ia++) { |
| 3597 | ProcessRecord app = apps.valueAt(ia); |
| 3598 | if (app.persistent) { |
| 3599 | // we don't kill persistent processes |
| 3600 | continue; |
| 3601 | } |
| 3602 | if (app.removed) { |
| 3603 | procs.add(app); |
| 3604 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3605 | app.removed = true; |
| 3606 | procs.add(app); |
| 3607 | } |
| 3608 | } |
| 3609 | } |
| 3610 | |
| 3611 | int N = procs.size(); |
| 3612 | for (int i=0; i<N; i++) { |
| 3613 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3614 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3615 | } |
| 3616 | } finally { |
| 3617 | Binder.restoreCallingIdentity(callingId); |
| 3618 | } |
| 3619 | } |
| 3620 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3621 | public void forceStopPackage(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3622 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3623 | != PackageManager.PERMISSION_GRANTED) { |
| 3624 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3625 | + Binder.getCallingPid() |
| 3626 | + ", uid=" + Binder.getCallingUid() |
| 3627 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3628 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3629 | throw new SecurityException(msg); |
| 3630 | } |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3631 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3632 | userId, true, true, "forceStopPackage", null); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3633 | long callingId = Binder.clearCallingIdentity(); |
| 3634 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3635 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3636 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3637 | int[] users = userId == UserHandle.USER_ALL |
| 3638 | ? getUsersLocked() : new int[] { userId }; |
| 3639 | for (int user : users) { |
| 3640 | int pkgUid = -1; |
| 3641 | try { |
| 3642 | pkgUid = pm.getPackageUid(packageName, user); |
| 3643 | } catch (RemoteException e) { |
| 3644 | } |
| 3645 | if (pkgUid == -1) { |
| 3646 | Slog.w(TAG, "Invalid packageName: " + packageName); |
| 3647 | continue; |
| 3648 | } |
| 3649 | try { |
| 3650 | pm.setPackageStoppedState(packageName, true, user); |
| 3651 | } catch (RemoteException e) { |
| 3652 | } catch (IllegalArgumentException e) { |
| 3653 | Slog.w(TAG, "Failed trying to unstop package " |
| 3654 | + packageName + ": " + e); |
| 3655 | } |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 3656 | if (isUserRunningLocked(user, false)) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3657 | forceStopPackageLocked(packageName, pkgUid); |
| 3658 | } |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3660 | } |
| 3661 | } finally { |
| 3662 | Binder.restoreCallingIdentity(callingId); |
| 3663 | } |
| 3664 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3665 | |
| 3666 | /* |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3667 | * The pkg name and app id have to be specified. |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3668 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3669 | public void killApplicationWithAppId(String pkg, int appid) { |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3670 | if (pkg == null) { |
| 3671 | return; |
| 3672 | } |
| 3673 | // Make sure the uid is valid. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3674 | if (appid < 0) { |
| 3675 | Slog.w(TAG, "Invalid appid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3676 | return; |
| 3677 | } |
| 3678 | int callerUid = Binder.getCallingUid(); |
| 3679 | // Only the system server can kill an application |
| 3680 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3681 | // Post an aysnc message to kill the application |
| 3682 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3683 | msg.arg1 = appid; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3684 | msg.arg2 = 0; |
| 3685 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3686 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3687 | } else { |
| 3688 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3689 | pkg); |
| 3690 | } |
| 3691 | } |
| 3692 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3693 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3694 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3695 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3696 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3697 | final int uid = Binder.getCallingUid(); |
| 3698 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3699 | try { |
| 3700 | synchronized (this) { |
| 3701 | // Only allow this from foreground processes, so that background |
| 3702 | // applications can't abuse it to prevent system UI from being shown. |
| 3703 | if (uid >= Process.FIRST_APPLICATION_UID) { |
| 3704 | ProcessRecord proc; |
| 3705 | synchronized (mPidsSelfLocked) { |
| 3706 | proc = mPidsSelfLocked.get(pid); |
| 3707 | } |
| 3708 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 3709 | Slog.w(TAG, "Ignoring closeSystemDialogs " + reason |
| 3710 | + " from background process " + proc); |
| 3711 | return; |
| 3712 | } |
| 3713 | } |
| 3714 | closeSystemDialogsLocked(reason); |
| 3715 | } |
| 3716 | } finally { |
| 3717 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3718 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3719 | } |
| 3720 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3721 | void closeSystemDialogsLocked(String reason) { |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3722 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3723 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3724 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3725 | if (reason != null) { |
| 3726 | intent.putExtra("reason", reason); |
| 3727 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3728 | mWindowManager.closeSystemDialogs(reason); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3729 | |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3730 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3731 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 3732 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3733 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3734 | Activity.RESULT_CANCELED, null, "close-sys", true); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3735 | } |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3736 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3737 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3738 | broadcastIntentLocked(null, null, intent, null, |
| 3739 | null, 0, null, null, null, false, false, -1, |
| 3740 | Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3741 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3742 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3743 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3744 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3745 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3746 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3747 | for (int i=pids.length-1; i>=0; i--) { |
| 3748 | infos[i] = new Debug.MemoryInfo(); |
| 3749 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3750 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3751 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3752 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3753 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3754 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3755 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3756 | long[] pss = new long[pids.length]; |
| 3757 | for (int i=pids.length-1; i>=0; i--) { |
| 3758 | pss[i] = Debug.getPss(pids[i]); |
| 3759 | } |
| 3760 | return pss; |
| 3761 | } |
| 3762 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3763 | public void killApplicationProcess(String processName, int uid) { |
| 3764 | if (processName == null) { |
| 3765 | return; |
| 3766 | } |
| 3767 | |
| 3768 | int callerUid = Binder.getCallingUid(); |
| 3769 | // Only the system server can kill an application |
| 3770 | if (callerUid == Process.SYSTEM_UID) { |
| 3771 | synchronized (this) { |
| 3772 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3773 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3774 | try { |
| 3775 | app.thread.scheduleSuicide(); |
| 3776 | } catch (RemoteException e) { |
| 3777 | // If the other end already died, then our work here is done. |
| 3778 | } |
| 3779 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3780 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3781 | + processName + " / " + uid); |
| 3782 | } |
| 3783 | } |
| 3784 | } else { |
| 3785 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3786 | processName); |
| 3787 | } |
| 3788 | } |
| 3789 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3790 | private void forceStopPackageLocked(final String packageName, int uid) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3791 | forceStopPackageLocked(packageName, UserHandle.getAppId(uid), false, |
| 3792 | false, true, false, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3793 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3794 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3795 | if (!mProcessesReady) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3796 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3797 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3798 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3799 | intent.putExtra(Intent.EXTRA_UID, uid); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 3800 | intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3801 | broadcastIntentLocked(null, null, intent, |
| 3802 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3803 | false, false, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3804 | MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3805 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3806 | |
| 3807 | private void forceStopUserLocked(int userId) { |
| 3808 | forceStopPackageLocked(null, -1, false, false, true, false, userId); |
| 3809 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 3810 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3811 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3812 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 3813 | broadcastIntentLocked(null, null, intent, |
| 3814 | null, null, 0, null, null, null, |
| 3815 | false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 3816 | MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3817 | } |
| 3818 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3819 | private final boolean killPackageProcessesLocked(String packageName, int appId, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3820 | int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart, |
| 3821 | boolean doit, boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3822 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3823 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3824 | // Remove all processes this package may have touched: all with the |
| 3825 | // same UID (except for the system or root user), and all whose name |
| 3826 | // matches the package name. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3827 | final String procNamePrefix = packageName != null ? (packageName + ":") : null; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3828 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3829 | final int NA = apps.size(); |
| 3830 | for (int ia=0; ia<NA; ia++) { |
| 3831 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3832 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3833 | // we don't kill persistent processes |
| 3834 | continue; |
| 3835 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3836 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3837 | if (doit) { |
| 3838 | procs.add(app); |
| 3839 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3840 | continue; |
| 3841 | } |
| 3842 | |
| 3843 | // Skip process if it doesn't meet our oom adj requirement. |
| 3844 | if (app.setAdj < minOomAdj) { |
| 3845 | continue; |
| 3846 | } |
| 3847 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3848 | // If no package is specified, we call all processes under the |
| 3849 | // give user id. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3850 | if (packageName == null) { |
| 3851 | if (app.userId != userId) { |
| 3852 | continue; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3853 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3854 | // Package has been specified, we want to hit all processes |
| 3855 | // that match it. We need to qualify this by the processes |
| 3856 | // that are running under the specified app and user ID. |
| 3857 | } else { |
| 3858 | if (UserHandle.getAppId(app.uid) != appId) { |
| 3859 | continue; |
| 3860 | } |
| 3861 | if (userId != UserHandle.USER_ALL && app.userId != userId) { |
| 3862 | continue; |
| 3863 | } |
| 3864 | if (!app.pkgList.contains(packageName)) { |
| 3865 | continue; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3866 | } |
| 3867 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3868 | |
| 3869 | // Process has passed all conditions, kill it! |
| 3870 | if (!doit) { |
| 3871 | return true; |
| 3872 | } |
| 3873 | app.removed = true; |
| 3874 | procs.add(app); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | int N = procs.size(); |
| 3879 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3880 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3881 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3882 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3883 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3884 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3885 | private final boolean forceStopPackageLocked(String name, int appId, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3886 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3887 | boolean evenPersistent, int userId) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3888 | int i; |
| 3889 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3890 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3891 | if (userId == UserHandle.USER_ALL && name == null) { |
| 3892 | Slog.w(TAG, "Can't force stop all processes of all users, that is insane!"); |
| 3893 | } |
| 3894 | |
| 3895 | if (appId < 0 && name != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3896 | try { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3897 | appId = UserHandle.getAppId( |
| 3898 | AppGlobals.getPackageManager().getPackageUid(name, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3899 | } catch (RemoteException e) { |
| 3900 | } |
| 3901 | } |
| 3902 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3903 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3904 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3905 | Slog.i(TAG, "Force stopping package " + name + " appid=" + appId |
| 3906 | + " user=" + userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3907 | } else { |
| 3908 | Slog.i(TAG, "Force stopping user " + userId); |
| 3909 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3910 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3911 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3912 | while (badApps.hasNext()) { |
| 3913 | SparseArray<Long> ba = badApps.next(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3914 | for (i=ba.size()-1; i>=0; i--) { |
| 3915 | boolean remove = false; |
| 3916 | final int entUid = ba.keyAt(i); |
| 3917 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3918 | if (userId == UserHandle.USER_ALL) { |
| 3919 | if (UserHandle.getAppId(entUid) == appId) { |
| 3920 | remove = true; |
| 3921 | } |
| 3922 | } else { |
| 3923 | if (entUid == UserHandle.getUid(userId, appId)) { |
| 3924 | remove = true; |
| 3925 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3926 | } |
| 3927 | } else if (UserHandle.getUserId(entUid) == userId) { |
| 3928 | remove = true; |
| 3929 | } |
| 3930 | if (remove) { |
| 3931 | ba.removeAt(i); |
| 3932 | } |
| 3933 | } |
| 3934 | if (ba.size() == 0) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3935 | badApps.remove(); |
| 3936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3937 | } |
| 3938 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3939 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3940 | boolean didSomething = killPackageProcessesLocked(name, appId, userId, |
Dianne Hackborn | e62fa82 | 2013-01-09 18:31:37 -0800 | [diff] [blame] | 3941 | -100, callerWillRestart, true, doit, evenPersistent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3942 | name == null ? ("force stop user " + userId) : ("force stop " + name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3943 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3944 | TaskRecord lastTask = null; |
| 3945 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3946 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3947 | final boolean samePackage = r.packageName.equals(name) |
| 3948 | || (name == null && r.userId == userId); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3949 | if ((userId == UserHandle.USER_ALL || r.userId == userId) |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3950 | && (samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3951 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3952 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3953 | if (r.finishing) { |
| 3954 | // If this activity is just finishing, then it is not |
| 3955 | // interesting as far as something to stop. |
| 3956 | continue; |
| 3957 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3958 | return true; |
| 3959 | } |
| 3960 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3961 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3962 | if (samePackage) { |
| 3963 | if (r.app != null) { |
| 3964 | r.app.removed = true; |
| 3965 | } |
| 3966 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3967 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3968 | lastTask = r.task; |
| 3969 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3970 | null, "force-stop", true)) { |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3971 | i--; |
| 3972 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3976 | if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) { |
| 3977 | if (!doit) { |
| 3978 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3979 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3980 | didSomething = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3981 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3982 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3983 | if (name == null) { |
| 3984 | // Remove all sticky broadcasts from this user. |
| 3985 | mStickyBroadcasts.remove(userId); |
| 3986 | } |
| 3987 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3988 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3989 | if (mProviderMap.collectForceStopProviders(name, appId, doit, evenPersistent, |
| 3990 | userId, providers)) { |
| 3991 | if (!doit) { |
| 3992 | return true; |
| 3993 | } |
| 3994 | didSomething = true; |
| 3995 | } |
| 3996 | N = providers.size(); |
| 3997 | for (i=0; i<N; i++) { |
| 3998 | removeDyingProviderLocked(null, providers.get(i), true); |
| 3999 | } |
| 4000 | |
Dianne Hackborn | 861a3b2 | 2012-10-12 15:25:20 -0700 | [diff] [blame] | 4001 | if (name == null) { |
| 4002 | // Remove pending intents. For now we only do this when force |
| 4003 | // stopping users, because we have some problems when doing this |
| 4004 | // for packages -- app widgets are not currently cleaned up for |
| 4005 | // such packages, so they can be left with bad pending intents. |
| 4006 | if (mIntentSenderRecords.size() > 0) { |
| 4007 | Iterator<WeakReference<PendingIntentRecord>> it |
| 4008 | = mIntentSenderRecords.values().iterator(); |
| 4009 | while (it.hasNext()) { |
| 4010 | WeakReference<PendingIntentRecord> wpir = it.next(); |
| 4011 | if (wpir == null) { |
| 4012 | it.remove(); |
| 4013 | continue; |
| 4014 | } |
| 4015 | PendingIntentRecord pir = wpir.get(); |
| 4016 | if (pir == null) { |
| 4017 | it.remove(); |
| 4018 | continue; |
| 4019 | } |
| 4020 | if (name == null) { |
| 4021 | // Stopping user, remove all objects for the user. |
| 4022 | if (pir.key.userId != userId) { |
| 4023 | // Not the same user, skip it. |
| 4024 | continue; |
| 4025 | } |
| 4026 | } else { |
| 4027 | if (UserHandle.getAppId(pir.uid) != appId) { |
| 4028 | // Different app id, skip it. |
| 4029 | continue; |
| 4030 | } |
| 4031 | if (userId != UserHandle.USER_ALL && pir.key.userId != userId) { |
| 4032 | // Different user, skip it. |
| 4033 | continue; |
| 4034 | } |
| 4035 | if (!pir.key.packageName.equals(name)) { |
| 4036 | // Different package, skip it. |
| 4037 | continue; |
| 4038 | } |
| 4039 | } |
| 4040 | if (!doit) { |
| 4041 | return true; |
| 4042 | } |
| 4043 | didSomething = true; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4044 | it.remove(); |
Dianne Hackborn | 861a3b2 | 2012-10-12 15:25:20 -0700 | [diff] [blame] | 4045 | pir.canceled = true; |
| 4046 | if (pir.key.activity != null) { |
| 4047 | pir.key.activity.pendingResults.remove(pir.ref); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4048 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4049 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 4050 | } |
| 4051 | } |
| 4052 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4053 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4054 | if (purgeCache && name != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4055 | AttributeCache ac = AttributeCache.instance(); |
| 4056 | if (ac != null) { |
| 4057 | ac.removePackage(name); |
| 4058 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4059 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 4060 | if (mBooted) { |
| 4061 | mMainStack.resumeTopActivityLocked(null); |
| 4062 | mMainStack.scheduleIdleLocked(); |
| 4063 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4064 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4065 | |
| 4066 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4067 | } |
| 4068 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4069 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4070 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4071 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4072 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4073 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4074 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4075 | + "/" + uid + ")"); |
| 4076 | |
| 4077 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4078 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4079 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4080 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4081 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4082 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4083 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4084 | boolean needRestart = false; |
| 4085 | if (app.pid > 0 && app.pid != MY_PID) { |
| 4086 | int pid = app.pid; |
| 4087 | synchronized (mPidsSelfLocked) { |
| 4088 | mPidsSelfLocked.remove(pid); |
| 4089 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4090 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4091 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4092 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 4093 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4094 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4095 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4096 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4097 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4098 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4099 | } else { |
| 4100 | needRestart = true; |
| 4101 | } |
| 4102 | } |
| 4103 | } else { |
| 4104 | mRemovedProcesses.add(app); |
| 4105 | } |
| 4106 | |
| 4107 | return needRestart; |
| 4108 | } |
| 4109 | |
| 4110 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 4111 | final int pid = app.pid; |
| 4112 | boolean gone = false; |
| 4113 | synchronized (mPidsSelfLocked) { |
| 4114 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 4115 | if (knownApp != null && knownApp.thread == null) { |
| 4116 | mPidsSelfLocked.remove(pid); |
| 4117 | gone = true; |
| 4118 | } |
| 4119 | } |
| 4120 | |
| 4121 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4122 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4123 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, app.userId, |
| 4124 | pid, app.uid, app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4125 | mProcessNames.remove(app.processName, app.uid); |
| 4126 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4127 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4128 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4129 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4130 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4131 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4132 | // Take care of any launching providers waiting for this process. |
| 4133 | checkAppInLaunchingProvidersLocked(app, true); |
| 4134 | // Take care of any services that are waiting for the process. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4135 | mServices.processStartTimedOutLocked(app); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4136 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4137 | app.processName, app.setAdj, "start timeout"); |
| 4138 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4139 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4140 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4141 | try { |
| 4142 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 4143 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 4144 | bm.agentDisconnected(app.info.packageName); |
| 4145 | } catch (RemoteException e) { |
| 4146 | // Can't happen; the backup manager is local |
| 4147 | } |
| 4148 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4149 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4150 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4151 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4152 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4153 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4154 | 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] | 4155 | } |
| 4156 | } |
| 4157 | |
| 4158 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 4159 | int pid) { |
| 4160 | |
| 4161 | // Find the application record that is being attached... either via |
| 4162 | // the pid if we are running in multiple processes, or just pull the |
| 4163 | // next app record if we are emulating process with anonymous threads. |
| 4164 | ProcessRecord app; |
| 4165 | if (pid != MY_PID && pid >= 0) { |
| 4166 | synchronized (mPidsSelfLocked) { |
| 4167 | app = mPidsSelfLocked.get(pid); |
| 4168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4169 | } else { |
| 4170 | app = null; |
| 4171 | } |
| 4172 | |
| 4173 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4174 | 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] | 4175 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4176 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4177 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4178 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4179 | } else { |
| 4180 | try { |
| 4181 | thread.scheduleExit(); |
| 4182 | } catch (Exception e) { |
| 4183 | // Ignore exceptions. |
| 4184 | } |
| 4185 | } |
| 4186 | return false; |
| 4187 | } |
| 4188 | |
| 4189 | // If this application record is still attached to a previous |
| 4190 | // process, clean it up now. |
| 4191 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4192 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | // Tell the process all about itself. |
| 4196 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4197 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4198 | TAG, "Binding process pid " + pid + " to record " + app); |
| 4199 | |
| 4200 | String processName = app.processName; |
| 4201 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4202 | AppDeathRecipient adr = new AppDeathRecipient( |
| 4203 | app, pid, thread); |
| 4204 | thread.asBinder().linkToDeath(adr, 0); |
| 4205 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4206 | } catch (RemoteException e) { |
| 4207 | app.resetPackageList(); |
| 4208 | startProcessLocked(app, "link fail", processName); |
| 4209 | return false; |
| 4210 | } |
| 4211 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4212 | 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] | 4213 | |
| 4214 | app.thread = thread; |
| 4215 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 4216 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 4217 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4218 | app.forcingToForeground = null; |
| 4219 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 4220 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4221 | app.debugging = false; |
| 4222 | |
| 4223 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4224 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4225 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4226 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4227 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4228 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4229 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4230 | } |
| 4231 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4232 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4233 | TAG, "New app record " + app |
| 4234 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 4235 | try { |
| 4236 | int testMode = IApplicationThread.DEBUG_OFF; |
| 4237 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 4238 | testMode = mWaitForDebugger |
| 4239 | ? IApplicationThread.DEBUG_WAIT |
| 4240 | : IApplicationThread.DEBUG_ON; |
| 4241 | app.debugging = true; |
| 4242 | if (mDebugTransient) { |
| 4243 | mDebugApp = mOrigDebugApp; |
| 4244 | mWaitForDebugger = mOrigWaitForDebugger; |
| 4245 | } |
| 4246 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4247 | String profileFile = app.instrumentationProfileFile; |
| 4248 | ParcelFileDescriptor profileFd = null; |
| 4249 | boolean profileAutoStop = false; |
| 4250 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 4251 | mProfileProc = app; |
| 4252 | profileFile = mProfileFile; |
| 4253 | profileFd = mProfileFd; |
| 4254 | profileAutoStop = mAutoStopProfiler; |
| 4255 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4256 | boolean enableOpenGlTrace = false; |
| 4257 | if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) { |
| 4258 | enableOpenGlTrace = true; |
| 4259 | mOpenGlTraceApp = null; |
| 4260 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4261 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4262 | // If the app is being launched for restore or full backup, set it up specially |
| 4263 | boolean isRestrictedBackupMode = false; |
| 4264 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 4265 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4266 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4267 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 4268 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4269 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 4270 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 4271 | ? app.instrumentationInfo.packageName |
| 4272 | : app.info.packageName); |
| 4273 | if (app.instrumentationClass != null) { |
| 4274 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 4275 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4276 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 4277 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4278 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 4279 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 4280 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4281 | if (profileFd != null) { |
| 4282 | profileFd = profileFd.dup(); |
| 4283 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4284 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4285 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4286 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
| 4287 | enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 4288 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 4289 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4290 | updateLruProcessLocked(app, false); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 4291 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4292 | } catch (Exception e) { |
| 4293 | // todo: Yikes! What should we do? For now we will try to |
| 4294 | // start another process, but that could easily get us in |
| 4295 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4296 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4297 | |
| 4298 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4299 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4300 | startProcessLocked(app, "bind fail", processName); |
| 4301 | return false; |
| 4302 | } |
| 4303 | |
| 4304 | // Remove this record from the list of starting applications. |
| 4305 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4306 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 4307 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4308 | mProcessesOnHold.remove(app); |
| 4309 | |
| 4310 | boolean badApp = false; |
| 4311 | boolean didSomething = false; |
| 4312 | |
| 4313 | // 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] | 4314 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 4315 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4316 | 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] | 4317 | && processName.equals(hr.processName)) { |
| 4318 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 4319 | if (mHeadless) { |
| 4320 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 4321 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4322 | didSomething = true; |
| 4323 | } |
| 4324 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4325 | 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] | 4326 | + hr.intent.getComponent().flattenToShortString(), e); |
| 4327 | badApp = true; |
| 4328 | } |
| 4329 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4330 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | // Find any services that should be running in this process... |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4335 | if (!badApp) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4336 | try { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4337 | didSomething |= mServices.attachApplicationLocked(app, processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4338 | } catch (Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4339 | badApp = true; |
| 4340 | } |
| 4341 | } |
| 4342 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4343 | // Check if a next-broadcast receiver is in this process... |
| 4344 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4345 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4346 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4347 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4348 | // 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] | 4349 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4350 | } |
| 4351 | } |
| 4352 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4353 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4354 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4355 | 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] | 4356 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4357 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4358 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 4359 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 4360 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4361 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4362 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4363 | e.printStackTrace(); |
| 4364 | } |
| 4365 | } |
| 4366 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4367 | if (badApp) { |
| 4368 | // todo: Also need to kill application to deal with all |
| 4369 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4370 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4371 | return false; |
| 4372 | } |
| 4373 | |
| 4374 | if (!didSomething) { |
| 4375 | updateOomAdjLocked(); |
| 4376 | } |
| 4377 | |
| 4378 | return true; |
| 4379 | } |
| 4380 | |
| 4381 | public final void attachApplication(IApplicationThread thread) { |
| 4382 | synchronized (this) { |
| 4383 | int callingPid = Binder.getCallingPid(); |
| 4384 | final long origId = Binder.clearCallingIdentity(); |
| 4385 | attachApplicationLocked(thread, callingPid); |
| 4386 | Binder.restoreCallingIdentity(origId); |
| 4387 | } |
| 4388 | } |
| 4389 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4390 | 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] | 4391 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4392 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4393 | if (stopProfiling) { |
| 4394 | synchronized (this) { |
| 4395 | if (mProfileProc == r.app) { |
| 4396 | if (mProfileFd != null) { |
| 4397 | try { |
| 4398 | mProfileFd.close(); |
| 4399 | } catch (IOException e) { |
| 4400 | } |
| 4401 | clearProfilerLocked(); |
| 4402 | } |
| 4403 | } |
| 4404 | } |
| 4405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4406 | Binder.restoreCallingIdentity(origId); |
| 4407 | } |
| 4408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4409 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4410 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4411 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4412 | mWindowManager.enableScreenAfterBoot(); |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 4413 | |
| 4414 | synchronized (this) { |
| 4415 | updateEventDispatchingLocked(); |
| 4416 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4417 | } |
| 4418 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4419 | public void showBootMessage(final CharSequence msg, final boolean always) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4420 | enforceNotIsolatedCaller("showBootMessage"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4421 | mWindowManager.showBootMessage(msg, always); |
| 4422 | } |
| 4423 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4424 | public void dismissKeyguardOnNextActivity() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4425 | enforceNotIsolatedCaller("dismissKeyguardOnNextActivity"); |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4426 | final long token = Binder.clearCallingIdentity(); |
| 4427 | try { |
| 4428 | synchronized (this) { |
| 4429 | if (mLockScreenShown) { |
| 4430 | mLockScreenShown = false; |
| 4431 | comeOutOfSleepIfNeededLocked(); |
| 4432 | } |
| 4433 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
Dianne Hackborn | 1e88e98 | 2012-04-24 18:35:55 -0700 | [diff] [blame] | 4434 | } |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4435 | } finally { |
| 4436 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4437 | } |
| 4438 | } |
| 4439 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4440 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4441 | IntentFilter pkgFilter = new IntentFilter(); |
| 4442 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4443 | pkgFilter.addDataScheme("package"); |
| 4444 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4445 | @Override |
| 4446 | public void onReceive(Context context, Intent intent) { |
| 4447 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4448 | if (pkgs != null) { |
| 4449 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4450 | synchronized (ActivityManagerService.this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4451 | if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) { |
| 4452 | setResultCode(Activity.RESULT_OK); |
| 4453 | return; |
| 4454 | } |
| 4455 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4456 | } |
| 4457 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4458 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4459 | }, pkgFilter); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4460 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4461 | synchronized (this) { |
| 4462 | // Ensure that any processes we had put on hold are now started |
| 4463 | // up. |
| 4464 | final int NP = mProcessesOnHold.size(); |
| 4465 | if (NP > 0) { |
| 4466 | ArrayList<ProcessRecord> procs = |
| 4467 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4468 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4469 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4470 | + procs.get(ip)); |
| 4471 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4476 | // Start looking for apps that are abusing wake locks. |
| 4477 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4478 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4479 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4480 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4481 | SystemProperties.set("dev.bootcomplete", "1"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4482 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 4483 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 4484 | if (uss.mState == UserStartedState.STATE_BOOTING) { |
| 4485 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4486 | final int userId = mStartedUsers.keyAt(i); |
| 4487 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 4488 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 4489 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4490 | null, null, 0, null, null, |
| 4491 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4492 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4493 | } |
Amith Yamasani | 4860cfc | 2012-08-08 19:14:18 -0700 | [diff] [blame] | 4494 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4495 | } |
| 4496 | } |
| 4497 | } |
| 4498 | |
| 4499 | final void ensureBootCompleted() { |
| 4500 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4501 | boolean enableScreen; |
| 4502 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4503 | booting = mBooting; |
| 4504 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4505 | enableScreen = !mBooted; |
| 4506 | mBooted = true; |
| 4507 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4508 | |
| 4509 | if (booting) { |
| 4510 | finishBooting(); |
| 4511 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4512 | |
| 4513 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4514 | enableScreenAfterBoot(); |
| 4515 | } |
| 4516 | } |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 4517 | |
| 4518 | public final void activityResumed(IBinder token) { |
| 4519 | final long origId = Binder.clearCallingIdentity(); |
| 4520 | mMainStack.activityResumed(token); |
| 4521 | Binder.restoreCallingIdentity(origId); |
| 4522 | } |
| 4523 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4524 | public final void activityPaused(IBinder token) { |
| 4525 | final long origId = Binder.clearCallingIdentity(); |
| 4526 | mMainStack.activityPaused(token, false); |
| 4527 | Binder.restoreCallingIdentity(origId); |
| 4528 | } |
| 4529 | |
| 4530 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4531 | CharSequence description) { |
| 4532 | if (localLOGV) Slog.v( |
| 4533 | TAG, "Activity stopped: token=" + token); |
| 4534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4535 | // Refuse possible leaked file descriptors |
| 4536 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4537 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4538 | } |
| 4539 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4540 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4541 | |
| 4542 | final long origId = Binder.clearCallingIdentity(); |
| 4543 | |
| 4544 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4545 | r = mMainStack.isInStackLocked(token); |
| 4546 | if (r != null) { |
| 4547 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4548 | } |
| 4549 | } |
| 4550 | |
| 4551 | if (r != null) { |
| 4552 | sendPendingThumbnail(r, null, null, null, false); |
| 4553 | } |
| 4554 | |
| 4555 | trimApplications(); |
| 4556 | |
| 4557 | Binder.restoreCallingIdentity(origId); |
| 4558 | } |
| 4559 | |
| 4560 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4561 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4562 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | public String getCallingPackage(IBinder token) { |
| 4566 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4567 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4568 | 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] | 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | public ComponentName getCallingActivity(IBinder token) { |
| 4573 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4574 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4575 | return r != null ? r.intent.getComponent() : null; |
| 4576 | } |
| 4577 | } |
| 4578 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4579 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4580 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4581 | if (r == null) { |
| 4582 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4583 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4584 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4588 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4589 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4590 | if (r == null) { |
| 4591 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4592 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4593 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4594 | } |
| 4595 | } |
| 4596 | |
| 4597 | public String getPackageForToken(IBinder token) { |
| 4598 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4599 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4600 | if (r == null) { |
| 4601 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4602 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4603 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4604 | } |
| 4605 | } |
| 4606 | |
| 4607 | public IIntentSender getIntentSender(int type, |
| 4608 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4609 | int requestCode, Intent[] intents, String[] resolvedTypes, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4610 | int flags, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4611 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4612 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4613 | if (intents != null) { |
| 4614 | if (intents.length < 1) { |
| 4615 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4616 | } |
| 4617 | for (int i=0; i<intents.length; i++) { |
| 4618 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4619 | if (intent != null) { |
| 4620 | if (intent.hasFileDescriptors()) { |
| 4621 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4622 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4623 | if (type == ActivityManager.INTENT_SENDER_BROADCAST && |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4624 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4625 | throw new IllegalArgumentException( |
| 4626 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4627 | } |
| 4628 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4629 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4630 | } |
| 4631 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4632 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4633 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4634 | } |
| 4635 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4636 | if (options != null) { |
| 4637 | if (options.hasFileDescriptors()) { |
| 4638 | throw new IllegalArgumentException("File descriptors passed in options"); |
| 4639 | } |
| 4640 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4641 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4642 | synchronized(this) { |
| 4643 | int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4644 | int origUserId = userId; |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 4645 | userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId, |
Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 4646 | type == ActivityManager.INTENT_SENDER_BROADCAST, false, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4647 | "getIntentSender", null); |
| 4648 | if (origUserId == UserHandle.USER_CURRENT) { |
| 4649 | // We don't want to evaluate this until the pending intent is |
| 4650 | // actually executed. However, we do want to always do the |
| 4651 | // security checking for it above. |
| 4652 | userId = UserHandle.USER_CURRENT; |
| 4653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4654 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4655 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4656 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4657 | .getPackageUid(packageName, UserHandle.getUserId(callingUid)); |
| 4658 | if (!UserHandle.isSameApp(callingUid, uid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4659 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4660 | + Binder.getCallingPid() |
| 4661 | + ", uid=" + Binder.getCallingUid() |
| 4662 | + ", (need uid=" + uid + ")" |
| 4663 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4664 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4665 | throw new SecurityException(msg); |
| 4666 | } |
| 4667 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4668 | |
| 4669 | return getIntentSenderLocked(type, packageName, callingUid, userId, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4670 | token, resultWho, requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4672 | } catch (RemoteException e) { |
| 4673 | throw new SecurityException(e); |
| 4674 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4675 | } |
| 4676 | } |
| 4677 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4678 | IIntentSender getIntentSenderLocked(int type, String packageName, |
| 4679 | int callingUid, int userId, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4680 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags, |
| 4681 | Bundle options) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4682 | if (DEBUG_MU) |
| 4683 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4684 | ActivityRecord activity = null; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4685 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4686 | activity = mMainStack.isInStackLocked(token); |
| 4687 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4688 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4689 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4690 | if (activity.finishing) { |
| 4691 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4692 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4696 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4697 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4698 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4699 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4700 | |
| 4701 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4702 | type, packageName, activity, resultWho, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4703 | requestCode, intents, resolvedTypes, flags, options, userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4704 | WeakReference<PendingIntentRecord> ref; |
| 4705 | ref = mIntentSenderRecords.get(key); |
| 4706 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4707 | if (rec != null) { |
| 4708 | if (!cancelCurrent) { |
| 4709 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4710 | if (rec.key.requestIntent != null) { |
Adam Powell | 501d4a5 | 2012-04-30 15:03:57 -0700 | [diff] [blame] | 4711 | rec.key.requestIntent.replaceExtras(intents != null ? |
| 4712 | intents[intents.length - 1] : null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4713 | } |
| 4714 | if (intents != null) { |
| 4715 | intents[intents.length-1] = rec.key.requestIntent; |
| 4716 | rec.key.allIntents = intents; |
| 4717 | rec.key.allResolvedTypes = resolvedTypes; |
| 4718 | } else { |
| 4719 | rec.key.allIntents = null; |
| 4720 | rec.key.allResolvedTypes = null; |
| 4721 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4722 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4723 | return rec; |
| 4724 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4725 | rec.canceled = true; |
| 4726 | mIntentSenderRecords.remove(key); |
| 4727 | } |
| 4728 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4729 | return rec; |
| 4730 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4731 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4732 | mIntentSenderRecords.put(key, rec.ref); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4733 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4734 | if (activity.pendingResults == null) { |
| 4735 | activity.pendingResults |
| 4736 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4737 | } |
| 4738 | activity.pendingResults.add(rec.ref); |
| 4739 | } |
| 4740 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | public void cancelIntentSender(IIntentSender sender) { |
| 4744 | if (!(sender instanceof PendingIntentRecord)) { |
| 4745 | return; |
| 4746 | } |
| 4747 | synchronized(this) { |
| 4748 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4749 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4750 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4751 | .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId()); |
| 4752 | if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4753 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4754 | + Binder.getCallingPid() |
| 4755 | + ", uid=" + Binder.getCallingUid() |
| 4756 | + " is not allowed to cancel packges " |
| 4757 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4758 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4759 | throw new SecurityException(msg); |
| 4760 | } |
| 4761 | } catch (RemoteException e) { |
| 4762 | throw new SecurityException(e); |
| 4763 | } |
| 4764 | cancelIntentSenderLocked(rec, true); |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4769 | rec.canceled = true; |
| 4770 | mIntentSenderRecords.remove(rec.key); |
| 4771 | if (cleanActivity && rec.key.activity != null) { |
| 4772 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4777 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4778 | return null; |
| 4779 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4780 | try { |
| 4781 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4782 | return res.key.packageName; |
| 4783 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4784 | } |
| 4785 | return null; |
| 4786 | } |
| 4787 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4788 | public int getUidForIntentSender(IIntentSender sender) { |
| 4789 | if (sender instanceof PendingIntentRecord) { |
| 4790 | try { |
| 4791 | PendingIntentRecord res = (PendingIntentRecord)sender; |
| 4792 | return res.uid; |
| 4793 | } catch (ClassCastException e) { |
| 4794 | } |
| 4795 | } |
| 4796 | return -1; |
| 4797 | } |
| 4798 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4799 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4800 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4801 | return false; |
| 4802 | } |
| 4803 | try { |
| 4804 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4805 | if (res.key.allIntents == null) { |
| 4806 | return false; |
| 4807 | } |
| 4808 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4809 | Intent intent = res.key.allIntents[i]; |
| 4810 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4811 | return false; |
| 4812 | } |
| 4813 | } |
| 4814 | return true; |
| 4815 | } catch (ClassCastException e) { |
| 4816 | } |
| 4817 | return false; |
| 4818 | } |
| 4819 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 4820 | public boolean isIntentSenderAnActivity(IIntentSender pendingResult) { |
| 4821 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4822 | return false; |
| 4823 | } |
| 4824 | try { |
| 4825 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4826 | if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) { |
| 4827 | return true; |
| 4828 | } |
| 4829 | return false; |
| 4830 | } catch (ClassCastException e) { |
| 4831 | } |
| 4832 | return false; |
| 4833 | } |
| 4834 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 4835 | public Intent getIntentForIntentSender(IIntentSender pendingResult) { |
| 4836 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4837 | return null; |
| 4838 | } |
| 4839 | try { |
| 4840 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4841 | return res.key.requestIntent != null ? new Intent(res.key.requestIntent) : null; |
| 4842 | } catch (ClassCastException e) { |
| 4843 | } |
| 4844 | return null; |
| 4845 | } |
| 4846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4847 | public void setProcessLimit(int max) { |
| 4848 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4849 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4850 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4851 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4852 | mProcessLimitOverride = max; |
| 4853 | } |
| 4854 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4855 | } |
| 4856 | |
| 4857 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4858 | synchronized (this) { |
| 4859 | return mProcessLimitOverride; |
| 4860 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | void foregroundTokenDied(ForegroundToken token) { |
| 4864 | synchronized (ActivityManagerService.this) { |
| 4865 | synchronized (mPidsSelfLocked) { |
| 4866 | ForegroundToken cur |
| 4867 | = mForegroundProcesses.get(token.pid); |
| 4868 | if (cur != token) { |
| 4869 | return; |
| 4870 | } |
| 4871 | mForegroundProcesses.remove(token.pid); |
| 4872 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4873 | if (pr == null) { |
| 4874 | return; |
| 4875 | } |
| 4876 | pr.forcingToForeground = null; |
| 4877 | pr.foregroundServices = false; |
| 4878 | } |
| 4879 | updateOomAdjLocked(); |
| 4880 | } |
| 4881 | } |
| 4882 | |
| 4883 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4884 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4885 | "setProcessForeground()"); |
| 4886 | synchronized(this) { |
| 4887 | boolean changed = false; |
| 4888 | |
| 4889 | synchronized (mPidsSelfLocked) { |
| 4890 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4891 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4892 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4893 | return; |
| 4894 | } |
| 4895 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4896 | if (oldToken != null) { |
| 4897 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4898 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4899 | if (pr != null) { |
| 4900 | pr.forcingToForeground = null; |
| 4901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4902 | changed = true; |
| 4903 | } |
| 4904 | if (isForeground && token != null) { |
| 4905 | ForegroundToken newToken = new ForegroundToken() { |
| 4906 | public void binderDied() { |
| 4907 | foregroundTokenDied(this); |
| 4908 | } |
| 4909 | }; |
| 4910 | newToken.pid = pid; |
| 4911 | newToken.token = token; |
| 4912 | try { |
| 4913 | token.linkToDeath(newToken, 0); |
| 4914 | mForegroundProcesses.put(pid, newToken); |
| 4915 | pr.forcingToForeground = token; |
| 4916 | changed = true; |
| 4917 | } catch (RemoteException e) { |
| 4918 | // If the process died while doing this, we will later |
| 4919 | // do the cleanup with the process death link. |
| 4920 | } |
| 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | if (changed) { |
| 4925 | updateOomAdjLocked(); |
| 4926 | } |
| 4927 | } |
| 4928 | } |
| 4929 | |
| 4930 | // ========================================================= |
| 4931 | // PERMISSIONS |
| 4932 | // ========================================================= |
| 4933 | |
| 4934 | static class PermissionController extends IPermissionController.Stub { |
| 4935 | ActivityManagerService mActivityManagerService; |
| 4936 | PermissionController(ActivityManagerService activityManagerService) { |
| 4937 | mActivityManagerService = activityManagerService; |
| 4938 | } |
| 4939 | |
| 4940 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4941 | return mActivityManagerService.checkPermission(permission, pid, |
| 4942 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4943 | } |
| 4944 | } |
| 4945 | |
| 4946 | /** |
| 4947 | * This can be called with or without the global lock held. |
| 4948 | */ |
| 4949 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4950 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4951 | // We might be performing an operation on behalf of an indirect binder |
| 4952 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4953 | // client identity accordingly before proceeding. |
| 4954 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4955 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4956 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4957 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4958 | uid = tlsIdentity.uid; |
| 4959 | pid = tlsIdentity.pid; |
| 4960 | } |
| 4961 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4962 | if (pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4963 | return PackageManager.PERMISSION_GRANTED; |
| 4964 | } |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4965 | |
| 4966 | return ActivityManager.checkComponentPermission(permission, uid, |
| 4967 | owningUid, exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4968 | } |
| 4969 | |
| 4970 | /** |
| 4971 | * As the only public entry point for permissions checking, this method |
| 4972 | * can enforce the semantic that requesting a check on a null global |
| 4973 | * permission is automatically denied. (Internally a null permission |
| 4974 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4975 | * when only uid-based security is needed.) |
| 4976 | * |
| 4977 | * This can be called with or without the global lock held. |
| 4978 | */ |
| 4979 | public int checkPermission(String permission, int pid, int uid) { |
| 4980 | if (permission == null) { |
| 4981 | return PackageManager.PERMISSION_DENIED; |
| 4982 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4983 | return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | /** |
| 4987 | * Binder IPC calls go through the public entry point. |
| 4988 | * This can be called with or without the global lock held. |
| 4989 | */ |
| 4990 | int checkCallingPermission(String permission) { |
| 4991 | return checkPermission(permission, |
| 4992 | Binder.getCallingPid(), |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4993 | UserHandle.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4994 | } |
| 4995 | |
| 4996 | /** |
| 4997 | * This can be called with or without the global lock held. |
| 4998 | */ |
| 4999 | void enforceCallingPermission(String permission, String func) { |
| 5000 | if (checkCallingPermission(permission) |
| 5001 | == PackageManager.PERMISSION_GRANTED) { |
| 5002 | return; |
| 5003 | } |
| 5004 | |
| 5005 | String msg = "Permission Denial: " + func + " from pid=" |
| 5006 | + Binder.getCallingPid() |
| 5007 | + ", uid=" + Binder.getCallingUid() |
| 5008 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5009 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5010 | throw new SecurityException(msg); |
| 5011 | } |
| 5012 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5013 | /** |
| 5014 | * Determine if UID is holding permissions required to access {@link Uri} in |
| 5015 | * the given {@link ProviderInfo}. Final permission checking is always done |
| 5016 | * in {@link ContentProvider}. |
| 5017 | */ |
| 5018 | private final boolean checkHoldingPermissionsLocked( |
| 5019 | IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5020 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5021 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5022 | |
| 5023 | if (pi.applicationInfo.uid == uid) { |
| 5024 | return true; |
| 5025 | } else if (!pi.exported) { |
| 5026 | return false; |
| 5027 | } |
| 5028 | |
| 5029 | boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 5030 | 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] | 5031 | try { |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5032 | // check if target holds top-level <provider> permissions |
| 5033 | if (!readMet && pi.readPermission != null |
| 5034 | && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) { |
| 5035 | readMet = true; |
| 5036 | } |
| 5037 | if (!writeMet && pi.writePermission != null |
| 5038 | && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) { |
| 5039 | writeMet = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | // track if unprotected read/write is allowed; any denied |
| 5043 | // <path-permission> below removes this ability |
| 5044 | boolean allowDefaultRead = pi.readPermission == null; |
| 5045 | boolean allowDefaultWrite = pi.writePermission == null; |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5046 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5047 | // check if target holds any <path-permission> that match uri |
| 5048 | final PathPermission[] pps = pi.pathPermissions; |
| 5049 | if (pps != null) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5050 | final String path = uri.getPath(); |
| 5051 | int i = pps.length; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5052 | while (i > 0 && (!readMet || !writeMet)) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5053 | i--; |
| 5054 | PathPermission pp = pps[i]; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5055 | if (pp.match(path)) { |
| 5056 | if (!readMet) { |
| 5057 | final String pprperm = pp.getReadPermission(); |
| 5058 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 5059 | + pprperm + " for " + pp.getPath() |
| 5060 | + ": match=" + pp.match(path) |
| 5061 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 5062 | if (pprperm != null) { |
| 5063 | if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) { |
| 5064 | readMet = true; |
| 5065 | } else { |
| 5066 | allowDefaultRead = false; |
| 5067 | } |
| 5068 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5069 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5070 | if (!writeMet) { |
| 5071 | final String ppwperm = pp.getWritePermission(); |
| 5072 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 5073 | + ppwperm + " for " + pp.getPath() |
| 5074 | + ": match=" + pp.match(path) |
| 5075 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 5076 | if (ppwperm != null) { |
| 5077 | if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) { |
| 5078 | writeMet = true; |
| 5079 | } else { |
| 5080 | allowDefaultWrite = false; |
| 5081 | } |
| 5082 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5083 | } |
| 5084 | } |
| 5085 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5086 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5087 | |
| 5088 | // grant unprotected <provider> read/write, if not blocked by |
| 5089 | // <path-permission> above |
| 5090 | if (allowDefaultRead) readMet = true; |
| 5091 | if (allowDefaultWrite) writeMet = true; |
| 5092 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5093 | } catch (RemoteException e) { |
| 5094 | return false; |
| 5095 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5096 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5097 | return readMet && writeMet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5098 | } |
| 5099 | |
| 5100 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 5101 | int modeFlags) { |
| 5102 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5103 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5104 | return true; |
| 5105 | } |
| 5106 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 5107 | if (perms == null) return false; |
| 5108 | UriPermission perm = perms.get(uri); |
| 5109 | if (perm == null) return false; |
| 5110 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 5111 | } |
| 5112 | |
| 5113 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5114 | enforceNotIsolatedCaller("checkUriPermission"); |
| 5115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5116 | // Another redirected-binder-call permissions check as in |
| 5117 | // {@link checkComponentPermission}. |
| 5118 | Identity tlsIdentity = sCallerIdentity.get(); |
| 5119 | if (tlsIdentity != null) { |
| 5120 | uid = tlsIdentity.uid; |
| 5121 | pid = tlsIdentity.pid; |
| 5122 | } |
| 5123 | |
| 5124 | // Our own process gets to do everything. |
| 5125 | if (pid == MY_PID) { |
| 5126 | return PackageManager.PERMISSION_GRANTED; |
| 5127 | } |
| 5128 | synchronized(this) { |
| 5129 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 5130 | ? PackageManager.PERMISSION_GRANTED |
| 5131 | : PackageManager.PERMISSION_DENIED; |
| 5132 | } |
| 5133 | } |
| 5134 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5135 | /** |
| 5136 | * Check if the targetPkg can be granted permission to access uri by |
| 5137 | * the callingUid using the given modeFlags. Throws a security exception |
| 5138 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 5139 | * if the URI permission grant should be performed; returns -1 if it is not |
| 5140 | * needed (for example targetPkg already has permission to access the URI). |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5141 | * If you already know the uid of the target, you can supply it in |
| 5142 | * lastTargetUid else set that to -1. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5143 | */ |
| 5144 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5145 | Uri uri, int modeFlags, int lastTargetUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5146 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5147 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5148 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5149 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5150 | } |
| 5151 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5152 | if (targetPkg != null) { |
| 5153 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5154 | "Checking grant " + targetPkg + " permission to " + uri); |
| 5155 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5156 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5157 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5158 | |
| 5159 | // If this is not a content: uri, we can't do anything with it. |
| 5160 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5161 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5162 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5163 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | String name = uri.getAuthority(); |
| 5167 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5168 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5169 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5170 | if (cpr != null) { |
| 5171 | pi = cpr.info; |
| 5172 | } else { |
| 5173 | try { |
| 5174 | pi = pm.resolveContentProvider(name, |
Dianne Hackborn | 11f0cb7 | 2012-09-16 17:12:34 -0700 | [diff] [blame] | 5175 | PackageManager.GET_URI_PERMISSION_PATTERNS, |
| 5176 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5177 | } catch (RemoteException ex) { |
| 5178 | } |
| 5179 | } |
| 5180 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5181 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5182 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5183 | } |
| 5184 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5185 | int targetUid = lastTargetUid; |
| 5186 | if (targetUid < 0 && targetPkg != null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5187 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5188 | targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5189 | if (targetUid < 0) { |
| 5190 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5191 | "Can't grant URI permission no uid for: " + targetPkg); |
| 5192 | return -1; |
| 5193 | } |
| 5194 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5195 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5196 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5197 | } |
| 5198 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5199 | if (targetUid >= 0) { |
| 5200 | // First... does the target actually need this permission? |
| 5201 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 5202 | // No need to grant the target this permission. |
| 5203 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5204 | "Target " + targetPkg + " already has full permission to " + uri); |
| 5205 | return -1; |
| 5206 | } |
| 5207 | } else { |
| 5208 | // First... there is no target package, so can anyone access it? |
| 5209 | boolean allowed = pi.exported; |
| 5210 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5211 | if (pi.readPermission != null) { |
| 5212 | allowed = false; |
| 5213 | } |
| 5214 | } |
| 5215 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5216 | if (pi.writePermission != null) { |
| 5217 | allowed = false; |
| 5218 | } |
| 5219 | } |
| 5220 | if (allowed) { |
| 5221 | return -1; |
| 5222 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5223 | } |
| 5224 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5225 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5226 | if (!pi.grantUriPermissions) { |
| 5227 | throw new SecurityException("Provider " + pi.packageName |
| 5228 | + "/" + pi.name |
| 5229 | + " does not allow granting of Uri permissions (uri " |
| 5230 | + uri + ")"); |
| 5231 | } |
| 5232 | if (pi.uriPermissionPatterns != null) { |
| 5233 | final int N = pi.uriPermissionPatterns.length; |
| 5234 | boolean allowed = false; |
| 5235 | for (int i=0; i<N; i++) { |
| 5236 | if (pi.uriPermissionPatterns[i] != null |
| 5237 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 5238 | allowed = true; |
| 5239 | break; |
| 5240 | } |
| 5241 | } |
| 5242 | if (!allowed) { |
| 5243 | throw new SecurityException("Provider " + pi.packageName |
| 5244 | + "/" + pi.name |
| 5245 | + " does not allow granting of permission to path of Uri " |
| 5246 | + uri); |
| 5247 | } |
| 5248 | } |
| 5249 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5250 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5251 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5252 | if (callingUid != Process.myUid()) { |
| 5253 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 5254 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5255 | throw new SecurityException("Uid " + callingUid |
| 5256 | + " does not have permission to uri " + uri); |
| 5257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5258 | } |
| 5259 | } |
| 5260 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5261 | return targetUid; |
| 5262 | } |
| 5263 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5264 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 5265 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5266 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5267 | synchronized(this) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5268 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5269 | } |
| 5270 | } |
| 5271 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5272 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 5273 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 5274 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5275 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5276 | if (modeFlags == 0) { |
| 5277 | return; |
| 5278 | } |
| 5279 | |
| 5280 | // 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] | 5281 | // to the uri, and the target doesn't. Let's now give this to |
| 5282 | // the target. |
| 5283 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5284 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5285 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5287 | HashMap<Uri, UriPermission> targetUris |
| 5288 | = mGrantedUriPermissions.get(targetUid); |
| 5289 | if (targetUris == null) { |
| 5290 | targetUris = new HashMap<Uri, UriPermission>(); |
| 5291 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 5292 | } |
| 5293 | |
| 5294 | UriPermission perm = targetUris.get(uri); |
| 5295 | if (perm == null) { |
| 5296 | perm = new UriPermission(targetUid, uri); |
| 5297 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5298 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5300 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5301 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5302 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 5303 | } else { |
| 5304 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5305 | perm.readOwners.add(owner); |
| 5306 | owner.addReadPermission(perm); |
| 5307 | } |
| 5308 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5309 | perm.writeOwners.add(owner); |
| 5310 | owner.addWritePermission(perm); |
| 5311 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5312 | } |
| 5313 | } |
| 5314 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5315 | void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri, |
| 5316 | int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5317 | if (targetPkg == null) { |
| 5318 | throw new NullPointerException("targetPkg"); |
| 5319 | } |
| 5320 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5321 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5322 | if (targetUid < 0) { |
| 5323 | return; |
| 5324 | } |
| 5325 | |
| 5326 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 5327 | } |
| 5328 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5329 | static class NeededUriGrants extends ArrayList<Uri> { |
| 5330 | final String targetPkg; |
| 5331 | final int targetUid; |
| 5332 | final int flags; |
| 5333 | |
| 5334 | NeededUriGrants(String _targetPkg, int _targetUid, int _flags) { |
| 5335 | targetPkg = _targetPkg; |
| 5336 | targetUid = _targetUid; |
| 5337 | flags = _flags; |
| 5338 | } |
| 5339 | } |
| 5340 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5341 | /** |
| 5342 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 5343 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5344 | NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 5345 | String targetPkg, Intent intent, int mode, NeededUriGrants needed) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5346 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5347 | "Checking URI perm to data=" + (intent != null ? intent.getData() : null) |
| 5348 | + " clip=" + (intent != null ? intent.getClipData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5349 | + " from " + intent + "; flags=0x" |
| 5350 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 5351 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5352 | if (targetPkg == null) { |
| 5353 | throw new NullPointerException("targetPkg"); |
| 5354 | } |
| 5355 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5356 | if (intent == null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5357 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5358 | } |
| 5359 | Uri data = intent.getData(); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5360 | ClipData clip = intent.getClipData(); |
| 5361 | if (data == null && clip == null) { |
| 5362 | return null; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5363 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5364 | if (data != null) { |
| 5365 | int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 5366 | mode, needed != null ? needed.targetUid : -1); |
| 5367 | if (target > 0) { |
| 5368 | if (needed == null) { |
| 5369 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5370 | } |
| 5371 | needed.add(data); |
| 5372 | } |
| 5373 | } |
| 5374 | if (clip != null) { |
| 5375 | for (int i=0; i<clip.getItemCount(); i++) { |
| 5376 | Uri uri = clip.getItemAt(i).getUri(); |
| 5377 | if (uri != null) { |
| 5378 | int target = -1; |
| 5379 | target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, |
| 5380 | mode, needed != null ? needed.targetUid : -1); |
| 5381 | if (target > 0) { |
| 5382 | if (needed == null) { |
| 5383 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5384 | } |
| 5385 | needed.add(uri); |
| 5386 | } |
| 5387 | } else { |
| 5388 | Intent clipIntent = clip.getItemAt(i).getIntent(); |
| 5389 | if (clipIntent != null) { |
| 5390 | NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked( |
| 5391 | callingUid, targetPkg, clipIntent, mode, needed); |
| 5392 | if (newNeeded != null) { |
| 5393 | needed = newNeeded; |
| 5394 | } |
| 5395 | } |
| 5396 | } |
| 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | return needed; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | /** |
| 5404 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 5405 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5406 | void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed, |
| 5407 | UriPermissionOwner owner) { |
| 5408 | if (needed != null) { |
| 5409 | for (int i=0; i<needed.size(); i++) { |
| 5410 | grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg, |
| 5411 | needed.get(i), needed.flags, owner); |
| 5412 | } |
| 5413 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 5417 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5418 | NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, |
Dianne Hackborn | d9781fe | 2012-03-08 18:04:18 -0800 | [diff] [blame] | 5419 | intent, intent != null ? intent.getFlags() : 0, null); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5420 | if (needed == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5421 | return; |
| 5422 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5423 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5424 | grantUriPermissionUncheckedFromIntentLocked(needed, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 5428 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5429 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5430 | synchronized(this) { |
| 5431 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5432 | if (r == null) { |
| 5433 | throw new SecurityException("Unable to find app for caller " |
| 5434 | + caller |
| 5435 | + " when granting permission to uri " + uri); |
| 5436 | } |
| 5437 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5438 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5439 | } |
| 5440 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5441 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5442 | } |
| 5443 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5444 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5445 | null); |
| 5446 | } |
| 5447 | } |
| 5448 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5449 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5450 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5451 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 5452 | HashMap<Uri, UriPermission> perms |
| 5453 | = mGrantedUriPermissions.get(perm.uid); |
| 5454 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5455 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5456 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5457 | perms.remove(perm.uri); |
| 5458 | if (perms.size() == 0) { |
| 5459 | mGrantedUriPermissions.remove(perm.uid); |
| 5460 | } |
| 5461 | } |
| 5462 | } |
| 5463 | } |
| 5464 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5465 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 5466 | int modeFlags) { |
| 5467 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5468 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5469 | if (modeFlags == 0) { |
| 5470 | return; |
| 5471 | } |
| 5472 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5473 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5474 | "Revoking all granted permissions to " + uri); |
| 5475 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5476 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5477 | |
| 5478 | final String authority = uri.getAuthority(); |
| 5479 | ProviderInfo pi = null; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5480 | int userId = UserHandle.getUserId(callingUid); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5481 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5482 | if (cpr != null) { |
| 5483 | pi = cpr.info; |
| 5484 | } else { |
| 5485 | try { |
| 5486 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5487 | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5488 | } catch (RemoteException ex) { |
| 5489 | } |
| 5490 | } |
| 5491 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5492 | 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] | 5493 | return; |
| 5494 | } |
| 5495 | |
| 5496 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5497 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5498 | // Right now, if you are not the original owner of the permission, |
| 5499 | // you are not allowed to revoke it. |
| 5500 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5501 | throw new SecurityException("Uid " + callingUid |
| 5502 | + " does not have permission to uri " + uri); |
| 5503 | //} |
| 5504 | } |
| 5505 | |
| 5506 | // Go through all of the permissions and remove any that match. |
| 5507 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 5508 | if (SEGMENTS != null) { |
| 5509 | final int NS = SEGMENTS.size(); |
| 5510 | int N = mGrantedUriPermissions.size(); |
| 5511 | for (int i=0; i<N; i++) { |
| 5512 | HashMap<Uri, UriPermission> perms |
| 5513 | = mGrantedUriPermissions.valueAt(i); |
| 5514 | Iterator<UriPermission> it = perms.values().iterator(); |
| 5515 | toploop: |
| 5516 | while (it.hasNext()) { |
| 5517 | UriPermission perm = it.next(); |
| 5518 | Uri targetUri = perm.uri; |
| 5519 | if (!authority.equals(targetUri.getAuthority())) { |
| 5520 | continue; |
| 5521 | } |
| 5522 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5523 | if (targetSegments == null) { |
| 5524 | continue; |
| 5525 | } |
| 5526 | if (targetSegments.size() < NS) { |
| 5527 | continue; |
| 5528 | } |
| 5529 | for (int j=0; j<NS; j++) { |
| 5530 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5531 | continue toploop; |
| 5532 | } |
| 5533 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5534 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5535 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5536 | perm.clearModes(modeFlags); |
| 5537 | if (perm.modeFlags == 0) { |
| 5538 | it.remove(); |
| 5539 | } |
| 5540 | } |
| 5541 | if (perms.size() == 0) { |
| 5542 | mGrantedUriPermissions.remove( |
| 5543 | mGrantedUriPermissions.keyAt(i)); |
| 5544 | N--; |
| 5545 | i--; |
| 5546 | } |
| 5547 | } |
| 5548 | } |
| 5549 | } |
| 5550 | |
| 5551 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5552 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5553 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5554 | synchronized(this) { |
| 5555 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5556 | if (r == null) { |
| 5557 | throw new SecurityException("Unable to find app for caller " |
| 5558 | + caller |
| 5559 | + " when revoking permission to uri " + uri); |
| 5560 | } |
| 5561 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5562 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5563 | return; |
| 5564 | } |
| 5565 | |
| 5566 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5567 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5568 | if (modeFlags == 0) { |
| 5569 | return; |
| 5570 | } |
| 5571 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5572 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5573 | |
| 5574 | final String authority = uri.getAuthority(); |
| 5575 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5576 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5577 | if (cpr != null) { |
| 5578 | pi = cpr.info; |
| 5579 | } else { |
| 5580 | try { |
| 5581 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5582 | PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5583 | } catch (RemoteException ex) { |
| 5584 | } |
| 5585 | } |
| 5586 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5587 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5588 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5589 | return; |
| 5590 | } |
| 5591 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5592 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5593 | } |
| 5594 | } |
| 5595 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5596 | @Override |
| 5597 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5598 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5599 | synchronized(this) { |
| 5600 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5601 | return owner.getExternalTokenLocked(); |
| 5602 | } |
| 5603 | } |
| 5604 | |
| 5605 | @Override |
| 5606 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5607 | Uri uri, int modeFlags) { |
| 5608 | synchronized(this) { |
| 5609 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5610 | if (owner == null) { |
| 5611 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5612 | } |
| 5613 | if (fromUid != Binder.getCallingUid()) { |
| 5614 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5615 | // Only system code can grant URI permissions on behalf |
| 5616 | // of other users. |
| 5617 | throw new SecurityException("nice try"); |
| 5618 | } |
| 5619 | } |
| 5620 | if (targetPkg == null) { |
| 5621 | throw new IllegalArgumentException("null target"); |
| 5622 | } |
| 5623 | if (uri == null) { |
| 5624 | throw new IllegalArgumentException("null uri"); |
| 5625 | } |
| 5626 | |
| 5627 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5628 | } |
| 5629 | } |
| 5630 | |
| 5631 | @Override |
| 5632 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5633 | synchronized(this) { |
| 5634 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5635 | if (owner == null) { |
| 5636 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5637 | } |
| 5638 | |
| 5639 | if (uri == null) { |
| 5640 | owner.removeUriPermissionsLocked(mode); |
| 5641 | } else { |
| 5642 | owner.removeUriPermissionLocked(uri, mode); |
| 5643 | } |
| 5644 | } |
| 5645 | } |
| 5646 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5647 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5648 | synchronized (this) { |
| 5649 | ProcessRecord app = |
| 5650 | who != null ? getRecordForAppLocked(who) : null; |
| 5651 | if (app == null) return; |
| 5652 | |
| 5653 | Message msg = Message.obtain(); |
| 5654 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5655 | msg.obj = app; |
| 5656 | msg.arg1 = waiting ? 1 : 0; |
| 5657 | mHandler.sendMessage(msg); |
| 5658 | } |
| 5659 | } |
| 5660 | |
| 5661 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5662 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5663 | 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] | 5664 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 5665 | outInfo.totalMem = Process.getTotalMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5666 | outInfo.threshold = homeAppMem; |
| 5667 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5668 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5669 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5670 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5671 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5672 | ProcessList.VISIBLE_APP_ADJ); |
| 5673 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5674 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5675 | } |
| 5676 | |
| 5677 | // ========================================================= |
| 5678 | // TASK MANAGEMENT |
| 5679 | // ========================================================= |
| 5680 | |
| 5681 | public List getTasks(int maxNum, int flags, |
| 5682 | IThumbnailReceiver receiver) { |
| 5683 | ArrayList list = new ArrayList(); |
| 5684 | |
| 5685 | PendingThumbnailsRecord pending = null; |
| 5686 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5687 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5688 | |
| 5689 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5690 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5691 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5692 | + ", receiver=" + receiver); |
| 5693 | |
| 5694 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5695 | != PackageManager.PERMISSION_GRANTED) { |
| 5696 | if (receiver != null) { |
| 5697 | // If the caller wants to wait for pending thumbnails, |
| 5698 | // it ain't gonna get them. |
| 5699 | try { |
| 5700 | receiver.finished(); |
| 5701 | } catch (RemoteException ex) { |
| 5702 | } |
| 5703 | } |
| 5704 | String msg = "Permission Denial: getTasks() from pid=" |
| 5705 | + Binder.getCallingPid() |
| 5706 | + ", uid=" + Binder.getCallingUid() |
| 5707 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5708 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5709 | throw new SecurityException(msg); |
| 5710 | } |
| 5711 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5712 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5713 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5714 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5715 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5716 | TaskRecord curTask = null; |
| 5717 | int numActivities = 0; |
| 5718 | int numRunning = 0; |
| 5719 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5720 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5721 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5722 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5723 | |
| 5724 | // Initialize state for next task if needed. |
| 5725 | if (top == null || |
| 5726 | (top.state == ActivityState.INITIALIZING |
| 5727 | && top.task == r.task)) { |
| 5728 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5729 | curTask = r.task; |
| 5730 | numActivities = numRunning = 0; |
| 5731 | } |
| 5732 | |
| 5733 | // Add 'r' into the current task. |
| 5734 | numActivities++; |
| 5735 | if (r.app != null && r.app.thread != null) { |
| 5736 | numRunning++; |
| 5737 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5738 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5739 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5740 | TAG, r.intent.getComponent().flattenToShortString() |
| 5741 | + ": task=" + r.task); |
| 5742 | |
| 5743 | // If the next one is a different task, generate a new |
| 5744 | // TaskInfo entry for what we have. |
| 5745 | if (next == null || next.task != curTask) { |
| 5746 | ActivityManager.RunningTaskInfo ci |
| 5747 | = new ActivityManager.RunningTaskInfo(); |
| 5748 | ci.id = curTask.taskId; |
| 5749 | ci.baseActivity = r.intent.getComponent(); |
| 5750 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5751 | if (top.thumbHolder != null) { |
| 5752 | ci.description = top.thumbHolder.lastDescription; |
| 5753 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5754 | ci.numActivities = numActivities; |
| 5755 | ci.numRunning = numRunning; |
| 5756 | //System.out.println( |
| 5757 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5758 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5759 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5760 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5761 | + " app=" + top.app |
| 5762 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5763 | if (top.state == ActivityState.RESUMED |
| 5764 | || top.state == ActivityState.PAUSING) { |
| 5765 | if (top.idle && top.app != null |
| 5766 | && top.app.thread != null) { |
| 5767 | topRecord = top; |
| 5768 | topThumbnail = top.app.thread; |
| 5769 | } else { |
| 5770 | top.thumbnailNeeded = true; |
| 5771 | } |
| 5772 | } |
| 5773 | if (pending == null) { |
| 5774 | pending = new PendingThumbnailsRecord(receiver); |
| 5775 | } |
| 5776 | pending.pendingRecords.add(top); |
| 5777 | } |
| 5778 | list.add(ci); |
| 5779 | maxNum--; |
| 5780 | top = null; |
| 5781 | } |
| 5782 | } |
| 5783 | |
| 5784 | if (pending != null) { |
| 5785 | mPendingThumbnails.add(pending); |
| 5786 | } |
| 5787 | } |
| 5788 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5789 | 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] | 5790 | |
| 5791 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5792 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5793 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5794 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5795 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5796 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5797 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5798 | } |
| 5799 | } |
| 5800 | |
| 5801 | if (pending == null && receiver != null) { |
| 5802 | // In this case all thumbnails were available and the client |
| 5803 | // is being asked to be told when the remaining ones come in... |
| 5804 | // which is unusually, since the top-most currently running |
| 5805 | // activity should never have a canned thumbnail! Oh well. |
| 5806 | try { |
| 5807 | receiver.finished(); |
| 5808 | } catch (RemoteException ex) { |
| 5809 | } |
| 5810 | } |
| 5811 | |
| 5812 | return list; |
| 5813 | } |
| 5814 | |
| 5815 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5816 | int flags, int userId) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 5817 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 5818 | false, true, "getRecentTasks", null); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5819 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5820 | synchronized (this) { |
| 5821 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5822 | "getRecentTasks()"); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5823 | final boolean detailed = checkCallingPermission( |
| 5824 | android.Manifest.permission.GET_DETAILED_TASKS) |
| 5825 | == PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5826 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5827 | IPackageManager pm = AppGlobals.getPackageManager(); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5828 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5829 | final int N = mRecentTasks.size(); |
| 5830 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5831 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5832 | maxNum < N ? maxNum : N); |
| 5833 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5834 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5835 | // Only add calling user's recent tasks |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5836 | if (tr.userId != userId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5837 | // Return the entry if desired by the caller. We always return |
| 5838 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5839 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5840 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5841 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5842 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5843 | if (i == 0 |
| 5844 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5845 | || (tr.intent == null) |
| 5846 | || ((tr.intent.getFlags() |
| 5847 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5848 | ActivityManager.RecentTaskInfo rti |
| 5849 | = new ActivityManager.RecentTaskInfo(); |
| 5850 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5851 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5852 | rti.baseIntent = new Intent( |
| 5853 | tr.intent != null ? tr.intent : tr.affinityIntent); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5854 | if (!detailed) { |
| 5855 | rti.baseIntent.replaceExtras((Bundle)null); |
| 5856 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5857 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5858 | rti.description = tr.lastDescription; |
| 5859 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5860 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5861 | // Check whether this activity is currently available. |
| 5862 | try { |
| 5863 | if (rti.origActivity != null) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5864 | if (pm.getActivityInfo(rti.origActivity, 0, userId) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5865 | == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5866 | continue; |
| 5867 | } |
| 5868 | } else if (rti.baseIntent != null) { |
| 5869 | if (pm.queryIntentActivities(rti.baseIntent, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5870 | null, 0, userId) == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5871 | continue; |
| 5872 | } |
| 5873 | } |
| 5874 | } catch (RemoteException e) { |
| 5875 | // Will never happen. |
| 5876 | } |
| 5877 | } |
| 5878 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5879 | res.add(rti); |
| 5880 | maxNum--; |
| 5881 | } |
| 5882 | } |
| 5883 | return res; |
| 5884 | } |
| 5885 | } |
| 5886 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5887 | private TaskRecord taskForIdLocked(int id) { |
| 5888 | final int N = mRecentTasks.size(); |
| 5889 | for (int i=0; i<N; i++) { |
| 5890 | TaskRecord tr = mRecentTasks.get(i); |
| 5891 | if (tr.taskId == id) { |
| 5892 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5893 | } |
| 5894 | } |
| 5895 | return null; |
| 5896 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5897 | |
| 5898 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5899 | synchronized (this) { |
| 5900 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5901 | "getTaskThumbnails()"); |
| 5902 | TaskRecord tr = taskForIdLocked(id); |
| 5903 | if (tr != null) { |
| 5904 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5905 | } |
| 5906 | } |
| 5907 | return null; |
| 5908 | } |
| 5909 | |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 5910 | public Bitmap getTaskTopThumbnail(int id) { |
| 5911 | synchronized (this) { |
| 5912 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5913 | "getTaskTopThumbnail()"); |
| 5914 | TaskRecord tr = taskForIdLocked(id); |
| 5915 | if (tr != null) { |
| 5916 | return mMainStack.getTaskTopThumbnailLocked(tr); |
| 5917 | } |
| 5918 | } |
| 5919 | return null; |
| 5920 | } |
| 5921 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5922 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5923 | synchronized (this) { |
| 5924 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5925 | "removeSubTask()"); |
| 5926 | long ident = Binder.clearCallingIdentity(); |
| 5927 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5928 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex, |
| 5929 | true) != null; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5930 | } finally { |
| 5931 | Binder.restoreCallingIdentity(ident); |
| 5932 | } |
| 5933 | } |
| 5934 | } |
| 5935 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5936 | private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) { |
| 5937 | final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5938 | Intent baseIntent = new Intent( |
| 5939 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5940 | ComponentName component = baseIntent.getComponent(); |
| 5941 | if (component == null) { |
| 5942 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5943 | return; |
| 5944 | } |
| 5945 | |
| 5946 | // Find any running services associated with this app. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 5947 | mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5948 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5949 | if (killProcesses) { |
| 5950 | // Find any running processes associated with this app. |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5951 | final String pkg = component.getPackageName(); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5952 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5953 | HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap(); |
| 5954 | for (SparseArray<ProcessRecord> uids : pmap.values()) { |
| 5955 | for (int i=0; i<uids.size(); i++) { |
| 5956 | ProcessRecord proc = uids.valueAt(i); |
| 5957 | if (proc.userId != tr.userId) { |
| 5958 | continue; |
| 5959 | } |
| 5960 | if (!proc.pkgList.contains(pkg)) { |
| 5961 | continue; |
| 5962 | } |
| 5963 | procs.add(proc); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5964 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5965 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5966 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5967 | // Kill the running processes. |
| 5968 | for (int i=0; i<procs.size(); i++) { |
| 5969 | ProcessRecord pr = procs.get(i); |
| 5970 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5971 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 5972 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.userId, pr.pid, |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5973 | pr.processName, pr.setAdj, "remove task"); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5974 | pr.killedBackground = true; |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5975 | Process.killProcessQuiet(pr.pid); |
| 5976 | } else { |
| 5977 | pr.waitingToKill = "remove task"; |
| 5978 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5979 | } |
| 5980 | } |
| 5981 | } |
| 5982 | |
| 5983 | public boolean removeTask(int taskId, int flags) { |
| 5984 | synchronized (this) { |
| 5985 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5986 | "removeTask()"); |
| 5987 | long ident = Binder.clearCallingIdentity(); |
| 5988 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5989 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1, |
| 5990 | false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5991 | if (r != null) { |
| 5992 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5993 | cleanUpRemovedTaskLocked(r.task, flags); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5994 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5995 | } else { |
| 5996 | TaskRecord tr = null; |
| 5997 | int i=0; |
| 5998 | while (i < mRecentTasks.size()) { |
| 5999 | TaskRecord t = mRecentTasks.get(i); |
| 6000 | if (t.taskId == taskId) { |
| 6001 | tr = t; |
| 6002 | break; |
| 6003 | } |
| 6004 | i++; |
| 6005 | } |
| 6006 | if (tr != null) { |
| 6007 | if (tr.numActivities <= 0) { |
| 6008 | // Caller is just removing a recent task that is |
| 6009 | // not actively running. That is easy! |
| 6010 | mRecentTasks.remove(i); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 6011 | cleanUpRemovedTaskLocked(tr, flags); |
| 6012 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 6013 | } else { |
| 6014 | Slog.w(TAG, "removeTask: task " + taskId |
| 6015 | + " does not have activities to remove, " |
| 6016 | + " but numActivities=" + tr.numActivities |
| 6017 | + ": " + tr); |
| 6018 | } |
| 6019 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6020 | } |
| 6021 | } finally { |
| 6022 | Binder.restoreCallingIdentity(ident); |
| 6023 | } |
| 6024 | } |
| 6025 | return false; |
| 6026 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 6027 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6028 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 6029 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6030 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6031 | TaskRecord jt = startTask; |
| 6032 | |
| 6033 | // First look backwards |
| 6034 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6035 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6036 | if (r.task != jt) { |
| 6037 | jt = r.task; |
| 6038 | if (affinity.equals(jt.affinity)) { |
| 6039 | return j; |
| 6040 | } |
| 6041 | } |
| 6042 | } |
| 6043 | |
| 6044 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6045 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6046 | jt = startTask; |
| 6047 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6048 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6049 | if (r.task != jt) { |
| 6050 | if (affinity.equals(jt.affinity)) { |
| 6051 | return j; |
| 6052 | } |
| 6053 | jt = r.task; |
| 6054 | } |
| 6055 | } |
| 6056 | |
| 6057 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6058 | if (affinity.equals(((ActivityRecord)mMainStack.mHistory.get(N-1)).task.affinity)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6059 | return N-1; |
| 6060 | } |
| 6061 | |
| 6062 | return -1; |
| 6063 | } |
| 6064 | |
| 6065 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6066 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6067 | */ |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6068 | public void moveTaskToFront(int task, int flags, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6069 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6070 | "moveTaskToFront()"); |
| 6071 | |
| 6072 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6073 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6074 | Binder.getCallingUid(), "Task to front")) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6075 | ActivityOptions.abort(options); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6076 | return; |
| 6077 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6078 | final long origId = Binder.clearCallingIdentity(); |
| 6079 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6080 | TaskRecord tr = taskForIdLocked(task); |
| 6081 | if (tr != null) { |
| 6082 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 6083 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6084 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6085 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 6086 | // Caller wants the home activity moved with it. To accomplish this, |
| 6087 | // we'll just move the home task to the top first. |
| 6088 | mMainStack.moveHomeToFrontLocked(); |
| 6089 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6090 | mMainStack.moveTaskToFrontLocked(tr, null, options); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6091 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6092 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6093 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 6094 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6095 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 6096 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 6097 | mMainStack.mUserLeaving = true; |
| 6098 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 6099 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 6100 | // Caller wants the home activity moved with it. To accomplish this, |
| 6101 | // we'll just move the home task to the top first. |
| 6102 | mMainStack.moveHomeToFrontLocked(); |
| 6103 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6104 | mMainStack.moveTaskToFrontLocked(hr.task, null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6105 | return; |
| 6106 | } |
| 6107 | } |
| 6108 | } finally { |
| 6109 | Binder.restoreCallingIdentity(origId); |
| 6110 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6111 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6112 | } |
| 6113 | } |
| 6114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6115 | public void moveTaskToBack(int task) { |
| 6116 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6117 | "moveTaskToBack()"); |
| 6118 | |
| 6119 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6120 | if (mMainStack.mResumedActivity != null |
| 6121 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6122 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6123 | Binder.getCallingUid(), "Task to back")) { |
| 6124 | return; |
| 6125 | } |
| 6126 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6127 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6128 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6129 | Binder.restoreCallingIdentity(origId); |
| 6130 | } |
| 6131 | } |
| 6132 | |
| 6133 | /** |
| 6134 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 6135 | * of the history stack. The activity's order within the task is unchanged. |
| 6136 | * |
| 6137 | * @param token A reference to the activity we wish to move |
| 6138 | * @param nonRoot If false then this only works if the activity is the root |
| 6139 | * of a task; if true it will work for any activity in a task. |
| 6140 | * @return Returns true if the move completed, false if not. |
| 6141 | */ |
| 6142 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6143 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6144 | synchronized(this) { |
| 6145 | final long origId = Binder.clearCallingIdentity(); |
| 6146 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 6147 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6148 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6149 | } |
| 6150 | Binder.restoreCallingIdentity(origId); |
| 6151 | } |
| 6152 | return false; |
| 6153 | } |
| 6154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6155 | public void moveTaskBackwards(int task) { |
| 6156 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6157 | "moveTaskBackwards()"); |
| 6158 | |
| 6159 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6160 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6161 | Binder.getCallingUid(), "Task backwards")) { |
| 6162 | return; |
| 6163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6164 | final long origId = Binder.clearCallingIdentity(); |
| 6165 | moveTaskBackwardsLocked(task); |
| 6166 | Binder.restoreCallingIdentity(origId); |
| 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6171 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 6175 | synchronized(this) { |
| 6176 | return getTaskForActivityLocked(token, onlyRoot); |
| 6177 | } |
| 6178 | } |
| 6179 | |
| 6180 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6181 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6182 | TaskRecord lastTask = null; |
| 6183 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6184 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 6185 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6186 | if (!onlyRoot || lastTask != r.task) { |
| 6187 | return r.task.taskId; |
| 6188 | } |
| 6189 | return -1; |
| 6190 | } |
| 6191 | lastTask = r.task; |
| 6192 | } |
| 6193 | |
| 6194 | return -1; |
| 6195 | } |
| 6196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6197 | // ========================================================= |
| 6198 | // THUMBNAILS |
| 6199 | // ========================================================= |
| 6200 | |
| 6201 | public void reportThumbnail(IBinder token, |
| 6202 | Bitmap thumbnail, CharSequence description) { |
| 6203 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 6204 | final long origId = Binder.clearCallingIdentity(); |
| 6205 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 6206 | Binder.restoreCallingIdentity(origId); |
| 6207 | } |
| 6208 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6209 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6210 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 6211 | TaskRecord task = null; |
| 6212 | ArrayList receivers = null; |
| 6213 | |
| 6214 | //System.out.println("Send pending thumbnail: " + r); |
| 6215 | |
| 6216 | synchronized(this) { |
| 6217 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6218 | r = mMainStack.isInStackLocked(token); |
| 6219 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6220 | return; |
| 6221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6222 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 6223 | if (thumbnail == null && r.thumbHolder != null) { |
| 6224 | thumbnail = r.thumbHolder.lastThumbnail; |
| 6225 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6226 | } |
| 6227 | if (thumbnail == null && !always) { |
| 6228 | // If there is no thumbnail, and this entry is not actually |
| 6229 | // going away, then abort for now and pick up the next |
| 6230 | // thumbnail we get. |
| 6231 | return; |
| 6232 | } |
| 6233 | task = r.task; |
| 6234 | |
| 6235 | int N = mPendingThumbnails.size(); |
| 6236 | int i=0; |
| 6237 | while (i<N) { |
| 6238 | PendingThumbnailsRecord pr = |
| 6239 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 6240 | //System.out.println("Looking in " + pr.pendingRecords); |
| 6241 | if (pr.pendingRecords.remove(r)) { |
| 6242 | if (receivers == null) { |
| 6243 | receivers = new ArrayList(); |
| 6244 | } |
| 6245 | receivers.add(pr); |
| 6246 | if (pr.pendingRecords.size() == 0) { |
| 6247 | pr.finished = true; |
| 6248 | mPendingThumbnails.remove(i); |
| 6249 | N--; |
| 6250 | continue; |
| 6251 | } |
| 6252 | } |
| 6253 | i++; |
| 6254 | } |
| 6255 | } |
| 6256 | |
| 6257 | if (receivers != null) { |
| 6258 | final int N = receivers.size(); |
| 6259 | for (int i=0; i<N; i++) { |
| 6260 | try { |
| 6261 | PendingThumbnailsRecord pr = |
| 6262 | (PendingThumbnailsRecord)receivers.get(i); |
| 6263 | pr.receiver.newThumbnail( |
| 6264 | task != null ? task.taskId : -1, thumbnail, description); |
| 6265 | if (pr.finished) { |
| 6266 | pr.receiver.finished(); |
| 6267 | } |
| 6268 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6269 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6270 | } |
| 6271 | } |
| 6272 | } |
| 6273 | } |
| 6274 | |
| 6275 | // ========================================================= |
| 6276 | // CONTENT PROVIDERS |
| 6277 | // ========================================================= |
| 6278 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6279 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 6280 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6281 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6282 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6283 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6284 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6285 | } catch (RemoteException ex) { |
| 6286 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6287 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6288 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 6289 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6290 | if (providers != null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6291 | int N = providers.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6292 | for (int i=0; i<N; i++) { |
| 6293 | ProviderInfo cpi = |
| 6294 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6295 | boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6296 | cpi.name, cpi.flags); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6297 | if (singleton && UserHandle.getUserId(app.uid) != 0) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6298 | // This is a singleton provider, but a user besides the |
| 6299 | // default user is asking to initialize a process it runs |
| 6300 | // in... well, no, it doesn't actually run in this process, |
| 6301 | // it runs in the process of the default user. Get rid of it. |
| 6302 | providers.remove(i); |
| 6303 | N--; |
| 6304 | continue; |
| 6305 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6306 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6307 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6308 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6309 | if (cpr == null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6310 | cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6311 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6312 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6313 | if (DEBUG_MU) |
| 6314 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6315 | app.pubProviders.put(cpi.name, cpr); |
| 6316 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 6317 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6318 | } |
| 6319 | } |
| 6320 | return providers; |
| 6321 | } |
| 6322 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 6323 | /** |
| 6324 | * Check if {@link ProcessRecord} has a possible chance at accessing the |
| 6325 | * given {@link ProviderInfo}. Final permission checking is always done |
| 6326 | * in {@link ContentProvider}. |
| 6327 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6328 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6329 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6330 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6331 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6332 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6333 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6334 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6335 | return null; |
| 6336 | } |
| 6337 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6338 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6339 | == PackageManager.PERMISSION_GRANTED) { |
| 6340 | return null; |
| 6341 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6342 | |
| 6343 | PathPermission[] pps = cpi.pathPermissions; |
| 6344 | if (pps != null) { |
| 6345 | int i = pps.length; |
| 6346 | while (i > 0) { |
| 6347 | i--; |
| 6348 | PathPermission pp = pps[i]; |
| 6349 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6350 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6351 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6352 | return null; |
| 6353 | } |
| 6354 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6355 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6356 | == PackageManager.PERMISSION_GRANTED) { |
| 6357 | return null; |
| 6358 | } |
| 6359 | } |
| 6360 | } |
| 6361 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6362 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 6363 | if (perms != null) { |
| 6364 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 6365 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 6366 | return null; |
| 6367 | } |
| 6368 | } |
| 6369 | } |
| 6370 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6371 | String msg; |
| 6372 | if (!cpi.exported) { |
| 6373 | msg = "Permission Denial: opening provider " + cpi.name |
| 6374 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6375 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 6376 | + cpi.applicationInfo.uid; |
| 6377 | } else { |
| 6378 | msg = "Permission Denial: opening provider " + cpi.name |
| 6379 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6380 | + ", uid=" + callingUid + ") requires " |
| 6381 | + cpi.readPermission + " or " + cpi.writePermission; |
| 6382 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6383 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6384 | return msg; |
| 6385 | } |
| 6386 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6387 | ContentProviderConnection incProviderCountLocked(ProcessRecord r, |
| 6388 | final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6389 | if (r != null) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6390 | for (int i=0; i<r.conProviders.size(); i++) { |
| 6391 | ContentProviderConnection conn = r.conProviders.get(i); |
| 6392 | if (conn.provider == cpr) { |
| 6393 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6394 | "Adding provider requested by " |
| 6395 | + r.processName + " from process " |
| 6396 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6397 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6398 | if (stable) { |
| 6399 | conn.stableCount++; |
| 6400 | conn.numStableIncs++; |
| 6401 | } else { |
| 6402 | conn.unstableCount++; |
| 6403 | conn.numUnstableIncs++; |
| 6404 | } |
| 6405 | return conn; |
| 6406 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6407 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6408 | ContentProviderConnection conn = new ContentProviderConnection(cpr, r); |
| 6409 | if (stable) { |
| 6410 | conn.stableCount = 1; |
| 6411 | conn.numStableIncs = 1; |
| 6412 | } else { |
| 6413 | conn.unstableCount = 1; |
| 6414 | conn.numUnstableIncs = 1; |
| 6415 | } |
| 6416 | cpr.connections.add(conn); |
| 6417 | r.conProviders.add(conn); |
| 6418 | return conn; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6419 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6420 | cpr.addExternalProcessHandleLocked(externalProcessToken); |
| 6421 | return null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6422 | } |
| 6423 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6424 | boolean decProviderCountLocked(ContentProviderConnection conn, |
| 6425 | ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
| 6426 | if (conn != null) { |
| 6427 | cpr = conn.provider; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6428 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6429 | "Removing provider requested by " |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6430 | + conn.client.processName + " from process " |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6431 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6432 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6433 | if (stable) { |
| 6434 | conn.stableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6435 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6436 | conn.unstableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6437 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6438 | if (conn.stableCount == 0 && conn.unstableCount == 0) { |
| 6439 | cpr.connections.remove(conn); |
| 6440 | conn.client.conProviders.remove(conn); |
| 6441 | return true; |
| 6442 | } |
| 6443 | return false; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6444 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6445 | cpr.removeExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6446 | return false; |
| 6447 | } |
| 6448 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6449 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6450 | String name, IBinder token, boolean stable, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6451 | ContentProviderRecord cpr; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6452 | ContentProviderConnection conn = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6453 | ProviderInfo cpi = null; |
| 6454 | |
| 6455 | synchronized(this) { |
| 6456 | ProcessRecord r = null; |
| 6457 | if (caller != null) { |
| 6458 | r = getRecordForAppLocked(caller); |
| 6459 | if (r == null) { |
| 6460 | throw new SecurityException( |
| 6461 | "Unable to find app for caller " + caller |
| 6462 | + " (pid=" + Binder.getCallingPid() |
| 6463 | + ") when getting content provider " + name); |
| 6464 | } |
| 6465 | } |
| 6466 | |
| 6467 | // First check if this content provider has been published... |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6468 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6469 | boolean providerRunning = cpr != null; |
| 6470 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6471 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6472 | String msg; |
| 6473 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6474 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6475 | } |
| 6476 | |
| 6477 | if (r != null && cpr.canRunHere(r)) { |
| 6478 | // This provider has been published or is in the process |
| 6479 | // of being published... but it is also allowed to run |
| 6480 | // in the caller's process, so don't make a connection |
| 6481 | // and just let the caller instantiate its own instance. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6482 | ContentProviderHolder holder = cpr.newHolder(null); |
| 6483 | // don't give caller the provider object, it needs |
| 6484 | // to make its own. |
| 6485 | holder.provider = null; |
| 6486 | return holder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6487 | } |
| 6488 | |
| 6489 | final long origId = Binder.clearCallingIdentity(); |
| 6490 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6491 | // 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] | 6492 | // return it right away. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6493 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6494 | if (conn != null && (conn.stableCount+conn.unstableCount) == 1) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6495 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 6496 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6497 | // make sure to count it as being accessed and thus |
| 6498 | // back up on the LRU list. This is good because |
| 6499 | // content providers are often expensive to start. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6500 | updateLruProcessLocked(cpr.proc, false); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6501 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 6502 | } |
| 6503 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6504 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6505 | if (false) { |
| 6506 | if (cpr.name.flattenToShortString().equals( |
| 6507 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 6508 | Slog.v(TAG, "****************** KILLING " |
| 6509 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6510 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6511 | } |
| 6512 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6513 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6514 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 6515 | // NOTE: there is still a race here where a signal could be |
| 6516 | // pending on the process even though we managed to update its |
| 6517 | // adj level. Not sure what to do about this, but at least |
| 6518 | // the race is now smaller. |
| 6519 | if (!success) { |
| 6520 | // Uh oh... it looks like the provider's process |
| 6521 | // has been killed on us. We need to wait for a new |
| 6522 | // process to be started, and make sure its death |
| 6523 | // doesn't kill our process. |
| 6524 | Slog.i(TAG, |
| 6525 | "Existing provider " + cpr.name.flattenToShortString() |
| 6526 | + " is crashing; detaching " + r); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6527 | boolean lastRef = decProviderCountLocked(conn, cpr, token, stable); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6528 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6529 | if (!lastRef) { |
| 6530 | // This wasn't the last ref our process had on |
| 6531 | // the provider... we have now been killed, bail. |
| 6532 | return null; |
| 6533 | } |
| 6534 | providerRunning = false; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6535 | conn = null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6536 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6537 | } |
| 6538 | |
| 6539 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6540 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6541 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6542 | boolean singleton; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6543 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6544 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6545 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6546 | resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6547 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6548 | } catch (RemoteException ex) { |
| 6549 | } |
| 6550 | if (cpi == null) { |
| 6551 | return null; |
| 6552 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6553 | singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6554 | cpi.name, cpi.flags); |
| 6555 | if (singleton) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 6556 | userId = 0; |
| 6557 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6558 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6559 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6560 | String msg; |
| 6561 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6562 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6563 | } |
| 6564 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6565 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6566 | && !cpi.processName.equals("system")) { |
| 6567 | // If this content provider does not run in the system |
| 6568 | // process, and the system is not yet ready to run other |
| 6569 | // processes, then fail fast instead of hanging. |
| 6570 | throw new IllegalArgumentException( |
| 6571 | "Attempt to launch content provider before system ready"); |
| 6572 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6573 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 6574 | // Make sure that the user who owns this provider is started. If not, |
| 6575 | // we don't want to allow it to run. |
| 6576 | if (mStartedUsers.get(userId) == null) { |
| 6577 | Slog.w(TAG, "Unable to launch app " |
| 6578 | + cpi.applicationInfo.packageName + "/" |
| 6579 | + cpi.applicationInfo.uid + " for provider " |
| 6580 | + name + ": user " + userId + " is stopped"); |
| 6581 | return null; |
| 6582 | } |
| 6583 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6584 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6585 | cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6586 | final boolean firstClass = cpr == null; |
| 6587 | if (firstClass) { |
| 6588 | try { |
| 6589 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6590 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6591 | getApplicationInfo( |
| 6592 | cpi.applicationInfo.packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6593 | STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6594 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6595 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6596 | + cpi.name); |
| 6597 | return null; |
| 6598 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6599 | ai = getAppInfoForUser(ai, userId); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6600 | cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6601 | } catch (RemoteException ex) { |
| 6602 | // pm is in same process, this will never happen. |
| 6603 | } |
| 6604 | } |
| 6605 | |
| 6606 | if (r != null && cpr.canRunHere(r)) { |
| 6607 | // If this is a multiprocess provider, then just return its |
| 6608 | // info and allow the caller to instantiate it. Only do |
| 6609 | // this if the provider is the same user as the caller's |
| 6610 | // process, or can run as root (so can be in any process). |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6611 | return cpr.newHolder(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6612 | } |
| 6613 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6614 | if (DEBUG_PROVIDER) { |
| 6615 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6616 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6617 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6618 | } |
| 6619 | |
| 6620 | // This is single process, and our app is now connecting to it. |
| 6621 | // See if we are already in the process of launching this |
| 6622 | // provider. |
| 6623 | final int N = mLaunchingProviders.size(); |
| 6624 | int i; |
| 6625 | for (i=0; i<N; i++) { |
| 6626 | if (mLaunchingProviders.get(i) == cpr) { |
| 6627 | break; |
| 6628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6629 | } |
| 6630 | |
| 6631 | // If the provider is not already being launched, then get it |
| 6632 | // started. |
| 6633 | if (i >= N) { |
| 6634 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6635 | |
| 6636 | try { |
| 6637 | // Content provider is now in use, its package can't be stopped. |
| 6638 | try { |
| 6639 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6640 | cpr.appInfo.packageName, false, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6641 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6642 | } catch (IllegalArgumentException e) { |
| 6643 | Slog.w(TAG, "Failed trying to unstop package " |
| 6644 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6645 | } |
| 6646 | |
| 6647 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6648 | cpr.appInfo, false, 0, "content provider", |
| 6649 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6650 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6651 | if (proc == null) { |
| 6652 | Slog.w(TAG, "Unable to launch app " |
| 6653 | + cpi.applicationInfo.packageName + "/" |
| 6654 | + cpi.applicationInfo.uid + " for provider " |
| 6655 | + name + ": process is bad"); |
| 6656 | return null; |
| 6657 | } |
| 6658 | cpr.launchingApp = proc; |
| 6659 | mLaunchingProviders.add(cpr); |
| 6660 | } finally { |
| 6661 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6662 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6663 | } |
| 6664 | |
| 6665 | // Make sure the provider is published (the same provider class |
| 6666 | // may be published under multiple names). |
| 6667 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6668 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6669 | } |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6670 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6671 | mProviderMap.putProviderByName(name, cpr); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6672 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6673 | if (conn != null) { |
| 6674 | conn.waiting = true; |
| 6675 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6676 | } |
| 6677 | } |
| 6678 | |
| 6679 | // Wait for the provider to be published... |
| 6680 | synchronized (cpr) { |
| 6681 | while (cpr.provider == null) { |
| 6682 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6683 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6684 | + cpi.applicationInfo.packageName + "/" |
| 6685 | + cpi.applicationInfo.uid + " for provider " |
| 6686 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6687 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6688 | UserHandle.getUserId(cpi.applicationInfo.uid), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6689 | cpi.applicationInfo.packageName, |
| 6690 | cpi.applicationInfo.uid, name); |
| 6691 | return null; |
| 6692 | } |
| 6693 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6694 | if (DEBUG_MU) { |
| 6695 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6696 | + cpr.launchingApp); |
| 6697 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6698 | if (conn != null) { |
| 6699 | conn.waiting = true; |
| 6700 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6701 | cpr.wait(); |
| 6702 | } catch (InterruptedException ex) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6703 | } finally { |
| 6704 | if (conn != null) { |
| 6705 | conn.waiting = false; |
| 6706 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6707 | } |
| 6708 | } |
| 6709 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6710 | return cpr != null ? cpr.newHolder(conn) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | public final ContentProviderHolder getContentProvider( |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6714 | IApplicationThread caller, String name, int userId, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6715 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6716 | if (caller == null) { |
| 6717 | String msg = "null IApplicationThread when getting content provider " |
| 6718 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6719 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6720 | throw new SecurityException(msg); |
| 6721 | } |
| 6722 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6723 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6724 | false, true, "getContentProvider", null); |
| 6725 | return getContentProviderImpl(caller, name, null, stable, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6726 | } |
| 6727 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6728 | public ContentProviderHolder getContentProviderExternal( |
| 6729 | String name, int userId, IBinder token) { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6730 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6731 | "Do not have permission in call getContentProviderExternal()"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6732 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6733 | false, true, "getContentProvider", null); |
| 6734 | return getContentProviderExternalUnchecked(name, token, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6735 | } |
| 6736 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6737 | private ContentProviderHolder getContentProviderExternalUnchecked(String name, |
| 6738 | IBinder token, int userId) { |
| 6739 | return getContentProviderImpl(null, name, token, true, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6740 | } |
| 6741 | |
| 6742 | /** |
| 6743 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6744 | * @param cpr |
| 6745 | */ |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6746 | public void removeContentProvider(IBinder connection, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6747 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6748 | synchronized (this) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6749 | ContentProviderConnection conn; |
| 6750 | try { |
| 6751 | conn = (ContentProviderConnection)connection; |
| 6752 | } catch (ClassCastException e) { |
| 6753 | String msg ="removeContentProvider: " + connection |
| 6754 | + " not a ContentProviderConnection"; |
| 6755 | Slog.w(TAG, msg); |
| 6756 | throw new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6757 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6758 | if (conn == null) { |
| 6759 | throw new NullPointerException("connection is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6760 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6761 | if (decProviderCountLocked(conn, null, null, stable)) { |
| 6762 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6763 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6764 | } |
| 6765 | } |
| 6766 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6767 | public void removeContentProviderExternal(String name, IBinder token) { |
| 6768 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6769 | "Do not have permission in call removeContentProviderExternal()"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6770 | removeContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6771 | } |
| 6772 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6773 | private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6774 | synchronized (this) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6775 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6776 | if(cpr == null) { |
| 6777 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6778 | 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] | 6779 | return; |
| 6780 | } |
| 6781 | |
| 6782 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6783 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6784 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6785 | if (localCpr.hasExternalProcessHandles()) { |
| 6786 | if (localCpr.removeExternalProcessHandleLocked(token)) { |
| 6787 | updateOomAdjLocked(); |
| 6788 | } else { |
| 6789 | Slog.e(TAG, "Attmpt to remove content provider " + localCpr |
| 6790 | + " with no external reference for token: " |
| 6791 | + token + "."); |
| 6792 | } |
| 6793 | } else { |
| 6794 | Slog.e(TAG, "Attmpt to remove content provider: " + localCpr |
| 6795 | + " with no external references."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6796 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6797 | } |
| 6798 | } |
| 6799 | |
| 6800 | public final void publishContentProviders(IApplicationThread caller, |
| 6801 | List<ContentProviderHolder> providers) { |
| 6802 | if (providers == null) { |
| 6803 | return; |
| 6804 | } |
| 6805 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6806 | enforceNotIsolatedCaller("publishContentProviders"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6807 | synchronized (this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6808 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6809 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6810 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6811 | if (r == null) { |
| 6812 | throw new SecurityException( |
| 6813 | "Unable to find app for caller " + caller |
| 6814 | + " (pid=" + Binder.getCallingPid() |
| 6815 | + ") when publishing content providers"); |
| 6816 | } |
| 6817 | |
| 6818 | final long origId = Binder.clearCallingIdentity(); |
| 6819 | |
| 6820 | final int N = providers.size(); |
| 6821 | for (int i=0; i<N; i++) { |
| 6822 | ContentProviderHolder src = providers.get(i); |
| 6823 | if (src == null || src.info == null || src.provider == null) { |
| 6824 | continue; |
| 6825 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6826 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6827 | if (DEBUG_MU) |
| 6828 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6829 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6830 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6831 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6832 | String names[] = dst.info.authority.split(";"); |
| 6833 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6834 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | int NL = mLaunchingProviders.size(); |
| 6838 | int j; |
| 6839 | for (j=0; j<NL; j++) { |
| 6840 | if (mLaunchingProviders.get(j) == dst) { |
| 6841 | mLaunchingProviders.remove(j); |
| 6842 | j--; |
| 6843 | NL--; |
| 6844 | } |
| 6845 | } |
| 6846 | synchronized (dst) { |
| 6847 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6848 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6849 | dst.notifyAll(); |
| 6850 | } |
| 6851 | updateOomAdjLocked(r); |
| 6852 | } |
| 6853 | } |
| 6854 | |
| 6855 | Binder.restoreCallingIdentity(origId); |
| 6856 | } |
| 6857 | } |
| 6858 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6859 | public boolean refContentProvider(IBinder connection, int stable, int unstable) { |
| 6860 | ContentProviderConnection conn; |
| 6861 | try { |
| 6862 | conn = (ContentProviderConnection)connection; |
| 6863 | } catch (ClassCastException e) { |
| 6864 | String msg ="refContentProvider: " + connection |
| 6865 | + " not a ContentProviderConnection"; |
| 6866 | Slog.w(TAG, msg); |
| 6867 | throw new IllegalArgumentException(msg); |
| 6868 | } |
| 6869 | if (conn == null) { |
| 6870 | throw new NullPointerException("connection is null"); |
| 6871 | } |
| 6872 | |
| 6873 | synchronized (this) { |
| 6874 | if (stable > 0) { |
| 6875 | conn.numStableIncs += stable; |
| 6876 | } |
| 6877 | stable = conn.stableCount + stable; |
| 6878 | if (stable < 0) { |
| 6879 | throw new IllegalStateException("stableCount < 0: " + stable); |
| 6880 | } |
| 6881 | |
| 6882 | if (unstable > 0) { |
| 6883 | conn.numUnstableIncs += unstable; |
| 6884 | } |
| 6885 | unstable = conn.unstableCount + unstable; |
| 6886 | if (unstable < 0) { |
| 6887 | throw new IllegalStateException("unstableCount < 0: " + unstable); |
| 6888 | } |
| 6889 | |
| 6890 | if ((stable+unstable) <= 0) { |
| 6891 | throw new IllegalStateException("ref counts can't go to zero here: stable=" |
| 6892 | + stable + " unstable=" + unstable); |
| 6893 | } |
| 6894 | conn.stableCount = stable; |
| 6895 | conn.unstableCount = unstable; |
| 6896 | return !conn.dead; |
| 6897 | } |
| 6898 | } |
| 6899 | |
| 6900 | public void unstableProviderDied(IBinder connection) { |
| 6901 | ContentProviderConnection conn; |
| 6902 | try { |
| 6903 | conn = (ContentProviderConnection)connection; |
| 6904 | } catch (ClassCastException e) { |
| 6905 | String msg ="refContentProvider: " + connection |
| 6906 | + " not a ContentProviderConnection"; |
| 6907 | Slog.w(TAG, msg); |
| 6908 | throw new IllegalArgumentException(msg); |
| 6909 | } |
| 6910 | if (conn == null) { |
| 6911 | throw new NullPointerException("connection is null"); |
| 6912 | } |
| 6913 | |
| 6914 | // Safely retrieve the content provider associated with the connection. |
| 6915 | IContentProvider provider; |
| 6916 | synchronized (this) { |
| 6917 | provider = conn.provider.provider; |
| 6918 | } |
| 6919 | |
| 6920 | if (provider == null) { |
| 6921 | // Um, yeah, we're way ahead of you. |
| 6922 | return; |
| 6923 | } |
| 6924 | |
| 6925 | // Make sure the caller is being honest with us. |
| 6926 | if (provider.asBinder().pingBinder()) { |
| 6927 | // Er, no, still looks good to us. |
| 6928 | synchronized (this) { |
| 6929 | Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid() |
| 6930 | + " says " + conn + " died, but we don't agree"); |
| 6931 | return; |
| 6932 | } |
| 6933 | } |
| 6934 | |
| 6935 | // Well look at that! It's dead! |
| 6936 | synchronized (this) { |
| 6937 | if (conn.provider.provider != provider) { |
| 6938 | // But something changed... good enough. |
| 6939 | return; |
| 6940 | } |
| 6941 | |
| 6942 | ProcessRecord proc = conn.provider.proc; |
| 6943 | if (proc == null || proc.thread == null) { |
| 6944 | // Seems like the process is already cleaned up. |
| 6945 | return; |
| 6946 | } |
| 6947 | |
| 6948 | // As far as we're concerned, this is just like receiving a |
| 6949 | // death notification... just a bit prematurely. |
| 6950 | Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid |
| 6951 | + ") early provider death"); |
Dianne Hackborn | bd145db | 2012-06-05 16:20:46 -0700 | [diff] [blame] | 6952 | final long ident = Binder.clearCallingIdentity(); |
| 6953 | try { |
| 6954 | appDiedLocked(proc, proc.pid, proc.thread); |
| 6955 | } finally { |
| 6956 | Binder.restoreCallingIdentity(ident); |
| 6957 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6958 | } |
| 6959 | } |
| 6960 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6961 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6962 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6963 | synchronized (mSelf) { |
| 6964 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6965 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6966 | if (providers != null) { |
| 6967 | for (int i=providers.size()-1; i>=0; i--) { |
| 6968 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6969 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6970 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6971 | + ": not system .apk"); |
| 6972 | providers.remove(i); |
| 6973 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6974 | } |
| 6975 | } |
| 6976 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6977 | if (providers != null) { |
| 6978 | mSystemThread.installSystemProviders(providers); |
| 6979 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6980 | |
| 6981 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6982 | |
| 6983 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6984 | } |
| 6985 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6986 | /** |
| 6987 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6988 | * to access its content provider. |
| 6989 | * |
| 6990 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6991 | * |
| 6992 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6993 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6994 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6995 | public String getProviderMimeType(Uri uri, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6996 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6997 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6998 | userId, false, true, "getProviderMimeType", null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6999 | final String name = uri.getAuthority(); |
| 7000 | final long ident = Binder.clearCallingIdentity(); |
| 7001 | ContentProviderHolder holder = null; |
| 7002 | |
| 7003 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7004 | holder = getContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 7005 | if (holder != null) { |
| 7006 | return holder.provider.getType(uri); |
| 7007 | } |
| 7008 | } catch (RemoteException e) { |
| 7009 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 7010 | return null; |
| 7011 | } finally { |
| 7012 | if (holder != null) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7013 | removeContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 7014 | } |
| 7015 | Binder.restoreCallingIdentity(ident); |
| 7016 | } |
| 7017 | |
| 7018 | return null; |
| 7019 | } |
| 7020 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7021 | // ========================================================= |
| 7022 | // GLOBAL MANAGEMENT |
| 7023 | // ========================================================= |
| 7024 | |
| 7025 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7026 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7027 | String proc = customProcess != null ? customProcess : info.processName; |
| 7028 | BatteryStatsImpl.Uid.Proc ps = null; |
| 7029 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7030 | int uid = info.uid; |
| 7031 | if (isolated) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7032 | int userId = UserHandle.getUserId(uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7033 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 7034 | uid = 0; |
| 7035 | while (true) { |
| 7036 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 7037 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 7038 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 7039 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7040 | uid = UserHandle.getUid(userId, mNextIsolatedProcessUid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7041 | mNextIsolatedProcessUid++; |
| 7042 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 7043 | // No process for this uid, use it. |
| 7044 | break; |
| 7045 | } |
| 7046 | stepsLeft--; |
| 7047 | if (stepsLeft <= 0) { |
| 7048 | return null; |
| 7049 | } |
| 7050 | } |
| 7051 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7052 | synchronized (stats) { |
| 7053 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 7054 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7055 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7056 | } |
| 7057 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7058 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 7059 | ProcessRecord app; |
| 7060 | if (!isolated) { |
| 7061 | app = getProcessRecordLocked(info.processName, info.uid); |
| 7062 | } else { |
| 7063 | app = null; |
| 7064 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7065 | |
| 7066 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7067 | app = newProcessRecordLocked(null, info, null, isolated); |
| 7068 | mProcessNames.put(info.processName, app.uid, app); |
| 7069 | if (isolated) { |
| 7070 | mIsolatedProcesses.put(app.uid, app); |
| 7071 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7072 | updateLruProcessLocked(app, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7073 | } |
| 7074 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7075 | // This package really, really can not be stopped. |
| 7076 | try { |
| 7077 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7078 | info.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7079 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 7080 | } catch (IllegalArgumentException e) { |
| 7081 | Slog.w(TAG, "Failed trying to unstop package " |
| 7082 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7083 | } |
| 7084 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7085 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 7086 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 7087 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7088 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7089 | } |
| 7090 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 7091 | mPersistentStartingProcesses.add(app); |
| 7092 | startProcessLocked(app, "added application", app.processName); |
| 7093 | } |
| 7094 | |
| 7095 | return app; |
| 7096 | } |
| 7097 | |
| 7098 | public void unhandledBack() { |
| 7099 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 7100 | "unhandledBack()"); |
| 7101 | |
| 7102 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7103 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7104 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7105 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 7106 | if (count > 1) { |
| 7107 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7108 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 7109 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7110 | Binder.restoreCallingIdentity(origId); |
| 7111 | } |
| 7112 | } |
| 7113 | } |
| 7114 | |
| 7115 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7116 | enforceNotIsolatedCaller("openContentUri"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7117 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7118 | String name = uri.getAuthority(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7119 | ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7120 | ParcelFileDescriptor pfd = null; |
| 7121 | if (cph != null) { |
| 7122 | // We record the binder invoker's uid in thread-local storage before |
| 7123 | // going to the content provider to open the file. Later, in the code |
| 7124 | // that handles all permissions checks, we look for this uid and use |
| 7125 | // that rather than the Activity Manager's own uid. The effect is that |
| 7126 | // we do the check against the caller's permissions even though it looks |
| 7127 | // to the content provider like the Activity Manager itself is making |
| 7128 | // the request. |
| 7129 | sCallerIdentity.set(new Identity( |
| 7130 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 7131 | try { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 7132 | pfd = cph.provider.openFile(null, uri, "r"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7133 | } catch (FileNotFoundException e) { |
| 7134 | // do nothing; pfd will be returned null |
| 7135 | } finally { |
| 7136 | // Ensure that whatever happens, we clean up the identity state |
| 7137 | sCallerIdentity.remove(); |
| 7138 | } |
| 7139 | |
| 7140 | // 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] | 7141 | removeContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7142 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7143 | 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] | 7144 | } |
| 7145 | return pfd; |
| 7146 | } |
| 7147 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7148 | // Actually is sleeping or shutting down or whatever else in the future |
| 7149 | // is an inactive state. |
| 7150 | public boolean isSleeping() { |
| 7151 | return mSleeping || mShuttingDown; |
| 7152 | } |
| 7153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7154 | public void goingToSleep() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7155 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7156 | != PackageManager.PERMISSION_GRANTED) { |
| 7157 | throw new SecurityException("Requires permission " |
| 7158 | + android.Manifest.permission.DEVICE_POWER); |
| 7159 | } |
| 7160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7161 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7162 | mWentToSleep = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7163 | updateEventDispatchingLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7164 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7165 | if (!mSleeping) { |
| 7166 | mSleeping = true; |
| 7167 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 7168 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7169 | // Initialize the wake times of all processes. |
| 7170 | checkExcessivePowerUsageLocked(false); |
| 7171 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 7172 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 7173 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
| 7174 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7175 | } |
| 7176 | } |
| 7177 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7178 | public boolean shutdown(int timeout) { |
| 7179 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 7180 | != PackageManager.PERMISSION_GRANTED) { |
| 7181 | throw new SecurityException("Requires permission " |
| 7182 | + android.Manifest.permission.SHUTDOWN); |
| 7183 | } |
| 7184 | |
| 7185 | boolean timedout = false; |
| 7186 | |
| 7187 | synchronized(this) { |
| 7188 | mShuttingDown = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7189 | updateEventDispatchingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7190 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7191 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7192 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7193 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7194 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 7195 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7196 | long delay = endTime - System.currentTimeMillis(); |
| 7197 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7198 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7199 | timedout = true; |
| 7200 | break; |
| 7201 | } |
| 7202 | try { |
| 7203 | this.wait(); |
| 7204 | } catch (InterruptedException e) { |
| 7205 | } |
| 7206 | } |
| 7207 | } |
| 7208 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 7209 | |
| 7210 | mAppOpsService.shutdown(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7211 | mUsageStatsService.shutdown(); |
| 7212 | mBatteryStatsService.shutdown(); |
| 7213 | |
| 7214 | return timedout; |
| 7215 | } |
| 7216 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7217 | public final void activitySlept(IBinder token) { |
| 7218 | if (localLOGV) Slog.v( |
| 7219 | TAG, "Activity slept: token=" + token); |
| 7220 | |
| 7221 | ActivityRecord r = null; |
| 7222 | |
| 7223 | final long origId = Binder.clearCallingIdentity(); |
| 7224 | |
| 7225 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7226 | r = mMainStack.isInStackLocked(token); |
| 7227 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7228 | mMainStack.activitySleptLocked(r); |
| 7229 | } |
| 7230 | } |
| 7231 | |
| 7232 | Binder.restoreCallingIdentity(origId); |
| 7233 | } |
| 7234 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7235 | private void comeOutOfSleepIfNeededLocked() { |
| 7236 | if (!mWentToSleep && !mLockScreenShown) { |
| 7237 | if (mSleeping) { |
| 7238 | mSleeping = false; |
| 7239 | mMainStack.awakeFromSleepingLocked(); |
| 7240 | mMainStack.resumeTopActivityLocked(null); |
| 7241 | } |
| 7242 | } |
| 7243 | } |
| 7244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7245 | public void wakingUp() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7246 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7247 | != PackageManager.PERMISSION_GRANTED) { |
| 7248 | throw new SecurityException("Requires permission " |
| 7249 | + android.Manifest.permission.DEVICE_POWER); |
| 7250 | } |
| 7251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7252 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7253 | mWentToSleep = false; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7254 | updateEventDispatchingLocked(); |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7255 | comeOutOfSleepIfNeededLocked(); |
| 7256 | } |
| 7257 | } |
| 7258 | |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7259 | private void updateEventDispatchingLocked() { |
| 7260 | mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown); |
| 7261 | } |
| 7262 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7263 | public void setLockScreenShown(boolean shown) { |
| 7264 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7265 | != PackageManager.PERMISSION_GRANTED) { |
| 7266 | throw new SecurityException("Requires permission " |
| 7267 | + android.Manifest.permission.DEVICE_POWER); |
| 7268 | } |
| 7269 | |
| 7270 | synchronized(this) { |
| 7271 | mLockScreenShown = shown; |
| 7272 | comeOutOfSleepIfNeededLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7273 | } |
| 7274 | } |
| 7275 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7276 | public void stopAppSwitches() { |
| 7277 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7278 | != PackageManager.PERMISSION_GRANTED) { |
| 7279 | throw new SecurityException("Requires permission " |
| 7280 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7281 | } |
| 7282 | |
| 7283 | synchronized(this) { |
| 7284 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 7285 | + APP_SWITCH_DELAY_TIME; |
| 7286 | mDidAppSwitch = false; |
| 7287 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7288 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7289 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 7290 | } |
| 7291 | } |
| 7292 | |
| 7293 | public void resumeAppSwitches() { |
| 7294 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7295 | != PackageManager.PERMISSION_GRANTED) { |
| 7296 | throw new SecurityException("Requires permission " |
| 7297 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7298 | } |
| 7299 | |
| 7300 | synchronized(this) { |
| 7301 | // Note that we don't execute any pending app switches... we will |
| 7302 | // let those wait until either the timeout, or the next start |
| 7303 | // activity request. |
| 7304 | mAppSwitchesAllowedTime = 0; |
| 7305 | } |
| 7306 | } |
| 7307 | |
| 7308 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 7309 | String name) { |
| 7310 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 7311 | return true; |
| 7312 | } |
| 7313 | |
| 7314 | final int perm = checkComponentPermission( |
| 7315 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 7316 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7317 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 7318 | return true; |
| 7319 | } |
| 7320 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7321 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7322 | return false; |
| 7323 | } |
| 7324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7325 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 7326 | boolean persistent) { |
| 7327 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 7328 | "setDebugApp()"); |
| 7329 | |
| 7330 | // Note that this is not really thread safe if there are multiple |
| 7331 | // callers into it at the same time, but that's not a situation we |
| 7332 | // care about. |
| 7333 | if (persistent) { |
| 7334 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7335 | Settings.Global.putString( |
| 7336 | resolver, Settings.Global.DEBUG_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7337 | packageName); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7338 | Settings.Global.putInt( |
| 7339 | resolver, Settings.Global.WAIT_FOR_DEBUGGER, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7340 | waitForDebugger ? 1 : 0); |
| 7341 | } |
| 7342 | |
| 7343 | synchronized (this) { |
| 7344 | if (!persistent) { |
| 7345 | mOrigDebugApp = mDebugApp; |
| 7346 | mOrigWaitForDebugger = mWaitForDebugger; |
| 7347 | } |
| 7348 | mDebugApp = packageName; |
| 7349 | mWaitForDebugger = waitForDebugger; |
| 7350 | mDebugTransient = !persistent; |
| 7351 | if (packageName != null) { |
| 7352 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 7353 | forceStopPackageLocked(packageName, -1, false, false, true, true, |
| 7354 | UserHandle.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7355 | Binder.restoreCallingIdentity(origId); |
| 7356 | } |
| 7357 | } |
| 7358 | } |
| 7359 | |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 7360 | void setOpenGlTraceApp(ApplicationInfo app, String processName) { |
| 7361 | synchronized (this) { |
| 7362 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7363 | if (!isDebuggable) { |
| 7364 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7365 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7366 | } |
| 7367 | } |
| 7368 | |
| 7369 | mOpenGlTraceApp = processName; |
| 7370 | } |
| 7371 | } |
| 7372 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 7373 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 7374 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 7375 | synchronized (this) { |
| 7376 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7377 | if (!isDebuggable) { |
| 7378 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7379 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7380 | } |
| 7381 | } |
| 7382 | mProfileApp = processName; |
| 7383 | mProfileFile = profileFile; |
| 7384 | if (mProfileFd != null) { |
| 7385 | try { |
| 7386 | mProfileFd.close(); |
| 7387 | } catch (IOException e) { |
| 7388 | } |
| 7389 | mProfileFd = null; |
| 7390 | } |
| 7391 | mProfileFd = profileFd; |
| 7392 | mProfileType = 0; |
| 7393 | mAutoStopProfiler = autoStopProfiler; |
| 7394 | } |
| 7395 | } |
| 7396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7397 | public void setAlwaysFinish(boolean enabled) { |
| 7398 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 7399 | "setAlwaysFinish()"); |
| 7400 | |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7401 | Settings.Global.putInt( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7402 | mContext.getContentResolver(), |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7403 | Settings.Global.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7404 | |
| 7405 | synchronized (this) { |
| 7406 | mAlwaysFinishActivities = enabled; |
| 7407 | } |
| 7408 | } |
| 7409 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7410 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7411 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7412 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7413 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7414 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7415 | } |
| 7416 | } |
| 7417 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 7418 | public boolean isUserAMonkey() { |
| 7419 | // For now the fact that there is a controller implies |
| 7420 | // we have a monkey. |
| 7421 | synchronized (this) { |
| 7422 | return mController != null; |
| 7423 | } |
| 7424 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7425 | |
| 7426 | public void requestBugReport() { |
| 7427 | // No permission check because this can't do anything harmful -- |
| 7428 | // it will just eventually cause the user to be presented with |
| 7429 | // a UI to select where the bug report goes. |
| 7430 | SystemProperties.set("ctl.start", "bugreport"); |
| 7431 | } |
| 7432 | |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 7433 | public long inputDispatchingTimedOut(int pid, boolean aboveSystem) { |
| 7434 | if (checkCallingPermission(android.Manifest.permission.FILTER_EVENTS) |
| 7435 | != PackageManager.PERMISSION_GRANTED) { |
| 7436 | throw new SecurityException("Requires permission " |
| 7437 | + android.Manifest.permission.FILTER_EVENTS); |
| 7438 | } |
| 7439 | |
| 7440 | ProcessRecord proc; |
| 7441 | |
| 7442 | // TODO: Unify this code with ActivityRecord.keyDispatchingTimedOut(). |
| 7443 | synchronized (this) { |
| 7444 | synchronized (mPidsSelfLocked) { |
| 7445 | proc = mPidsSelfLocked.get(pid); |
| 7446 | } |
| 7447 | if (proc != null) { |
| 7448 | if (proc.debugging) { |
| 7449 | return -1; |
| 7450 | } |
| 7451 | |
| 7452 | if (mDidDexOpt) { |
| 7453 | // Give more time since we were dexopting. |
| 7454 | mDidDexOpt = false; |
| 7455 | return -1; |
| 7456 | } |
| 7457 | |
| 7458 | if (proc.instrumentationClass != null) { |
| 7459 | Bundle info = new Bundle(); |
| 7460 | info.putString("shortMsg", "keyDispatchingTimedOut"); |
| 7461 | info.putString("longMsg", "Timed out while dispatching key event"); |
| 7462 | finishInstrumentationLocked(proc, Activity.RESULT_CANCELED, info); |
| 7463 | proc = null; |
| 7464 | } |
| 7465 | } |
| 7466 | } |
| 7467 | |
| 7468 | if (proc != null) { |
| 7469 | appNotResponding(proc, null, null, aboveSystem, "keyDispatchingTimedOut"); |
| 7470 | if (proc.instrumentationClass != null || proc.usingWrapper) { |
| 7471 | return INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT; |
| 7472 | } |
| 7473 | } |
| 7474 | |
| 7475 | return KEY_DISPATCHING_TIMEOUT; |
| 7476 | } |
| 7477 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 7478 | public Bundle getTopActivityExtras(int requestType) { |
| 7479 | enforceCallingPermission(android.Manifest.permission.GET_TOP_ACTIVITY_INFO, |
| 7480 | "getTopActivityExtras()"); |
| 7481 | PendingActivityExtras pae; |
| 7482 | Bundle extras = new Bundle(); |
| 7483 | synchronized (this) { |
| 7484 | ActivityRecord activity = mMainStack.mResumedActivity; |
| 7485 | if (activity == null) { |
| 7486 | Slog.w(TAG, "getTopActivityExtras failed: no resumed activity"); |
| 7487 | return null; |
| 7488 | } |
| 7489 | extras.putString(Intent.EXTRA_ASSIST_PACKAGE, activity.packageName); |
| 7490 | if (activity.app == null || activity.app.thread == null) { |
| 7491 | Slog.w(TAG, "getTopActivityExtras failed: no process for " + activity); |
| 7492 | return extras; |
| 7493 | } |
| 7494 | if (activity.app.pid == Binder.getCallingPid()) { |
| 7495 | Slog.w(TAG, "getTopActivityExtras failed: request process same as " + activity); |
| 7496 | return extras; |
| 7497 | } |
| 7498 | pae = new PendingActivityExtras(activity); |
| 7499 | try { |
| 7500 | activity.app.thread.requestActivityExtras(activity.appToken, pae, requestType); |
| 7501 | mPendingActivityExtras.add(pae); |
| 7502 | mHandler.postDelayed(pae, PENDING_ACTIVITY_RESULT_TIMEOUT); |
| 7503 | } catch (RemoteException e) { |
| 7504 | Slog.w(TAG, "getTopActivityExtras failed: crash calling " + activity); |
| 7505 | return extras; |
| 7506 | } |
| 7507 | } |
| 7508 | synchronized (pae) { |
| 7509 | while (!pae.haveResult) { |
| 7510 | try { |
| 7511 | pae.wait(); |
| 7512 | } catch (InterruptedException e) { |
| 7513 | } |
| 7514 | } |
| 7515 | if (pae.result != null) { |
| 7516 | extras.putBundle(Intent.EXTRA_ASSIST_CONTEXT, pae.result); |
| 7517 | } |
| 7518 | } |
| 7519 | synchronized (this) { |
| 7520 | mPendingActivityExtras.remove(pae); |
| 7521 | mHandler.removeCallbacks(pae); |
| 7522 | } |
| 7523 | return extras; |
| 7524 | } |
| 7525 | |
| 7526 | public void reportTopActivityExtras(IBinder token, Bundle extras) { |
| 7527 | PendingActivityExtras pae = (PendingActivityExtras)token; |
| 7528 | synchronized (pae) { |
| 7529 | pae.result = extras; |
| 7530 | pae.haveResult = true; |
| 7531 | pae.notifyAll(); |
| 7532 | } |
| 7533 | } |
| 7534 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7535 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7536 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 7537 | "registerProcessObserver()"); |
| 7538 | synchronized (this) { |
| 7539 | mProcessObservers.register(observer); |
| 7540 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7541 | } |
| 7542 | |
| 7543 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7544 | synchronized (this) { |
| 7545 | mProcessObservers.unregister(observer); |
| 7546 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7547 | } |
| 7548 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7549 | public void setImmersive(IBinder token, boolean immersive) { |
| 7550 | synchronized(this) { |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 7551 | final ActivityRecord r = mMainStack.isInStackLocked(token); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7552 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7553 | throw new IllegalArgumentException(); |
| 7554 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7555 | r.immersive = immersive; |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 7556 | |
| 7557 | // update associated state if we're frontmost |
| 7558 | if (r == mFocusedActivity) { |
| 7559 | if (DEBUG_IMMERSIVE) { |
| 7560 | Slog.d(TAG, "Frontmost changed immersion: "+ r); |
| 7561 | } |
| 7562 | applyUpdateLockStateLocked(r); |
| 7563 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7564 | } |
| 7565 | } |
| 7566 | |
| 7567 | public boolean isImmersive(IBinder token) { |
| 7568 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7569 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7570 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7571 | throw new IllegalArgumentException(); |
| 7572 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7573 | return r.immersive; |
| 7574 | } |
| 7575 | } |
| 7576 | |
| 7577 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7578 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7579 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7580 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7581 | return (r != null) ? r.immersive : false; |
| 7582 | } |
| 7583 | } |
| 7584 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7585 | public final void enterSafeMode() { |
| 7586 | synchronized(this) { |
| 7587 | // It only makes sense to do this before the system is ready |
| 7588 | // and started launching other packages. |
| 7589 | if (!mSystemReady) { |
| 7590 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7591 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7592 | } catch (RemoteException e) { |
| 7593 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7594 | } |
| 7595 | } |
| 7596 | } |
| 7597 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7598 | public final void showSafeModeOverlay() { |
| 7599 | View v = LayoutInflater.from(mContext).inflate( |
| 7600 | com.android.internal.R.layout.safe_mode, null); |
| 7601 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 7602 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 7603 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 7604 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 7605 | lp.gravity = Gravity.BOTTOM | Gravity.START; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7606 | lp.format = v.getBackground().getOpacity(); |
| 7607 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 7608 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
Craig Mautner | 5962b12 | 2012-10-05 14:45:52 -0700 | [diff] [blame] | 7609 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7610 | ((WindowManager)mContext.getSystemService( |
| 7611 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 7612 | } |
| 7613 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7614 | public void noteWakeupAlarm(IIntentSender sender) { |
| 7615 | if (!(sender instanceof PendingIntentRecord)) { |
| 7616 | return; |
| 7617 | } |
| 7618 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 7619 | synchronized (stats) { |
| 7620 | if (mBatteryStatsService.isOnBattery()) { |
| 7621 | mBatteryStatsService.enforceCallingPermission(); |
| 7622 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 7623 | int MY_UID = Binder.getCallingUid(); |
| 7624 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 7625 | BatteryStatsImpl.Uid.Pkg pkg = |
| 7626 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 7627 | pkg.incWakeupsLocked(); |
| 7628 | } |
| 7629 | } |
| 7630 | } |
| 7631 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7632 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7633 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7634 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7635 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7636 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7637 | // XXX Note: don't acquire main activity lock here, because the window |
| 7638 | // manager calls in with its locks held. |
| 7639 | |
| 7640 | boolean killed = false; |
| 7641 | synchronized (mPidsSelfLocked) { |
| 7642 | int[] types = new int[pids.length]; |
| 7643 | int worstType = 0; |
| 7644 | for (int i=0; i<pids.length; i++) { |
| 7645 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7646 | if (proc != null) { |
| 7647 | int type = proc.setAdj; |
| 7648 | types[i] = type; |
| 7649 | if (type > worstType) { |
| 7650 | worstType = type; |
| 7651 | } |
| 7652 | } |
| 7653 | } |
| 7654 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7655 | // 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] | 7656 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7657 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 7658 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7659 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7660 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7661 | |
| 7662 | // If this is not a secure call, don't let it kill processes that |
| 7663 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7664 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 7665 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7666 | } |
| 7667 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7668 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7669 | for (int i=0; i<pids.length; i++) { |
| 7670 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7671 | if (proc == null) { |
| 7672 | continue; |
| 7673 | } |
| 7674 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7675 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7676 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7677 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.userId, proc.pid, |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7678 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7679 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7680 | proc.killedBackground = true; |
| 7681 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7682 | } |
| 7683 | } |
| 7684 | } |
| 7685 | return killed; |
| 7686 | } |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7687 | |
| 7688 | @Override |
| 7689 | public boolean killProcessesBelowForeground(String reason) { |
| 7690 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7691 | throw new SecurityException("killProcessesBelowForeground() only available to system"); |
| 7692 | } |
| 7693 | |
| 7694 | return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason); |
| 7695 | } |
| 7696 | |
| 7697 | private boolean killProcessesBelowAdj(int belowAdj, String reason) { |
| 7698 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7699 | throw new SecurityException("killProcessesBelowAdj() only available to system"); |
| 7700 | } |
| 7701 | |
| 7702 | boolean killed = false; |
| 7703 | synchronized (mPidsSelfLocked) { |
| 7704 | final int size = mPidsSelfLocked.size(); |
| 7705 | for (int i = 0; i < size; i++) { |
| 7706 | final int pid = mPidsSelfLocked.keyAt(i); |
| 7707 | final ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7708 | if (proc == null) continue; |
| 7709 | |
| 7710 | final int adj = proc.setAdj; |
| 7711 | if (adj > belowAdj && !proc.killedBackground) { |
| 7712 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7713 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.userId, |
| 7714 | proc.pid, proc.processName, adj, reason); |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7715 | killed = true; |
| 7716 | proc.killedBackground = true; |
| 7717 | Process.killProcessQuiet(pid); |
| 7718 | } |
| 7719 | } |
| 7720 | } |
| 7721 | return killed; |
| 7722 | } |
| 7723 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7724 | public final void startRunning(String pkg, String cls, String action, |
| 7725 | String data) { |
| 7726 | synchronized(this) { |
| 7727 | if (mStartRunning) { |
| 7728 | return; |
| 7729 | } |
| 7730 | mStartRunning = true; |
| 7731 | mTopComponent = pkg != null && cls != null |
| 7732 | ? new ComponentName(pkg, cls) : null; |
| 7733 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 7734 | mTopData = data; |
| 7735 | if (!mSystemReady) { |
| 7736 | return; |
| 7737 | } |
| 7738 | } |
| 7739 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7740 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7741 | } |
| 7742 | |
| 7743 | private void retrieveSettings() { |
| 7744 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 7745 | String debugApp = Settings.Global.getString( |
| 7746 | resolver, Settings.Global.DEBUG_APP); |
| 7747 | boolean waitForDebugger = Settings.Global.getInt( |
| 7748 | resolver, Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0; |
| 7749 | boolean alwaysFinishActivities = Settings.Global.getInt( |
| 7750 | resolver, Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7751 | |
| 7752 | Configuration configuration = new Configuration(); |
| 7753 | Settings.System.getConfiguration(resolver, configuration); |
| 7754 | |
| 7755 | synchronized (this) { |
| 7756 | mDebugApp = mOrigDebugApp = debugApp; |
| 7757 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 7758 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 7759 | // This happens before any activities are started, so we can |
| 7760 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 7761 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7762 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7763 | } |
| 7764 | } |
| 7765 | |
| 7766 | public boolean testIsSystemReady() { |
| 7767 | // no need to synchronize(this) just to read & return the value |
| 7768 | return mSystemReady; |
| 7769 | } |
| 7770 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7771 | private static File getCalledPreBootReceiversFile() { |
| 7772 | File dataDir = Environment.getDataDirectory(); |
| 7773 | File systemDir = new File(dataDir, "system"); |
| 7774 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 7775 | return fname; |
| 7776 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7777 | |
| 7778 | static final int LAST_DONE_VERSION = 10000; |
| 7779 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7780 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 7781 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 7782 | File file = getCalledPreBootReceiversFile(); |
| 7783 | FileInputStream fis = null; |
| 7784 | try { |
| 7785 | fis = new FileInputStream(file); |
| 7786 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7787 | int fvers = dis.readInt(); |
| 7788 | if (fvers == LAST_DONE_VERSION) { |
| 7789 | String vers = dis.readUTF(); |
| 7790 | String codename = dis.readUTF(); |
| 7791 | String build = dis.readUTF(); |
| 7792 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 7793 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 7794 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 7795 | int num = dis.readInt(); |
| 7796 | while (num > 0) { |
| 7797 | num--; |
| 7798 | String pkg = dis.readUTF(); |
| 7799 | String cls = dis.readUTF(); |
| 7800 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 7801 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7802 | } |
| 7803 | } |
| 7804 | } catch (FileNotFoundException e) { |
| 7805 | } catch (IOException e) { |
| 7806 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 7807 | } finally { |
| 7808 | if (fis != null) { |
| 7809 | try { |
| 7810 | fis.close(); |
| 7811 | } catch (IOException e) { |
| 7812 | } |
| 7813 | } |
| 7814 | } |
| 7815 | return lastDoneReceivers; |
| 7816 | } |
| 7817 | |
| 7818 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 7819 | File file = getCalledPreBootReceiversFile(); |
| 7820 | FileOutputStream fos = null; |
| 7821 | DataOutputStream dos = null; |
| 7822 | try { |
| 7823 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 7824 | fos = new FileOutputStream(file); |
| 7825 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7826 | dos.writeInt(LAST_DONE_VERSION); |
| 7827 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7828 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7829 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7830 | dos.writeInt(list.size()); |
| 7831 | for (int i=0; i<list.size(); i++) { |
| 7832 | dos.writeUTF(list.get(i).getPackageName()); |
| 7833 | dos.writeUTF(list.get(i).getClassName()); |
| 7834 | } |
| 7835 | } catch (IOException e) { |
| 7836 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 7837 | file.delete(); |
| 7838 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 7839 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7840 | if (dos != null) { |
| 7841 | try { |
| 7842 | dos.close(); |
| 7843 | } catch (IOException e) { |
| 7844 | // TODO Auto-generated catch block |
| 7845 | e.printStackTrace(); |
| 7846 | } |
| 7847 | } |
| 7848 | } |
| 7849 | } |
| 7850 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7851 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7852 | synchronized(this) { |
| 7853 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7854 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7855 | return; |
| 7856 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7857 | |
| 7858 | // Check to see if there are any update receivers to run. |
| 7859 | if (!mDidUpdate) { |
| 7860 | if (mWaitingUpdate) { |
| 7861 | return; |
| 7862 | } |
| 7863 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 7864 | List<ResolveInfo> ris = null; |
| 7865 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7866 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7867 | intent, null, 0, 0); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7868 | } catch (RemoteException e) { |
| 7869 | } |
| 7870 | if (ris != null) { |
| 7871 | for (int i=ris.size()-1; i>=0; i--) { |
| 7872 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 7873 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7874 | ris.remove(i); |
| 7875 | } |
| 7876 | } |
| 7877 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7878 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7879 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 7880 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7881 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7882 | for (int i=0; i<ris.size(); i++) { |
| 7883 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7884 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7885 | if (lastDoneReceivers.contains(comp)) { |
| 7886 | ris.remove(i); |
| 7887 | i--; |
| 7888 | } |
| 7889 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 7890 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7891 | final int[] users = getUsersLocked(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7892 | for (int i=0; i<ris.size(); i++) { |
| 7893 | ActivityInfo ai = ris.get(i).activityInfo; |
| 7894 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7895 | doneReceivers.add(comp); |
| 7896 | intent.setComponent(comp); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7897 | for (int j=0; j<users.length; j++) { |
| 7898 | IIntentReceiver finisher = null; |
| 7899 | if (i == ris.size()-1 && j == users.length-1) { |
| 7900 | finisher = new IIntentReceiver.Stub() { |
| 7901 | public void performReceive(Intent intent, int resultCode, |
| 7902 | String data, Bundle extras, boolean ordered, |
| 7903 | boolean sticky, int sendingUser) { |
| 7904 | // The raw IIntentReceiver interface is called |
| 7905 | // with the AM lock held, so redispatch to |
| 7906 | // execute our code without the lock. |
| 7907 | mHandler.post(new Runnable() { |
| 7908 | public void run() { |
| 7909 | synchronized (ActivityManagerService.this) { |
| 7910 | mDidUpdate = true; |
| 7911 | } |
| 7912 | writeLastDonePreBootReceivers(doneReceivers); |
| 7913 | showBootMessage(mContext.getText( |
| 7914 | R.string.android_upgrading_complete), |
| 7915 | false); |
| 7916 | systemReady(goingCallback); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7917 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7918 | }); |
| 7919 | } |
| 7920 | }; |
| 7921 | } |
| 7922 | Slog.i(TAG, "Sending system update to " + intent.getComponent() |
| 7923 | + " for user " + users[j]); |
| 7924 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 7925 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 7926 | users[j]); |
| 7927 | if (finisher != null) { |
| 7928 | mWaitingUpdate = true; |
| 7929 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7930 | } |
| 7931 | } |
| 7932 | } |
| 7933 | if (mWaitingUpdate) { |
| 7934 | return; |
| 7935 | } |
| 7936 | mDidUpdate = true; |
| 7937 | } |
| 7938 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7939 | mSystemReady = true; |
| 7940 | if (!mStartRunning) { |
| 7941 | return; |
| 7942 | } |
| 7943 | } |
| 7944 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7945 | ArrayList<ProcessRecord> procsToKill = null; |
| 7946 | synchronized(mPidsSelfLocked) { |
| 7947 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7948 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7949 | if (!isAllowedWhileBooting(proc.info)){ |
| 7950 | if (procsToKill == null) { |
| 7951 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7952 | } |
| 7953 | procsToKill.add(proc); |
| 7954 | } |
| 7955 | } |
| 7956 | } |
| 7957 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7958 | synchronized(this) { |
| 7959 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7960 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7961 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7962 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7963 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7964 | } |
| 7965 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7966 | |
| 7967 | // Now that we have cleaned up any update processes, we |
| 7968 | // are ready to start launching real processes and know that |
| 7969 | // we won't trample on them any more. |
| 7970 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7971 | } |
| 7972 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7973 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7974 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7975 | SystemClock.uptimeMillis()); |
| 7976 | |
| 7977 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7978 | // Make sure we have no pre-ready processes sitting around. |
| 7979 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7980 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7981 | ResolveInfo ri = mContext.getPackageManager() |
| 7982 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7983 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7984 | CharSequence errorMsg = null; |
| 7985 | if (ri != null) { |
| 7986 | ActivityInfo ai = ri.activityInfo; |
| 7987 | ApplicationInfo app = ai.applicationInfo; |
| 7988 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7989 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7990 | mTopData = null; |
| 7991 | mTopComponent = new ComponentName(app.packageName, |
| 7992 | ai.name); |
| 7993 | } else { |
| 7994 | errorMsg = mContext.getResources().getText( |
| 7995 | com.android.internal.R.string.factorytest_not_system); |
| 7996 | } |
| 7997 | } else { |
| 7998 | errorMsg = mContext.getResources().getText( |
| 7999 | com.android.internal.R.string.factorytest_no_action); |
| 8000 | } |
| 8001 | if (errorMsg != null) { |
| 8002 | mTopAction = null; |
| 8003 | mTopData = null; |
| 8004 | mTopComponent = null; |
| 8005 | Message msg = Message.obtain(); |
| 8006 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 8007 | msg.getData().putCharSequence("msg", errorMsg); |
| 8008 | mHandler.sendMessage(msg); |
| 8009 | } |
| 8010 | } |
| 8011 | } |
| 8012 | |
| 8013 | retrieveSettings(); |
| 8014 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 8015 | if (goingCallback != null) goingCallback.run(); |
| 8016 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8017 | synchronized (this) { |
| 8018 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 8019 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8020 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 8021 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8022 | if (apps != null) { |
| 8023 | int N = apps.size(); |
| 8024 | int i; |
| 8025 | for (i=0; i<N; i++) { |
| 8026 | ApplicationInfo info |
| 8027 | = (ApplicationInfo)apps.get(i); |
| 8028 | if (info != null && |
| 8029 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8030 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8031 | } |
| 8032 | } |
| 8033 | } |
| 8034 | } catch (RemoteException ex) { |
| 8035 | // pm is in same process, this will never happen. |
| 8036 | } |
| 8037 | } |
| 8038 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 8039 | // Start up initial activity. |
| 8040 | mBooting = true; |
| 8041 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8042 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8043 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8044 | Message msg = Message.obtain(); |
| 8045 | msg.what = SHOW_UID_ERROR_MSG; |
| 8046 | mHandler.sendMessage(msg); |
| 8047 | } |
| 8048 | } catch (RemoteException e) { |
| 8049 | } |
| 8050 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 8051 | long ident = Binder.clearCallingIdentity(); |
| 8052 | try { |
| 8053 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 8054 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 8055 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 8056 | intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId); |
| 8057 | broadcastIntentLocked(null, null, intent, |
| 8058 | null, null, 0, null, null, null, |
| 8059 | false, false, MY_PID, Process.SYSTEM_UID, mCurrentUserId); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 8060 | intent = new Intent(Intent.ACTION_USER_STARTING); |
| 8061 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 8062 | intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId); |
| 8063 | broadcastIntentLocked(null, null, intent, |
| 8064 | null, new IIntentReceiver.Stub() { |
| 8065 | @Override |
| 8066 | public void performReceive(Intent intent, int resultCode, String data, |
| 8067 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) |
| 8068 | throws RemoteException { |
| 8069 | } |
| 8070 | }, 0, null, null, |
| 8071 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 8072 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 8073 | } finally { |
| 8074 | Binder.restoreCallingIdentity(ident); |
| 8075 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8076 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 8077 | sendUserSwitchBroadcastsLocked(-1, mCurrentUserId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8078 | } |
| 8079 | } |
| 8080 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8081 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8082 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8083 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8084 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8085 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8086 | startAppProblemLocked(app); |
| 8087 | app.stopFreezingAllLocked(); |
| 8088 | return handleAppCrashLocked(app); |
| 8089 | } |
| 8090 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8091 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8092 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8093 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8094 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8095 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 8096 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8097 | startAppProblemLocked(app); |
| 8098 | app.stopFreezingAllLocked(); |
| 8099 | } |
| 8100 | |
| 8101 | /** |
| 8102 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 8103 | * |
| 8104 | * @param app The ProcessRecord in which the error occurred. |
| 8105 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 8106 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8107 | * @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] | 8108 | * @param shortMsg Short message describing the crash. |
| 8109 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8110 | * @param stackTrace Full crash stack trace, may be null. |
| 8111 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8112 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 8113 | */ |
| 8114 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8115 | 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] | 8116 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8118 | report.condition = condition; |
| 8119 | report.processName = app.processName; |
| 8120 | report.pid = app.pid; |
| 8121 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8122 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8123 | report.shortMsg = shortMsg; |
| 8124 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8125 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8126 | |
| 8127 | return report; |
| 8128 | } |
| 8129 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8130 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8131 | synchronized (this) { |
| 8132 | app.crashing = false; |
| 8133 | app.crashingReport = null; |
| 8134 | app.notResponding = false; |
| 8135 | app.notRespondingReport = null; |
| 8136 | if (app.anrDialog == fromDialog) { |
| 8137 | app.anrDialog = null; |
| 8138 | } |
| 8139 | if (app.waitDialog == fromDialog) { |
| 8140 | app.waitDialog = null; |
| 8141 | } |
| 8142 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8143 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 8144 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8145 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 8146 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 8147 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8148 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8149 | } |
| 8150 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8151 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8152 | private boolean handleAppCrashLocked(ProcessRecord app) { |
Mike Lockwood | 86548c4 | 2011-09-13 17:21:46 -0400 | [diff] [blame] | 8153 | if (mHeadless) { |
| 8154 | Log.e(TAG, "handleAppCrashLocked: " + app.processName); |
| 8155 | return false; |
| 8156 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8157 | long now = SystemClock.uptimeMillis(); |
| 8158 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8159 | Long crashTime; |
| 8160 | if (!app.isolated) { |
| 8161 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 8162 | } else { |
| 8163 | crashTime = null; |
| 8164 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8165 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8166 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8167 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8168 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 8169 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8170 | app.userId, app.info.processName, app.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8171 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 8172 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8173 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8174 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8175 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8176 | r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
| 8177 | null, "crashed", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8178 | } |
| 8179 | } |
| 8180 | if (!app.persistent) { |
| 8181 | // We don't want to start this process again until the user |
| 8182 | // explicitly does so... but for persistent process, we really |
| 8183 | // need to keep it running. If a persistent process is actually |
| 8184 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8185 | 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] | 8186 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8187 | if (!app.isolated) { |
| 8188 | // XXX We don't have a way to mark isolated processes |
| 8189 | // as bad, since they don't have a peristent identity. |
| 8190 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 8191 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 8192 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8193 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8194 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 8195 | // Don't let services in this process be restarted and potentially |
| 8196 | // annoy the user repeatedly. Unless it is persistent, since those |
| 8197 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 8198 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8199 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8200 | return false; |
| 8201 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8202 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8203 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8204 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Kevin Hester-Chow | d87a9be | 2012-03-05 08:01:00 -0800 | [diff] [blame] | 8205 | if (r != null && r.app == app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8206 | // If the top running activity is from this crashing |
| 8207 | // process, then terminate it to avoid getting in a loop. |
| 8208 | Slog.w(TAG, " Force finishing activity " |
| 8209 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 8210 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8211 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8212 | Activity.RESULT_CANCELED, null, "crashed", false); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 8213 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8214 | // stopped, to avoid a situation where one will get |
| 8215 | // re-start our crashing activity once it gets resumed again. |
| 8216 | index--; |
| 8217 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8218 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8219 | if (r.state == ActivityState.RESUMED |
| 8220 | || r.state == ActivityState.PAUSING |
| 8221 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 8222 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8223 | Slog.w(TAG, " Force finishing activity " |
| 8224 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8225 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8226 | Activity.RESULT_CANCELED, null, "crashed", false); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8227 | } |
| 8228 | } |
| 8229 | } |
| 8230 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8231 | } |
| 8232 | |
| 8233 | // Bump up the crash count of any services currently running in the proc. |
| 8234 | if (app.services.size() != 0) { |
| 8235 | // Any services running in the application need to be placed |
| 8236 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8237 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8238 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8239 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8240 | sr.crashCount++; |
| 8241 | } |
| 8242 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8243 | |
| 8244 | // If the crashing process is what we consider to be the "home process" and it has been |
| 8245 | // replaced by a third-party app, clear the package preferred activities from packages |
| 8246 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 8247 | // from blocking the user to manually clear the list. |
| 8248 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 8249 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 8250 | Iterator it = mHomeProcess.activities.iterator(); |
| 8251 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 8252 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8253 | if (r.isHomeActivity) { |
| 8254 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 8255 | try { |
| 8256 | ActivityThread.getPackageManager() |
| 8257 | .clearPackagePreferredActivities(r.packageName); |
| 8258 | } catch (RemoteException c) { |
| 8259 | // pm is in same process, this will never happen. |
| 8260 | } |
| 8261 | } |
| 8262 | } |
| 8263 | } |
| 8264 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8265 | if (!app.isolated) { |
| 8266 | // XXX Can't keep track of crash times for isolated processes, |
| 8267 | // because they don't have a perisistent identity. |
| 8268 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 8269 | } |
| 8270 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8271 | return true; |
| 8272 | } |
| 8273 | |
| 8274 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 8275 | if (app.userId == mCurrentUserId) { |
| 8276 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 8277 | mContext, app.info.packageName, app.info.flags); |
| 8278 | } else { |
| 8279 | // If this app is not running under the current user, then we |
| 8280 | // can't give it a report button because that would require |
| 8281 | // launching the report UI under a different user. |
| 8282 | app.errorReportReceiver = null; |
| 8283 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8284 | skipCurrentReceiverLocked(app); |
| 8285 | } |
| 8286 | |
| 8287 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 8288 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 8289 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8290 | } |
| 8291 | } |
| 8292 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8293 | /** |
| 8294 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 8295 | * The application process will exit immediately after this call returns. |
| 8296 | * @param app object of the crashing app, null for the system server |
| 8297 | * @param crashInfo describing the exception |
| 8298 | */ |
| 8299 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8300 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8301 | final String processName = app == null ? "system_server" |
| 8302 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8303 | |
| 8304 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8305 | UserHandle.getUserId(Binder.getCallingUid()), processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8306 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8307 | crashInfo.exceptionClassName, |
| 8308 | crashInfo.exceptionMessage, |
| 8309 | crashInfo.throwFileName, |
| 8310 | crashInfo.throwLineNumber); |
| 8311 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8312 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8313 | |
| 8314 | crashApplication(r, crashInfo); |
| 8315 | } |
| 8316 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8317 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8318 | IBinder app, |
| 8319 | int violationMask, |
| 8320 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8321 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 8322 | if (r == null) { |
| 8323 | return; |
| 8324 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8325 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8326 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 8327 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8328 | boolean logIt = true; |
| 8329 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 8330 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 8331 | logIt = false; |
| 8332 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8333 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8334 | // the relative pain numbers, without logging all |
| 8335 | // the stack traces repeatedly. We'd want to do |
| 8336 | // likewise in the client code, which also does |
| 8337 | // dup suppression, before the Binder call. |
| 8338 | } else { |
| 8339 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 8340 | mAlreadyLoggedViolatedStacks.clear(); |
| 8341 | } |
| 8342 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 8343 | } |
| 8344 | } |
| 8345 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8346 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8347 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8348 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8349 | |
| 8350 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 8351 | AppErrorResult result = new AppErrorResult(); |
| 8352 | synchronized (this) { |
| 8353 | final long origId = Binder.clearCallingIdentity(); |
| 8354 | |
| 8355 | Message msg = Message.obtain(); |
| 8356 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 8357 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 8358 | data.put("result", result); |
| 8359 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8360 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8361 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8362 | msg.obj = data; |
| 8363 | mHandler.sendMessage(msg); |
| 8364 | |
| 8365 | Binder.restoreCallingIdentity(origId); |
| 8366 | } |
| 8367 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 8368 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8369 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8370 | } |
| 8371 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8372 | // Depending on the policy in effect, there could be a bunch of |
| 8373 | // these in quick succession so we try to batch these together to |
| 8374 | // minimize disk writes, number of dropbox entries, and maximize |
| 8375 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8376 | private void logStrictModeViolationToDropBox( |
| 8377 | ProcessRecord process, |
| 8378 | StrictMode.ViolationInfo info) { |
| 8379 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8380 | return; |
| 8381 | } |
| 8382 | final boolean isSystemApp = process == null || |
| 8383 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 8384 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8385 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8386 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 8387 | final DropBoxManager dbox = (DropBoxManager) |
| 8388 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8389 | |
| 8390 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8391 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8392 | |
| 8393 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8394 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8395 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 8396 | synchronized (sb) { |
| 8397 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8398 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8399 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 8400 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8401 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 8402 | if (info.violationNumThisLoop != 0) { |
| 8403 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 8404 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 8405 | if (info.numAnimationsRunning != 0) { |
| 8406 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 8407 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 8408 | if (info.broadcastIntentAction != null) { |
| 8409 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 8410 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8411 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8412 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8413 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 8414 | if (info.numInstances != -1) { |
| 8415 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 8416 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8417 | if (info.tags != null) { |
| 8418 | for (String tag : info.tags) { |
| 8419 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 8420 | } |
| 8421 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8422 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8423 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 8424 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8425 | } |
| 8426 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8427 | |
| 8428 | // Only buffer up to ~64k. Various logging bits truncate |
| 8429 | // things at 128k. |
| 8430 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8431 | } |
| 8432 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8433 | // Flush immediately if the buffer's grown too large, or this |
| 8434 | // is a non-system app. Non-system apps are isolated with a |
| 8435 | // different tag & policy and not batched. |
| 8436 | // |
| 8437 | // Batching is useful during internal testing with |
| 8438 | // StrictMode settings turned up high. Without batching, |
| 8439 | // thousands of separate files could be created on boot. |
| 8440 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8441 | new Thread("Error dump: " + dropboxTag) { |
| 8442 | @Override |
| 8443 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8444 | String report; |
| 8445 | synchronized (sb) { |
| 8446 | report = sb.toString(); |
| 8447 | sb.delete(0, sb.length()); |
| 8448 | sb.trimToSize(); |
| 8449 | } |
| 8450 | if (report.length() != 0) { |
| 8451 | dbox.addText(dropboxTag, report); |
| 8452 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8453 | } |
| 8454 | }.start(); |
| 8455 | return; |
| 8456 | } |
| 8457 | |
| 8458 | // System app batching: |
| 8459 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8460 | // An existing dropbox-writing thread is outstanding, so |
| 8461 | // we don't need to start it up. The existing thread will |
| 8462 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8463 | return; |
| 8464 | } |
| 8465 | |
| 8466 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 8467 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8468 | new Thread("Error dump: " + dropboxTag) { |
| 8469 | @Override |
| 8470 | public void run() { |
| 8471 | // 5 second sleep to let stacks arrive and be batched together |
| 8472 | try { |
| 8473 | Thread.sleep(5000); // 5 seconds |
| 8474 | } catch (InterruptedException e) {} |
| 8475 | |
| 8476 | String errorReport; |
| 8477 | synchronized (mStrictModeBuffer) { |
| 8478 | errorReport = mStrictModeBuffer.toString(); |
| 8479 | if (errorReport.length() == 0) { |
| 8480 | return; |
| 8481 | } |
| 8482 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 8483 | mStrictModeBuffer.trimToSize(); |
| 8484 | } |
| 8485 | dbox.addText(dropboxTag, errorReport); |
| 8486 | } |
| 8487 | }.start(); |
| 8488 | } |
| 8489 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8490 | /** |
| 8491 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 8492 | * @param app object of the crashing app, null for the system server |
| 8493 | * @param tag reported by the caller |
| 8494 | * @param crashInfo describing the context of the error |
| 8495 | * @return true if the process should exit immediately (WTF is fatal) |
| 8496 | */ |
| 8497 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8498 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8499 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8500 | final String processName = app == null ? "system_server" |
| 8501 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8502 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8503 | EventLog.writeEvent(EventLogTags.AM_WTF, |
| 8504 | UserHandle.getUserId(Binder.getCallingUid()), Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8505 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8506 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8507 | tag, crashInfo.exceptionMessage); |
| 8508 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8509 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8510 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 8511 | if (r != null && r.pid != Process.myPid() && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 8512 | Settings.Global.getInt(mContext.getContentResolver(), |
| 8513 | Settings.Global.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8514 | crashApplication(r, crashInfo); |
| 8515 | return true; |
| 8516 | } else { |
| 8517 | return false; |
| 8518 | } |
| 8519 | } |
| 8520 | |
| 8521 | /** |
| 8522 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 8523 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 8524 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8525 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8526 | if (app == null) { |
| 8527 | return null; |
| 8528 | } |
| 8529 | |
| 8530 | synchronized (this) { |
| 8531 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 8532 | final int NA = apps.size(); |
| 8533 | for (int ia=0; ia<NA; ia++) { |
| 8534 | ProcessRecord p = apps.valueAt(ia); |
| 8535 | if (p.thread != null && p.thread.asBinder() == app) { |
| 8536 | return p; |
| 8537 | } |
| 8538 | } |
| 8539 | } |
| 8540 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8541 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 8542 | + " from pid=" + Binder.getCallingPid() |
| 8543 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8544 | return null; |
| 8545 | } |
| 8546 | } |
| 8547 | |
| 8548 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8549 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 8550 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8551 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8552 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 8553 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8554 | // Watchdog thread ends up invoking this function (with |
| 8555 | // a null ProcessRecord) to add the stack file to dropbox. |
| 8556 | // Do not acquire a lock on this (am) in such cases, as it |
| 8557 | // could cause a potential deadlock, if and when watchdog |
| 8558 | // is invoked due to unavailability of lock on am and it |
| 8559 | // would prevent watchdog from killing system_server. |
| 8560 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8561 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8562 | return; |
| 8563 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 8564 | // Note: ProcessRecord 'process' is guarded by the service |
| 8565 | // instance. (notably process.pkgList, which could otherwise change |
| 8566 | // concurrently during execution of this method) |
| 8567 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8568 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8569 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8570 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8571 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 8572 | for (String pkg : process.pkgList) { |
| 8573 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8574 | try { |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 8575 | PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId()); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8576 | if (pi != null) { |
| 8577 | sb.append(" v").append(pi.versionCode); |
| 8578 | if (pi.versionName != null) { |
| 8579 | sb.append(" (").append(pi.versionName).append(")"); |
| 8580 | } |
| 8581 | } |
| 8582 | } catch (RemoteException e) { |
| 8583 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8584 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8585 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8586 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8587 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8588 | } |
| 8589 | |
| 8590 | private static String processClass(ProcessRecord process) { |
| 8591 | if (process == null || process.pid == MY_PID) { |
| 8592 | return "system_server"; |
| 8593 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 8594 | return "system_app"; |
| 8595 | } else { |
| 8596 | return "data_app"; |
| 8597 | } |
| 8598 | } |
| 8599 | |
| 8600 | /** |
| 8601 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 8602 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 8603 | * @param process which caused the error, null means the system server |
| 8604 | * @param activity which triggered the error, null if unknown |
| 8605 | * @param parent activity related to the error, null if unknown |
| 8606 | * @param subject line related to the error, null if absent |
| 8607 | * @param report in long form describing the error, null if absent |
| 8608 | * @param logFile to include in the report, null if none |
| 8609 | * @param crashInfo giving an application stack trace, null if absent |
| 8610 | */ |
| 8611 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8612 | ProcessRecord process, String processName, ActivityRecord activity, |
| 8613 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8614 | final String report, final File logFile, |
| 8615 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 8616 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 8617 | // otherwise the watchdog may be prevented from resetting the system. |
| 8618 | |
| 8619 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 8620 | final DropBoxManager dbox = (DropBoxManager) |
| 8621 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8622 | |
| 8623 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8624 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8625 | |
| 8626 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8627 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8628 | if (activity != null) { |
| 8629 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 8630 | } |
| 8631 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 8632 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 8633 | } |
| 8634 | if (parent != null && parent != activity) { |
| 8635 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 8636 | } |
| 8637 | if (subject != null) { |
| 8638 | sb.append("Subject: ").append(subject).append("\n"); |
| 8639 | } |
| 8640 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 8641 | if (Debug.isDebuggerConnected()) { |
| 8642 | sb.append("Debugger: Connected\n"); |
| 8643 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8644 | sb.append("\n"); |
| 8645 | |
| 8646 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 8647 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8648 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 8649 | @Override |
| 8650 | public void run() { |
| 8651 | if (report != null) { |
| 8652 | sb.append(report); |
| 8653 | } |
| 8654 | if (logFile != null) { |
| 8655 | try { |
| 8656 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 8657 | } catch (IOException e) { |
| 8658 | Slog.e(TAG, "Error reading " + logFile, e); |
| 8659 | } |
| 8660 | } |
| 8661 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 8662 | sb.append(crashInfo.stackTrace); |
| 8663 | } |
| 8664 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 8665 | String setting = Settings.Global.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 8666 | int lines = Settings.Global.getInt(mContext.getContentResolver(), setting, 0); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8667 | if (lines > 0) { |
| 8668 | sb.append("\n"); |
| 8669 | |
| 8670 | // Merge several logcat streams, and take the last N lines |
| 8671 | InputStreamReader input = null; |
| 8672 | try { |
| 8673 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 8674 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 8675 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 8676 | |
| 8677 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 8678 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 8679 | input = new InputStreamReader(logcat.getInputStream()); |
| 8680 | |
| 8681 | int num; |
| 8682 | char[] buf = new char[8192]; |
| 8683 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 8684 | } catch (IOException e) { |
| 8685 | Slog.e(TAG, "Error running logcat", e); |
| 8686 | } finally { |
| 8687 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 8688 | } |
| 8689 | } |
| 8690 | |
| 8691 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8692 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8693 | }; |
| 8694 | |
Dianne Hackborn | 56385cc | 2012-04-30 15:07:47 -0700 | [diff] [blame] | 8695 | if (process == null) { |
| 8696 | // If process is null, we are being called from some internal code |
| 8697 | // and may be about to die -- run this synchronously. |
| 8698 | worker.run(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8699 | } else { |
| 8700 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8701 | } |
| 8702 | } |
| 8703 | |
| 8704 | /** |
| 8705 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 8706 | * Deal with edge cases (intercepts from instrumented applications, |
| 8707 | * ActivityController, error intent receivers, that sort of thing). |
| 8708 | * @param r the application crashing |
| 8709 | * @param crashInfo describing the failure |
| 8710 | */ |
| 8711 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8712 | long timeMillis = System.currentTimeMillis(); |
| 8713 | String shortMsg = crashInfo.exceptionClassName; |
| 8714 | String longMsg = crashInfo.exceptionMessage; |
| 8715 | String stackTrace = crashInfo.stackTrace; |
| 8716 | if (shortMsg != null && longMsg != null) { |
| 8717 | longMsg = shortMsg + ": " + longMsg; |
| 8718 | } else if (shortMsg != null) { |
| 8719 | longMsg = shortMsg; |
| 8720 | } |
| 8721 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8722 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8723 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8724 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8725 | try { |
| 8726 | String name = r != null ? r.processName : null; |
| 8727 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8728 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8729 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8730 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8731 | + " at watcher's request"); |
| 8732 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8733 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8734 | } |
| 8735 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8736 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8737 | } |
| 8738 | } |
| 8739 | |
| 8740 | final long origId = Binder.clearCallingIdentity(); |
| 8741 | |
| 8742 | // If this process is running instrumentation, finish it. |
| 8743 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8744 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8745 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8746 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 8747 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8748 | Bundle info = new Bundle(); |
| 8749 | info.putString("shortMsg", shortMsg); |
| 8750 | info.putString("longMsg", longMsg); |
| 8751 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 8752 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8753 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8754 | } |
| 8755 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8756 | // If we can't identify the process or it's already exceeded its crash quota, |
| 8757 | // quit right away without showing a crash dialog. |
| 8758 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8759 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8760 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8761 | } |
| 8762 | |
| 8763 | Message msg = Message.obtain(); |
| 8764 | msg.what = SHOW_ERROR_MSG; |
| 8765 | HashMap data = new HashMap(); |
| 8766 | data.put("result", result); |
| 8767 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8768 | msg.obj = data; |
| 8769 | mHandler.sendMessage(msg); |
| 8770 | |
| 8771 | Binder.restoreCallingIdentity(origId); |
| 8772 | } |
| 8773 | |
| 8774 | int res = result.get(); |
| 8775 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8776 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8777 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8778 | if (r != null && !r.isolated) { |
| 8779 | // XXX Can't keep track of crash time for isolated processes, |
| 8780 | // since they don't have a persistent identity. |
| 8781 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8782 | SystemClock.uptimeMillis()); |
| 8783 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8784 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8785 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8786 | } |
| 8787 | } |
| 8788 | |
| 8789 | if (appErrorIntent != null) { |
| 8790 | try { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 8791 | mContext.startActivityAsUser(appErrorIntent, new UserHandle(r.userId)); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8792 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8793 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8794 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8795 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8796 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8797 | |
| 8798 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 8799 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 8800 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8801 | if (report == null) { |
| 8802 | return null; |
| 8803 | } |
| 8804 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 8805 | result.setComponent(r.errorReportReceiver); |
| 8806 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 8807 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 8808 | return result; |
| 8809 | } |
| 8810 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8811 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 8812 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8813 | if (r.errorReportReceiver == null) { |
| 8814 | return null; |
| 8815 | } |
| 8816 | |
| 8817 | if (!r.crashing && !r.notResponding) { |
| 8818 | return null; |
| 8819 | } |
| 8820 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8821 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 8822 | report.packageName = r.info.packageName; |
| 8823 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 8824 | report.processName = r.processName; |
| 8825 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 8826 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8827 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8828 | if (r.crashing) { |
| 8829 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 8830 | report.crashInfo = crashInfo; |
| 8831 | } else if (r.notResponding) { |
| 8832 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 8833 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8834 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8835 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 8836 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 8837 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8838 | } |
| 8839 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8840 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8841 | } |
| 8842 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8843 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8844 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8845 | // assume our apps are happy - lazy create the list |
| 8846 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 8847 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8848 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8849 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8850 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8851 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 8852 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8853 | synchronized (this) { |
| 8854 | |
| 8855 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8856 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8857 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8858 | if (!allUsers && app.userId != userId) { |
| 8859 | continue; |
| 8860 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8861 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 8862 | // This one's in trouble, so we'll generate a report for it |
| 8863 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 8864 | ActivityManager.ProcessErrorStateInfo report = null; |
| 8865 | if (app.crashing) { |
| 8866 | report = app.crashingReport; |
| 8867 | } else if (app.notResponding) { |
| 8868 | report = app.notRespondingReport; |
| 8869 | } |
| 8870 | |
| 8871 | if (report != null) { |
| 8872 | if (errList == null) { |
| 8873 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 8874 | } |
| 8875 | errList.add(report); |
| 8876 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8877 | 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] | 8878 | " crashing = " + app.crashing + |
| 8879 | " notResponding = " + app.notResponding); |
| 8880 | } |
| 8881 | } |
| 8882 | } |
| 8883 | } |
| 8884 | |
| 8885 | return errList; |
| 8886 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8887 | |
| 8888 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8889 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8890 | if (currApp != null) { |
| 8891 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 8892 | } |
| 8893 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8894 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 8895 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8896 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 8897 | if (currApp != null) { |
| 8898 | currApp.lru = 0; |
| 8899 | } |
| 8900 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8901 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8902 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 8903 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8904 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 8905 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8906 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 8907 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8908 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 8909 | } else { |
| 8910 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 8911 | } |
| 8912 | } |
| 8913 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8914 | private void fillInProcMemInfo(ProcessRecord app, |
| 8915 | ActivityManager.RunningAppProcessInfo outInfo) { |
| 8916 | outInfo.pid = app.pid; |
| 8917 | outInfo.uid = app.info.uid; |
| 8918 | if (mHeavyWeightProcess == app) { |
| 8919 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
| 8920 | } |
| 8921 | if (app.persistent) { |
| 8922 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 8923 | } |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8924 | if (app.hasActivities) { |
| 8925 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES; |
| 8926 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8927 | outInfo.lastTrimLevel = app.trimMemoryLevel; |
| 8928 | int adj = app.curAdj; |
| 8929 | outInfo.importance = oomAdjToImportance(adj, outInfo); |
| 8930 | outInfo.importanceReasonCode = app.adjTypeCode; |
| 8931 | } |
| 8932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8933 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8934 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8935 | // Lazy instantiation of list |
| 8936 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8937 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8938 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8939 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8940 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8941 | synchronized (this) { |
| 8942 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8943 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8944 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8945 | if (!allUsers && app.userId != userId) { |
| 8946 | continue; |
| 8947 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8948 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 8949 | // Generate process state info for running application |
| 8950 | ActivityManager.RunningAppProcessInfo currApp = |
| 8951 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 8952 | app.pid, app.getPackageList()); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8953 | fillInProcMemInfo(app, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8954 | if (app.adjSource instanceof ProcessRecord) { |
| 8955 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8956 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 8957 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8958 | } else if (app.adjSource instanceof ActivityRecord) { |
| 8959 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8960 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 8961 | } |
| 8962 | if (app.adjTarget instanceof ComponentName) { |
| 8963 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 8964 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8965 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8966 | // + " lru=" + currApp.lru); |
| 8967 | if (runList == null) { |
| 8968 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 8969 | } |
| 8970 | runList.add(currApp); |
| 8971 | } |
| 8972 | } |
| 8973 | } |
| 8974 | return runList; |
| 8975 | } |
| 8976 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8977 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8978 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8979 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8980 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8981 | if (runningApps != null && runningApps.size() > 0) { |
| 8982 | Set<String> extList = new HashSet<String>(); |
| 8983 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8984 | if (app.pkgList != null) { |
| 8985 | for (String pkg : app.pkgList) { |
| 8986 | extList.add(pkg); |
| 8987 | } |
| 8988 | } |
| 8989 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8990 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8991 | for (String pkg : extList) { |
| 8992 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 8993 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId()); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8994 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8995 | retList.add(info); |
| 8996 | } |
| 8997 | } catch (RemoteException e) { |
| 8998 | } |
| 8999 | } |
| 9000 | } |
| 9001 | return retList; |
| 9002 | } |
| 9003 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9004 | @Override |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 9005 | public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) { |
| 9006 | enforceNotIsolatedCaller("getMyMemoryState"); |
| 9007 | synchronized (this) { |
| 9008 | ProcessRecord proc; |
| 9009 | synchronized (mPidsSelfLocked) { |
| 9010 | proc = mPidsSelfLocked.get(Binder.getCallingPid()); |
| 9011 | } |
| 9012 | fillInProcMemInfo(proc, outInfo); |
| 9013 | } |
| 9014 | } |
| 9015 | |
| 9016 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9017 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9018 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 9019 | != PackageManager.PERMISSION_GRANTED) { |
| 9020 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 9021 | + Binder.getCallingPid() |
| 9022 | + ", uid=" + Binder.getCallingUid() |
| 9023 | + " without permission " |
| 9024 | + android.Manifest.permission.DUMP); |
| 9025 | return; |
| 9026 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9027 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9028 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9029 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9030 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9031 | |
| 9032 | int opti = 0; |
| 9033 | while (opti < args.length) { |
| 9034 | String opt = args[opti]; |
| 9035 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 9036 | break; |
| 9037 | } |
| 9038 | opti++; |
| 9039 | if ("-a".equals(opt)) { |
| 9040 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9041 | } else if ("-c".equals(opt)) { |
| 9042 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9043 | } else if ("-h".equals(opt)) { |
| 9044 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9045 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9046 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9047 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 9048 | pw.println(" b[roadcasts] [PACKAGE_NAME] [history [-s]]: broadcast state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9049 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 9050 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9051 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9052 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9053 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9054 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9055 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9056 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9057 | pw.println(" all: dump all activities"); |
| 9058 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9059 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9060 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 9061 | pw.println(" a partial substring in a component name, a"); |
| 9062 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9063 | pw.println(" -a: include all available server state."); |
| 9064 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9065 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9066 | } else { |
| 9067 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9068 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9069 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9070 | |
| 9071 | long origId = Binder.clearCallingIdentity(); |
| 9072 | boolean more = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9073 | // Is the caller requesting to dump a particular piece of data? |
| 9074 | if (opti < args.length) { |
| 9075 | String cmd = args[opti]; |
| 9076 | opti++; |
| 9077 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 9078 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9079 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9080 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9081 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9082 | String[] newArgs; |
| 9083 | String name; |
| 9084 | if (opti >= args.length) { |
| 9085 | name = null; |
| 9086 | newArgs = EMPTY_STRING_ARRAY; |
| 9087 | } else { |
| 9088 | name = args[opti]; |
| 9089 | opti++; |
| 9090 | newArgs = new String[args.length - opti]; |
| 9091 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9092 | args.length - opti); |
| 9093 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9094 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9095 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9096 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9097 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9098 | String[] newArgs; |
| 9099 | String name; |
| 9100 | if (opti >= args.length) { |
| 9101 | name = null; |
| 9102 | newArgs = EMPTY_STRING_ARRAY; |
| 9103 | } else { |
| 9104 | name = args[opti]; |
| 9105 | opti++; |
| 9106 | newArgs = new String[args.length - opti]; |
| 9107 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9108 | args.length - opti); |
| 9109 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9110 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9111 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9112 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9113 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9114 | String[] newArgs; |
| 9115 | String name; |
| 9116 | if (opti >= args.length) { |
| 9117 | name = null; |
| 9118 | newArgs = EMPTY_STRING_ARRAY; |
| 9119 | } else { |
| 9120 | name = args[opti]; |
| 9121 | opti++; |
| 9122 | newArgs = new String[args.length - opti]; |
| 9123 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9124 | args.length - opti); |
| 9125 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9126 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9127 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9128 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9129 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 9130 | synchronized (this) { |
| 9131 | dumpOomLocked(fd, pw, args, opti, true); |
| 9132 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9133 | } else if ("provider".equals(cmd)) { |
| 9134 | String[] newArgs; |
| 9135 | String name; |
| 9136 | if (opti >= args.length) { |
| 9137 | name = null; |
| 9138 | newArgs = EMPTY_STRING_ARRAY; |
| 9139 | } else { |
| 9140 | name = args[opti]; |
| 9141 | opti++; |
| 9142 | newArgs = new String[args.length - opti]; |
| 9143 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9144 | } |
| 9145 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 9146 | pw.println("No providers match: " + name); |
| 9147 | pw.println("Use -h for help."); |
| 9148 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9149 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 9150 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9151 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9152 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9153 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9154 | String[] newArgs; |
| 9155 | String name; |
| 9156 | if (opti >= args.length) { |
| 9157 | name = null; |
| 9158 | newArgs = EMPTY_STRING_ARRAY; |
| 9159 | } else { |
| 9160 | name = args[opti]; |
| 9161 | opti++; |
| 9162 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9163 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9164 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9165 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9166 | if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9167 | pw.println("No services match: " + name); |
| 9168 | pw.println("Use -h for help."); |
| 9169 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9170 | } else if ("package".equals(cmd)) { |
| 9171 | String[] newArgs; |
| 9172 | if (opti >= args.length) { |
| 9173 | pw.println("package: no package name specified"); |
| 9174 | pw.println("Use -h for help."); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9175 | } else { |
| 9176 | dumpPackage = args[opti]; |
| 9177 | opti++; |
| 9178 | newArgs = new String[args.length - opti]; |
| 9179 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9180 | args.length - opti); |
| 9181 | args = newArgs; |
| 9182 | opti = 0; |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9183 | more = true; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9184 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9185 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 9186 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9187 | mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9188 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9189 | } else { |
| 9190 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9191 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 9192 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 9193 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9194 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9195 | } |
| 9196 | if (!more) { |
| 9197 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9198 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9199 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9200 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9201 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9202 | // No piece of data specified, dump everything. |
| 9203 | synchronized (this) { |
| 9204 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9205 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9206 | if (needSep) { |
| 9207 | pw.println(" "); |
| 9208 | } |
| 9209 | if (dumpAll) { |
| 9210 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9211 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9212 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9213 | if (needSep) { |
| 9214 | pw.println(" "); |
| 9215 | } |
| 9216 | if (dumpAll) { |
| 9217 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9218 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9219 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9220 | if (needSep) { |
| 9221 | pw.println(" "); |
| 9222 | } |
| 9223 | if (dumpAll) { |
| 9224 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9225 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9226 | needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9227 | if (needSep) { |
| 9228 | pw.println(" "); |
| 9229 | } |
| 9230 | if (dumpAll) { |
| 9231 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9232 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9233 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9234 | if (needSep) { |
| 9235 | pw.println(" "); |
| 9236 | } |
| 9237 | if (dumpAll) { |
| 9238 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9239 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9240 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9241 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9242 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9243 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9244 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9245 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9246 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9247 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 9248 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9249 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 9250 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9251 | pw.println(" "); |
| 9252 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9253 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 9254 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9255 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9256 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9257 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9258 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9259 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9260 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9261 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9262 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9263 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9264 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9265 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9266 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9267 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 9268 | pw.println(" "); |
| 9269 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9270 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9271 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9272 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9273 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9274 | pw.println(" "); |
| 9275 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9276 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9277 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9279 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9280 | pw.println(" "); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 9281 | if (mMainStack.mPausingActivity != null) { |
| 9282 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9283 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9284 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9285 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9286 | if (dumpAll) { |
| 9287 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 9288 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9289 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 9290 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9291 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9292 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9293 | if (mRecentTasks.size() > 0) { |
| 9294 | pw.println(); |
| 9295 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9296 | |
| 9297 | final int N = mRecentTasks.size(); |
| 9298 | for (int i=0; i<N; i++) { |
| 9299 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9300 | if (dumpPackage != null) { |
| 9301 | if (tr.realActivity == null || |
| 9302 | !dumpPackage.equals(tr.realActivity)) { |
| 9303 | continue; |
| 9304 | } |
| 9305 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9306 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 9307 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9308 | if (dumpAll) { |
| 9309 | mRecentTasks.get(i).dump(pw, " "); |
| 9310 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9311 | } |
| 9312 | } |
| 9313 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9314 | if (dumpAll) { |
| 9315 | pw.println(" "); |
| 9316 | pw.println(" mCurTask: " + mCurTask); |
| 9317 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9318 | |
| 9319 | return true; |
| 9320 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9321 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9322 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9323 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9324 | boolean needSep = false; |
| 9325 | int numPers = 0; |
| 9326 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9327 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 9328 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9329 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9330 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 9331 | final int NA = procs.size(); |
| 9332 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9333 | ProcessRecord r = procs.valueAt(ia); |
| 9334 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9335 | continue; |
| 9336 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9337 | if (!needSep) { |
| 9338 | pw.println(" All known processes:"); |
| 9339 | needSep = true; |
| 9340 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9341 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 9342 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 9343 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9344 | r.dump(pw, " "); |
| 9345 | if (r.persistent) { |
| 9346 | numPers++; |
| 9347 | } |
| 9348 | } |
| 9349 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9350 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 9351 | |
| 9352 | if (mIsolatedProcesses.size() > 0) { |
| 9353 | if (needSep) pw.println(" "); |
| 9354 | needSep = true; |
| 9355 | pw.println(" Isolated process list (sorted by uid):"); |
| 9356 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 9357 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 9358 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9359 | continue; |
| 9360 | } |
| 9361 | pw.println(String.format("%sIsolated #%2d: %s", |
| 9362 | " ", i, r.toString())); |
| 9363 | } |
| 9364 | } |
| 9365 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9366 | if (mLruProcesses.size() > 0) { |
| 9367 | if (needSep) pw.println(" "); |
| 9368 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9369 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9370 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9371 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9372 | needSep = true; |
| 9373 | } |
| 9374 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9375 | if (dumpAll) { |
| 9376 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9377 | boolean printed = false; |
| 9378 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 9379 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 9380 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9381 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9382 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9383 | if (!printed) { |
| 9384 | if (needSep) pw.println(" "); |
| 9385 | needSep = true; |
| 9386 | pw.println(" PID mappings:"); |
| 9387 | printed = true; |
| 9388 | } |
| 9389 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 9390 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9391 | } |
| 9392 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9393 | } |
| 9394 | |
| 9395 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9396 | synchronized (mPidsSelfLocked) { |
| 9397 | boolean printed = false; |
| 9398 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 9399 | ProcessRecord r = mPidsSelfLocked.get( |
| 9400 | mForegroundProcesses.valueAt(i).pid); |
| 9401 | if (dumpPackage != null && (r == null |
| 9402 | || !dumpPackage.equals(r.info.packageName))) { |
| 9403 | continue; |
| 9404 | } |
| 9405 | if (!printed) { |
| 9406 | if (needSep) pw.println(" "); |
| 9407 | needSep = true; |
| 9408 | pw.println(" Foreground Processes:"); |
| 9409 | printed = true; |
| 9410 | } |
| 9411 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 9412 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 9413 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9414 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9415 | } |
| 9416 | |
| 9417 | if (mPersistentStartingProcesses.size() > 0) { |
| 9418 | if (needSep) pw.println(" "); |
| 9419 | needSep = true; |
| 9420 | pw.println(" Persisent processes that are starting:"); |
| 9421 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9422 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9423 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9424 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9425 | if (mRemovedProcesses.size() > 0) { |
| 9426 | if (needSep) pw.println(" "); |
| 9427 | needSep = true; |
| 9428 | pw.println(" Processes that are being removed:"); |
| 9429 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9430 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9431 | } |
| 9432 | |
| 9433 | if (mProcessesOnHold.size() > 0) { |
| 9434 | if (needSep) pw.println(" "); |
| 9435 | needSep = true; |
| 9436 | pw.println(" Processes that are on old until the system is ready:"); |
| 9437 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9438 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9439 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9440 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9441 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9442 | |
| 9443 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9444 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9445 | long now = SystemClock.uptimeMillis(); |
| 9446 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9447 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9448 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9449 | SparseArray<Long> uids = procs.getValue(); |
| 9450 | final int N = uids.size(); |
| 9451 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9452 | int puid = uids.keyAt(i); |
| 9453 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9454 | if (dumpPackage != null && (r == null |
| 9455 | || !dumpPackage.equals(r.info.packageName))) { |
| 9456 | continue; |
| 9457 | } |
| 9458 | if (!printed) { |
| 9459 | if (needSep) pw.println(" "); |
| 9460 | needSep = true; |
| 9461 | pw.println(" Time since processes crashed:"); |
| 9462 | printed = true; |
| 9463 | } |
| 9464 | pw.print(" Process "); pw.print(pname); |
| 9465 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9466 | pw.print(": last crashed "); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 9467 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 9468 | pw.println(" ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9469 | } |
| 9470 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9472 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9473 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9474 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9475 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9476 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9477 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9478 | SparseArray<Long> uids = procs.getValue(); |
| 9479 | final int N = uids.size(); |
| 9480 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9481 | int puid = uids.keyAt(i); |
| 9482 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9483 | if (dumpPackage != null && (r == null |
| 9484 | || !dumpPackage.equals(r.info.packageName))) { |
| 9485 | continue; |
| 9486 | } |
| 9487 | if (!printed) { |
| 9488 | if (needSep) pw.println(" "); |
| 9489 | needSep = true; |
| 9490 | pw.println(" Bad processes:"); |
| 9491 | } |
| 9492 | pw.print(" Bad process "); pw.print(pname); |
| 9493 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9494 | pw.print(": crashed at time "); |
| 9495 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9496 | } |
| 9497 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9498 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9499 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9500 | pw.println(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9501 | pw.println(" mStartedUsers:"); |
| 9502 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 9503 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 9504 | pw.print(" User #"); pw.print(uss.mHandle.getIdentifier()); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9505 | pw.print(": "); uss.dump("", pw); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9506 | } |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 9507 | pw.print(" mStartedUserArray: ["); |
| 9508 | for (int i=0; i<mStartedUserArray.length; i++) { |
| 9509 | if (i > 0) pw.print(", "); |
| 9510 | pw.print(mStartedUserArray[i]); |
| 9511 | } |
| 9512 | pw.println("]"); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9513 | pw.print(" mUserLru: ["); |
| 9514 | for (int i=0; i<mUserLru.size(); i++) { |
| 9515 | if (i > 0) pw.print(", "); |
| 9516 | pw.print(mUserLru.get(i)); |
| 9517 | } |
| 9518 | pw.println("]"); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 9519 | if (dumpAll) { |
| 9520 | pw.print(" mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray)); |
| 9521 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9522 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9523 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 9524 | if (dumpAll) { |
| 9525 | StringBuilder sb = new StringBuilder(128); |
| 9526 | sb.append(" mPreviousProcessVisibleTime: "); |
| 9527 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 9528 | pw.println(sb); |
| 9529 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9530 | if (mHeavyWeightProcess != null) { |
| 9531 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9532 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9533 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9534 | if (dumpAll) { |
| 9535 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 9536 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9537 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9538 | for (Map.Entry<String, Integer> entry |
| 9539 | : mCompatModePackages.getPackages().entrySet()) { |
| 9540 | String pkg = entry.getKey(); |
| 9541 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9542 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 9543 | continue; |
| 9544 | } |
| 9545 | if (!printed) { |
| 9546 | pw.println(" mScreenCompatPackages:"); |
| 9547 | printed = true; |
| 9548 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9549 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 9550 | pw.print(mode); pw.println(); |
| 9551 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 9552 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9553 | } |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 9554 | if (mSleeping || mWentToSleep || mLockScreenShown) { |
| 9555 | pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep |
| 9556 | + " mLockScreenShown " + mLockScreenShown); |
| 9557 | } |
| 9558 | if (mShuttingDown) { |
| 9559 | pw.println(" mShuttingDown=" + mShuttingDown); |
| 9560 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9561 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 9562 | || mOrigWaitForDebugger) { |
| 9563 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 9564 | + " mDebugTransient=" + mDebugTransient |
| 9565 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 9566 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 9567 | if (mOpenGlTraceApp != null) { |
| 9568 | pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp); |
| 9569 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 9570 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 9571 | || mProfileFd != null) { |
| 9572 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 9573 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 9574 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 9575 | + mAutoStopProfiler); |
| 9576 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9577 | if (mAlwaysFinishActivities || mController != null) { |
| 9578 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 9579 | + " mController=" + mController); |
| 9580 | } |
| 9581 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9582 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9583 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9584 | + " mProcessesReady=" + mProcessesReady |
| 9585 | + " mSystemReady=" + mSystemReady); |
| 9586 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9587 | + " mBooted=" + mBooted |
| 9588 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9589 | pw.print(" mLastPowerCheckRealtime="); |
| 9590 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 9591 | pw.println(""); |
| 9592 | pw.print(" mLastPowerCheckUptime="); |
| 9593 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 9594 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9595 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 9596 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 9597 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 9598 | pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs |
| 9599 | + " mNumHiddenProcs=" + mNumHiddenProcs |
| 9600 | + " mNumServiceProcs=" + mNumServiceProcs |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9601 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9602 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9603 | |
| 9604 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9605 | } |
| 9606 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9607 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9608 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9609 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9610 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9611 | long now = SystemClock.uptimeMillis(); |
| 9612 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 9613 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9614 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 9615 | continue; |
| 9616 | } |
| 9617 | if (!printed) { |
| 9618 | if (needSep) pw.println(" "); |
| 9619 | needSep = true; |
| 9620 | pw.println(" Processes that are waiting to GC:"); |
| 9621 | printed = true; |
| 9622 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9623 | pw.print(" Process "); pw.println(proc); |
| 9624 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 9625 | pw.print(", last gced="); |
| 9626 | pw.print(now-proc.lastRequestedGc); |
| 9627 | pw.print(" ms ago, last lowMem="); |
| 9628 | pw.print(now-proc.lastLowMemory); |
| 9629 | pw.println(" ms ago"); |
| 9630 | |
| 9631 | } |
| 9632 | } |
| 9633 | return needSep; |
| 9634 | } |
| 9635 | |
| 9636 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 9637 | int opti, boolean dumpAll) { |
| 9638 | boolean needSep = false; |
| 9639 | |
| 9640 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9641 | if (needSep) pw.println(" "); |
| 9642 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9643 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9644 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9645 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9646 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 9647 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 9648 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9649 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9650 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9651 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9652 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9653 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9654 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9655 | 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] | 9656 | 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] | 9657 | |
| 9658 | if (needSep) pw.println(" "); |
| 9659 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9660 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9661 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9662 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9663 | needSep = true; |
| 9664 | } |
| 9665 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9666 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9667 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9668 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9669 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9670 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9671 | if (mHeavyWeightProcess != null) { |
| 9672 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9673 | } |
| 9674 | |
| 9675 | return true; |
| 9676 | } |
| 9677 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9678 | /** |
| 9679 | * There are three ways to call this: |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9680 | * - no provider specified: dump all the providers |
| 9681 | * - a flattened component name that matched an existing provider was specified as the |
| 9682 | * first arg: dump that one provider |
| 9683 | * - the first arg isn't the flattened component name of an existing provider: |
| 9684 | * dump all providers whose component contains the first arg as a substring |
| 9685 | */ |
| 9686 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9687 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 9688 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9689 | } |
| 9690 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9691 | static class ItemMatcher { |
| 9692 | ArrayList<ComponentName> components; |
| 9693 | ArrayList<String> strings; |
| 9694 | ArrayList<Integer> objects; |
| 9695 | boolean all; |
| 9696 | |
| 9697 | ItemMatcher() { |
| 9698 | all = true; |
| 9699 | } |
| 9700 | |
| 9701 | void build(String name) { |
| 9702 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 9703 | if (componentName != null) { |
| 9704 | if (components == null) { |
| 9705 | components = new ArrayList<ComponentName>(); |
| 9706 | } |
| 9707 | components.add(componentName); |
| 9708 | all = false; |
| 9709 | } else { |
| 9710 | int objectId = 0; |
| 9711 | // Not a '/' separated full component name; maybe an object ID? |
| 9712 | try { |
| 9713 | objectId = Integer.parseInt(name, 16); |
| 9714 | if (objects == null) { |
| 9715 | objects = new ArrayList<Integer>(); |
| 9716 | } |
| 9717 | objects.add(objectId); |
| 9718 | all = false; |
| 9719 | } catch (RuntimeException e) { |
| 9720 | // Not an integer; just do string match. |
| 9721 | if (strings == null) { |
| 9722 | strings = new ArrayList<String>(); |
| 9723 | } |
| 9724 | strings.add(name); |
| 9725 | all = false; |
| 9726 | } |
| 9727 | } |
| 9728 | } |
| 9729 | |
| 9730 | int build(String[] args, int opti) { |
| 9731 | for (; opti<args.length; opti++) { |
| 9732 | String name = args[opti]; |
| 9733 | if ("--".equals(name)) { |
| 9734 | return opti+1; |
| 9735 | } |
| 9736 | build(name); |
| 9737 | } |
| 9738 | return opti; |
| 9739 | } |
| 9740 | |
| 9741 | boolean match(Object object, ComponentName comp) { |
| 9742 | if (all) { |
| 9743 | return true; |
| 9744 | } |
| 9745 | if (components != null) { |
| 9746 | for (int i=0; i<components.size(); i++) { |
| 9747 | if (components.get(i).equals(comp)) { |
| 9748 | return true; |
| 9749 | } |
| 9750 | } |
| 9751 | } |
| 9752 | if (objects != null) { |
| 9753 | for (int i=0; i<objects.size(); i++) { |
| 9754 | if (System.identityHashCode(object) == objects.get(i)) { |
| 9755 | return true; |
| 9756 | } |
| 9757 | } |
| 9758 | } |
| 9759 | if (strings != null) { |
| 9760 | String flat = comp.flattenToString(); |
| 9761 | for (int i=0; i<strings.size(); i++) { |
| 9762 | if (flat.contains(strings.get(i))) { |
| 9763 | return true; |
| 9764 | } |
| 9765 | } |
| 9766 | } |
| 9767 | return false; |
| 9768 | } |
| 9769 | } |
| 9770 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9771 | /** |
| 9772 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9773 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9774 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 9775 | * dump all activity whose component contains the cmd as a substring |
| 9776 | * - A hex number of the ActivityRecord object instance. |
| 9777 | */ |
| 9778 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9779 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9780 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9781 | |
| 9782 | if ("all".equals(name)) { |
| 9783 | synchronized (this) { |
| 9784 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9785 | activities.add(r1); |
| 9786 | } |
| 9787 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 9788 | } else if ("top".equals(name)) { |
| 9789 | synchronized (this) { |
| 9790 | final int N = mMainStack.mHistory.size(); |
| 9791 | if (N > 0) { |
| 9792 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 9793 | } |
| 9794 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9795 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9796 | ItemMatcher matcher = new ItemMatcher(); |
| 9797 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9798 | |
| 9799 | synchronized (this) { |
| 9800 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9801 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9802 | activities.add(r1); |
| 9803 | } |
| 9804 | } |
| 9805 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9806 | } |
| 9807 | |
| 9808 | if (activities.size() <= 0) { |
| 9809 | return false; |
| 9810 | } |
| 9811 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9812 | String[] newArgs = new String[args.length - opti]; |
| 9813 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9814 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9815 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9816 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9817 | for (int i=activities.size()-1; i>=0; i--) { |
| 9818 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9819 | if (needSep) { |
| 9820 | pw.println(); |
| 9821 | } |
| 9822 | needSep = true; |
| 9823 | synchronized (this) { |
| 9824 | if (lastTask != r.task) { |
| 9825 | lastTask = r.task; |
| 9826 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 9827 | pw.print(" id="); pw.println(lastTask.taskId); |
| 9828 | if (dumpAll) { |
| 9829 | lastTask.dump(pw, " "); |
| 9830 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9831 | } |
| 9832 | } |
| 9833 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9834 | } |
| 9835 | return true; |
| 9836 | } |
| 9837 | |
| 9838 | /** |
| 9839 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 9840 | * there is a thread associated with the activity. |
| 9841 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9842 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9843 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 9844 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9845 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9846 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 9847 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9848 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9849 | if (r.app != null) pw.println(r.app.pid); |
| 9850 | else pw.println("(not running)"); |
| 9851 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9852 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9853 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9854 | } |
| 9855 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9856 | // flush anything that is already in the PrintWriter since the thread is going |
| 9857 | // to write to the file descriptor directly |
| 9858 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9859 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9860 | TransferPipe tp = new TransferPipe(); |
| 9861 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9862 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9863 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9864 | tp.go(fd); |
| 9865 | } finally { |
| 9866 | tp.kill(); |
| 9867 | } |
| 9868 | } catch (IOException e) { |
| 9869 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9870 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9871 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9872 | } |
| 9873 | } |
| 9874 | } |
| 9875 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9876 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9877 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9878 | boolean needSep = false; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9879 | boolean onlyHistory = false; |
| 9880 | |
| 9881 | if ("history".equals(dumpPackage)) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 9882 | if (opti < args.length && "-s".equals(args[opti])) { |
| 9883 | dumpAll = false; |
| 9884 | } |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9885 | onlyHistory = true; |
| 9886 | dumpPackage = null; |
| 9887 | } |
| 9888 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9889 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9890 | if (!onlyHistory && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9891 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9892 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9893 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 9894 | while (it.hasNext()) { |
| 9895 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9896 | if (dumpPackage != null && (r.app == null || |
| 9897 | !dumpPackage.equals(r.app.info.packageName))) { |
| 9898 | continue; |
| 9899 | } |
| 9900 | if (!printed) { |
| 9901 | pw.println(" Registered Receivers:"); |
| 9902 | needSep = true; |
| 9903 | printed = true; |
| 9904 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9905 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9906 | r.dump(pw, " "); |
| 9907 | } |
| 9908 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9909 | |
| 9910 | if (mReceiverResolver.dump(pw, needSep ? |
| 9911 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9912 | " ", dumpPackage, false)) { |
| 9913 | needSep = true; |
| 9914 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9915 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9916 | |
| 9917 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9918 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9919 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9920 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9921 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9922 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9923 | if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9924 | for (int user=0; user<mStickyBroadcasts.size(); user++) { |
| 9925 | if (needSep) { |
| 9926 | pw.println(); |
| 9927 | } |
| 9928 | needSep = true; |
| 9929 | pw.print(" Sticky broadcasts for user "); |
| 9930 | pw.print(mStickyBroadcasts.keyAt(user)); pw.println(":"); |
| 9931 | StringBuilder sb = new StringBuilder(128); |
| 9932 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9933 | : mStickyBroadcasts.valueAt(user).entrySet()) { |
| 9934 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
| 9935 | if (dumpAll) { |
| 9936 | pw.println(":"); |
| 9937 | ArrayList<Intent> intents = ent.getValue(); |
| 9938 | final int N = intents.size(); |
| 9939 | for (int i=0; i<N; i++) { |
| 9940 | sb.setLength(0); |
| 9941 | sb.append(" Intent: "); |
| 9942 | intents.get(i).toShortString(sb, false, true, false, false); |
| 9943 | pw.println(sb.toString()); |
| 9944 | Bundle bundle = intents.get(i).getExtras(); |
| 9945 | if (bundle != null) { |
| 9946 | pw.print(" "); |
| 9947 | pw.println(bundle.toString()); |
| 9948 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9949 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9950 | } else { |
| 9951 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9952 | } |
| 9953 | } |
| 9954 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9955 | } |
| 9956 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9957 | if (!onlyHistory && dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9958 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9959 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9960 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9961 | + queue.mBroadcastsScheduled); |
| 9962 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9963 | pw.println(" mHandler:"); |
| 9964 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9965 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9966 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9967 | |
| 9968 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9969 | } |
| 9970 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9971 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9972 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 9973 | boolean needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9974 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9975 | ItemMatcher matcher = new ItemMatcher(); |
| 9976 | matcher.build(args, opti); |
| 9977 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9978 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9979 | |
| 9980 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9981 | |
| 9982 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9983 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9984 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9985 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9986 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9987 | continue; |
| 9988 | } |
| 9989 | if (!printed) { |
| 9990 | if (needSep) pw.println(" "); |
| 9991 | needSep = true; |
| 9992 | pw.println(" Launching content providers:"); |
| 9993 | printed = true; |
| 9994 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9995 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9996 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9997 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9998 | } |
| 9999 | |
| 10000 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10001 | if (needSep) pw.println(); |
| 10002 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10003 | pw.println("Granted Uri Permissions:"); |
| 10004 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 10005 | int uid = mGrantedUriPermissions.keyAt(i); |
| 10006 | HashMap<Uri, UriPermission> perms |
| 10007 | = mGrantedUriPermissions.valueAt(i); |
| 10008 | pw.print(" * UID "); pw.print(uid); |
| 10009 | pw.println(" holds:"); |
| 10010 | for (UriPermission perm : perms.values()) { |
| 10011 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10012 | if (dumpAll) { |
| 10013 | perm.dump(pw, " "); |
| 10014 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10015 | } |
| 10016 | } |
| 10017 | needSep = true; |
| 10018 | } |
| 10019 | |
| 10020 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10021 | } |
| 10022 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10023 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10024 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10025 | boolean needSep = false; |
| 10026 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10027 | if (mIntentSenderRecords.size() > 0) { |
| 10028 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10029 | Iterator<WeakReference<PendingIntentRecord>> it |
| 10030 | = mIntentSenderRecords.values().iterator(); |
| 10031 | while (it.hasNext()) { |
| 10032 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 10033 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10034 | if (dumpPackage != null && (rec == null |
| 10035 | || !dumpPackage.equals(rec.key.packageName))) { |
| 10036 | continue; |
| 10037 | } |
| 10038 | if (!printed) { |
| 10039 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 10040 | printed = true; |
| 10041 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10042 | needSep = true; |
| 10043 | if (rec != null) { |
| 10044 | pw.print(" * "); pw.println(rec); |
| 10045 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10046 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10047 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10048 | } else { |
| 10049 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10050 | } |
| 10051 | } |
| 10052 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 10053 | |
| 10054 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10055 | } |
| 10056 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10057 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10058 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 10059 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10060 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10061 | boolean needNL = false; |
| 10062 | final String innerPrefix = prefix + " "; |
| 10063 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10064 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10065 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10066 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 10067 | continue; |
| 10068 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 10069 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10070 | if (needNL) { |
| 10071 | pw.println(" "); |
| 10072 | needNL = false; |
| 10073 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10074 | if (lastTask != r.task) { |
| 10075 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 10076 | pw.print(prefix); |
| 10077 | pw.print(full ? "* " : " "); |
| 10078 | pw.println(lastTask); |
| 10079 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10080 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10081 | } else if (complete) { |
| 10082 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 10083 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 10084 | pw.print(prefix); pw.print(" "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 10085 | pw.println(lastTask.intent.toInsecureStringWithClip()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10086 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10088 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 10089 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 10090 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 10091 | pw.println(r); |
| 10092 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10093 | r.dump(pw, innerPrefix); |
| 10094 | } else if (complete) { |
| 10095 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 10096 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10097 | if (r.app != null) { |
| 10098 | pw.print(innerPrefix); pw.println(r.app); |
| 10099 | } |
| 10100 | } |
| 10101 | if (client && r.app != null && r.app.thread != null) { |
| 10102 | // flush anything that is already in the PrintWriter since the thread is going |
| 10103 | // to write to the file descriptor directly |
| 10104 | pw.flush(); |
| 10105 | try { |
| 10106 | TransferPipe tp = new TransferPipe(); |
| 10107 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 10108 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 10109 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10110 | // Short timeout, since blocking here can |
| 10111 | // deadlock with the application. |
| 10112 | tp.go(fd, 2000); |
| 10113 | } finally { |
| 10114 | tp.kill(); |
| 10115 | } |
| 10116 | } catch (IOException e) { |
| 10117 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 10118 | } catch (RemoteException e) { |
| 10119 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 10120 | } |
| 10121 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10123 | } |
| 10124 | } |
| 10125 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 10126 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 10127 | if (val == base) { |
| 10128 | if (space == null) return prefix; |
| 10129 | return prefix + " "; |
| 10130 | } |
| 10131 | return prefix + "+" + Integer.toString(val-base); |
| 10132 | } |
| 10133 | |
| 10134 | private static final int dumpProcessList(PrintWriter pw, |
| 10135 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10136 | String prefix, String normalLabel, String persistentLabel, |
| 10137 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10138 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 10139 | final int N = list.size()-1; |
| 10140 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10141 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10142 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 10143 | continue; |
| 10144 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10145 | pw.println(String.format("%s%s #%2d: %s", |
| 10146 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 10147 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10148 | if (r.persistent) { |
| 10149 | numPers++; |
| 10150 | } |
| 10151 | } |
| 10152 | return numPers; |
| 10153 | } |
| 10154 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10155 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10156 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10157 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10158 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10159 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10160 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 10161 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 10162 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10163 | ProcessRecord r = origList.get(i); |
| 10164 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 10165 | continue; |
| 10166 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10167 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 10168 | } |
| 10169 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10170 | if (list.size() <= 0) { |
| 10171 | return false; |
| 10172 | } |
| 10173 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10174 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 10175 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 10176 | @Override |
| 10177 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 10178 | Pair<ProcessRecord, Integer> object2) { |
| 10179 | if (object1.first.setAdj != object2.first.setAdj) { |
| 10180 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 10181 | } |
| 10182 | if (object1.second.intValue() != object2.second.intValue()) { |
| 10183 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 10184 | } |
| 10185 | return 0; |
| 10186 | } |
| 10187 | }; |
| 10188 | |
| 10189 | Collections.sort(list, comparator); |
| 10190 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10191 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 10192 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 10193 | final long curUptime = SystemClock.uptimeMillis(); |
| 10194 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 10195 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10196 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10197 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10198 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10199 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10200 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10201 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 10202 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10203 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 10204 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10205 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 10206 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10207 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 10208 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10209 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10210 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10211 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 10212 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 10213 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 10214 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 10215 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 10216 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 10217 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10218 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10219 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 10220 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10221 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 10222 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10223 | } else { |
| 10224 | oomAdj = Integer.toString(r.setAdj); |
| 10225 | } |
| 10226 | String schedGroup; |
| 10227 | switch (r.setSchedGroup) { |
| 10228 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 10229 | schedGroup = "B"; |
| 10230 | break; |
| 10231 | case Process.THREAD_GROUP_DEFAULT: |
| 10232 | schedGroup = "F"; |
| 10233 | break; |
| 10234 | default: |
| 10235 | schedGroup = Integer.toString(r.setSchedGroup); |
| 10236 | break; |
| 10237 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10238 | String foreground; |
| 10239 | if (r.foregroundActivities) { |
| 10240 | foreground = "A"; |
| 10241 | } else if (r.foregroundServices) { |
| 10242 | foreground = "S"; |
| 10243 | } else { |
| 10244 | foreground = " "; |
| 10245 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 10246 | 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] | 10247 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10248 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 10249 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10250 | if (r.adjSource != null || r.adjTarget != null) { |
| 10251 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10252 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10253 | if (r.adjTarget instanceof ComponentName) { |
| 10254 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 10255 | } else if (r.adjTarget != null) { |
| 10256 | pw.print(r.adjTarget.toString()); |
| 10257 | } else { |
| 10258 | pw.print("{null}"); |
| 10259 | } |
| 10260 | pw.print("<="); |
| 10261 | if (r.adjSource instanceof ProcessRecord) { |
| 10262 | pw.print("Proc{"); |
| 10263 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 10264 | pw.println("}"); |
| 10265 | } else if (r.adjSource != null) { |
| 10266 | pw.println(r.adjSource.toString()); |
| 10267 | } else { |
| 10268 | pw.println("{null}"); |
| 10269 | } |
| 10270 | } |
| 10271 | if (inclDetails) { |
| 10272 | pw.print(prefix); |
| 10273 | pw.print(" "); |
| 10274 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 10275 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 10276 | pw.print(" client="); pw.print(r.clientHiddenAdj); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 10277 | pw.print(" empty="); pw.print(r.emptyAdj); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10278 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 10279 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 10280 | pw.print(" cur="); pw.print(r.curAdj); |
| 10281 | pw.print(" set="); pw.println(r.setAdj); |
| 10282 | pw.print(prefix); |
| 10283 | pw.print(" "); |
| 10284 | pw.print("keeping="); pw.print(r.keeping); |
| 10285 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10286 | pw.print(" empty="); pw.print(r.empty); |
| 10287 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10288 | |
| 10289 | if (!r.keeping) { |
| 10290 | if (r.lastWakeTime != 0) { |
| 10291 | long wtime; |
| 10292 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 10293 | synchronized (stats) { |
| 10294 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 10295 | r.pid, curRealtime); |
| 10296 | } |
| 10297 | long timeUsed = wtime - r.lastWakeTime; |
| 10298 | pw.print(prefix); |
| 10299 | pw.print(" "); |
| 10300 | pw.print("keep awake over "); |
| 10301 | TimeUtils.formatDuration(realtimeSince, pw); |
| 10302 | pw.print(" used "); |
| 10303 | TimeUtils.formatDuration(timeUsed, pw); |
| 10304 | pw.print(" ("); |
| 10305 | pw.print((timeUsed*100)/realtimeSince); |
| 10306 | pw.println("%)"); |
| 10307 | } |
| 10308 | if (r.lastCpuTime != 0) { |
| 10309 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 10310 | pw.print(prefix); |
| 10311 | pw.print(" "); |
| 10312 | pw.print("run cpu over "); |
| 10313 | TimeUtils.formatDuration(uptimeSince, pw); |
| 10314 | pw.print(" used "); |
| 10315 | TimeUtils.formatDuration(timeUsed, pw); |
| 10316 | pw.print(" ("); |
| 10317 | pw.print((timeUsed*100)/uptimeSince); |
| 10318 | pw.println("%)"); |
| 10319 | } |
| 10320 | } |
| 10321 | } |
| 10322 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10323 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10324 | } |
| 10325 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10326 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10327 | ArrayList<ProcessRecord> procs; |
| 10328 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10329 | if (args != null && args.length > start |
| 10330 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10331 | procs = new ArrayList<ProcessRecord>(); |
| 10332 | int pid = -1; |
| 10333 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10334 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10335 | } catch (NumberFormatException e) { |
| 10336 | |
| 10337 | } |
| 10338 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 10339 | ProcessRecord proc = mLruProcesses.get(i); |
| 10340 | if (proc.pid == pid) { |
| 10341 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10342 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10343 | procs.add(proc); |
| 10344 | } |
| 10345 | } |
| 10346 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10347 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10348 | return null; |
| 10349 | } |
| 10350 | } else { |
| 10351 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 10352 | } |
| 10353 | } |
| 10354 | return procs; |
| 10355 | } |
| 10356 | |
| 10357 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 10358 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10359 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10360 | if (procs == null) { |
| 10361 | return; |
| 10362 | } |
| 10363 | |
| 10364 | long uptime = SystemClock.uptimeMillis(); |
| 10365 | long realtime = SystemClock.elapsedRealtime(); |
| 10366 | pw.println("Applications Graphics Acceleration Info:"); |
| 10367 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10368 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10369 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10370 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10371 | if (r.thread != null) { |
| 10372 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10373 | pw.flush(); |
| 10374 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10375 | TransferPipe tp = new TransferPipe(); |
| 10376 | try { |
| 10377 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10378 | tp.go(fd); |
| 10379 | } finally { |
| 10380 | tp.kill(); |
| 10381 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10382 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10383 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10384 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10385 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10386 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10387 | pw.flush(); |
| 10388 | } |
| 10389 | } |
| 10390 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10391 | } |
| 10392 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 10393 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 10394 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 10395 | if (procs == null) { |
| 10396 | return; |
| 10397 | } |
| 10398 | |
| 10399 | pw.println("Applications Database Info:"); |
| 10400 | |
| 10401 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10402 | ProcessRecord r = procs.get(i); |
| 10403 | if (r.thread != null) { |
| 10404 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10405 | pw.flush(); |
| 10406 | try { |
| 10407 | TransferPipe tp = new TransferPipe(); |
| 10408 | try { |
| 10409 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10410 | tp.go(fd); |
| 10411 | } finally { |
| 10412 | tp.kill(); |
| 10413 | } |
| 10414 | } catch (IOException e) { |
| 10415 | pw.println("Failure while dumping the app: " + r); |
| 10416 | pw.flush(); |
| 10417 | } catch (RemoteException e) { |
| 10418 | pw.println("Got a RemoteException while dumping the app " + r); |
| 10419 | pw.flush(); |
| 10420 | } |
| 10421 | } |
| 10422 | } |
| 10423 | } |
| 10424 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10425 | final static class MemItem { |
| 10426 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10427 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10428 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10429 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10430 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10431 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10432 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10433 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10434 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10435 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10436 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10437 | } |
| 10438 | } |
| 10439 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10440 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10441 | boolean sort) { |
| 10442 | if (sort) { |
| 10443 | Collections.sort(items, new Comparator<MemItem>() { |
| 10444 | @Override |
| 10445 | public int compare(MemItem lhs, MemItem rhs) { |
| 10446 | if (lhs.pss < rhs.pss) { |
| 10447 | return 1; |
| 10448 | } else if (lhs.pss > rhs.pss) { |
| 10449 | return -1; |
| 10450 | } |
| 10451 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10452 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10453 | }); |
| 10454 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10455 | |
| 10456 | for (int i=0; i<items.size(); i++) { |
| 10457 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10458 | 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] | 10459 | if (mi.subitems != null) { |
| 10460 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 10461 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10462 | } |
| 10463 | } |
| 10464 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10465 | // These are in KB. |
| 10466 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 10467 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 10468 | 120*1024, 160*1024, 200*1024, |
| 10469 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 10470 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 10471 | }; |
| 10472 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10473 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 10474 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10475 | int start = label.lastIndexOf('.'); |
| 10476 | if (start >= 0) start++; |
| 10477 | else start = 0; |
| 10478 | int end = label.length(); |
| 10479 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 10480 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 10481 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 10482 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10483 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10484 | out.append(label, start, end); |
| 10485 | return; |
| 10486 | } |
| 10487 | } |
| 10488 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10489 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10490 | out.append(label, start, end); |
| 10491 | } |
| 10492 | |
| 10493 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 10494 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 10495 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 10496 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 10497 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 10498 | }; |
| 10499 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 10500 | "System", "Persistent", "Foreground", |
| 10501 | "Visible", "Perceptible", "Heavy Weight", |
| 10502 | "Backup", "A Services", "Home", "Previous", |
| 10503 | "B Services", "Background" |
| 10504 | }; |
| 10505 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10506 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10507 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10508 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10509 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10510 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10511 | |
| 10512 | int opti = 0; |
| 10513 | while (opti < args.length) { |
| 10514 | String opt = args[opti]; |
| 10515 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 10516 | break; |
| 10517 | } |
| 10518 | opti++; |
| 10519 | if ("-a".equals(opt)) { |
| 10520 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10521 | } else if ("--oom".equals(opt)) { |
| 10522 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10523 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10524 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10525 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10526 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10527 | pw.println("If [process] is specified it can be the name or "); |
| 10528 | pw.println("pid of a specific process to dump."); |
| 10529 | return; |
| 10530 | } else { |
| 10531 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 10532 | } |
| 10533 | } |
| 10534 | |
| 10535 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10536 | if (procs == null) { |
| 10537 | return; |
| 10538 | } |
| 10539 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 10540 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10541 | long uptime = SystemClock.uptimeMillis(); |
| 10542 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10543 | |
| 10544 | if (procs.size() == 1 || isCheckinRequest) { |
| 10545 | dumpAll = true; |
| 10546 | } |
| 10547 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10548 | if (isCheckinRequest) { |
| 10549 | // short checkin version |
| 10550 | pw.println(uptime + "," + realtime); |
| 10551 | pw.flush(); |
| 10552 | } else { |
| 10553 | pw.println("Applications Memory Usage (kB):"); |
| 10554 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10555 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10556 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10557 | String[] innerArgs = new String[args.length-opti]; |
| 10558 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 10559 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10560 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 10561 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 10562 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 10563 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10564 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 10565 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 10566 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10567 | |
| 10568 | long totalPss = 0; |
| 10569 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10570 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10571 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10572 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10573 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10574 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 10575 | pw.flush(); |
| 10576 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10577 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10578 | if (dumpAll) { |
| 10579 | try { |
| 10580 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 10581 | } catch (RemoteException e) { |
| 10582 | if (!isCheckinRequest) { |
| 10583 | pw.println("Got RemoteException!"); |
| 10584 | pw.flush(); |
| 10585 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10586 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10587 | } else { |
| 10588 | mi = new Debug.MemoryInfo(); |
| 10589 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10590 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10591 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10592 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10593 | long myTotalPss = mi.getTotalPss(); |
| 10594 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10595 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10596 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10597 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10598 | |
| 10599 | nativePss += mi.nativePss; |
| 10600 | dalvikPss += mi.dalvikPss; |
| 10601 | otherPss += mi.otherPss; |
| 10602 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 10603 | long mem = mi.getOtherPss(j); |
| 10604 | miscPss[j] += mem; |
| 10605 | otherPss -= mem; |
| 10606 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10607 | |
| 10608 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10609 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 10610 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10611 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10612 | if (oomProcs[oomIndex] == null) { |
| 10613 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 10614 | } |
| 10615 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10616 | break; |
| 10617 | } |
| 10618 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10619 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10620 | } |
| 10621 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10622 | |
| 10623 | if (!isCheckinRequest && procs.size() > 1) { |
| 10624 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 10625 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10626 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10627 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10628 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10629 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10630 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10631 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10632 | } |
| 10633 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10634 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10635 | for (int j=0; j<oomPss.length; j++) { |
| 10636 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10637 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10638 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10639 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10640 | item.subitems = oomProcs[j]; |
| 10641 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10642 | } |
| 10643 | } |
| 10644 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10645 | if (outTag != null || outStack != null) { |
| 10646 | if (outTag != null) { |
| 10647 | appendMemBucket(outTag, totalPss, "total", false); |
| 10648 | } |
| 10649 | if (outStack != null) { |
| 10650 | appendMemBucket(outStack, totalPss, "total", true); |
| 10651 | } |
| 10652 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10653 | for (int i=0; i<oomMems.size(); i++) { |
| 10654 | MemItem miCat = oomMems.get(i); |
| 10655 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10656 | continue; |
| 10657 | } |
| 10658 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10659 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10660 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10661 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10662 | outTag.append(" / "); |
| 10663 | } |
| 10664 | if (outStack != null) { |
| 10665 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10666 | if (firstLine) { |
| 10667 | outStack.append(":"); |
| 10668 | firstLine = false; |
| 10669 | } |
| 10670 | outStack.append("\n\t at "); |
| 10671 | } else { |
| 10672 | outStack.append("$"); |
| 10673 | } |
| 10674 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10675 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10676 | MemItem mi = miCat.subitems.get(j); |
| 10677 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10678 | if (outTag != null) { |
| 10679 | outTag.append(" "); |
| 10680 | } |
| 10681 | if (outStack != null) { |
| 10682 | outStack.append("$"); |
| 10683 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10684 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10685 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10686 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10687 | } |
| 10688 | if (outStack != null) { |
| 10689 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10690 | } |
| 10691 | } |
| 10692 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10693 | outStack.append("("); |
| 10694 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10695 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10696 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10697 | outStack.append(":"); |
| 10698 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10699 | } |
| 10700 | } |
| 10701 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10702 | } |
| 10703 | } |
| 10704 | } |
| 10705 | } |
| 10706 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10707 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10708 | pw.println(); |
| 10709 | pw.println("Total PSS by process:"); |
| 10710 | dumpMemItems(pw, " ", procMems, true); |
| 10711 | pw.println(); |
| 10712 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10713 | pw.println("Total PSS by OOM adjustment:"); |
| 10714 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10715 | if (!oomOnly) { |
| 10716 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10717 | out.println(); |
| 10718 | out.println("Total PSS by category:"); |
| 10719 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10720 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10721 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10722 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | d3e677b | 2012-04-05 14:58:18 -0700 | [diff] [blame] | 10723 | final int[] SINGLE_LONG_FORMAT = new int[] { |
| 10724 | Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG |
| 10725 | }; |
| 10726 | long[] longOut = new long[1]; |
| 10727 | Process.readProcFile("/sys/kernel/mm/ksm/pages_shared", |
| 10728 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10729 | long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10730 | longOut[0] = 0; |
| 10731 | Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing", |
| 10732 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10733 | long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10734 | longOut[0] = 0; |
| 10735 | Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared", |
| 10736 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10737 | long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10738 | longOut[0] = 0; |
| 10739 | Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile", |
| 10740 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10741 | long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10742 | pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared "); |
| 10743 | pw.print(shared); pw.println(" kB"); |
| 10744 | pw.print(" "); pw.print(unshared); pw.print(" kB unshared; "); |
| 10745 | pw.print(voltile); pw.println(" kB volatile"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10746 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10747 | } |
| 10748 | |
| 10749 | /** |
| 10750 | * Searches array of arguments for the specified string |
| 10751 | * @param args array of argument strings |
| 10752 | * @param value value to search for |
| 10753 | * @return true if the value is contained in the array |
| 10754 | */ |
| 10755 | private static boolean scanArgs(String[] args, String value) { |
| 10756 | if (args != null) { |
| 10757 | for (String arg : args) { |
| 10758 | if (value.equals(arg)) { |
| 10759 | return true; |
| 10760 | } |
| 10761 | } |
| 10762 | } |
| 10763 | return false; |
| 10764 | } |
| 10765 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10766 | private final boolean removeDyingProviderLocked(ProcessRecord proc, |
| 10767 | ContentProviderRecord cpr, boolean always) { |
| 10768 | final boolean inLaunching = mLaunchingProviders.contains(cpr); |
| 10769 | |
| 10770 | if (!inLaunching || always) { |
| 10771 | synchronized (cpr) { |
| 10772 | cpr.launchingApp = null; |
| 10773 | cpr.notifyAll(); |
| 10774 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10775 | mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid)); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10776 | String names[] = cpr.info.authority.split(";"); |
| 10777 | for (int j = 0; j < names.length; j++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10778 | mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10779 | } |
| 10780 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10781 | |
| 10782 | for (int i=0; i<cpr.connections.size(); i++) { |
| 10783 | ContentProviderConnection conn = cpr.connections.get(i); |
| 10784 | if (conn.waiting) { |
| 10785 | // If this connection is waiting for the provider, then we don't |
| 10786 | // need to mess with its process unless we are always removing |
| 10787 | // or for some reason the provider is not currently launching. |
| 10788 | if (inLaunching && !always) { |
| 10789 | continue; |
| 10790 | } |
| 10791 | } |
| 10792 | ProcessRecord capp = conn.client; |
| 10793 | conn.dead = true; |
| 10794 | if (conn.stableCount > 0) { |
| 10795 | if (!capp.persistent && capp.thread != null |
| 10796 | && capp.pid != 0 |
| 10797 | && capp.pid != MY_PID) { |
| 10798 | Slog.i(TAG, "Kill " + capp.processName |
| 10799 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 10800 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 10801 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.userId, capp.pid, |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10802 | capp.processName, capp.setAdj, "dying provider " |
| 10803 | + cpr.name.toShortString()); |
| 10804 | Process.killProcessQuiet(capp.pid); |
| 10805 | } |
| 10806 | } else if (capp.thread != null && conn.provider.provider != null) { |
| 10807 | try { |
| 10808 | capp.thread.unstableProviderDied(conn.provider.provider.asBinder()); |
| 10809 | } catch (RemoteException e) { |
| 10810 | } |
| 10811 | // In the protocol here, we don't expect the client to correctly |
| 10812 | // clean up this connection, we'll just remove it. |
| 10813 | cpr.connections.remove(i); |
| 10814 | conn.client.conProviders.remove(conn); |
| 10815 | } |
| 10816 | } |
| 10817 | |
| 10818 | if (inLaunching && always) { |
| 10819 | mLaunchingProviders.remove(cpr); |
| 10820 | } |
| 10821 | return inLaunching; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10822 | } |
| 10823 | |
| 10824 | /** |
| 10825 | * Main code for cleaning up a process when it has gone away. This is |
| 10826 | * called both as a result of the process dying, or directly when stopping |
| 10827 | * a process when running in single process mode. |
| 10828 | */ |
| 10829 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10830 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10831 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10832 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10833 | } |
| 10834 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10835 | mProcessesToGc.remove(app); |
| 10836 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10837 | // Dismiss any open dialogs. |
| 10838 | if (app.crashDialog != null) { |
| 10839 | app.crashDialog.dismiss(); |
| 10840 | app.crashDialog = null; |
| 10841 | } |
| 10842 | if (app.anrDialog != null) { |
| 10843 | app.anrDialog.dismiss(); |
| 10844 | app.anrDialog = null; |
| 10845 | } |
| 10846 | if (app.waitDialog != null) { |
| 10847 | app.waitDialog.dismiss(); |
| 10848 | app.waitDialog = null; |
| 10849 | } |
| 10850 | |
| 10851 | app.crashing = false; |
| 10852 | app.notResponding = false; |
| 10853 | |
| 10854 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10855 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10856 | app.thread = null; |
| 10857 | app.forcingToForeground = null; |
| 10858 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10859 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10860 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10861 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10862 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10863 | mServices.killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10864 | |
| 10865 | boolean restart = false; |
| 10866 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10867 | // Remove published content providers. |
| 10868 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10869 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10870 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10871 | ContentProviderRecord cpr = it.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10872 | |
| 10873 | final boolean always = app.bad || !allowRestart; |
| 10874 | if (removeDyingProviderLocked(app, cpr, always) || always) { |
| 10875 | // We left the provider in the launching list, need to |
| 10876 | // restart it. |
| 10877 | restart = true; |
| 10878 | } |
| 10879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10880 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10881 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10882 | } |
| 10883 | app.pubProviders.clear(); |
| 10884 | } |
| 10885 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10886 | // Take care of any launching providers waiting for this process. |
| 10887 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10888 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10889 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10890 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10891 | // Unregister from connected content providers. |
| 10892 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10893 | for (int i=0; i<app.conProviders.size(); i++) { |
| 10894 | ContentProviderConnection conn = app.conProviders.get(i); |
| 10895 | conn.provider.connections.remove(conn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10896 | } |
| 10897 | app.conProviders.clear(); |
| 10898 | } |
| 10899 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10900 | // At this point there may be remaining entries in mLaunchingProviders |
| 10901 | // where we were the only one waiting, so they are no longer of use. |
| 10902 | // Look for these and clean up if found. |
| 10903 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10904 | // the actual situation to identify what is actually going on. |
| 10905 | if (false) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10906 | for (int i=0; i<mLaunchingProviders.size(); i++) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10907 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10908 | mLaunchingProviders.get(i); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10909 | if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10910 | synchronized (cpr) { |
| 10911 | cpr.launchingApp = null; |
| 10912 | cpr.notifyAll(); |
| 10913 | } |
| 10914 | } |
| 10915 | } |
| 10916 | } |
| 10917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10918 | skipCurrentReceiverLocked(app); |
| 10919 | |
| 10920 | // Unregister any receivers. |
| 10921 | if (app.receivers.size() > 0) { |
| 10922 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10923 | while (it.hasNext()) { |
| 10924 | removeReceiverLocked(it.next()); |
| 10925 | } |
| 10926 | app.receivers.clear(); |
| 10927 | } |
| 10928 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10929 | // If the app is undergoing backup, tell the backup manager about it |
| 10930 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10931 | if (DEBUG_BACKUP || DEBUG_CLEANUP) Slog.d(TAG, "App " |
| 10932 | + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10933 | try { |
| 10934 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10935 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10936 | bm.agentDisconnected(app.info.packageName); |
| 10937 | } catch (RemoteException e) { |
| 10938 | // can't happen; backup manager is local |
| 10939 | } |
| 10940 | } |
| 10941 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 10942 | for (int i = mPendingProcessChanges.size()-1; i>=0; i--) { |
| 10943 | ProcessChangeItem item = mPendingProcessChanges.get(i); |
| 10944 | if (item.pid == app.pid) { |
| 10945 | mPendingProcessChanges.remove(i); |
| 10946 | mAvailProcessChanges.add(item); |
| 10947 | } |
| 10948 | } |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10949 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10950 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10951 | // If the caller is restarting this app, then leave it in its |
| 10952 | // current lists and let the caller take care of it. |
| 10953 | if (restarting) { |
| 10954 | return; |
| 10955 | } |
| 10956 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10957 | if (!app.persistent || app.isolated) { |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10958 | if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10959 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10960 | mProcessNames.remove(app.processName, app.uid); |
| 10961 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10962 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10963 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 10964 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10965 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10966 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10967 | } else if (!app.removed) { |
| 10968 | // This app is persistent, so we need to keep its record around. |
| 10969 | // If it is not already on the pending app list, add it there |
| 10970 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10971 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10972 | mPersistentStartingProcesses.add(app); |
| 10973 | restart = true; |
| 10974 | } |
| 10975 | } |
Dianne Hackborn | cc5a055 | 2012-10-01 16:32:39 -0700 | [diff] [blame] | 10976 | if ((DEBUG_PROCESSES || DEBUG_CLEANUP) && mProcessesOnHold.contains(app)) Slog.v(TAG, |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10977 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10978 | mProcessesOnHold.remove(app); |
| 10979 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10980 | if (app == mHomeProcess) { |
| 10981 | mHomeProcess = null; |
| 10982 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10983 | if (app == mPreviousProcess) { |
| 10984 | mPreviousProcess = null; |
| 10985 | } |
| 10986 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10987 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10988 | // We have components that still need to be running in the |
| 10989 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10990 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10991 | startProcessLocked(app, "restart", app.processName); |
| 10992 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10993 | // Goodbye! |
| 10994 | synchronized (mPidsSelfLocked) { |
| 10995 | mPidsSelfLocked.remove(app.pid); |
| 10996 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10997 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10998 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10999 | } |
| 11000 | } |
| 11001 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 11002 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 11003 | // Look through the content providers we are waiting to have launched, |
| 11004 | // and if any run in this process then either schedule a restart of |
| 11005 | // the process or kill the client waiting for it if this process has |
| 11006 | // gone bad. |
| 11007 | int NL = mLaunchingProviders.size(); |
| 11008 | boolean restart = false; |
| 11009 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 11010 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 11011 | if (cpr.launchingApp == app) { |
| 11012 | if (!alwaysBad && !app.bad) { |
| 11013 | restart = true; |
| 11014 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 11015 | removeDyingProviderLocked(app, cpr, true); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 11016 | // cpr should have been removed from mLaunchingProviders |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 11017 | NL = mLaunchingProviders.size(); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 11018 | i--; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 11019 | } |
| 11020 | } |
| 11021 | } |
| 11022 | return restart; |
| 11023 | } |
| 11024 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11025 | // ========================================================= |
| 11026 | // SERVICES |
| 11027 | // ========================================================= |
| 11028 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11029 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 11030 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11031 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11032 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11033 | return mServices.getRunningServiceInfoLocked(maxNum, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11034 | } |
| 11035 | } |
| 11036 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11037 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11038 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11039 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11040 | return mServices.getRunningServiceControlPanelLocked(name); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11041 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11042 | } |
| 11043 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11044 | public ComponentName startService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11045 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11046 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11047 | // Refuse possible leaked file descriptors |
| 11048 | if (service != null && service.hasFileDescriptors() == true) { |
| 11049 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11050 | } |
| 11051 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11052 | if (DEBUG_SERVICE) |
| 11053 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11054 | synchronized(this) { |
| 11055 | final int callingPid = Binder.getCallingPid(); |
| 11056 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11057 | checkValidCaller(callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11058 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11059 | ComponentName res = mServices.startServiceLocked(caller, service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11060 | resolvedType, callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11061 | Binder.restoreCallingIdentity(origId); |
| 11062 | return res; |
| 11063 | } |
| 11064 | } |
| 11065 | |
| 11066 | ComponentName startServiceInPackage(int uid, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11067 | Intent service, String resolvedType, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11068 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11069 | if (DEBUG_SERVICE) |
| 11070 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11071 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11072 | ComponentName res = mServices.startServiceLocked(null, service, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11073 | resolvedType, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11074 | Binder.restoreCallingIdentity(origId); |
| 11075 | return res; |
| 11076 | } |
| 11077 | } |
| 11078 | |
| 11079 | public int stopService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11080 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11081 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11082 | // Refuse possible leaked file descriptors |
| 11083 | if (service != null && service.hasFileDescriptors() == true) { |
| 11084 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11085 | } |
| 11086 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11087 | checkValidCaller(Binder.getCallingUid(), userId); |
| 11088 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11089 | synchronized(this) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 11090 | return mServices.stopServiceLocked(caller, service, resolvedType, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11091 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11092 | } |
| 11093 | |
| 11094 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11095 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11096 | // Refuse possible leaked file descriptors |
| 11097 | if (service != null && service.hasFileDescriptors() == true) { |
| 11098 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11099 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11100 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11101 | return mServices.peekServiceLocked(service, resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11102 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11103 | } |
| 11104 | |
| 11105 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 11106 | int startId) { |
| 11107 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11108 | return mServices.stopServiceTokenLocked(className, token, startId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11110 | } |
| 11111 | |
| 11112 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11113 | int id, Notification notification, boolean removeNotification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11114 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11115 | mServices.setServiceForegroundLocked(className, token, id, notification, |
| 11116 | removeNotification); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11117 | } |
| 11118 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11119 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11120 | public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 11121 | boolean requireFull, String name, String callerPackage) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11122 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 11123 | if (callingUserId != userId) { |
| 11124 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 11125 | if ((requireFull || checkComponentPermission( |
| 11126 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 11127 | callingPid, callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) |
| 11128 | && checkComponentPermission( |
| 11129 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 11130 | callingPid, callingUid, -1, true) |
| 11131 | != PackageManager.PERMISSION_GRANTED) { |
| 11132 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 11133 | // In this case, they would like to just execute as their |
| 11134 | // owner user instead of failing. |
| 11135 | userId = callingUserId; |
| 11136 | } else { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11137 | StringBuilder builder = new StringBuilder(128); |
| 11138 | builder.append("Permission Denial: "); |
| 11139 | builder.append(name); |
| 11140 | if (callerPackage != null) { |
| 11141 | builder.append(" from "); |
| 11142 | builder.append(callerPackage); |
| 11143 | } |
| 11144 | builder.append(" asks to run as user "); |
| 11145 | builder.append(userId); |
| 11146 | builder.append(" but is calling from user "); |
| 11147 | builder.append(UserHandle.getUserId(callingUid)); |
| 11148 | builder.append("; this requires "); |
| 11149 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 11150 | if (!requireFull) { |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 11151 | builder.append(" or "); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 11152 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 11153 | } |
| 11154 | String msg = builder.toString(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11155 | Slog.w(TAG, msg); |
| 11156 | throw new SecurityException(msg); |
| 11157 | } |
| 11158 | } |
| 11159 | } |
| 11160 | if (userId == UserHandle.USER_CURRENT |
| 11161 | || userId == UserHandle.USER_CURRENT_OR_SELF) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11162 | // Note that we may be accessing this outside of a lock... |
| 11163 | // shouldn't be a big deal, if this is being called outside |
| 11164 | // of a locked context there is intrinsically a race with |
| 11165 | // the value the caller will receive and someone else changing it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11166 | userId = mCurrentUserId; |
| 11167 | } |
| 11168 | if (!allowAll && userId < 0) { |
| 11169 | throw new IllegalArgumentException( |
| 11170 | "Call does not support special user #" + userId); |
| 11171 | } |
| 11172 | } |
| 11173 | return userId; |
| 11174 | } |
| 11175 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11176 | boolean isSingleton(String componentProcessName, ApplicationInfo aInfo, |
| 11177 | String className, int flags) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11178 | boolean result = false; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11179 | if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11180 | if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) { |
| 11181 | if (ActivityManager.checkUidPermission( |
| 11182 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 11183 | aInfo.uid) != PackageManager.PERMISSION_GRANTED) { |
| 11184 | ComponentName comp = new ComponentName(aInfo.packageName, className); |
| 11185 | String msg = "Permission Denial: Component " + comp.flattenToShortString() |
| 11186 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 11187 | + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 11188 | Slog.w(TAG, msg); |
| 11189 | throw new SecurityException(msg); |
| 11190 | } |
| 11191 | result = true; |
| 11192 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11193 | } else if (componentProcessName == aInfo.packageName) { |
| 11194 | result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 11195 | } else if ("system".equals(componentProcessName)) { |
| 11196 | result = true; |
| 11197 | } |
| 11198 | if (DEBUG_MU) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11199 | Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo |
| 11200 | + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11201 | } |
| 11202 | return result; |
| 11203 | } |
| 11204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11205 | public int bindService(IApplicationThread caller, IBinder token, |
| 11206 | Intent service, String resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 11207 | IServiceConnection connection, int flags, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11208 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11209 | // Refuse possible leaked file descriptors |
| 11210 | if (service != null && service.hasFileDescriptors() == true) { |
| 11211 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11212 | } |
| 11213 | |
| 11214 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11215 | return mServices.bindServiceLocked(caller, token, service, resolvedType, |
| 11216 | connection, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11217 | } |
| 11218 | } |
| 11219 | |
| 11220 | public boolean unbindService(IServiceConnection connection) { |
| 11221 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11222 | return mServices.unbindServiceLocked(connection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11223 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11224 | } |
| 11225 | |
| 11226 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 11227 | // Refuse possible leaked file descriptors |
| 11228 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11229 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11230 | } |
| 11231 | |
| 11232 | synchronized(this) { |
| 11233 | if (!(token instanceof ServiceRecord)) { |
| 11234 | throw new IllegalArgumentException("Invalid service token"); |
| 11235 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11236 | mServices.publishServiceLocked((ServiceRecord)token, intent, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11237 | } |
| 11238 | } |
| 11239 | |
| 11240 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 11241 | // Refuse possible leaked file descriptors |
| 11242 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11243 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11244 | } |
| 11245 | |
| 11246 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11247 | mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11248 | } |
| 11249 | } |
| 11250 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11251 | 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] | 11252 | synchronized(this) { |
| 11253 | if (!(token instanceof ServiceRecord)) { |
| 11254 | throw new IllegalArgumentException("Invalid service token"); |
| 11255 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11256 | mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11258 | } |
| 11259 | |
| 11260 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11261 | // BACKUP AND RESTORE |
| 11262 | // ========================================================= |
| 11263 | |
| 11264 | // Cause the target app to be launched if necessary and its backup agent |
| 11265 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11266 | // activity manager to announce its creation. |
| 11267 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11268 | if (DEBUG_BACKUP) Slog.v(TAG, "bindBackupAgent: app=" + app + " mode=" + backupMode); |
| 11269 | enforceCallingPermission("android.permission.BACKUP", "bindBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11270 | |
| 11271 | synchronized(this) { |
| 11272 | // !!! TODO: currently no check here that we're already bound |
| 11273 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11274 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11275 | synchronized (stats) { |
| 11276 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11277 | } |
| 11278 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11279 | // Backup agent is now in use, its package can't be stopped. |
| 11280 | try { |
| 11281 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11282 | app.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11283 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11284 | } catch (IllegalArgumentException e) { |
| 11285 | Slog.w(TAG, "Failed trying to unstop package " |
| 11286 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11287 | } |
| 11288 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11289 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11290 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11291 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11292 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11293 | // startProcessLocked() returns existing proc's record if it's already running |
| 11294 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11295 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11296 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11297 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11298 | return false; |
| 11299 | } |
| 11300 | |
| 11301 | r.app = proc; |
| 11302 | mBackupTarget = r; |
| 11303 | mBackupAppName = app.packageName; |
| 11304 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11305 | // Try not to kill the process during backup |
| 11306 | updateOomAdjLocked(proc); |
| 11307 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11308 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11309 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11310 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11311 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11312 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11313 | proc.thread.scheduleCreateBackupAgent(app, |
| 11314 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11315 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11316 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11317 | } |
| 11318 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11319 | 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] | 11320 | } |
| 11321 | // Invariants: at this point, the target app process exists and the application |
| 11322 | // is either already running or in the process of coming up. mBackupTarget and |
| 11323 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11324 | // know that it's scheduled for a backup-agent operation. |
| 11325 | } |
| 11326 | |
| 11327 | return true; |
| 11328 | } |
| 11329 | |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11330 | @Override |
| 11331 | public void clearPendingBackup() { |
| 11332 | if (DEBUG_BACKUP) Slog.v(TAG, "clearPendingBackup"); |
| 11333 | enforceCallingPermission("android.permission.BACKUP", "clearPendingBackup"); |
| 11334 | |
| 11335 | synchronized (this) { |
| 11336 | mBackupTarget = null; |
| 11337 | mBackupAppName = null; |
| 11338 | } |
| 11339 | } |
| 11340 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11341 | // A backup agent has just come up |
| 11342 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11343 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11344 | + " = " + agent); |
| 11345 | |
| 11346 | synchronized(this) { |
| 11347 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11348 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11349 | return; |
| 11350 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11351 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11352 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11353 | long oldIdent = Binder.clearCallingIdentity(); |
| 11354 | try { |
| 11355 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 11356 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 11357 | bm.agentConnected(agentPackageName, agent); |
| 11358 | } catch (RemoteException e) { |
| 11359 | // can't happen; the backup manager service is local |
| 11360 | } catch (Exception e) { |
| 11361 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 11362 | e.printStackTrace(); |
| 11363 | } finally { |
| 11364 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11365 | } |
| 11366 | } |
| 11367 | |
| 11368 | // done with this agent |
| 11369 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11370 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11371 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11372 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11373 | return; |
| 11374 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11375 | |
| 11376 | synchronized(this) { |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11377 | try { |
| 11378 | if (mBackupAppName == null) { |
| 11379 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
| 11380 | return; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11381 | } |
Christopher Tate | 346acb1 | 2012-10-15 19:20:25 -0700 | [diff] [blame] | 11382 | |
| 11383 | if (!mBackupAppName.equals(appInfo.packageName)) { |
| 11384 | Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target"); |
| 11385 | return; |
| 11386 | } |
| 11387 | |
| 11388 | // Not backing this app up any more; reset its OOM adjustment |
| 11389 | final ProcessRecord proc = mBackupTarget.app; |
| 11390 | updateOomAdjLocked(proc); |
| 11391 | |
| 11392 | // If the app crashed during backup, 'thread' will be null here |
| 11393 | if (proc.thread != null) { |
| 11394 | try { |
| 11395 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 11396 | compatibilityInfoForPackageLocked(appInfo)); |
| 11397 | } catch (Exception e) { |
| 11398 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
| 11399 | e.printStackTrace(); |
| 11400 | } |
| 11401 | } |
| 11402 | } finally { |
| 11403 | mBackupTarget = null; |
| 11404 | mBackupAppName = null; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11405 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11406 | } |
| 11407 | } |
| 11408 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11409 | // BROADCASTS |
| 11410 | // ========================================================= |
| 11411 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11412 | private final List getStickiesLocked(String action, IntentFilter filter, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11413 | List cur, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11414 | final ContentResolver resolver = mContext.getContentResolver(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11415 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11416 | if (stickies == null) { |
| 11417 | return cur; |
| 11418 | } |
| 11419 | final ArrayList<Intent> list = stickies.get(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11420 | if (list == null) { |
| 11421 | return cur; |
| 11422 | } |
| 11423 | int N = list.size(); |
| 11424 | for (int i=0; i<N; i++) { |
| 11425 | Intent intent = list.get(i); |
| 11426 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 11427 | if (cur == null) { |
| 11428 | cur = new ArrayList<Intent>(); |
| 11429 | } |
| 11430 | cur.add(intent); |
| 11431 | } |
| 11432 | } |
| 11433 | return cur; |
| 11434 | } |
| 11435 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11436 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 11437 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 11438 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 11439 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11440 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11441 | void skipPendingBroadcastLocked(int pid) { |
| 11442 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 11443 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11444 | queue.skipPendingBroadcastLocked(pid); |
| 11445 | } |
| 11446 | } |
| 11447 | |
| 11448 | // The app just attached; send any pending broadcasts that it should receive |
| 11449 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 11450 | boolean didSomething = false; |
| 11451 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11452 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11453 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11454 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11455 | } |
| 11456 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11457 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11458 | IIntentReceiver receiver, IntentFilter filter, String permission, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11459 | enforceNotIsolatedCaller("registerReceiver"); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11460 | int callingUid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11461 | int callingPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11462 | synchronized(this) { |
| 11463 | ProcessRecord callerApp = null; |
| 11464 | if (caller != null) { |
| 11465 | callerApp = getRecordForAppLocked(caller); |
| 11466 | if (callerApp == null) { |
| 11467 | throw new SecurityException( |
| 11468 | "Unable to find app for caller " + caller |
| 11469 | + " (pid=" + Binder.getCallingPid() |
| 11470 | + ") when registering receiver " + receiver); |
| 11471 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11472 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 11473 | !callerApp.pkgList.contains(callerPackage)) { |
| 11474 | throw new SecurityException("Given caller package " + callerPackage |
| 11475 | + " is not running in process " + callerApp); |
| 11476 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11477 | callingUid = callerApp.info.uid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11478 | callingPid = callerApp.pid; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11479 | } else { |
| 11480 | callerPackage = null; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11481 | callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11482 | callingPid = Binder.getCallingPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11483 | } |
| 11484 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11485 | userId = this.handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11486 | true, true, "registerReceiver", callerPackage); |
| 11487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11488 | List allSticky = null; |
| 11489 | |
| 11490 | // Look for any matching sticky broadcasts... |
| 11491 | Iterator actions = filter.actionsIterator(); |
| 11492 | if (actions != null) { |
| 11493 | while (actions.hasNext()) { |
| 11494 | String action = (String)actions.next(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11495 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11496 | UserHandle.USER_ALL); |
| 11497 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11498 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11499 | } |
| 11500 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11501 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11502 | UserHandle.USER_ALL); |
| 11503 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11504 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11505 | } |
| 11506 | |
| 11507 | // The first sticky in the list is returned directly back to |
| 11508 | // the client. |
| 11509 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 11510 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11511 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11512 | + ": " + sticky); |
| 11513 | |
| 11514 | if (receiver == null) { |
| 11515 | return sticky; |
| 11516 | } |
| 11517 | |
| 11518 | ReceiverList rl |
| 11519 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11520 | if (rl == null) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11521 | rl = new ReceiverList(this, callerApp, callingPid, callingUid, |
| 11522 | userId, receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11523 | if (rl.app != null) { |
| 11524 | rl.app.receivers.add(rl); |
| 11525 | } else { |
| 11526 | try { |
| 11527 | receiver.asBinder().linkToDeath(rl, 0); |
| 11528 | } catch (RemoteException e) { |
| 11529 | return sticky; |
| 11530 | } |
| 11531 | rl.linkedToDeath = true; |
| 11532 | } |
| 11533 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11534 | } else if (rl.uid != callingUid) { |
| 11535 | throw new IllegalArgumentException( |
| 11536 | "Receiver requested to register for uid " + callingUid |
| 11537 | + " was previously registered for uid " + rl.uid); |
| 11538 | } else if (rl.pid != callingPid) { |
| 11539 | throw new IllegalArgumentException( |
| 11540 | "Receiver requested to register for pid " + callingPid |
| 11541 | + " was previously registered for pid " + rl.pid); |
| 11542 | } else if (rl.userId != userId) { |
| 11543 | throw new IllegalArgumentException( |
| 11544 | "Receiver requested to register for user " + userId |
| 11545 | + " was previously registered for user " + rl.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11546 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11547 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11548 | permission, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11549 | rl.add(bf); |
| 11550 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11551 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11552 | } |
| 11553 | mReceiverResolver.addFilter(bf); |
| 11554 | |
| 11555 | // Enqueue broadcasts for all existing stickies that match |
| 11556 | // this filter. |
| 11557 | if (allSticky != null) { |
| 11558 | ArrayList receivers = new ArrayList(); |
| 11559 | receivers.add(bf); |
| 11560 | |
| 11561 | int N = allSticky.size(); |
| 11562 | for (int i=0; i<N; i++) { |
| 11563 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11564 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11565 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11566 | null, -1, -1, null, receivers, null, 0, null, null, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11567 | false, true, true, -1); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11568 | queue.enqueueParallelBroadcastLocked(r); |
| 11569 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11570 | } |
| 11571 | } |
| 11572 | |
| 11573 | return sticky; |
| 11574 | } |
| 11575 | } |
| 11576 | |
| 11577 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11578 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11579 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11580 | final long origId = Binder.clearCallingIdentity(); |
| 11581 | try { |
| 11582 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11583 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11584 | synchronized(this) { |
| 11585 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11586 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11587 | if (rl != null) { |
| 11588 | if (rl.curBroadcast != null) { |
| 11589 | BroadcastRecord r = rl.curBroadcast; |
| 11590 | final boolean doNext = finishReceiverLocked( |
| 11591 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11592 | r.resultExtras, r.resultAbort, true); |
| 11593 | if (doNext) { |
| 11594 | doTrim = true; |
| 11595 | r.queue.processNextBroadcast(false); |
| 11596 | } |
| 11597 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11598 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11599 | if (rl.app != null) { |
| 11600 | rl.app.receivers.remove(rl); |
| 11601 | } |
| 11602 | removeReceiverLocked(rl); |
| 11603 | if (rl.linkedToDeath) { |
| 11604 | rl.linkedToDeath = false; |
| 11605 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 11606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11607 | } |
| 11608 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11609 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11610 | // If we actually concluded any broadcasts, we might now be able |
| 11611 | // to trim the recipients' apps from our working set |
| 11612 | if (doTrim) { |
| 11613 | trimApplications(); |
| 11614 | return; |
| 11615 | } |
| 11616 | |
| 11617 | } finally { |
| 11618 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11619 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11620 | } |
| 11621 | |
| 11622 | void removeReceiverLocked(ReceiverList rl) { |
| 11623 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 11624 | int N = rl.size(); |
| 11625 | for (int i=0; i<N; i++) { |
| 11626 | mReceiverResolver.removeFilter(rl.get(i)); |
| 11627 | } |
| 11628 | } |
| 11629 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11630 | private final void sendPackageBroadcastLocked(int cmd, String[] packages, int userId) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11631 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 11632 | ProcessRecord r = mLruProcesses.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11633 | if (r.thread != null && (userId == UserHandle.USER_ALL || r.userId == userId)) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11634 | try { |
| 11635 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 11636 | } catch (RemoteException ex) { |
| 11637 | } |
| 11638 | } |
| 11639 | } |
| 11640 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11641 | |
| 11642 | private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType, |
| 11643 | int[] users) { |
| 11644 | List<ResolveInfo> receivers = null; |
| 11645 | try { |
| 11646 | HashSet<ComponentName> singleUserReceivers = null; |
| 11647 | boolean scannedFirstReceivers = false; |
| 11648 | for (int user : users) { |
| 11649 | List<ResolveInfo> newReceivers = AppGlobals.getPackageManager() |
| 11650 | .queryIntentReceivers(intent, resolvedType, STOCK_PM_FLAGS, user); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 11651 | if (user != 0 && newReceivers != null) { |
| 11652 | // If this is not the primary user, we need to check for |
| 11653 | // any receivers that should be filtered out. |
| 11654 | for (int i=0; i<newReceivers.size(); i++) { |
| 11655 | ResolveInfo ri = newReceivers.get(i); |
| 11656 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_PRIMARY_USER_ONLY) != 0) { |
| 11657 | newReceivers.remove(i); |
| 11658 | i--; |
| 11659 | } |
| 11660 | } |
| 11661 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11662 | if (newReceivers != null && newReceivers.size() == 0) { |
| 11663 | newReceivers = null; |
| 11664 | } |
| 11665 | if (receivers == null) { |
| 11666 | receivers = newReceivers; |
| 11667 | } else if (newReceivers != null) { |
| 11668 | // We need to concatenate the additional receivers |
| 11669 | // found with what we have do far. This would be easy, |
| 11670 | // but we also need to de-dup any receivers that are |
| 11671 | // singleUser. |
| 11672 | if (!scannedFirstReceivers) { |
| 11673 | // Collect any single user receivers we had already retrieved. |
| 11674 | scannedFirstReceivers = true; |
| 11675 | for (int i=0; i<receivers.size(); i++) { |
| 11676 | ResolveInfo ri = receivers.get(i); |
| 11677 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11678 | ComponentName cn = new ComponentName( |
| 11679 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11680 | if (singleUserReceivers == null) { |
| 11681 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11682 | } |
| 11683 | singleUserReceivers.add(cn); |
| 11684 | } |
| 11685 | } |
| 11686 | } |
| 11687 | // Add the new results to the existing results, tracking |
| 11688 | // and de-dupping single user receivers. |
| 11689 | for (int i=0; i<newReceivers.size(); i++) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 11690 | ResolveInfo ri = newReceivers.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11691 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11692 | ComponentName cn = new ComponentName( |
| 11693 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11694 | if (singleUserReceivers == null) { |
| 11695 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11696 | } |
| 11697 | if (!singleUserReceivers.contains(cn)) { |
| 11698 | singleUserReceivers.add(cn); |
| 11699 | receivers.add(ri); |
| 11700 | } |
| 11701 | } else { |
| 11702 | receivers.add(ri); |
| 11703 | } |
| 11704 | } |
| 11705 | } |
| 11706 | } |
| 11707 | } catch (RemoteException ex) { |
| 11708 | // pm is in same process, this will never happen. |
| 11709 | } |
| 11710 | return receivers; |
| 11711 | } |
| 11712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11713 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 11714 | String callerPackage, Intent intent, String resolvedType, |
| 11715 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 11716 | Bundle map, String requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11717 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 11718 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11719 | intent = new Intent(intent); |
| 11720 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11721 | // By default broadcasts do not go to stopped apps. |
| 11722 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 11723 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11724 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11725 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 11726 | + " ordered=" + ordered + " userid=" + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11727 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11728 | 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] | 11729 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11730 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11731 | userId = handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11732 | true, false, "broadcast", callerPackage); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11733 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11734 | // Make sure that the user who is receiving this broadcast is started. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11735 | // If not, we will just skip it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11736 | if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 11737 | if (callingUid != Process.SYSTEM_UID || (intent.getFlags() |
| 11738 | & Intent.FLAG_RECEIVER_BOOT_UPGRADE) == 0) { |
| 11739 | Slog.w(TAG, "Skipping broadcast of " + intent |
| 11740 | + ": user " + userId + " is stopped"); |
| 11741 | return ActivityManager.BROADCAST_SUCCESS; |
| 11742 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11743 | } |
| 11744 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11745 | /* |
| 11746 | * Prevent non-system code (defined here to be non-persistent |
| 11747 | * processes) from sending protected broadcasts. |
| 11748 | */ |
Dianne Hackborn | c7ba771 | 2012-09-26 23:22:59 -0700 | [diff] [blame] | 11749 | int callingAppId = UserHandle.getAppId(callingUid); |
| 11750 | if (callingAppId == Process.SYSTEM_UID || callingAppId == Process.PHONE_UID |
| 11751 | || callingAppId == Process.SHELL_UID || callingAppId == Process.BLUETOOTH_UID || |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11752 | callingUid == 0) { |
| 11753 | // Always okay. |
| 11754 | } else if (callerApp == null || !callerApp.persistent) { |
| 11755 | try { |
| 11756 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
| 11757 | intent.getAction())) { |
| 11758 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11759 | + intent.getAction() + " from pid=" |
| 11760 | + callingPid + ", uid=" + callingUid; |
| 11761 | Slog.w(TAG, msg); |
| 11762 | throw new SecurityException(msg); |
| 11763 | } |
| 11764 | } catch (RemoteException e) { |
| 11765 | Slog.w(TAG, "Remote exception", e); |
| 11766 | return ActivityManager.BROADCAST_SUCCESS; |
| 11767 | } |
| 11768 | } |
| 11769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11770 | // Handle special intents: if this broadcast is from the package |
| 11771 | // manager about a package being removed, we need to remove all of |
| 11772 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11773 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11774 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11775 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11776 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11777 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11778 | || uidRemoved) { |
| 11779 | if (checkComponentPermission( |
| 11780 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11781 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11782 | == PackageManager.PERMISSION_GRANTED) { |
| 11783 | if (uidRemoved) { |
| 11784 | final Bundle intentExtras = intent.getExtras(); |
| 11785 | final int uid = intentExtras != null |
| 11786 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11787 | if (uid >= 0) { |
| 11788 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11789 | synchronized (bs) { |
| 11790 | bs.removeUidStatsLocked(uid); |
| 11791 | } |
| 11792 | } |
| 11793 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11794 | // If resources are unavailable just force stop all |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11795 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11796 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11797 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11798 | if (list != null && (list.length > 0)) { |
| 11799 | for (String pkg : list) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11800 | forceStopPackageLocked(pkg, -1, false, true, true, false, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11801 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11802 | sendPackageBroadcastLocked( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11803 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11804 | } |
| 11805 | } else { |
| 11806 | Uri data = intent.getData(); |
| 11807 | String ssp; |
| 11808 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11809 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11810 | forceStopPackageLocked(ssp, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11811 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, |
| 11812 | false, userId); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11813 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11814 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11815 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11816 | new String[] {ssp}, userId); |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11817 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11818 | } |
| 11819 | } |
| 11820 | } |
| 11821 | } else { |
| 11822 | String msg = "Permission Denial: " + intent.getAction() |
| 11823 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11824 | + ", uid=" + callingUid + ")" |
| 11825 | + " requires " |
| 11826 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11827 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11828 | throw new SecurityException(msg); |
| 11829 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11830 | |
| 11831 | // Special case for adding a package: by default turn on compatibility |
| 11832 | // mode. |
| 11833 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 11834 | Uri data = intent.getData(); |
| 11835 | String ssp; |
| 11836 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11837 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 11838 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11839 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11840 | } |
| 11841 | |
| 11842 | /* |
| 11843 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11844 | * of all currently running processes. This message will get queued up before the broadcast |
| 11845 | * happens. |
| 11846 | */ |
| 11847 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11848 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11849 | } |
| 11850 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11851 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11852 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11853 | } |
| 11854 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11855 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11856 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11857 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11858 | } |
| 11859 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11860 | // Add to the sticky list if requested. |
| 11861 | if (sticky) { |
| 11862 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11863 | callingPid, callingUid) |
| 11864 | != PackageManager.PERMISSION_GRANTED) { |
| 11865 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11866 | + callingPid + ", uid=" + callingUid |
| 11867 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11868 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11869 | throw new SecurityException(msg); |
| 11870 | } |
| 11871 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11872 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11873 | + " and enforce permission " + requiredPermission); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11874 | return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11875 | } |
| 11876 | if (intent.getComponent() != null) { |
| 11877 | throw new SecurityException( |
| 11878 | "Sticky broadcasts can't target a specific component"); |
| 11879 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11880 | // We use userId directly here, since the "all" target is maintained |
| 11881 | // as a separate set of sticky broadcasts. |
| 11882 | if (userId != UserHandle.USER_ALL) { |
| 11883 | // But first, if this is not a broadcast to all users, then |
| 11884 | // make sure it doesn't conflict with an existing broadcast to |
| 11885 | // all users. |
| 11886 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get( |
| 11887 | UserHandle.USER_ALL); |
| 11888 | if (stickies != null) { |
| 11889 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11890 | if (list != null) { |
| 11891 | int N = list.size(); |
| 11892 | int i; |
| 11893 | for (i=0; i<N; i++) { |
| 11894 | if (intent.filterEquals(list.get(i))) { |
| 11895 | throw new IllegalArgumentException( |
| 11896 | "Sticky broadcast " + intent + " for user " |
| 11897 | + userId + " conflicts with existing global broadcast"); |
| 11898 | } |
| 11899 | } |
| 11900 | } |
| 11901 | } |
| 11902 | } |
| 11903 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11904 | if (stickies == null) { |
| 11905 | stickies = new HashMap<String, ArrayList<Intent>>(); |
| 11906 | mStickyBroadcasts.put(userId, stickies); |
| 11907 | } |
| 11908 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11909 | if (list == null) { |
| 11910 | list = new ArrayList<Intent>(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11911 | stickies.put(intent.getAction(), list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11912 | } |
| 11913 | int N = list.size(); |
| 11914 | int i; |
| 11915 | for (i=0; i<N; i++) { |
| 11916 | if (intent.filterEquals(list.get(i))) { |
| 11917 | // This sticky already exists, replace it. |
| 11918 | list.set(i, new Intent(intent)); |
| 11919 | break; |
| 11920 | } |
| 11921 | } |
| 11922 | if (i >= N) { |
| 11923 | list.add(new Intent(intent)); |
| 11924 | } |
| 11925 | } |
| 11926 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11927 | int[] users; |
| 11928 | if (userId == UserHandle.USER_ALL) { |
| 11929 | // Caller wants broadcast to go to all started users. |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11930 | users = mStartedUserArray; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11931 | } else { |
| 11932 | // Caller wants broadcast to go to one specific user. |
Amith Yamasani | e98bde0 | 2012-10-01 11:30:47 -0700 | [diff] [blame] | 11933 | users = new int[] {userId}; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11934 | } |
| 11935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11936 | // Figure out who all will receive this broadcast. |
| 11937 | List receivers = null; |
| 11938 | List<BroadcastFilter> registeredReceivers = null; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11939 | // Need to resolve the intent to interested receivers... |
| 11940 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11941 | == 0) { |
| 11942 | receivers = collectReceiverComponents(intent, resolvedType, users); |
| 11943 | } |
| 11944 | if (intent.getComponent() == null) { |
| 11945 | registeredReceivers = mReceiverResolver.queryIntent(intent, |
| 11946 | resolvedType, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11947 | } |
| 11948 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11949 | final boolean replacePending = |
| 11950 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11951 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11952 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11953 | + " replacePending=" + replacePending); |
| 11954 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11955 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11956 | if (!ordered && NR > 0) { |
| 11957 | // If we are not serializing this broadcast, then send the |
| 11958 | // registered receivers separately so they don't wait for the |
| 11959 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11960 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11961 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11962 | callerPackage, callingPid, callingUid, requiredPermission, |
| 11963 | registeredReceivers, resultTo, resultCode, resultData, map, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11964 | ordered, sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11965 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11966 | TAG, "Enqueueing parallel broadcast " + r); |
| 11967 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11968 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11969 | queue.enqueueParallelBroadcastLocked(r); |
| 11970 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11971 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11972 | registeredReceivers = null; |
| 11973 | NR = 0; |
| 11974 | } |
| 11975 | |
| 11976 | // Merge into one list. |
| 11977 | int ir = 0; |
| 11978 | if (receivers != null) { |
| 11979 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11980 | // being added to see this broadcast. This prevents them from |
| 11981 | // using this as a back door to get run as soon as they are |
| 11982 | // installed. Maybe in the future we want to have a special install |
| 11983 | // broadcast or such for apps, but we'd like to deliberately make |
| 11984 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11985 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11986 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11987 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11988 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11989 | Uri data = intent.getData(); |
| 11990 | if (data != null) { |
| 11991 | String pkgName = data.getSchemeSpecificPart(); |
| 11992 | if (pkgName != null) { |
| 11993 | skipPackages = new String[] { pkgName }; |
| 11994 | } |
| 11995 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11996 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11997 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11998 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11999 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 12000 | for (String skipPackage : skipPackages) { |
| 12001 | if (skipPackage != null) { |
| 12002 | int NT = receivers.size(); |
| 12003 | for (int it=0; it<NT; it++) { |
| 12004 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 12005 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 12006 | receivers.remove(it); |
| 12007 | it--; |
| 12008 | NT--; |
| 12009 | } |
| 12010 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12011 | } |
| 12012 | } |
| 12013 | } |
| 12014 | |
| 12015 | int NT = receivers != null ? receivers.size() : 0; |
| 12016 | int it = 0; |
| 12017 | ResolveInfo curt = null; |
| 12018 | BroadcastFilter curr = null; |
| 12019 | while (it < NT && ir < NR) { |
| 12020 | if (curt == null) { |
| 12021 | curt = (ResolveInfo)receivers.get(it); |
| 12022 | } |
| 12023 | if (curr == null) { |
| 12024 | curr = registeredReceivers.get(ir); |
| 12025 | } |
| 12026 | if (curr.getPriority() >= curt.priority) { |
| 12027 | // Insert this broadcast record into the final list. |
| 12028 | receivers.add(it, curr); |
| 12029 | ir++; |
| 12030 | curr = null; |
| 12031 | it++; |
| 12032 | NT++; |
| 12033 | } else { |
| 12034 | // Skip to the next ResolveInfo in the final list. |
| 12035 | it++; |
| 12036 | curt = null; |
| 12037 | } |
| 12038 | } |
| 12039 | } |
| 12040 | while (ir < NR) { |
| 12041 | if (receivers == null) { |
| 12042 | receivers = new ArrayList(); |
| 12043 | } |
| 12044 | receivers.add(registeredReceivers.get(ir)); |
| 12045 | ir++; |
| 12046 | } |
| 12047 | |
| 12048 | if ((receivers != null && receivers.size() > 0) |
| 12049 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12050 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 12051 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12052 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12053 | receivers, resultTo, resultCode, resultData, map, ordered, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 12054 | sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12055 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12056 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12057 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12058 | if (DEBUG_BROADCAST) { |
| 12059 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12060 | 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] | 12061 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12062 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12063 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12064 | queue.enqueueOrderedBroadcastLocked(r); |
| 12065 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12066 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12067 | } |
| 12068 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 12069 | return ActivityManager.BROADCAST_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12070 | } |
| 12071 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12072 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12073 | // Refuse possible leaked file descriptors |
| 12074 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12075 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12076 | } |
| 12077 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12078 | int flags = intent.getFlags(); |
| 12079 | |
| 12080 | if (!mProcessesReady) { |
| 12081 | // if the caller really truly claims to know what they're doing, go |
| 12082 | // ahead and allow the broadcast without launching any receivers |
| 12083 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 12084 | intent = new Intent(intent); |
| 12085 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 12086 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 12087 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 12088 | + " before boot completion"); |
| 12089 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 12090 | } |
| 12091 | } |
| 12092 | |
| 12093 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 12094 | throw new IllegalArgumentException( |
| 12095 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 12096 | } |
| 12097 | |
| 12098 | return intent; |
| 12099 | } |
| 12100 | |
| 12101 | public final int broadcastIntent(IApplicationThread caller, |
| 12102 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12103 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12104 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12105 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12106 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12107 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12109 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 12110 | final int callingPid = Binder.getCallingPid(); |
| 12111 | final int callingUid = Binder.getCallingUid(); |
| 12112 | final long origId = Binder.clearCallingIdentity(); |
| 12113 | int res = broadcastIntentLocked(callerApp, |
| 12114 | callerApp != null ? callerApp.info.packageName : null, |
| 12115 | intent, resolvedType, resultTo, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12116 | resultCode, resultData, map, requiredPermission, serialized, sticky, |
| 12117 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12118 | Binder.restoreCallingIdentity(origId); |
| 12119 | return res; |
| 12120 | } |
| 12121 | } |
| 12122 | |
| 12123 | int broadcastIntentInPackage(String packageName, int uid, |
| 12124 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12125 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12126 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12127 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12128 | intent = verifyBroadcastLocked(intent); |
| 12129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12130 | final long origId = Binder.clearCallingIdentity(); |
| 12131 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 12132 | resultTo, resultCode, resultData, map, requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12133 | serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12134 | Binder.restoreCallingIdentity(origId); |
| 12135 | return res; |
| 12136 | } |
| 12137 | } |
| 12138 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12139 | 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] | 12140 | // Refuse possible leaked file descriptors |
| 12141 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12142 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12143 | } |
| 12144 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 12145 | userId = handleIncomingUser(Binder.getCallingPid(), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12146 | Binder.getCallingUid(), userId, true, false, "removeStickyBroadcast", null); |
| 12147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12148 | synchronized(this) { |
| 12149 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 12150 | != PackageManager.PERMISSION_GRANTED) { |
| 12151 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 12152 | + Binder.getCallingPid() |
| 12153 | + ", uid=" + Binder.getCallingUid() |
| 12154 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12155 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12156 | throw new SecurityException(msg); |
| 12157 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12158 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 12159 | if (stickies != null) { |
| 12160 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 12161 | if (list != null) { |
| 12162 | int N = list.size(); |
| 12163 | int i; |
| 12164 | for (i=0; i<N; i++) { |
| 12165 | if (intent.filterEquals(list.get(i))) { |
| 12166 | list.remove(i); |
| 12167 | break; |
| 12168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12169 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12170 | if (list.size() <= 0) { |
| 12171 | stickies.remove(intent.getAction()); |
| 12172 | } |
| 12173 | } |
| 12174 | if (stickies.size() <= 0) { |
| 12175 | mStickyBroadcasts.remove(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12176 | } |
| 12177 | } |
| 12178 | } |
| 12179 | } |
| 12180 | |
| 12181 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 12182 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 12183 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12184 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 12185 | if (r == null) { |
| 12186 | 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] | 12187 | return false; |
| 12188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12189 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12190 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 12191 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12192 | } |
| 12193 | |
| 12194 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 12195 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12196 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12197 | |
| 12198 | // Refuse possible leaked file descriptors |
| 12199 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 12200 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12201 | } |
| 12202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12203 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12204 | try { |
| 12205 | boolean doNext = false; |
| 12206 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12207 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12208 | synchronized(this) { |
| 12209 | r = broadcastRecordForReceiverLocked(who); |
| 12210 | if (r != null) { |
| 12211 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 12212 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12214 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12215 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12216 | if (doNext) { |
| 12217 | r.queue.processNextBroadcast(false); |
| 12218 | } |
| 12219 | trimApplications(); |
| 12220 | } finally { |
| 12221 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12222 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12223 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12225 | // ========================================================= |
| 12226 | // INSTRUMENTATION |
| 12227 | // ========================================================= |
| 12228 | |
| 12229 | public boolean startInstrumentation(ComponentName className, |
| 12230 | String profileFile, int flags, Bundle arguments, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12231 | IInstrumentationWatcher watcher, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12232 | enforceNotIsolatedCaller("startInstrumentation"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 12233 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12234 | userId, false, true, "startInstrumentation", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12235 | // Refuse possible leaked file descriptors |
| 12236 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 12237 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12238 | } |
| 12239 | |
| 12240 | synchronized(this) { |
| 12241 | InstrumentationInfo ii = null; |
| 12242 | ApplicationInfo ai = null; |
| 12243 | try { |
| 12244 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12245 | className, STOCK_PM_FLAGS); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12246 | ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 12247 | ii.targetPackage, STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12248 | } catch (PackageManager.NameNotFoundException e) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12249 | } catch (RemoteException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12250 | } |
| 12251 | if (ii == null) { |
| 12252 | reportStartInstrumentationFailure(watcher, className, |
| 12253 | "Unable to find instrumentation info for: " + className); |
| 12254 | return false; |
| 12255 | } |
| 12256 | if (ai == null) { |
| 12257 | reportStartInstrumentationFailure(watcher, className, |
| 12258 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12259 | return false; |
| 12260 | } |
| 12261 | |
| 12262 | int match = mContext.getPackageManager().checkSignatures( |
| 12263 | ii.targetPackage, ii.packageName); |
| 12264 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12265 | String msg = "Permission Denial: starting instrumentation " |
| 12266 | + className + " from pid=" |
| 12267 | + Binder.getCallingPid() |
| 12268 | + ", uid=" + Binder.getCallingPid() |
| 12269 | + " not allowed because package " + ii.packageName |
| 12270 | + " does not have a signature matching the target " |
| 12271 | + ii.targetPackage; |
| 12272 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12273 | throw new SecurityException(msg); |
| 12274 | } |
| 12275 | |
| 12276 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12277 | // Instrumentation can kill and relaunch even persistent processes |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12278 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 12279 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12280 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12281 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12282 | app.instrumentationProfileFile = profileFile; |
| 12283 | app.instrumentationArguments = arguments; |
| 12284 | app.instrumentationWatcher = watcher; |
| 12285 | app.instrumentationResultClass = className; |
| 12286 | Binder.restoreCallingIdentity(origId); |
| 12287 | } |
| 12288 | |
| 12289 | return true; |
| 12290 | } |
| 12291 | |
| 12292 | /** |
| 12293 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 12294 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 12295 | * the "am" command to report errors with more information. |
| 12296 | * |
| 12297 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 12298 | * @param cn The component name of the instrumentation. |
| 12299 | * @param report The error report. |
| 12300 | */ |
| 12301 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 12302 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12303 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12304 | try { |
| 12305 | if (watcher != null) { |
| 12306 | Bundle results = new Bundle(); |
| 12307 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 12308 | results.putString("Error", report); |
| 12309 | watcher.instrumentationStatus(cn, -1, results); |
| 12310 | } |
| 12311 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12312 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12313 | } |
| 12314 | } |
| 12315 | |
| 12316 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 12317 | if (app.instrumentationWatcher != null) { |
| 12318 | try { |
| 12319 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 12320 | app.instrumentationWatcher.instrumentationFinished( |
| 12321 | app.instrumentationClass, |
| 12322 | resultCode, |
| 12323 | results); |
| 12324 | } catch (RemoteException e) { |
| 12325 | } |
| 12326 | } |
| 12327 | app.instrumentationWatcher = null; |
| 12328 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12329 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12330 | app.instrumentationProfileFile = null; |
| 12331 | app.instrumentationArguments = null; |
| 12332 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 12333 | 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] | 12334 | } |
| 12335 | |
| 12336 | public void finishInstrumentation(IApplicationThread target, |
| 12337 | int resultCode, Bundle results) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 12338 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12339 | // Refuse possible leaked file descriptors |
| 12340 | if (results != null && results.hasFileDescriptors()) { |
| 12341 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12342 | } |
| 12343 | |
| 12344 | synchronized(this) { |
| 12345 | ProcessRecord app = getRecordForAppLocked(target); |
| 12346 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12347 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12348 | return; |
| 12349 | } |
| 12350 | final long origId = Binder.clearCallingIdentity(); |
| 12351 | finishInstrumentationLocked(app, resultCode, results); |
| 12352 | Binder.restoreCallingIdentity(origId); |
| 12353 | } |
| 12354 | } |
| 12355 | |
| 12356 | // ========================================================= |
| 12357 | // CONFIGURATION |
| 12358 | // ========================================================= |
| 12359 | |
| 12360 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 12361 | ConfigurationInfo config = new ConfigurationInfo(); |
| 12362 | synchronized (this) { |
| 12363 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 12364 | config.reqKeyboardType = mConfiguration.keyboard; |
| 12365 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12366 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 12367 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12368 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 12369 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12370 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 12371 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12372 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 12373 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 12374 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12375 | } |
| 12376 | return config; |
| 12377 | } |
| 12378 | |
| 12379 | public Configuration getConfiguration() { |
| 12380 | Configuration ci; |
| 12381 | synchronized(this) { |
| 12382 | ci = new Configuration(mConfiguration); |
| 12383 | } |
| 12384 | return ci; |
| 12385 | } |
| 12386 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12387 | public void updatePersistentConfiguration(Configuration values) { |
| 12388 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12389 | "updateConfiguration()"); |
| 12390 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 12391 | "updateConfiguration()"); |
| 12392 | if (values == null) { |
| 12393 | throw new NullPointerException("Configuration must not be null"); |
| 12394 | } |
| 12395 | |
| 12396 | synchronized(this) { |
| 12397 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12398 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12399 | Binder.restoreCallingIdentity(origId); |
| 12400 | } |
| 12401 | } |
| 12402 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12403 | public void updateConfiguration(Configuration values) { |
| 12404 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12405 | "updateConfiguration()"); |
| 12406 | |
| 12407 | synchronized(this) { |
| 12408 | if (values == null && mWindowManager != null) { |
| 12409 | // sentinel: fetch the current configuration from the window manager |
| 12410 | values = mWindowManager.computeNewConfiguration(); |
| 12411 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12412 | |
| 12413 | if (mWindowManager != null) { |
| 12414 | mProcessList.applyDisplaySize(mWindowManager); |
| 12415 | } |
| 12416 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12417 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12418 | if (values != null) { |
| 12419 | Settings.System.clearConfiguration(values); |
| 12420 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12421 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12422 | Binder.restoreCallingIdentity(origId); |
| 12423 | } |
| 12424 | } |
| 12425 | |
| 12426 | /** |
| 12427 | * Do either or both things: (1) change the current configuration, and (2) |
| 12428 | * make sure the given activity is running with the (now) current |
| 12429 | * configuration. Returns true if the activity has been left running, or |
| 12430 | * false if <var>starting</var> is being destroyed to match the new |
| 12431 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12432 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12433 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12434 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12435 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 12436 | // do nothing if we are headless |
| 12437 | if (mHeadless) return true; |
| 12438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12439 | int changes = 0; |
| 12440 | |
| 12441 | boolean kept = true; |
| 12442 | |
| 12443 | if (values != null) { |
| 12444 | Configuration newConfig = new Configuration(mConfiguration); |
| 12445 | changes = newConfig.updateFrom(values); |
| 12446 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12447 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12448 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12449 | } |
| 12450 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12451 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12452 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12453 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12454 | saveLocaleLocked(values.locale, |
| 12455 | !values.locale.equals(mConfiguration.locale), |
| 12456 | values.userSetLocale); |
| 12457 | } |
| 12458 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12459 | mConfigurationSeq++; |
| 12460 | if (mConfigurationSeq <= 0) { |
| 12461 | mConfigurationSeq = 1; |
| 12462 | } |
| 12463 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12464 | mConfiguration = newConfig; |
Craig Mautner | ae44659 | 2012-12-06 19:05:05 -0800 | [diff] [blame] | 12465 | Slog.i(TAG, "Config changes=" + Integer.toHexString(changes) + " " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12466 | |
| 12467 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12468 | |
| 12469 | // TODO: If our config changes, should we auto dismiss any currently |
| 12470 | // showing dialogs? |
| 12471 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12472 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12473 | AttributeCache ac = AttributeCache.instance(); |
| 12474 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12475 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12476 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12477 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12478 | // Make sure all resources in our process are updated |
| 12479 | // right now, so that anyone who is going to retrieve |
| 12480 | // resource values after we return will be sure to get |
| 12481 | // the new ones. This is especially important during |
| 12482 | // boot, where the first config change needs to guarantee |
| 12483 | // all resources have that config before following boot |
| 12484 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12485 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12486 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12487 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12488 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12489 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12490 | mHandler.sendMessage(msg); |
| 12491 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12492 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12493 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12494 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12495 | try { |
| 12496 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12497 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12498 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12499 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12500 | } |
| 12501 | } catch (Exception e) { |
| 12502 | } |
| 12503 | } |
| 12504 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12505 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 12506 | | Intent.FLAG_RECEIVER_REPLACE_PENDING |
| 12507 | | Intent.FLAG_RECEIVER_FOREGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12508 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12509 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12510 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 12511 | intent = new Intent(Intent.ACTION_LOCALE_CHANGED); |
| 12512 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 12513 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12514 | null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12515 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12517 | } |
| 12518 | } |
| 12519 | |
| 12520 | if (changes != 0 && starting == null) { |
| 12521 | // If the configuration changed, and the caller is not already |
| 12522 | // in the process of starting an activity, then find the top |
| 12523 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12524 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12525 | } |
| 12526 | |
| 12527 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12528 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12529 | // And we need to make sure at this point that all other activities |
| 12530 | // are made visible with the correct configuration. |
| 12531 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12532 | } |
| 12533 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12534 | if (values != null && mWindowManager != null) { |
| 12535 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12536 | } |
| 12537 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12538 | return kept; |
| 12539 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12540 | |
| 12541 | /** |
| 12542 | * Decide based on the configuration whether we should shouw the ANR, |
| 12543 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 12544 | * press the on-screen buttons, we shouldn't show the dialog. |
| 12545 | * |
| 12546 | * A thought: SystemUI might also want to get told about this, the Power |
| 12547 | * dialog / global actions also might want different behaviors. |
| 12548 | */ |
| 12549 | private static final boolean shouldShowDialogs(Configuration config) { |
| 12550 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 12551 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 12552 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12553 | |
| 12554 | /** |
| 12555 | * Save the locale. You must be inside a synchronized (this) block. |
| 12556 | */ |
| 12557 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 12558 | if(isDiff) { |
| 12559 | SystemProperties.set("user.language", l.getLanguage()); |
| 12560 | SystemProperties.set("user.region", l.getCountry()); |
| 12561 | } |
| 12562 | |
| 12563 | if(isPersist) { |
| 12564 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 12565 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 12566 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 12567 | } |
| 12568 | } |
| 12569 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12570 | @Override |
| 12571 | public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) { |
| 12572 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12573 | return srec != null && srec.task.affinity != null && |
| 12574 | srec.task.affinity.equals(destAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12575 | } |
| 12576 | |
| 12577 | public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode, |
| 12578 | Intent resultData) { |
| 12579 | ComponentName dest = destIntent.getComponent(); |
| 12580 | |
| 12581 | synchronized (this) { |
| 12582 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12583 | if (srec == null) { |
| 12584 | return false; |
| 12585 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12586 | ArrayList<ActivityRecord> history = srec.stack.mHistory; |
| 12587 | final int start = history.indexOf(srec); |
| 12588 | if (start < 0) { |
| 12589 | // Current activity is not in history stack; do nothing. |
| 12590 | return false; |
| 12591 | } |
| 12592 | int finishTo = start - 1; |
| 12593 | ActivityRecord parent = null; |
| 12594 | boolean foundParentInTask = false; |
| 12595 | if (dest != null) { |
| 12596 | TaskRecord tr = srec.task; |
| 12597 | for (int i = start - 1; i >= 0; i--) { |
| 12598 | ActivityRecord r = history.get(i); |
| 12599 | if (tr != r.task) { |
| 12600 | // Couldn't find parent in the same task; stop at the one above this. |
| 12601 | // (Root of current task; in-app "home" behavior) |
| 12602 | // Always at least finish the current activity. |
| 12603 | finishTo = Math.min(start - 1, i + 1); |
| 12604 | parent = history.get(finishTo); |
| 12605 | break; |
| 12606 | } else if (r.info.packageName.equals(dest.getPackageName()) && |
| 12607 | r.info.name.equals(dest.getClassName())) { |
| 12608 | finishTo = i; |
| 12609 | parent = r; |
| 12610 | foundParentInTask = true; |
| 12611 | break; |
| 12612 | } |
| 12613 | } |
| 12614 | } |
| 12615 | |
| 12616 | if (mController != null) { |
| 12617 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
| 12618 | if (next != null) { |
| 12619 | // ask watcher if this is allowed |
| 12620 | boolean resumeOK = true; |
| 12621 | try { |
| 12622 | resumeOK = mController.activityResuming(next.packageName); |
| 12623 | } catch (RemoteException e) { |
| 12624 | mController = null; |
| 12625 | } |
| 12626 | |
| 12627 | if (!resumeOK) { |
| 12628 | return false; |
| 12629 | } |
| 12630 | } |
| 12631 | } |
| 12632 | final long origId = Binder.clearCallingIdentity(); |
| 12633 | for (int i = start; i > finishTo; i--) { |
| 12634 | ActivityRecord r = history.get(i); |
| 12635 | mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 12636 | "navigate-up", true); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12637 | // Only return the supplied result for the first activity finished |
| 12638 | resultCode = Activity.RESULT_CANCELED; |
| 12639 | resultData = null; |
| 12640 | } |
| 12641 | |
| 12642 | if (parent != null && foundParentInTask) { |
| 12643 | final int parentLaunchMode = parent.info.launchMode; |
| 12644 | final int destIntentFlags = destIntent.getFlags(); |
| 12645 | if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || |
| 12646 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK || |
| 12647 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP || |
| 12648 | (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
Adam Powell | 69de7e1 | 2012-05-07 18:42:24 -0700 | [diff] [blame] | 12649 | parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12650 | } else { |
| 12651 | try { |
| 12652 | ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo( |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 12653 | destIntent.getComponent(), 0, srec.userId); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12654 | int res = mMainStack.startActivityLocked(srec.app.thread, destIntent, |
| 12655 | null, aInfo, parent.appToken, null, |
| 12656 | 0, -1, parent.launchedFromUid, 0, null, true, null); |
| 12657 | foundParentInTask = res == ActivityManager.START_SUCCESS; |
| 12658 | } catch (RemoteException e) { |
| 12659 | foundParentInTask = false; |
| 12660 | } |
| 12661 | mMainStack.requestFinishActivityLocked(parent.appToken, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 12662 | resultData, "navigate-up", true); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12663 | } |
| 12664 | } |
| 12665 | Binder.restoreCallingIdentity(origId); |
| 12666 | return foundParentInTask; |
| 12667 | } |
| 12668 | } |
| 12669 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 12670 | public int getLaunchedFromUid(IBinder activityToken) { |
| 12671 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 12672 | if (srec == null) { |
| 12673 | return -1; |
| 12674 | } |
| 12675 | return srec.launchedFromUid; |
| 12676 | } |
| 12677 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12678 | // ========================================================= |
| 12679 | // LIFETIME MANAGEMENT |
| 12680 | // ========================================================= |
| 12681 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12682 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 12683 | // on, or 'null' if the app is not an active broadcast recipient. |
| 12684 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 12685 | BroadcastRecord r = app.curReceiver; |
| 12686 | if (r != null) { |
| 12687 | return r.queue; |
| 12688 | } |
| 12689 | |
| 12690 | // It's not the current receiver, but it might be starting up to become one |
| 12691 | synchronized (this) { |
| 12692 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12693 | r = queue.mPendingBroadcast; |
| 12694 | if (r != null && r.curApp == app) { |
| 12695 | // found it; report which queue it's in |
| 12696 | return queue; |
| 12697 | } |
| 12698 | } |
| 12699 | } |
| 12700 | |
| 12701 | return null; |
| 12702 | } |
| 12703 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12704 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, int clientHiddenAdj, |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12705 | int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12706 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12707 | // This adjustment has already been computed. If we are calling |
| 12708 | // from the top, we may have already computed our adjustment with |
| 12709 | // an earlier hidden adjustment that isn't really for us... if |
| 12710 | // so, use the new hidden adjustment. |
| 12711 | if (!recursed && app.hidden) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12712 | if (app.hasActivities) { |
| 12713 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = hiddenAdj; |
| 12714 | } else if (app.hasClientActivities) { |
| 12715 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = clientHiddenAdj; |
| 12716 | } else { |
| 12717 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = emptyAdj; |
| 12718 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12719 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12720 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12721 | } |
| 12722 | |
| 12723 | if (app.thread == null) { |
| 12724 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12725 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12726 | 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] | 12727 | } |
| 12728 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12729 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 12730 | app.adjSource = null; |
| 12731 | app.adjTarget = null; |
| 12732 | app.empty = false; |
| 12733 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12734 | app.hasClientActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12735 | |
| 12736 | final int activitiesSize = app.activities.size(); |
| 12737 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12738 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12739 | // The max adjustment doesn't allow this app to be anything |
| 12740 | // below foreground, so it is not worth doing work for it. |
| 12741 | app.adjType = "fixed"; |
| 12742 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12743 | app.curRawAdj = app.nonStoppingAdj = app.maxAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12744 | app.hasActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12745 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12746 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12747 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12748 | // System process can do UI, and when they do we want to have |
| 12749 | // them trim their memory after the user leaves the UI. To |
| 12750 | // facilitate this, here we need to determine whether or not it |
| 12751 | // is currently showing UI. |
| 12752 | app.systemNoUi = true; |
| 12753 | if (app == TOP_APP) { |
| 12754 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12755 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12756 | } else if (activitiesSize > 0) { |
| 12757 | for (int j = 0; j < activitiesSize; j++) { |
| 12758 | final ActivityRecord r = app.activities.get(j); |
| 12759 | if (r.visible) { |
| 12760 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12761 | } |
| 12762 | if (r.app == app) { |
| 12763 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12764 | } |
| 12765 | } |
| 12766 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12767 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12768 | } |
| 12769 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12770 | app.keeping = false; |
| 12771 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12772 | app.hasActivities = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12773 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12774 | // Determine the importance of the process, starting with most |
| 12775 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12776 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12777 | int schedGroup; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12778 | boolean foregroundActivities = false; |
| 12779 | boolean interesting = false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12780 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12781 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12782 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12783 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12784 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12785 | app.adjType = "top-activity"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12786 | foregroundActivities = true; |
| 12787 | interesting = true; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12788 | app.hasActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12789 | } else if (app.instrumentationClass != null) { |
| 12790 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12791 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12792 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12793 | app.adjType = "instrumentation"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12794 | interesting = true; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12795 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12796 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12797 | // counts as being in the foreground for OOM killer purposes. |
| 12798 | // It's placed in a sched group based on the nature of the |
| 12799 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12800 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12801 | schedGroup = (queue == mFgBroadcastQueue) |
| 12802 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12803 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12804 | } else if (app.executingServices.size() > 0) { |
| 12805 | // An app that is currently executing a service callback also |
| 12806 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12807 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12808 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12809 | app.adjType = "exec-service"; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12810 | } else { |
| 12811 | // Assume process is hidden (has activities); we will correct |
| 12812 | // later if this is not the case. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12813 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12814 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12815 | app.hidden = true; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12816 | app.adjType = "bg-act"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12817 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12818 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12819 | boolean hasStoppingActivities = false; |
| 12820 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12821 | // Examine all activities if not already foreground. |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12822 | if (!foregroundActivities && activitiesSize > 0) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12823 | for (int j = 0; j < activitiesSize; j++) { |
| 12824 | final ActivityRecord r = app.activities.get(j); |
| 12825 | if (r.visible) { |
| 12826 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12827 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12828 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12829 | app.adjType = "visible"; |
| 12830 | } |
| 12831 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12832 | app.hidden = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12833 | app.hasActivities = true; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12834 | foregroundActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12835 | break; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12836 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12837 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12838 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12839 | app.adjType = "pausing"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12840 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 12841 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12842 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12843 | } else if (r.state == ActivityState.STOPPING) { |
| 12844 | // We will apply the actual adjustment later, because |
| 12845 | // we want to allow this process to immediately go through |
| 12846 | // any memory trimming that is in effect. |
| 12847 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12848 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12849 | hasStoppingActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12850 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12851 | if (r.app == app) { |
| 12852 | app.hasActivities = true; |
| 12853 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12854 | } |
| 12855 | } |
| 12856 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12857 | if (adj == hiddenAdj && !app.hasActivities) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12858 | if (app.hasClientActivities) { |
| 12859 | adj = clientHiddenAdj; |
| 12860 | app.adjType = "bg-client-act"; |
| 12861 | } else { |
| 12862 | // Whoops, this process is completely empty as far as we know |
| 12863 | // at this point. |
| 12864 | adj = emptyAdj; |
| 12865 | app.empty = true; |
| 12866 | app.adjType = "bg-empty"; |
| 12867 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12868 | } |
| 12869 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12870 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12871 | if (app.foregroundServices) { |
| 12872 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12873 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12874 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12875 | app.adjType = "fg-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12876 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12877 | } else if (app.forcingToForeground != null) { |
| 12878 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12879 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12880 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12881 | app.adjType = "force-fg"; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12882 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12883 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12884 | } |
| 12885 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12886 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12887 | if (app.foregroundServices) { |
| 12888 | interesting = true; |
| 12889 | } |
| 12890 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12891 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12892 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12893 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12894 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12895 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12896 | app.adjType = "heavy"; |
| 12897 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12898 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12899 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12900 | // This process is hosting what we currently consider to be the |
| 12901 | // 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] | 12902 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12903 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12904 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12905 | app.adjType = "home"; |
| 12906 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12907 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 12908 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 12909 | && app.activities.size() > 0) { |
| 12910 | // This was the previous process that showed UI to the user. |
| 12911 | // We want to try to keep it around more aggressively, to give |
| 12912 | // a good experience around switching between two apps. |
| 12913 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 12914 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12915 | app.hidden = false; |
| 12916 | app.adjType = "previous"; |
| 12917 | } |
| 12918 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12919 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 12920 | + " reason=" + app.adjType); |
| 12921 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12922 | // 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] | 12923 | // there are applications dependent on our services or providers, but |
| 12924 | // this gives us a baseline and makes sure we don't get into an |
| 12925 | // infinite recursion. |
| 12926 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12927 | app.curRawAdj = app.nonStoppingAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12928 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12929 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 12930 | // 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] | 12931 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12932 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12933 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12934 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12935 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12936 | } |
| 12937 | } |
| 12938 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12939 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12940 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12941 | final long now = SystemClock.uptimeMillis(); |
| 12942 | // This process is more important if the top activity is |
| 12943 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12944 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12945 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12946 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12947 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12948 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12949 | // If this process has shown some UI, let it immediately |
| 12950 | // go to the LRU list because it may be pretty heavy with |
| 12951 | // UI stuff. We'll tag it with a label just to help |
| 12952 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12953 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12954 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12955 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12956 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12957 | if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12958 | // This service has seen some activity within |
| 12959 | // recent memory, so we will keep its process ahead |
| 12960 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12961 | if (adj > ProcessList.SERVICE_ADJ) { |
| 12962 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12963 | app.adjType = "started-services"; |
| 12964 | app.hidden = false; |
| 12965 | } |
| 12966 | } |
| 12967 | // If we have let the service slide into the background |
| 12968 | // state, still have some text describing what it is doing |
| 12969 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12970 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12971 | app.adjType = "started-bg-services"; |
| 12972 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12973 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12974 | // Don't kill this process because it is doing work; it |
| 12975 | // has said it is doing work. |
| 12976 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12977 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12978 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12979 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12980 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12981 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12982 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12983 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12984 | 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] | 12985 | // XXX should compute this based on the max of |
| 12986 | // all connected clients. |
| 12987 | ConnectionRecord cr = clist.get(i); |
| 12988 | if (cr.binding.client == app) { |
| 12989 | // Binding to ourself is not interesting. |
| 12990 | continue; |
| 12991 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12992 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12993 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12994 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12995 | int myHiddenAdj = hiddenAdj; |
| 12996 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12997 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12998 | myHiddenAdj = client.hiddenAdj; |
| 12999 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13000 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13001 | } |
| 13002 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13003 | int myClientHiddenAdj = clientHiddenAdj; |
| 13004 | if (myClientHiddenAdj > client.clientHiddenAdj) { |
| 13005 | if (client.clientHiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13006 | myClientHiddenAdj = client.clientHiddenAdj; |
| 13007 | } else { |
| 13008 | myClientHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
| 13009 | } |
| 13010 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13011 | int myEmptyAdj = emptyAdj; |
| 13012 | if (myEmptyAdj > client.emptyAdj) { |
| 13013 | if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13014 | myEmptyAdj = client.emptyAdj; |
| 13015 | } else { |
| 13016 | myEmptyAdj = ProcessList.VISIBLE_APP_ADJ; |
| 13017 | } |
| 13018 | } |
| 13019 | clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13020 | myClientHiddenAdj, myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13021 | String adjType = null; |
| 13022 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 13023 | // Not doing bind OOM management, so treat |
| 13024 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13025 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13026 | // If this process has shown some UI, let it immediately |
| 13027 | // go to the LRU list because it may be pretty heavy with |
| 13028 | // UI stuff. We'll tag it with a label just to help |
| 13029 | // debug and understand what is going on. |
| 13030 | if (adj > clientAdj) { |
| 13031 | adjType = "bound-bg-ui-services"; |
| 13032 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13033 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13034 | clientAdj = adj; |
| 13035 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13036 | if (now >= (s.lastActivity |
| 13037 | + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13038 | // This service has not seen activity within |
| 13039 | // recent memory, so allow it to drop to the |
| 13040 | // LRU list if there is no other reason to keep |
| 13041 | // it around. We'll also tag it with a label just |
| 13042 | // to help debug and undertand what is going on. |
| 13043 | if (adj > clientAdj) { |
| 13044 | adjType = "bound-bg-services"; |
| 13045 | } |
| 13046 | clientAdj = adj; |
| 13047 | } |
| 13048 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13049 | } else if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 13050 | if ((cr.flags&Context.BIND_NOT_VISIBLE) == 0) { |
| 13051 | // If this connection is keeping the service |
| 13052 | // created, then we want to try to better follow |
| 13053 | // its memory management semantics for activities. |
| 13054 | // That is, if it is sitting in the background |
| 13055 | // LRU list as a hidden process (with activities), |
| 13056 | // we don't want the service it is connected to |
| 13057 | // to go into the empty LRU and quickly get killed, |
| 13058 | // because I'll we'll do is just end up restarting |
| 13059 | // the service. |
| 13060 | app.hasClientActivities |= client.hasActivities; |
| 13061 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13062 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13063 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13064 | // If this process has recently shown UI, and |
| 13065 | // the process that is binding to it is less |
| 13066 | // important than being visible, then we don't |
| 13067 | // care about the binding as much as we care |
| 13068 | // about letting this process get into the LRU |
| 13069 | // list to be killed and restarted if needed for |
| 13070 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13071 | if (app.hasShownUi && app != mHomeProcess |
| 13072 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13073 | adjType = "bound-bg-ui-services"; |
| 13074 | } else { |
| 13075 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 13076 | |Context.BIND_IMPORTANT)) != 0) { |
| 13077 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13078 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 13079 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 13080 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13081 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13082 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13083 | adj = clientAdj; |
| 13084 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13085 | app.pendingUiClean = true; |
| 13086 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13087 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13088 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13089 | } |
| 13090 | if (!client.hidden) { |
| 13091 | app.hidden = false; |
| 13092 | } |
| 13093 | if (client.keeping) { |
| 13094 | app.keeping = true; |
| 13095 | } |
| 13096 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13097 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13098 | } |
| 13099 | if (adjType != null) { |
| 13100 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13101 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13102 | .REASON_SERVICE_IN_USE; |
| 13103 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13104 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13105 | app.adjTarget = s.name; |
| 13106 | } |
| 13107 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13108 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13109 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13110 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13111 | } |
| 13112 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13113 | final ActivityRecord a = cr.activity; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13114 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13115 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13116 | (a.visible || a.state == ActivityState.RESUMED |
| 13117 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13118 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13119 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13120 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13121 | } |
| 13122 | app.hidden = false; |
| 13123 | app.adjType = "service"; |
| 13124 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13125 | .REASON_SERVICE_IN_USE; |
| 13126 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13127 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13128 | app.adjTarget = s.name; |
| 13129 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13131 | } |
| 13132 | } |
| 13133 | } |
| 13134 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13135 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13136 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13137 | // would like to avoid killing it unless it would prevent the current |
| 13138 | // application from running. By default we put the process in |
| 13139 | // with the rest of the background processes; as we scan through |
| 13140 | // its services we may bump it up from there. |
| 13141 | if (adj > hiddenAdj) { |
| 13142 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13143 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13144 | app.adjType = "bg-services"; |
| 13145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13146 | } |
| 13147 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13148 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13149 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13150 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13151 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13152 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13153 | ContentProviderRecord cpr = jt.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13154 | for (int i = cpr.connections.size()-1; |
| 13155 | i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
| 13156 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE); |
| 13157 | i--) { |
| 13158 | ContentProviderConnection conn = cpr.connections.get(i); |
| 13159 | ProcessRecord client = conn.client; |
| 13160 | if (client == app) { |
| 13161 | // Being our own client is not interesting. |
| 13162 | continue; |
| 13163 | } |
| 13164 | int myHiddenAdj = hiddenAdj; |
| 13165 | if (myHiddenAdj > client.hiddenAdj) { |
| 13166 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13167 | myHiddenAdj = client.hiddenAdj; |
| 13168 | } else { |
| 13169 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 13170 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13171 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13172 | int myClientHiddenAdj = clientHiddenAdj; |
| 13173 | if (myClientHiddenAdj > client.clientHiddenAdj) { |
| 13174 | if (client.clientHiddenAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 13175 | myClientHiddenAdj = client.clientHiddenAdj; |
| 13176 | } else { |
| 13177 | myClientHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 13178 | } |
| 13179 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13180 | int myEmptyAdj = emptyAdj; |
| 13181 | if (myEmptyAdj > client.emptyAdj) { |
| 13182 | if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13183 | myEmptyAdj = client.emptyAdj; |
| 13184 | } else { |
| 13185 | myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 13186 | } |
| 13187 | } |
| 13188 | int clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13189 | myClientHiddenAdj, myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13190 | if (adj > clientAdj) { |
| 13191 | if (app.hasShownUi && app != mHomeProcess |
| 13192 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13193 | app.adjType = "bg-ui-provider"; |
| 13194 | } else { |
| 13195 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13196 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
| 13197 | app.adjType = "provider"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13198 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13199 | if (!client.hidden) { |
| 13200 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13201 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13202 | if (client.keeping) { |
| 13203 | app.keeping = true; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13204 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13205 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13206 | .REASON_PROVIDER_IN_USE; |
| 13207 | app.adjSource = client; |
| 13208 | app.adjSourceOom = clientAdj; |
| 13209 | app.adjTarget = cpr.name; |
| 13210 | } |
| 13211 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13212 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13213 | } |
| 13214 | } |
| 13215 | // If the provider has external (non-framework) process |
| 13216 | // dependencies, ensure that its adjustment is at least |
| 13217 | // FOREGROUND_APP_ADJ. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 13218 | if (cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13219 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13220 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13221 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13222 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13223 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13224 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13225 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13226 | } |
| 13227 | } |
| 13228 | } |
| 13229 | } |
| 13230 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13231 | if (adj == ProcessList.SERVICE_ADJ) { |
| 13232 | if (doingAll) { |
| 13233 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 13234 | mNewNumServiceProcs++; |
| 13235 | } |
| 13236 | if (app.serviceb) { |
| 13237 | adj = ProcessList.SERVICE_B_ADJ; |
| 13238 | } |
| 13239 | } else { |
| 13240 | app.serviceb = false; |
| 13241 | } |
| 13242 | |
| 13243 | app.nonStoppingAdj = adj; |
| 13244 | |
| 13245 | if (hasStoppingActivities) { |
| 13246 | // Only upgrade adjustment. |
| 13247 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13248 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13249 | app.adjType = "stopping"; |
| 13250 | } |
| 13251 | } |
| 13252 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13253 | app.curRawAdj = adj; |
| 13254 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13255 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13256 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 13257 | if (adj > app.maxAdj) { |
| 13258 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13259 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13260 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13261 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13262 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13263 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13264 | app.keeping = true; |
| 13265 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13266 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13267 | if (app.hasAboveClient) { |
| 13268 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 13269 | // then we need to drop its adjustment to be lower than the service's |
| 13270 | // in order to honor the request. We want to drop it by one adjustment |
| 13271 | // level... but there is special meaning applied to various levels so |
| 13272 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13273 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13274 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13275 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 13276 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13277 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13278 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13279 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13280 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13281 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13282 | adj++; |
| 13283 | } |
| 13284 | } |
| 13285 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13286 | int importance = app.memImportance; |
| 13287 | if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) { |
| 13288 | app.curAdj = adj; |
| 13289 | app.curSchedGroup = schedGroup; |
| 13290 | if (!interesting) { |
| 13291 | // For this reporting, if there is not something explicitly |
| 13292 | // interesting in this process then we will push it to the |
| 13293 | // background importance. |
| 13294 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13295 | } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13296 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13297 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 13298 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 13299 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 13300 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13301 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
| 13302 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 13303 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 13304 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 13305 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13306 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 13307 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13308 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 13309 | } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 13310 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 13311 | } else { |
| 13312 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT; |
| 13313 | } |
| 13314 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13315 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13316 | int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0; |
| 13317 | if (foregroundActivities != app.foregroundActivities) { |
| 13318 | changes |= ProcessChangeItem.CHANGE_ACTIVITIES; |
| 13319 | } |
| 13320 | if (changes != 0) { |
| 13321 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes); |
| 13322 | app.memImportance = importance; |
| 13323 | app.foregroundActivities = foregroundActivities; |
| 13324 | int i = mPendingProcessChanges.size()-1; |
| 13325 | ProcessChangeItem item = null; |
| 13326 | while (i >= 0) { |
| 13327 | item = mPendingProcessChanges.get(i); |
| 13328 | if (item.pid == app.pid) { |
| 13329 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item); |
| 13330 | break; |
| 13331 | } |
| 13332 | i--; |
| 13333 | } |
| 13334 | if (i < 0) { |
| 13335 | // No existing item in pending changes; need a new one. |
| 13336 | final int NA = mAvailProcessChanges.size(); |
| 13337 | if (NA > 0) { |
| 13338 | item = mAvailProcessChanges.remove(NA-1); |
| 13339 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item); |
| 13340 | } else { |
| 13341 | item = new ProcessChangeItem(); |
| 13342 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item); |
| 13343 | } |
| 13344 | item.changes = 0; |
| 13345 | item.pid = app.pid; |
| 13346 | item.uid = app.info.uid; |
| 13347 | if (mPendingProcessChanges.size() == 0) { |
| 13348 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, |
| 13349 | "*** Enqueueing dispatch processes changed!"); |
| 13350 | mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget(); |
| 13351 | } |
| 13352 | mPendingProcessChanges.add(item); |
| 13353 | } |
| 13354 | item.changes |= changes; |
| 13355 | item.importance = importance; |
| 13356 | item.foregroundActivities = foregroundActivities; |
| 13357 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item " |
| 13358 | + Integer.toHexString(System.identityHashCode(item)) |
| 13359 | + " " + app.toShortString() + ": changes=" + item.changes |
| 13360 | + " importance=" + item.importance |
| 13361 | + " foreground=" + item.foregroundActivities |
| 13362 | + " type=" + app.adjType + " source=" + app.adjSource |
| 13363 | + " target=" + app.adjTarget); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13364 | } |
| 13365 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13366 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13367 | } |
| 13368 | |
| 13369 | /** |
| 13370 | * Ask a given process to GC right now. |
| 13371 | */ |
| 13372 | final void performAppGcLocked(ProcessRecord app) { |
| 13373 | try { |
| 13374 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13375 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13376 | if (app.reportLowMemory) { |
| 13377 | app.reportLowMemory = false; |
| 13378 | app.thread.scheduleLowMemory(); |
| 13379 | } else { |
| 13380 | app.thread.processInBackground(); |
| 13381 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13382 | } |
| 13383 | } catch (Exception e) { |
| 13384 | // whatever. |
| 13385 | } |
| 13386 | } |
| 13387 | |
| 13388 | /** |
| 13389 | * Returns true if things are idle enough to perform GCs. |
| 13390 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13391 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13392 | boolean processingBroadcasts = false; |
| 13393 | for (BroadcastQueue q : mBroadcastQueues) { |
| 13394 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 13395 | processingBroadcasts = true; |
| 13396 | } |
| 13397 | } |
| 13398 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13399 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13400 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13401 | } |
| 13402 | |
| 13403 | /** |
| 13404 | * Perform GCs on all processes that are waiting for it, but only |
| 13405 | * if things are idle. |
| 13406 | */ |
| 13407 | final void performAppGcsLocked() { |
| 13408 | final int N = mProcessesToGc.size(); |
| 13409 | if (N <= 0) { |
| 13410 | return; |
| 13411 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13412 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13413 | while (mProcessesToGc.size() > 0) { |
| 13414 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13415 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13416 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 13417 | <= SystemClock.uptimeMillis()) { |
| 13418 | // To avoid spamming the system, we will GC processes one |
| 13419 | // at a time, waiting a few seconds between each. |
| 13420 | performAppGcLocked(proc); |
| 13421 | scheduleAppGcsLocked(); |
| 13422 | return; |
| 13423 | } else { |
| 13424 | // It hasn't been long enough since we last GCed this |
| 13425 | // process... put it in the list to wait for its time. |
| 13426 | addProcessToGcListLocked(proc); |
| 13427 | break; |
| 13428 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13429 | } |
| 13430 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13431 | |
| 13432 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13433 | } |
| 13434 | } |
| 13435 | |
| 13436 | /** |
| 13437 | * If all looks good, perform GCs on all processes waiting for them. |
| 13438 | */ |
| 13439 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13440 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13441 | performAppGcsLocked(); |
| 13442 | return; |
| 13443 | } |
| 13444 | // Still not idle, wait some more. |
| 13445 | scheduleAppGcsLocked(); |
| 13446 | } |
| 13447 | |
| 13448 | /** |
| 13449 | * Schedule the execution of all pending app GCs. |
| 13450 | */ |
| 13451 | final void scheduleAppGcsLocked() { |
| 13452 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13453 | |
| 13454 | if (mProcessesToGc.size() > 0) { |
| 13455 | // Schedule a GC for the time to the next process. |
| 13456 | ProcessRecord proc = mProcessesToGc.get(0); |
| 13457 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 13458 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13459 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13460 | long now = SystemClock.uptimeMillis(); |
| 13461 | if (when < (now+GC_TIMEOUT)) { |
| 13462 | when = now + GC_TIMEOUT; |
| 13463 | } |
| 13464 | mHandler.sendMessageAtTime(msg, when); |
| 13465 | } |
| 13466 | } |
| 13467 | |
| 13468 | /** |
| 13469 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 13470 | * list in sorted order by the last GC time. The process can't already be |
| 13471 | * on the list. |
| 13472 | */ |
| 13473 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 13474 | boolean added = false; |
| 13475 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 13476 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 13477 | proc.lastRequestedGc) { |
| 13478 | added = true; |
| 13479 | mProcessesToGc.add(i+1, proc); |
| 13480 | break; |
| 13481 | } |
| 13482 | } |
| 13483 | if (!added) { |
| 13484 | mProcessesToGc.add(0, proc); |
| 13485 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13486 | } |
| 13487 | |
| 13488 | /** |
| 13489 | * Set up to ask a process to GC itself. This will either do it |
| 13490 | * immediately, or put it on the list of processes to gc the next |
| 13491 | * time things are idle. |
| 13492 | */ |
| 13493 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 13494 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13495 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13496 | return; |
| 13497 | } |
| 13498 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13499 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13500 | scheduleAppGcsLocked(); |
| 13501 | } |
| 13502 | } |
| 13503 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13504 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 13505 | updateCpuStatsNow(); |
| 13506 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13507 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13508 | boolean doWakeKills = doKills; |
| 13509 | boolean doCpuKills = doKills; |
| 13510 | if (mLastPowerCheckRealtime == 0) { |
| 13511 | doWakeKills = false; |
| 13512 | } |
| 13513 | if (mLastPowerCheckUptime == 0) { |
| 13514 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13515 | } |
| 13516 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13517 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13518 | } |
| 13519 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13520 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 13521 | final long curUptime = SystemClock.uptimeMillis(); |
| 13522 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 13523 | mLastPowerCheckRealtime = curRealtime; |
| 13524 | mLastPowerCheckUptime = curUptime; |
| 13525 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 13526 | doWakeKills = false; |
| 13527 | } |
| 13528 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 13529 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13530 | } |
| 13531 | int i = mLruProcesses.size(); |
| 13532 | while (i > 0) { |
| 13533 | i--; |
| 13534 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13535 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13536 | long wtime; |
| 13537 | synchronized (stats) { |
| 13538 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 13539 | app.pid, curRealtime); |
| 13540 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13541 | long wtimeUsed = wtime - app.lastWakeTime; |
| 13542 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13543 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13544 | StringBuilder sb = new StringBuilder(128); |
| 13545 | sb.append("Wake for "); |
| 13546 | app.toShortString(sb); |
| 13547 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13548 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13549 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13550 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13551 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13552 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13553 | sb.append("%)"); |
| 13554 | Slog.i(TAG, sb.toString()); |
| 13555 | sb.setLength(0); |
| 13556 | sb.append("CPU for "); |
| 13557 | app.toShortString(sb); |
| 13558 | sb.append(": over "); |
| 13559 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13560 | sb.append(" used "); |
| 13561 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13562 | sb.append(" ("); |
| 13563 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13564 | sb.append("%)"); |
| 13565 | Slog.i(TAG, sb.toString()); |
| 13566 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13567 | // If a process has held a wake lock for more |
| 13568 | // than 50% of the time during this period, |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13569 | // that sounds bad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13570 | if (doWakeKills && realtimeSince > 0 |
| 13571 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13572 | synchronized (stats) { |
| 13573 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13574 | realtimeSince, wtimeUsed); |
| 13575 | } |
| 13576 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13577 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13578 | + " during " + realtimeSince); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13579 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13580 | app.processName, app.setAdj, "excessive wake lock"); |
| 13581 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13582 | } else if (doCpuKills && uptimeSince > 0 |
| 13583 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13584 | synchronized (stats) { |
| 13585 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13586 | uptimeSince, cputimeUsed); |
| 13587 | } |
| 13588 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13589 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13590 | + " during " + uptimeSince); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13591 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13592 | app.processName, app.setAdj, "excessive cpu"); |
| 13593 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13594 | } else { |
| 13595 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13596 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13597 | } |
| 13598 | } |
| 13599 | } |
| 13600 | } |
| 13601 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13602 | private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13603 | int clientHiddenAdj, int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13604 | app.hiddenAdj = hiddenAdj; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13605 | app.clientHiddenAdj = clientHiddenAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13606 | app.emptyAdj = emptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13607 | |
| 13608 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13609 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13610 | } |
| 13611 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13612 | final boolean wasKeeping = app.keeping; |
| 13613 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13614 | boolean success = true; |
| 13615 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13616 | computeOomAdjLocked(app, hiddenAdj, clientHiddenAdj, emptyAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13617 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13618 | if (app.curRawAdj != app.setRawAdj) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13619 | if (wasKeeping && !app.keeping) { |
| 13620 | // This app is no longer something we want to keep. Note |
| 13621 | // its current wake lock time to later know to kill it if |
| 13622 | // it is not behaving well. |
| 13623 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13624 | synchronized (stats) { |
| 13625 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13626 | app.pid, SystemClock.elapsedRealtime()); |
| 13627 | } |
| 13628 | app.lastCpuTime = app.curCpuTime; |
| 13629 | } |
| 13630 | |
| 13631 | app.setRawAdj = app.curRawAdj; |
| 13632 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13633 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13634 | if (app.curAdj != app.setAdj) { |
| 13635 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 13636 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13637 | TAG, "Set " + app.pid + " " + app.processName + |
| 13638 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13639 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13640 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13641 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13642 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13643 | } |
| 13644 | } |
| 13645 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13646 | app.setSchedGroup = app.curSchedGroup; |
| 13647 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 13648 | "Setting process group of " + app.processName |
| 13649 | + " to " + app.curSchedGroup); |
| 13650 | if (app.waitingToKill != null && |
| 13651 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13652 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13653 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13654 | app.processName, app.setAdj, app.waitingToKill); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 13655 | app.killedBackground = true; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13656 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13657 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13658 | } else { |
| 13659 | if (true) { |
| 13660 | long oldId = Binder.clearCallingIdentity(); |
| 13661 | try { |
| 13662 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13663 | } catch (Exception e) { |
| 13664 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13665 | + " to " + app.curSchedGroup); |
| 13666 | e.printStackTrace(); |
| 13667 | } finally { |
| 13668 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13669 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13670 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13671 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13672 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13673 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13674 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13675 | } |
| 13676 | } |
| 13677 | } |
| 13678 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13679 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13680 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13681 | } |
| 13682 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13683 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13684 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13685 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 13686 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13687 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13688 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13689 | } |
| 13690 | } |
| 13691 | return resumedActivity; |
| 13692 | } |
| 13693 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13694 | final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13695 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13696 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13697 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13698 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13699 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13700 | |
| 13701 | mAdjSeq++; |
| 13702 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13703 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.clientHiddenAdj, |
| 13704 | app.emptyAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13705 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13706 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13707 | if (nowHidden != wasHidden) { |
| 13708 | // Changed to/from hidden state, so apps after it in the LRU |
| 13709 | // list may also be changed. |
| 13710 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13711 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13712 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13713 | } |
| 13714 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13715 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13716 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13717 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13718 | final long oldTime = SystemClock.uptimeMillis() - ProcessList.MAX_EMPTY_TIME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13719 | |
| 13720 | if (false) { |
| 13721 | RuntimeException e = new RuntimeException(); |
| 13722 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13723 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13724 | } |
| 13725 | |
| 13726 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13727 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13728 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13729 | final int emptyProcessLimit; |
| 13730 | final int hiddenProcessLimit; |
| 13731 | if (mProcessLimit <= 0) { |
| 13732 | emptyProcessLimit = hiddenProcessLimit = 0; |
| 13733 | } else if (mProcessLimit == 1) { |
| 13734 | emptyProcessLimit = 1; |
| 13735 | hiddenProcessLimit = 0; |
| 13736 | } else { |
| 13737 | emptyProcessLimit = (mProcessLimit*2)/3; |
| 13738 | hiddenProcessLimit = mProcessLimit - emptyProcessLimit; |
| 13739 | } |
| 13740 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13741 | // Let's determine how many processes we have running vs. |
| 13742 | // how many slots we have for background processes; we may want |
| 13743 | // to put multiple processes in a slot of there are enough of |
| 13744 | // them. |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13745 | int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ |
| 13746 | - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13747 | int numEmptyProcs = mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs; |
| 13748 | if (numEmptyProcs > hiddenProcessLimit) { |
| 13749 | // If there are more empty processes than our limit on hidden |
| 13750 | // processes, then use the hidden process limit for the factor. |
| 13751 | // This ensures that the really old empty processes get pushed |
| 13752 | // down to the bottom, so if we are running low on memory we will |
| 13753 | // have a better chance at keeping around more hidden processes |
| 13754 | // instead of a gazillion empty processes. |
| 13755 | numEmptyProcs = hiddenProcessLimit; |
| 13756 | } |
| 13757 | int emptyFactor = numEmptyProcs/numSlots; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13758 | if (emptyFactor < 1) emptyFactor = 1; |
| 13759 | int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots; |
| 13760 | if (hiddenFactor < 1) hiddenFactor = 1; |
| 13761 | int stepHidden = 0; |
| 13762 | int stepEmpty = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13763 | int numHidden = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13764 | int numEmpty = 0; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13765 | int numTrimming = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13766 | |
| 13767 | mNumNonHiddenProcs = 0; |
| 13768 | mNumHiddenProcs = 0; |
| 13769 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13770 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13771 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13772 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13773 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13774 | int nextHiddenAdj = curHiddenAdj+1; |
| 13775 | int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
| 13776 | int nextEmptyAdj = curEmptyAdj+2; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13777 | int curClientHiddenAdj = curEmptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13778 | while (i > 0) { |
| 13779 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13780 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13781 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13782 | updateOomAdjLocked(app, curHiddenAdj, curClientHiddenAdj, curEmptyAdj, TOP_APP, true); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13783 | if (!app.killedBackground) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13784 | if (app.curRawAdj == curHiddenAdj && app.hasActivities) { |
| 13785 | // This process was assigned as a hidden process... step the |
| 13786 | // hidden level. |
| 13787 | mNumHiddenProcs++; |
| 13788 | if (curHiddenAdj != nextHiddenAdj) { |
| 13789 | stepHidden++; |
| 13790 | if (stepHidden >= hiddenFactor) { |
| 13791 | stepHidden = 0; |
| 13792 | curHiddenAdj = nextHiddenAdj; |
| 13793 | nextHiddenAdj += 2; |
| 13794 | if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13795 | nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13796 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13797 | if (curClientHiddenAdj <= curHiddenAdj) { |
| 13798 | curClientHiddenAdj = curHiddenAdj + 1; |
| 13799 | if (curClientHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13800 | curClientHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13801 | } |
| 13802 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13803 | } |
| 13804 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13805 | numHidden++; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13806 | if (numHidden > hiddenProcessLimit) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13807 | Slog.i(TAG, "No longer want " + app.processName |
| 13808 | + " (pid " + app.pid + "): hidden #" + numHidden); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13809 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13810 | app.processName, app.setAdj, "too many background"); |
| 13811 | app.killedBackground = true; |
| 13812 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13813 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13814 | } else if (app.curRawAdj == curHiddenAdj && app.hasClientActivities) { |
| 13815 | // This process has a client that has activities. We will have |
| 13816 | // given it the current hidden adj; here we will just leave it |
| 13817 | // without stepping the hidden adj. |
| 13818 | curClientHiddenAdj++; |
| 13819 | if (curClientHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13820 | curClientHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13821 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13822 | } else { |
| 13823 | if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) { |
| 13824 | // This process was assigned as an empty process... step the |
| 13825 | // empty level. |
| 13826 | if (curEmptyAdj != nextEmptyAdj) { |
| 13827 | stepEmpty++; |
| 13828 | if (stepEmpty >= emptyFactor) { |
| 13829 | stepEmpty = 0; |
| 13830 | curEmptyAdj = nextEmptyAdj; |
| 13831 | nextEmptyAdj += 2; |
| 13832 | if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13833 | nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13834 | } |
| 13835 | } |
| 13836 | } |
| 13837 | } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13838 | mNumNonHiddenProcs++; |
| 13839 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13840 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13841 | && !app.hasClientActivities) { |
| 13842 | if (numEmpty > ProcessList.TRIM_EMPTY_APPS |
| 13843 | && app.lastActivityTime < oldTime) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13844 | Slog.i(TAG, "No longer want " + app.processName |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13845 | + " (pid " + app.pid + "): empty for " |
| 13846 | + ((oldTime+ProcessList.MAX_EMPTY_TIME-app.lastActivityTime) |
| 13847 | / 1000) + "s"); |
| 13848 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
| 13849 | app.processName, app.setAdj, "old background process"); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13850 | app.killedBackground = true; |
| 13851 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13852 | } else { |
| 13853 | numEmpty++; |
| 13854 | if (numEmpty > emptyProcessLimit) { |
| 13855 | Slog.i(TAG, "No longer want " + app.processName |
| 13856 | + " (pid " + app.pid + "): empty #" + numEmpty); |
| 13857 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
| 13858 | app.processName, app.setAdj, "too many background"); |
| 13859 | app.killedBackground = true; |
| 13860 | Process.killProcessQuiet(app.pid); |
| 13861 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13862 | } |
| 13863 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13864 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13865 | if (app.isolated && app.services.size() <= 0) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13866 | // If this is an isolated process, and there are no |
| 13867 | // services running in it, then the process is no longer |
| 13868 | // needed. We agressively kill these because we can by |
| 13869 | // definition not re-use the same process again, and it is |
| 13870 | // good to avoid having whatever code was running in them |
| 13871 | // left sitting around after no longer needed. |
| 13872 | Slog.i(TAG, "Isolated process " + app.processName |
| 13873 | + " (pid " + app.pid + ") no longer needed"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13874 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13875 | app.processName, app.setAdj, "isolated not needed"); |
| 13876 | app.killedBackground = true; |
| 13877 | Process.killProcessQuiet(app.pid); |
| 13878 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13879 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13880 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
| 13881 | && !app.killedBackground) { |
| 13882 | numTrimming++; |
| 13883 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13884 | } |
| 13885 | } |
| 13886 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13887 | mNumServiceProcs = mNewNumServiceProcs; |
| 13888 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13889 | // Now determine the memory trimming level of background processes. |
| 13890 | // Unfortunately we need to start at the back of the list to do this |
| 13891 | // properly. We only do this if the number of background apps we |
| 13892 | // are managing to keep around is less than half the maximum we desire; |
| 13893 | // if we are keeping a good number around, we'll let them use whatever |
| 13894 | // memory they want. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13895 | if (numHidden <= ProcessList.TRIM_HIDDEN_APPS |
| 13896 | && numEmpty <= ProcessList.TRIM_EMPTY_APPS) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13897 | final int numHiddenAndEmpty = numHidden + numEmpty; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13898 | final int N = mLruProcesses.size(); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13899 | int factor = numTrimming/3; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13900 | int minFactor = 2; |
| 13901 | if (mHomeProcess != null) minFactor++; |
| 13902 | if (mPreviousProcess != null) minFactor++; |
| 13903 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13904 | int step = 0; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13905 | int fgTrimLevel; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13906 | if (numHiddenAndEmpty <= ProcessList.TRIM_CRITICAL_THRESHOLD) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13907 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13908 | } else if (numHiddenAndEmpty <= ProcessList.TRIM_LOW_THRESHOLD) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13909 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
| 13910 | } else { |
| 13911 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
| 13912 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13913 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13914 | for (i=0; i<N; i++) { |
| 13915 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13916 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13917 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13918 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13919 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 13920 | try { |
| 13921 | app.thread.scheduleTrimMemory(curLevel); |
| 13922 | } catch (RemoteException e) { |
| 13923 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13924 | if (false) { |
| 13925 | // For now we won't do this; our memory trimming seems |
| 13926 | // to be good enough at this point that destroying |
| 13927 | // activities causes more harm than good. |
| 13928 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 13929 | && app != mHomeProcess && app != mPreviousProcess) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13930 | // Need to do this on its own message because the stack may not |
| 13931 | // be in a consistent state at this point. |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13932 | // For these apps we will also finish their activities |
| 13933 | // to help them free memory. |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13934 | mMainStack.scheduleDestroyActivities(app, false, "trim"); |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13935 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13936 | } |
| 13937 | } |
| 13938 | app.trimMemoryLevel = curLevel; |
| 13939 | step++; |
| 13940 | if (step >= factor) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13941 | step = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13942 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13943 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 13944 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13945 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13946 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 13947 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13948 | break; |
| 13949 | } |
| 13950 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13951 | } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13952 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13953 | && app.thread != null) { |
| 13954 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13955 | app.thread.scheduleTrimMemory( |
| 13956 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13957 | } catch (RemoteException e) { |
| 13958 | } |
| 13959 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13960 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13961 | } else { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13962 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13963 | && app.pendingUiClean) { |
| 13964 | // If this application is now in the background and it |
| 13965 | // had done UI, then give it the special trim level to |
| 13966 | // have it free UI resources. |
| 13967 | final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13968 | if (app.trimMemoryLevel < level && app.thread != null) { |
| 13969 | try { |
| 13970 | app.thread.scheduleTrimMemory(level); |
| 13971 | } catch (RemoteException e) { |
| 13972 | } |
| 13973 | } |
| 13974 | app.pendingUiClean = false; |
| 13975 | } |
| 13976 | if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13977 | try { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13978 | app.thread.scheduleTrimMemory(fgTrimLevel); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13979 | } catch (RemoteException e) { |
| 13980 | } |
| 13981 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13982 | app.trimMemoryLevel = fgTrimLevel; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13983 | } |
| 13984 | } |
| 13985 | } else { |
| 13986 | final int N = mLruProcesses.size(); |
| 13987 | for (i=0; i<N; i++) { |
| 13988 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13989 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13990 | && app.pendingUiClean) { |
| 13991 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13992 | && app.thread != null) { |
| 13993 | try { |
| 13994 | app.thread.scheduleTrimMemory( |
| 13995 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 13996 | } catch (RemoteException e) { |
| 13997 | } |
| 13998 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13999 | app.pendingUiClean = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14000 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 14001 | app.trimMemoryLevel = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14002 | } |
| 14003 | } |
| 14004 | |
| 14005 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 14006 | // Need to do this on its own message because the stack may not |
| 14007 | // be in a consistent state at this point. |
| 14008 | mMainStack.scheduleDestroyActivities(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14009 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14010 | } |
| 14011 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14012 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14013 | synchronized (this) { |
| 14014 | int i; |
| 14015 | |
| 14016 | // First remove any unused application processes whose package |
| 14017 | // has been removed. |
| 14018 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 14019 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 14020 | if (app.activities.size() == 0 |
| 14021 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14022 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14023 | TAG, "Exiting empty application process " |
| 14024 | + app.processName + " (" |
| 14025 | + (app.thread != null ? app.thread.asBinder() : null) |
| 14026 | + ")\n"); |
| 14027 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 14028 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 14029 | app.processName, app.setAdj, "empty"); |
| 14030 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14031 | } else { |
| 14032 | try { |
| 14033 | app.thread.scheduleExit(); |
| 14034 | } catch (Exception e) { |
| 14035 | // Ignore exceptions. |
| 14036 | } |
| 14037 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 14038 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14039 | mRemovedProcesses.remove(i); |
| 14040 | |
| 14041 | if (app.persistent) { |
| 14042 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 14043 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14044 | } |
| 14045 | } |
| 14046 | } |
| 14047 | } |
| 14048 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14049 | // Now update the oom adj for all processes. |
| 14050 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14051 | } |
| 14052 | } |
| 14053 | |
| 14054 | /** This method sends the specified signal to each of the persistent apps */ |
| 14055 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 14056 | if (sig != Process.SIGNAL_USR1) { |
| 14057 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 14058 | } |
| 14059 | |
| 14060 | synchronized (this) { |
| 14061 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 14062 | != PackageManager.PERMISSION_GRANTED) { |
| 14063 | throw new SecurityException("Requires permission " |
| 14064 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 14065 | } |
| 14066 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14067 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 14068 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14069 | if (r.thread != null && r.persistent) { |
| 14070 | Process.sendSignal(r.pid, sig); |
| 14071 | } |
| 14072 | } |
| 14073 | } |
| 14074 | } |
| 14075 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14076 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 14077 | if (proc == null || proc == mProfileProc) { |
| 14078 | proc = mProfileProc; |
| 14079 | path = mProfileFile; |
| 14080 | profileType = mProfileType; |
| 14081 | clearProfilerLocked(); |
| 14082 | } |
| 14083 | if (proc == null) { |
| 14084 | return; |
| 14085 | } |
| 14086 | try { |
| 14087 | proc.thread.profilerControl(false, path, null, profileType); |
| 14088 | } catch (RemoteException e) { |
| 14089 | throw new IllegalStateException("Process disappeared"); |
| 14090 | } |
| 14091 | } |
| 14092 | |
| 14093 | private void clearProfilerLocked() { |
| 14094 | if (mProfileFd != null) { |
| 14095 | try { |
| 14096 | mProfileFd.close(); |
| 14097 | } catch (IOException e) { |
| 14098 | } |
| 14099 | } |
| 14100 | mProfileApp = null; |
| 14101 | mProfileProc = null; |
| 14102 | mProfileFile = null; |
| 14103 | mProfileType = 0; |
| 14104 | mAutoStopProfiler = false; |
| 14105 | } |
| 14106 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14107 | public boolean profileControl(String process, int userId, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 14108 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14109 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14110 | try { |
| 14111 | synchronized (this) { |
| 14112 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14113 | // its own permission. |
| 14114 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14115 | != PackageManager.PERMISSION_GRANTED) { |
| 14116 | throw new SecurityException("Requires permission " |
| 14117 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14118 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14119 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14120 | if (start && fd == null) { |
| 14121 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14122 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14123 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14124 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14125 | if (process != null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14126 | proc = findProcessLocked(process, userId, "profileControl"); |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14127 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14128 | |
| 14129 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14130 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14131 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14132 | |
| 14133 | if (start) { |
| 14134 | stopProfilerLocked(null, null, 0); |
| 14135 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 14136 | mProfileProc = proc; |
| 14137 | mProfileType = profileType; |
| 14138 | try { |
| 14139 | fd = fd.dup(); |
| 14140 | } catch (IOException e) { |
| 14141 | fd = null; |
| 14142 | } |
| 14143 | proc.thread.profilerControl(start, path, fd, profileType); |
| 14144 | fd = null; |
| 14145 | mProfileFd = null; |
| 14146 | } else { |
| 14147 | stopProfilerLocked(proc, path, profileType); |
| 14148 | if (fd != null) { |
| 14149 | try { |
| 14150 | fd.close(); |
| 14151 | } catch (IOException e) { |
| 14152 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14153 | } |
| 14154 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14155 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14156 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14157 | } |
| 14158 | } catch (RemoteException e) { |
| 14159 | throw new IllegalStateException("Process disappeared"); |
| 14160 | } finally { |
| 14161 | if (fd != null) { |
| 14162 | try { |
| 14163 | fd.close(); |
| 14164 | } catch (IOException e) { |
| 14165 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14166 | } |
| 14167 | } |
| 14168 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14169 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14170 | private ProcessRecord findProcessLocked(String process, int userId, String callName) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 14171 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14172 | userId, true, true, callName, null); |
| 14173 | ProcessRecord proc = null; |
| 14174 | try { |
| 14175 | int pid = Integer.parseInt(process); |
| 14176 | synchronized (mPidsSelfLocked) { |
| 14177 | proc = mPidsSelfLocked.get(pid); |
| 14178 | } |
| 14179 | } catch (NumberFormatException e) { |
| 14180 | } |
| 14181 | |
| 14182 | if (proc == null) { |
| 14183 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14184 | = mProcessNames.getMap(); |
| 14185 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14186 | if (procs != null && procs.size() > 0) { |
| 14187 | proc = procs.valueAt(0); |
| 14188 | if (userId != UserHandle.USER_ALL && proc.userId != userId) { |
| 14189 | for (int i=1; i<procs.size(); i++) { |
| 14190 | ProcessRecord thisProc = procs.valueAt(i); |
| 14191 | if (thisProc.userId == userId) { |
| 14192 | proc = thisProc; |
| 14193 | break; |
| 14194 | } |
| 14195 | } |
| 14196 | } |
| 14197 | } |
| 14198 | } |
| 14199 | |
| 14200 | return proc; |
| 14201 | } |
| 14202 | |
| 14203 | public boolean dumpHeap(String process, int userId, boolean managed, |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14204 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 14205 | |
| 14206 | try { |
| 14207 | synchronized (this) { |
| 14208 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14209 | // its own permission (same as profileControl). |
| 14210 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14211 | != PackageManager.PERMISSION_GRANTED) { |
| 14212 | throw new SecurityException("Requires permission " |
| 14213 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 14214 | } |
| 14215 | |
| 14216 | if (fd == null) { |
| 14217 | throw new IllegalArgumentException("null fd"); |
| 14218 | } |
| 14219 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14220 | ProcessRecord proc = findProcessLocked(process, userId, "dumpHeap"); |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14221 | if (proc == null || proc.thread == null) { |
| 14222 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14223 | } |
| 14224 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 14225 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 14226 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14227 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 14228 | throw new SecurityException("Process not debuggable: " + proc); |
| 14229 | } |
| 14230 | } |
| 14231 | |
| 14232 | proc.thread.dumpHeap(managed, path, fd); |
| 14233 | fd = null; |
| 14234 | return true; |
| 14235 | } |
| 14236 | } catch (RemoteException e) { |
| 14237 | throw new IllegalStateException("Process disappeared"); |
| 14238 | } finally { |
| 14239 | if (fd != null) { |
| 14240 | try { |
| 14241 | fd.close(); |
| 14242 | } catch (IOException e) { |
| 14243 | } |
| 14244 | } |
| 14245 | } |
| 14246 | } |
| 14247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14248 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 14249 | public void monitor() { |
| 14250 | synchronized (this) { } |
| 14251 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14252 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 14253 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14254 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 14255 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 14256 | try { |
| 14257 | if (processRecord.thread != null) { |
| 14258 | processRecord.thread.setCoreSettings(settings); |
| 14259 | } |
| 14260 | } catch (RemoteException re) { |
| 14261 | /* ignore */ |
| 14262 | } |
| 14263 | } |
| 14264 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14265 | |
| 14266 | // Multi-user methods |
| 14267 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14268 | @Override |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14269 | public boolean switchUser(final int userId) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14270 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14271 | != PackageManager.PERMISSION_GRANTED) { |
| 14272 | String msg = "Permission Denial: switchUser() from pid=" |
| 14273 | + Binder.getCallingPid() |
| 14274 | + ", uid=" + Binder.getCallingUid() |
| 14275 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14276 | Slog.w(TAG, msg); |
| 14277 | throw new SecurityException(msg); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14278 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14279 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14280 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14281 | try { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14282 | synchronized (this) { |
| 14283 | final int oldUserId = mCurrentUserId; |
| 14284 | if (oldUserId == userId) { |
| 14285 | return true; |
| 14286 | } |
| 14287 | |
| 14288 | final UserInfo userInfo = getUserManagerLocked().getUserInfo(userId); |
| 14289 | if (userInfo == null) { |
| 14290 | Slog.w(TAG, "No user info for user #" + userId); |
| 14291 | return false; |
| 14292 | } |
| 14293 | |
| 14294 | mWindowManager.startFreezingScreen(R.anim.screen_user_exit, |
| 14295 | R.anim.screen_user_enter); |
| 14296 | |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14297 | boolean needStart = false; |
| 14298 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14299 | // If the user we are switching to is not currently started, then |
| 14300 | // we need to start it now. |
| 14301 | if (mStartedUsers.get(userId) == null) { |
| 14302 | mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14303 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14304 | needStart = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14305 | } |
| 14306 | |
| 14307 | mCurrentUserId = userId; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14308 | mCurrentUserArray = new int[] { userId }; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14309 | final Integer userIdInt = Integer.valueOf(userId); |
| 14310 | mUserLru.remove(userIdInt); |
| 14311 | mUserLru.add(userIdInt); |
| 14312 | |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14313 | mWindowManager.setCurrentUser(userId); |
| 14314 | |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 14315 | // Once the internal notion of the active user has switched, we lock the device |
| 14316 | // with the option to show the user switcher on the keyguard. |
Winson Chung | f7614fc | 2012-11-26 14:43:24 -0800 | [diff] [blame] | 14317 | mWindowManager.lockNow(null); |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 14318 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14319 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14320 | |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14321 | // Make sure user is in the started state. If it is currently |
| 14322 | // stopping, we need to knock that off. |
| 14323 | if (uss.mState == UserStartedState.STATE_STOPPING) { |
| 14324 | // If we are stopping, we haven't sent ACTION_SHUTDOWN, |
| 14325 | // so we can just fairly silently bring the user back from |
| 14326 | // the almost-dead. |
| 14327 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14328 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14329 | needStart = true; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14330 | } else if (uss.mState == UserStartedState.STATE_SHUTDOWN) { |
| 14331 | // This means ACTION_SHUTDOWN has been sent, so we will |
| 14332 | // need to treat this as a new boot of the user. |
| 14333 | uss.mState = UserStartedState.STATE_BOOTING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14334 | updateStartedUserArrayLocked(); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14335 | needStart = true; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14336 | } |
| 14337 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14338 | mHandler.removeMessages(REPORT_USER_SWITCH_MSG); |
| 14339 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14340 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG, |
| 14341 | oldUserId, userId, uss)); |
| 14342 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG, |
| 14343 | oldUserId, userId, uss), USER_SWITCH_TIMEOUT); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14344 | if (needStart) { |
| 14345 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
| 14346 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14347 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 14348 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14349 | broadcastIntentLocked(null, null, intent, |
| 14350 | null, null, 0, null, null, null, |
| 14351 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14352 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14353 | |
| 14354 | if ((userInfo.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
| 14355 | if (userId != 0) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14356 | Intent intent = new Intent(Intent.ACTION_USER_INITIALIZE); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14357 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14358 | broadcastIntentLocked(null, null, intent, null, |
| 14359 | new IIntentReceiver.Stub() { |
| 14360 | public void performReceive(Intent intent, int resultCode, |
| 14361 | String data, Bundle extras, boolean ordered, |
| 14362 | boolean sticky, int sendingUser) { |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14363 | userInitialized(uss, userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14364 | } |
| 14365 | }, 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 14366 | userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14367 | uss.initializing = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14368 | } else { |
| 14369 | getUserManagerLocked().makeInitialized(userInfo.id); |
| 14370 | } |
| 14371 | } |
| 14372 | |
| 14373 | boolean haveActivities = mMainStack.switchUserLocked(userId, uss); |
| 14374 | if (!haveActivities) { |
| 14375 | startHomeActivityLocked(userId); |
| 14376 | } |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14377 | |
Jeff Sharkey | 86597df | 2012-11-09 15:00:31 -0800 | [diff] [blame] | 14378 | EventLogTags.writeAmSwitchUser(userId); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 14379 | getUserManagerLocked().userForeground(userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14380 | sendUserSwitchBroadcastsLocked(oldUserId, userId); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14381 | if (needStart) { |
| 14382 | Intent intent = new Intent(Intent.ACTION_USER_STARTING); |
| 14383 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14384 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14385 | broadcastIntentLocked(null, null, intent, |
| 14386 | null, new IIntentReceiver.Stub() { |
| 14387 | @Override |
| 14388 | public void performReceive(Intent intent, int resultCode, String data, |
| 14389 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) |
| 14390 | throws RemoteException { |
| 14391 | } |
| 14392 | }, 0, null, null, |
| 14393 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 14394 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 14395 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14396 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14397 | } finally { |
| 14398 | Binder.restoreCallingIdentity(ident); |
| 14399 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14400 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14401 | return true; |
| 14402 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14403 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14404 | void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) { |
| 14405 | long ident = Binder.clearCallingIdentity(); |
| 14406 | try { |
| 14407 | Intent intent; |
| 14408 | if (oldUserId >= 0) { |
| 14409 | intent = new Intent(Intent.ACTION_USER_BACKGROUND); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14410 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14411 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14412 | intent.putExtra(Intent.EXTRA_USER_HANDLE, oldUserId); |
| 14413 | broadcastIntentLocked(null, null, intent, |
| 14414 | null, null, 0, null, null, null, |
| 14415 | false, false, MY_PID, Process.SYSTEM_UID, oldUserId); |
| 14416 | } |
| 14417 | if (newUserId >= 0) { |
| 14418 | intent = new Intent(Intent.ACTION_USER_FOREGROUND); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14419 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14420 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14421 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14422 | broadcastIntentLocked(null, null, intent, |
| 14423 | null, null, 0, null, null, null, |
| 14424 | false, false, MY_PID, Process.SYSTEM_UID, newUserId); |
| 14425 | intent = new Intent(Intent.ACTION_USER_SWITCHED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14426 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14427 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14428 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14429 | broadcastIntentLocked(null, null, intent, |
| 14430 | null, null, 0, null, null, |
| 14431 | android.Manifest.permission.MANAGE_USERS, |
| 14432 | false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
| 14433 | } |
| 14434 | } finally { |
| 14435 | Binder.restoreCallingIdentity(ident); |
| 14436 | } |
| 14437 | } |
| 14438 | |
| 14439 | void dispatchUserSwitch(final UserStartedState uss, final int oldUserId, |
| 14440 | final int newUserId) { |
| 14441 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14442 | if (N > 0) { |
| 14443 | final IRemoteCallback callback = new IRemoteCallback.Stub() { |
| 14444 | int mCount = 0; |
| 14445 | @Override |
| 14446 | public void sendResult(Bundle data) throws RemoteException { |
| 14447 | synchronized (ActivityManagerService.this) { |
| 14448 | if (mCurUserSwitchCallback == this) { |
| 14449 | mCount++; |
| 14450 | if (mCount == N) { |
| 14451 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14452 | } |
| 14453 | } |
| 14454 | } |
| 14455 | } |
| 14456 | }; |
| 14457 | synchronized (this) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14458 | uss.switching = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14459 | mCurUserSwitchCallback = callback; |
| 14460 | } |
| 14461 | for (int i=0; i<N; i++) { |
| 14462 | try { |
| 14463 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitching( |
| 14464 | newUserId, callback); |
| 14465 | } catch (RemoteException e) { |
| 14466 | } |
| 14467 | } |
| 14468 | } else { |
| 14469 | synchronized (this) { |
| 14470 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14471 | } |
| 14472 | } |
| 14473 | mUserSwitchObservers.finishBroadcast(); |
| 14474 | } |
| 14475 | |
| 14476 | void timeoutUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
| 14477 | synchronized (this) { |
| 14478 | Slog.w(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId); |
| 14479 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14480 | } |
| 14481 | } |
| 14482 | |
| 14483 | void sendContinueUserSwitchLocked(UserStartedState uss, int oldUserId, int newUserId) { |
| 14484 | mCurUserSwitchCallback = null; |
| 14485 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14486 | mHandler.sendMessage(mHandler.obtainMessage(CONTINUE_USER_SWITCH_MSG, |
| 14487 | oldUserId, newUserId, uss)); |
| 14488 | } |
| 14489 | |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14490 | void userInitialized(UserStartedState uss, int newUserId) { |
| 14491 | completeSwitchAndInitalize(uss, newUserId, true, false); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14492 | } |
| 14493 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14494 | void continueUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14495 | completeSwitchAndInitalize(uss, newUserId, false, true); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 14496 | } |
| 14497 | |
Dianne Hackborn | 4d78abf | 2012-11-29 15:10:18 -0800 | [diff] [blame] | 14498 | void completeSwitchAndInitalize(UserStartedState uss, int newUserId, |
| 14499 | boolean clearInitializing, boolean clearSwitching) { |
| 14500 | boolean unfrozen = false; |
| 14501 | synchronized (this) { |
| 14502 | if (clearInitializing) { |
| 14503 | uss.initializing = false; |
| 14504 | getUserManagerLocked().makeInitialized(uss.mHandle.getIdentifier()); |
| 14505 | } |
| 14506 | if (clearSwitching) { |
| 14507 | uss.switching = false; |
| 14508 | } |
| 14509 | if (!uss.switching && !uss.initializing) { |
| 14510 | mWindowManager.stopFreezingScreen(); |
| 14511 | unfrozen = true; |
| 14512 | } |
| 14513 | } |
| 14514 | if (unfrozen) { |
| 14515 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14516 | for (int i=0; i<N; i++) { |
| 14517 | try { |
| 14518 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(newUserId); |
| 14519 | } catch (RemoteException e) { |
| 14520 | } |
| 14521 | } |
| 14522 | mUserSwitchObservers.finishBroadcast(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14523 | } |
| 14524 | } |
| 14525 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14526 | void finishUserSwitch(UserStartedState uss) { |
| 14527 | synchronized (this) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14528 | if (uss.mState == UserStartedState.STATE_BOOTING |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14529 | && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) { |
| 14530 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14531 | final int userId = uss.mHandle.getIdentifier(); |
| 14532 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 14533 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14534 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14535 | null, null, 0, null, null, |
| 14536 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14537 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14538 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14539 | int num = mUserLru.size(); |
| 14540 | int i = 0; |
| 14541 | while (num > MAX_RUNNING_USERS && i < mUserLru.size()) { |
| 14542 | Integer oldUserId = mUserLru.get(i); |
| 14543 | UserStartedState oldUss = mStartedUsers.get(oldUserId); |
| 14544 | if (oldUss == null) { |
| 14545 | // Shouldn't happen, but be sane if it does. |
| 14546 | mUserLru.remove(i); |
| 14547 | num--; |
| 14548 | continue; |
| 14549 | } |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14550 | if (oldUss.mState == UserStartedState.STATE_STOPPING |
| 14551 | || oldUss.mState == UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14552 | // This user is already stopping, doesn't count. |
| 14553 | num--; |
| 14554 | i++; |
| 14555 | continue; |
| 14556 | } |
| 14557 | if (oldUserId == UserHandle.USER_OWNER || oldUserId == mCurrentUserId) { |
| 14558 | // Owner and current can't be stopped, but count as running. |
| 14559 | i++; |
| 14560 | continue; |
| 14561 | } |
| 14562 | // This is a user to be stopped. |
| 14563 | stopUserLocked(oldUserId, null); |
| 14564 | num--; |
| 14565 | i++; |
| 14566 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14567 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14568 | } |
| 14569 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14570 | @Override |
| 14571 | public int stopUser(final int userId, final IStopUserCallback callback) { |
| 14572 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14573 | != PackageManager.PERMISSION_GRANTED) { |
| 14574 | String msg = "Permission Denial: switchUser() from pid=" |
| 14575 | + Binder.getCallingPid() |
| 14576 | + ", uid=" + Binder.getCallingUid() |
| 14577 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14578 | Slog.w(TAG, msg); |
| 14579 | throw new SecurityException(msg); |
| 14580 | } |
| 14581 | if (userId <= 0) { |
| 14582 | throw new IllegalArgumentException("Can't stop primary user " + userId); |
| 14583 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14584 | synchronized (this) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14585 | return stopUserLocked(userId, callback); |
| 14586 | } |
| 14587 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14588 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14589 | private int stopUserLocked(final int userId, final IStopUserCallback callback) { |
| 14590 | if (mCurrentUserId == userId) { |
| 14591 | return ActivityManager.USER_OP_IS_CURRENT; |
| 14592 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14593 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14594 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14595 | if (uss == null) { |
| 14596 | // User is not started, nothing to do... but we do need to |
| 14597 | // callback if requested. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14598 | if (callback != null) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14599 | mHandler.post(new Runnable() { |
| 14600 | @Override |
| 14601 | public void run() { |
| 14602 | try { |
| 14603 | callback.userStopped(userId); |
| 14604 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14605 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14606 | } |
| 14607 | }); |
| 14608 | } |
| 14609 | return ActivityManager.USER_OP_SUCCESS; |
| 14610 | } |
| 14611 | |
| 14612 | if (callback != null) { |
| 14613 | uss.mStopCallbacks.add(callback); |
| 14614 | } |
| 14615 | |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14616 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14617 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14618 | uss.mState = UserStartedState.STATE_STOPPING; |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14619 | updateStartedUserArrayLocked(); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14620 | |
| 14621 | long ident = Binder.clearCallingIdentity(); |
| 14622 | try { |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14623 | // We are going to broadcast ACTION_USER_STOPPING and then |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 14624 | // once that is done send a final ACTION_SHUTDOWN and then |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14625 | // stop the user. |
| 14626 | final Intent stoppingIntent = new Intent(Intent.ACTION_USER_STOPPING); |
| 14627 | stoppingIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 14628 | stoppingIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14629 | final Intent shutdownIntent = new Intent(Intent.ACTION_SHUTDOWN); |
| 14630 | // This is the result receiver for the final shutdown broadcast. |
| 14631 | final IIntentReceiver shutdownReceiver = new IIntentReceiver.Stub() { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14632 | @Override |
| 14633 | public void performReceive(Intent intent, int resultCode, String data, |
| 14634 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 14635 | finishUserStop(uss); |
| 14636 | } |
| 14637 | }; |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14638 | // This is the result receiver for the initial stopping broadcast. |
| 14639 | final IIntentReceiver stoppingReceiver = new IIntentReceiver.Stub() { |
| 14640 | @Override |
| 14641 | public void performReceive(Intent intent, int resultCode, String data, |
| 14642 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 14643 | // On to the next. |
| 14644 | synchronized (ActivityManagerService.this) { |
| 14645 | if (uss.mState != UserStartedState.STATE_STOPPING) { |
| 14646 | // Whoops, we are being started back up. Abort, abort! |
| 14647 | return; |
| 14648 | } |
| 14649 | uss.mState = UserStartedState.STATE_SHUTDOWN; |
| 14650 | } |
| 14651 | broadcastIntentLocked(null, null, shutdownIntent, |
| 14652 | null, shutdownReceiver, 0, null, null, null, |
| 14653 | true, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14654 | } |
| 14655 | }; |
| 14656 | // Kick things off. |
| 14657 | broadcastIntentLocked(null, null, stoppingIntent, |
| 14658 | null, stoppingReceiver, 0, null, null, |
| 14659 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 14660 | true, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14661 | } finally { |
| 14662 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14663 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14664 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14665 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14666 | return ActivityManager.USER_OP_SUCCESS; |
| 14667 | } |
| 14668 | |
| 14669 | void finishUserStop(UserStartedState uss) { |
| 14670 | final int userId = uss.mHandle.getIdentifier(); |
| 14671 | boolean stopped; |
| 14672 | ArrayList<IStopUserCallback> callbacks; |
| 14673 | synchronized (this) { |
| 14674 | callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks); |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14675 | if (mStartedUsers.get(userId) != uss) { |
| 14676 | stopped = false; |
| 14677 | } else if (uss.mState != UserStartedState.STATE_SHUTDOWN) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14678 | stopped = false; |
| 14679 | } else { |
| 14680 | stopped = true; |
| 14681 | // User can no longer run. |
| 14682 | mStartedUsers.remove(userId); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14683 | mUserLru.remove(Integer.valueOf(userId)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14684 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14685 | |
| 14686 | // Clean up all state and processes associated with the user. |
| 14687 | // Kill all the processes for the user. |
| 14688 | forceStopUserLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14689 | } |
| 14690 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14691 | |
| 14692 | for (int i=0; i<callbacks.size(); i++) { |
| 14693 | try { |
| 14694 | if (stopped) callbacks.get(i).userStopped(userId); |
| 14695 | else callbacks.get(i).userStopAborted(userId); |
| 14696 | } catch (RemoteException e) { |
| 14697 | } |
| 14698 | } |
| 14699 | } |
| 14700 | |
| 14701 | @Override |
| 14702 | public UserInfo getCurrentUser() { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14703 | if ((checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14704 | != PackageManager.PERMISSION_GRANTED) && ( |
| 14705 | checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14706 | != PackageManager.PERMISSION_GRANTED)) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14707 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 14708 | + Binder.getCallingPid() |
| 14709 | + ", uid=" + Binder.getCallingUid() |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14710 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14711 | Slog.w(TAG, msg); |
| 14712 | throw new SecurityException(msg); |
| 14713 | } |
| 14714 | synchronized (this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14715 | return getUserManagerLocked().getUserInfo(mCurrentUserId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14716 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14717 | } |
| 14718 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 14719 | int getCurrentUserIdLocked() { |
| 14720 | return mCurrentUserId; |
| 14721 | } |
| 14722 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14723 | @Override |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14724 | public boolean isUserRunning(int userId, boolean orStopped) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14725 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14726 | != PackageManager.PERMISSION_GRANTED) { |
| 14727 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14728 | + Binder.getCallingPid() |
| 14729 | + ", uid=" + Binder.getCallingUid() |
| 14730 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14731 | Slog.w(TAG, msg); |
| 14732 | throw new SecurityException(msg); |
| 14733 | } |
| 14734 | synchronized (this) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14735 | return isUserRunningLocked(userId, orStopped); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14736 | } |
| 14737 | } |
| 14738 | |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14739 | boolean isUserRunningLocked(int userId, boolean orStopped) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14740 | UserStartedState state = mStartedUsers.get(userId); |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14741 | if (state == null) { |
| 14742 | return false; |
| 14743 | } |
| 14744 | if (orStopped) { |
| 14745 | return true; |
| 14746 | } |
| 14747 | return state.mState != UserStartedState.STATE_STOPPING |
Dianne Hackborn | 36d337a | 2012-10-08 14:33:47 -0700 | [diff] [blame] | 14748 | && state.mState != UserStartedState.STATE_SHUTDOWN; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14749 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14750 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14751 | @Override |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14752 | public int[] getRunningUserIds() { |
| 14753 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14754 | != PackageManager.PERMISSION_GRANTED) { |
| 14755 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14756 | + Binder.getCallingPid() |
| 14757 | + ", uid=" + Binder.getCallingUid() |
| 14758 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14759 | Slog.w(TAG, msg); |
| 14760 | throw new SecurityException(msg); |
| 14761 | } |
| 14762 | synchronized (this) { |
| 14763 | return mStartedUserArray; |
| 14764 | } |
| 14765 | } |
| 14766 | |
| 14767 | private void updateStartedUserArrayLocked() { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14768 | int num = 0; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14769 | for (int i=0; i<mStartedUsers.size(); i++) { |
Dianne Hackborn | a8a9bd6 | 2012-10-09 15:36:59 -0700 | [diff] [blame] | 14770 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 14771 | // This list does not include stopping users. |
| 14772 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14773 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
| 14774 | num++; |
| 14775 | } |
| 14776 | } |
| 14777 | mStartedUserArray = new int[num]; |
| 14778 | num = 0; |
| 14779 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 14780 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 14781 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14782 | && uss.mState != UserStartedState.STATE_SHUTDOWN) { |
| 14783 | mStartedUserArray[num] = mStartedUsers.keyAt(i); |
| 14784 | num++; |
| 14785 | } |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14786 | } |
| 14787 | } |
| 14788 | |
| 14789 | @Override |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14790 | public void registerUserSwitchObserver(IUserSwitchObserver observer) { |
| 14791 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14792 | != PackageManager.PERMISSION_GRANTED) { |
| 14793 | String msg = "Permission Denial: registerUserSwitchObserver() from pid=" |
| 14794 | + Binder.getCallingPid() |
| 14795 | + ", uid=" + Binder.getCallingUid() |
| 14796 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14797 | Slog.w(TAG, msg); |
| 14798 | throw new SecurityException(msg); |
| 14799 | } |
| 14800 | |
| 14801 | mUserSwitchObservers.register(observer); |
| 14802 | } |
| 14803 | |
| 14804 | @Override |
| 14805 | public void unregisterUserSwitchObserver(IUserSwitchObserver observer) { |
| 14806 | mUserSwitchObservers.unregister(observer); |
| 14807 | } |
| 14808 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14809 | private boolean userExists(int userId) { |
| 14810 | if (userId == 0) { |
| 14811 | return true; |
| 14812 | } |
| 14813 | UserManagerService ums = getUserManagerLocked(); |
| 14814 | return ums != null ? (ums.getUserInfo(userId) != null) : false; |
| 14815 | } |
| 14816 | |
| 14817 | int[] getUsersLocked() { |
| 14818 | UserManagerService ums = getUserManagerLocked(); |
| 14819 | return ums != null ? ums.getUserIds() : new int[] { 0 }; |
| 14820 | } |
| 14821 | |
| 14822 | UserManagerService getUserManagerLocked() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14823 | if (mUserManager == null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14824 | IBinder b = ServiceManager.getService(Context.USER_SERVICE); |
| 14825 | mUserManager = (UserManagerService)IUserManager.Stub.asInterface(b); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14826 | } |
| 14827 | return mUserManager; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14828 | } |
| 14829 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14830 | private void checkValidCaller(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14831 | if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14832 | |
| 14833 | throw new SecurityException("Caller uid=" + uid |
| 14834 | + " is not privileged to communicate with user=" + userId); |
| 14835 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14836 | |
| 14837 | private int applyUserId(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14838 | return UserHandle.getUid(userId, uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14839 | } |
| 14840 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 14841 | ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14842 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14843 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 14844 | newInfo.uid = applyUserId(info.uid, userId); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14845 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 14846 | + info.packageName; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14847 | return newInfo; |
| 14848 | } |
| 14849 | |
| 14850 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14851 | if (aInfo == null |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14852 | || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14853 | return aInfo; |
| 14854 | } |
| 14855 | |
| 14856 | ActivityInfo info = new ActivityInfo(aInfo); |
| 14857 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 14858 | return info; |
| 14859 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14860 | } |