The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 20 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 21 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | 45ce864 | 2011-07-14 16:10:16 -0700 | [diff] [blame] | 23 | import com.android.internal.os.ProcessStats; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 24 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import com.android.server.IntentResolver; |
| 26 | import com.android.server.ProcessMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import com.android.server.SystemServer; |
| 28 | import com.android.server.Watchdog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 29 | import com.android.server.am.ActivityStack.ActivityState; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 30 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 32 | import dalvik.system.Zygote; |
| 33 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.app.Activity; |
| 35 | import android.app.ActivityManager; |
| 36 | import android.app.ActivityManagerNative; |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 37 | import android.app.ActivityOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.app.ActivityThread; |
| 39 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 40 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 41 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 43 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.app.IApplicationThread; |
| 45 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 46 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 47 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.app.IServiceConnection; |
| 49 | import android.app.IThumbnailReceiver; |
| 50 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 51 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 52 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.app.PendingIntent; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 54 | import android.app.Service; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 55 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 56 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 57 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 58 | import android.content.ClipData; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 59 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.content.ComponentName; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 61 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.content.ContentResolver; |
| 63 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 64 | import android.content.DialogInterface; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 65 | import android.content.IContentProvider; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 66 | import android.content.IIntentReceiver; |
| 67 | import android.content.IIntentSender; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 68 | import android.content.Intent; |
| 69 | import android.content.IntentFilter; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 70 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.content.pm.ActivityInfo; |
| 72 | import android.content.pm.ApplicationInfo; |
| 73 | import android.content.pm.ConfigurationInfo; |
| 74 | import android.content.pm.IPackageDataObserver; |
| 75 | import android.content.pm.IPackageManager; |
| 76 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 77 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import android.content.pm.PackageManager; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 79 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 80 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import android.content.pm.ProviderInfo; |
| 82 | import android.content.pm.ResolveInfo; |
| 83 | import android.content.pm.ServiceInfo; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 84 | import android.content.pm.UserInfo; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 85 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.content.res.Configuration; |
| 87 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 88 | import android.net.Proxy; |
| 89 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.net.Uri; |
| 91 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 92 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 93 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 94 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 95 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 97 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.os.FileUtils; |
| 99 | import android.os.Handler; |
| 100 | import android.os.IBinder; |
| 101 | import android.os.IPermissionController; |
| 102 | import android.os.Looper; |
| 103 | import android.os.Message; |
| 104 | import android.os.Parcel; |
| 105 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 107 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.os.RemoteException; |
| 109 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 110 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.os.SystemClock; |
| 112 | import android.os.SystemProperties; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 113 | import android.os.UserId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | import android.provider.Settings; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 115 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | import android.util.EventLog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 117 | import android.util.Log; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 118 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | import android.util.PrintWriterPrinter; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 120 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | import android.util.SparseArray; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 122 | import android.util.SparseIntArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 123 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | import android.view.Gravity; |
| 125 | import android.view.LayoutInflater; |
| 126 | import android.view.View; |
| 127 | import android.view.WindowManager; |
| 128 | import android.view.WindowManagerPolicy; |
| 129 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 130 | import java.io.BufferedInputStream; |
| 131 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 132 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 133 | import java.io.DataInputStream; |
| 134 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | import java.io.File; |
| 136 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 137 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 139 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 140 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 141 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 143 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | import java.lang.ref.WeakReference; |
| 145 | import java.util.ArrayList; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 146 | import java.util.Collection; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 147 | import java.util.Collections; |
| 148 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | import java.util.HashMap; |
| 150 | import java.util.HashSet; |
| 151 | import java.util.Iterator; |
| 152 | import java.util.List; |
| 153 | import java.util.Locale; |
| 154 | import java.util.Map; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 155 | import java.util.Map.Entry; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 156 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 157 | import java.util.concurrent.atomic.AtomicBoolean; |
| 158 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 160 | public final class ActivityManagerService extends ActivityManagerNative |
| 161 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 162 | private static final String USER_DATA_DIR = "/data/user/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | static final String TAG = "ActivityManager"; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 164 | static final String TAG_MU = "ActivityManagerServiceMU"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 166 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 168 | static final boolean DEBUG_TASKS = localLOGV || false; |
| 169 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 170 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 171 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 172 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 173 | static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 174 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 176 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 178 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 179 | static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 180 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 181 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 183 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 184 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 185 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 186 | static final boolean DEBUG_POWER = localLOGV || false; |
| 187 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 188 | static final boolean DEBUG_MU = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | static final boolean VALIDATE_TOKENS = false; |
| 190 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 191 | |
| 192 | // Control over CPU and battery monitoring. |
| 193 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 194 | static final boolean MONITOR_CPU_USAGE = true; |
| 195 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 196 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 197 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 198 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 199 | // 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] | 200 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 201 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 202 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 204 | static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); |
| 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | // Maximum number of recent tasks that we can remember. |
| 207 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 208 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 209 | // Amount of time after a call to stopAppSwitches() during which we will |
| 210 | // prevent further untrusted switches from happening. |
| 211 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | |
| 213 | // How long we wait for a launched process to attach to the activity manager |
| 214 | // before we decide it's never going to come up for real. |
| 215 | static final int PROC_START_TIMEOUT = 10*1000; |
| 216 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 217 | // How long we wait for a launched process to attach to the activity manager |
| 218 | // before we decide it's never going to come up for real, when the process was |
| 219 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 220 | // could take much longer than usual. |
| 221 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // How long to wait after going idle before forcing apps to GC. |
| 224 | static final int GC_TIMEOUT = 5*1000; |
| 225 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 226 | // The minimum amount of time between successive GC requests for a process. |
| 227 | static final int GC_MIN_INTERVAL = 60*1000; |
| 228 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 229 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 230 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 231 | |
| 232 | // The minimum sample duration we will allow before deciding we have |
| 233 | // enough data on wake locks to start killing things. |
| 234 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 235 | |
| 236 | // The minimum sample duration we will allow before deciding we have |
| 237 | // enough data on CPU usage to start killing things. |
| 238 | 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] | 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | // 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] | 241 | static final int BROADCAST_FG_TIMEOUT = 10*1000; |
| 242 | static final int BROADCAST_BG_TIMEOUT = 60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | // How long we wait until we timeout on key dispatching. |
| 245 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 248 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 249 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 250 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | |
| 252 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 253 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 254 | public ActivityStack mMainStack; |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 255 | |
| 256 | private final boolean mHeadless; |
| 257 | |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 258 | // Whether we should show our dialogs (ANR, crash, etc) or just perform their |
| 259 | // default actuion automatically. Important for devices without direct input |
| 260 | // devices. |
| 261 | private boolean mShowDialogs = true; |
| 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 264 | * Description of a request to start a new activity, which has been held |
| 265 | * due to app switches being disabled. |
| 266 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 267 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 268 | ActivityRecord r; |
| 269 | ActivityRecord sourceRecord; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 270 | int startFlags; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 274 | = new ArrayList<PendingActivityLaunch>(); |
| 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 277 | BroadcastQueue mFgBroadcastQueue; |
| 278 | BroadcastQueue mBgBroadcastQueue; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 279 | // Convenient for easy iteration over the queues. Foreground is first |
| 280 | // so that dispatch of foreground broadcasts gets precedence. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 281 | final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 282 | |
| 283 | BroadcastQueue broadcastQueueForIntent(Intent intent) { |
| 284 | final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; |
| 285 | if (DEBUG_BACKGROUND_BROADCAST) { |
| 286 | Slog.i(TAG, "Broadcast intent " + intent + " on " |
| 287 | + (isFg ? "foreground" : "background") |
| 288 | + " queue"); |
| 289 | } |
| 290 | return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; |
| 291 | } |
| 292 | |
| 293 | BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) { |
| 294 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 295 | BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver); |
| 296 | if (r != null) { |
| 297 | return r; |
| 298 | } |
| 299 | } |
| 300 | return null; |
| 301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
| 303 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | * Activity we have told the window manager to have key focus. |
| 305 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 306 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 307 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | * List of intents that were used to start the most recent tasks. |
| 309 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 310 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | |
| 312 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 313 | * Process management. |
| 314 | */ |
| 315 | final ProcessList mProcessList = new ProcessList(); |
| 316 | |
| 317 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | * All of the applications we currently have running organized by name. |
| 319 | * The keys are strings of the application package name (as |
| 320 | * returned by the package manager), and the keys are ApplicationRecord |
| 321 | * objects. |
| 322 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 323 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | |
| 325 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 326 | * The currently running isolated processes. |
| 327 | */ |
| 328 | final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>(); |
| 329 | |
| 330 | /** |
| 331 | * Counter for assigning isolated process uids, to avoid frequently reusing the |
| 332 | * same ones. |
| 333 | */ |
| 334 | int mNextIsolatedProcessUid = 0; |
| 335 | |
| 336 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 337 | * The currently running heavy-weight process, if any. |
| 338 | */ |
| 339 | ProcessRecord mHeavyWeightProcess = null; |
| 340 | |
| 341 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | * The last time that various processes have crashed. |
| 343 | */ |
| 344 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 345 | |
| 346 | /** |
| 347 | * Set of applications that we consider to be bad, and will reject |
| 348 | * incoming broadcasts from (which the user has no control over). |
| 349 | * Processes are added to this set when they have crashed twice within |
| 350 | * a minimum amount of time; they are removed from it when they are |
| 351 | * later restarted (hopefully due to some user action). The value is the |
| 352 | * time it was added to the list. |
| 353 | */ |
| 354 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 355 | |
| 356 | /** |
| 357 | * All of the processes we currently have running organized by pid. |
| 358 | * The keys are the pid running the application. |
| 359 | * |
| 360 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 361 | * activity manager lock! |
| 362 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 363 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * All of the processes that have been forced to be foreground. The key |
| 367 | * is the pid of the caller who requested it (we hold a death |
| 368 | * link on it). |
| 369 | */ |
| 370 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 371 | int pid; |
| 372 | IBinder token; |
| 373 | } |
| 374 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 375 | = new SparseArray<ForegroundToken>(); |
| 376 | |
| 377 | /** |
| 378 | * List of records for processes that someone had tried to start before the |
| 379 | * system was ready. We don't start them at that point, but ensure they |
| 380 | * are started by the time booting is complete. |
| 381 | */ |
| 382 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 383 | = new ArrayList<ProcessRecord>(); |
| 384 | |
| 385 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | * List of persistent applications that are in the process |
| 387 | * of being started. |
| 388 | */ |
| 389 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 390 | = new ArrayList<ProcessRecord>(); |
| 391 | |
| 392 | /** |
| 393 | * Processes that are being forcibly torn down. |
| 394 | */ |
| 395 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 396 | = new ArrayList<ProcessRecord>(); |
| 397 | |
| 398 | /** |
| 399 | * List of running applications, sorted by recent usage. |
| 400 | * The first entry in the list is the least recently used. |
| 401 | * It contains ApplicationRecord objects. This list does NOT include |
| 402 | * any persistent application records (since we never want to exit them). |
| 403 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 404 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | = new ArrayList<ProcessRecord>(); |
| 406 | |
| 407 | /** |
| 408 | * List of processes that should gc as soon as things are idle. |
| 409 | */ |
| 410 | final ArrayList<ProcessRecord> mProcessesToGc |
| 411 | = new ArrayList<ProcessRecord>(); |
| 412 | |
| 413 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 414 | * This is the process holding what we currently consider to be |
| 415 | * the "home" activity. |
| 416 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 417 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 418 | |
| 419 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 420 | * This is the process holding the activity the user last visited that |
| 421 | * is in a different process from the one they are currently in. |
| 422 | */ |
| 423 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * The time at which the previous process was last visible. |
| 427 | */ |
| 428 | long mPreviousProcessVisibleTime; |
| 429 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 430 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 431 | * Packages that the user has asked to have run in screen size |
| 432 | * compatibility mode instead of filling the screen. |
| 433 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 434 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 435 | |
| 436 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | * Set of PendingResultRecord objects that are currently active. |
| 438 | */ |
| 439 | final HashSet mPendingResultRecords = new HashSet(); |
| 440 | |
| 441 | /** |
| 442 | * Set of IntentSenderRecord objects that are currently active. |
| 443 | */ |
| 444 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 445 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 446 | |
| 447 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 448 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 449 | * already logged DropBox entries for. Guarded by itself. If |
| 450 | * something (rogue user app) forces this over |
| 451 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 452 | */ |
| 453 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 454 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 455 | |
| 456 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 457 | * Strict Mode background batched logging state. |
| 458 | * |
| 459 | * The string buffer is guarded by itself, and its lock is also |
| 460 | * used to determine if another batched write is already |
| 461 | * in-flight. |
| 462 | */ |
| 463 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 464 | |
| 465 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | * Keeps track of all IIntentReceivers that have been registered for |
| 467 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 468 | * a ReceiverList. |
| 469 | */ |
| 470 | final HashMap mRegisteredReceivers = new HashMap(); |
| 471 | |
| 472 | /** |
| 473 | * Resolver for broadcast intents to registered receivers. |
| 474 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 475 | */ |
| 476 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 477 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 478 | @Override |
| 479 | protected boolean allowFilterResult( |
| 480 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 481 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 482 | for (int i=dest.size()-1; i>=0; i--) { |
| 483 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 484 | return false; |
| 485 | } |
| 486 | } |
| 487 | return true; |
| 488 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 489 | |
| 490 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 491 | protected BroadcastFilter[] newArray(int size) { |
| 492 | return new BroadcastFilter[size]; |
| 493 | } |
| 494 | |
| 495 | @Override |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 496 | protected String packageForFilter(BroadcastFilter filter) { |
| 497 | return filter.packageName; |
| 498 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | /** |
| 502 | * State of all active sticky broadcasts. Keys are the action of the |
| 503 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
| 504 | * that action (which should usually be one). |
| 505 | */ |
| 506 | final HashMap<String, ArrayList<Intent>> mStickyBroadcasts = |
| 507 | new HashMap<String, ArrayList<Intent>>(); |
| 508 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 509 | final ActiveServices mServices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | |
| 511 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 512 | * Backup/restore process management |
| 513 | */ |
| 514 | String mBackupAppName = null; |
| 515 | BackupRecord mBackupTarget = null; |
| 516 | |
| 517 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | * List of PendingThumbnailsRecord objects of clients who are still |
| 519 | * waiting to receive all of the thumbnails for a task. |
| 520 | */ |
| 521 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 522 | |
| 523 | /** |
| 524 | * List of HistoryRecord objects that have been finished and must |
| 525 | * still report back to a pending thumbnail receiver. |
| 526 | */ |
| 527 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 528 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 529 | final ProviderMap mProviderMap = new ProviderMap(); |
| 530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | /** |
| 532 | * List of content providers who have clients waiting for them. The |
| 533 | * application is currently being launched and the provider will be |
| 534 | * removed from this list once it is published. |
| 535 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 536 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 537 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | |
| 539 | /** |
| 540 | * Global set of specific Uri permissions that have been granted. |
| 541 | */ |
| 542 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 543 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 544 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 545 | CoreSettingsObserver mCoreSettingsObserver; |
| 546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | /** |
| 548 | * Thread-local storage used to carry caller permissions over through |
| 549 | * indirect content-provider access. |
| 550 | * @see #ActivityManagerService.openContentUri() |
| 551 | */ |
| 552 | private class Identity { |
| 553 | public int pid; |
| 554 | public int uid; |
| 555 | |
| 556 | Identity(int _pid, int _uid) { |
| 557 | pid = _pid; |
| 558 | uid = _uid; |
| 559 | } |
| 560 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 561 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 563 | |
| 564 | /** |
| 565 | * All information we have collected about the runtime performance of |
| 566 | * any user id that can impact battery performance. |
| 567 | */ |
| 568 | final BatteryStatsService mBatteryStatsService; |
| 569 | |
| 570 | /** |
| 571 | * information about component usage |
| 572 | */ |
| 573 | final UsageStatsService mUsageStatsService; |
| 574 | |
| 575 | /** |
| 576 | * Current configuration information. HistoryRecord objects are given |
| 577 | * a reference to this object to indicate which configuration they are |
| 578 | * currently running in, so this object must be kept immutable. |
| 579 | */ |
| 580 | Configuration mConfiguration = new Configuration(); |
| 581 | |
| 582 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 583 | * Current sequencing integer of the configuration, for skipping old |
| 584 | * configurations. |
| 585 | */ |
| 586 | int mConfigurationSeq = 0; |
| 587 | |
| 588 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 589 | * Hardware-reported OpenGLES version. |
| 590 | */ |
| 591 | final int GL_ES_VERSION; |
| 592 | |
| 593 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 595 | * For example, references to the commonly used services. |
| 596 | */ |
| 597 | HashMap<String, IBinder> mAppBindArgs; |
| 598 | |
| 599 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 600 | * Temporary to avoid allocations. Protected by main lock. |
| 601 | */ |
| 602 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 603 | |
| 604 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | * Used to control how we initialize the service. |
| 606 | */ |
| 607 | boolean mStartRunning = false; |
| 608 | ComponentName mTopComponent; |
| 609 | String mTopAction; |
| 610 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 611 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | boolean mSystemReady = false; |
| 613 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 614 | boolean mWaitingUpdate = false; |
| 615 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 616 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 617 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | |
| 619 | Context mContext; |
| 620 | |
| 621 | int mFactoryTest; |
| 622 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 623 | boolean mCheckedForSetup; |
| 624 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 626 | * The time at which we will allow normal application switches again, |
| 627 | * after a call to {@link #stopAppSwitches()}. |
| 628 | */ |
| 629 | long mAppSwitchesAllowedTime; |
| 630 | |
| 631 | /** |
| 632 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 633 | * is set; any switches after that will clear the time. |
| 634 | */ |
| 635 | boolean mDidAppSwitch; |
| 636 | |
| 637 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 638 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 639 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 640 | long mLastPowerCheckRealtime; |
| 641 | |
| 642 | /** |
| 643 | * Last time (in uptime) at which we checked for power usage. |
| 644 | */ |
| 645 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 646 | |
| 647 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | * Set while we are wanting to sleep, to prevent any |
| 649 | * activities from being started/resumed. |
| 650 | */ |
| 651 | boolean mSleeping = false; |
| 652 | |
| 653 | /** |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 654 | * State of external calls telling us if the device is asleep. |
| 655 | */ |
| 656 | boolean mWentToSleep = false; |
| 657 | |
| 658 | /** |
| 659 | * State of external call telling us if the lock screen is shown. |
| 660 | */ |
| 661 | boolean mLockScreenShown = false; |
| 662 | |
| 663 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 664 | * Set if we are shutting down the system, similar to sleeping. |
| 665 | */ |
| 666 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | |
| 668 | /** |
| 669 | * Task identifier that activities are currently being started |
| 670 | * in. Incremented each time a new task is created. |
| 671 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 672 | * integers that won't wrap. |
| 673 | */ |
| 674 | int mCurTask = 1; |
| 675 | |
| 676 | /** |
| 677 | * Current sequence id for oom_adj computation traversal. |
| 678 | */ |
| 679 | int mAdjSeq = 0; |
| 680 | |
| 681 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 682 | * Current sequence id for process LRU updating. |
| 683 | */ |
| 684 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | |
| 686 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 687 | * Keep track of the number of service processes we last found, to |
| 688 | * determine on the next iteration which should be B services. |
| 689 | */ |
| 690 | int mNumServiceProcs = 0; |
| 691 | int mNewNumServiceProcs = 0; |
| 692 | |
| 693 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | * System monitoring: number of processes that died since the last |
| 695 | * N procs were started. |
| 696 | */ |
| 697 | int[] mProcDeaths = new int[20]; |
| 698 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 699 | /** |
| 700 | * This is set if we had to do a delayed dexopt of an app before launching |
| 701 | * it, to increasing the ANR timeouts in that case. |
| 702 | */ |
| 703 | boolean mDidDexOpt; |
| 704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | String mDebugApp = null; |
| 706 | boolean mWaitForDebugger = false; |
| 707 | boolean mDebugTransient = false; |
| 708 | String mOrigDebugApp = null; |
| 709 | boolean mOrigWaitForDebugger = false; |
| 710 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 711 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 712 | String mProfileApp = null; |
| 713 | ProcessRecord mProfileProc = null; |
| 714 | String mProfileFile; |
| 715 | ParcelFileDescriptor mProfileFd; |
| 716 | int mProfileType = 0; |
| 717 | boolean mAutoStopProfiler = false; |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 718 | String mOpenGlTraceApp = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 720 | static class ProcessChangeItem { |
| 721 | static final int CHANGE_ACTIVITIES = 1<<0; |
| 722 | static final int CHANGE_IMPORTANCE= 1<<1; |
| 723 | int changes; |
| 724 | int uid; |
| 725 | int pid; |
| 726 | int importance; |
| 727 | boolean foregroundActivities; |
| 728 | } |
| 729 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 730 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 731 | = new RemoteCallbackList<IProcessObserver>(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 732 | ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5]; |
| 733 | |
| 734 | final ArrayList<ProcessChangeItem> mPendingProcessChanges |
| 735 | = new ArrayList<ProcessChangeItem>(); |
| 736 | final ArrayList<ProcessChangeItem> mAvailProcessChanges |
| 737 | = new ArrayList<ProcessChangeItem>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 738 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | /** |
| 740 | * Callback of last caller to {@link #requestPss}. |
| 741 | */ |
| 742 | Runnable mRequestPssCallback; |
| 743 | |
| 744 | /** |
| 745 | * Remaining processes for which we are waiting results from the last |
| 746 | * call to {@link #requestPss}. |
| 747 | */ |
| 748 | final ArrayList<ProcessRecord> mRequestPssList |
| 749 | = new ArrayList<ProcessRecord>(); |
| 750 | |
| 751 | /** |
| 752 | * Runtime statistics collection thread. This object's lock is used to |
| 753 | * protect all related state. |
| 754 | */ |
| 755 | final Thread mProcessStatsThread; |
| 756 | |
| 757 | /** |
| 758 | * Used to collect process stats when showing not responding dialog. |
| 759 | * Protected by mProcessStatsThread. |
| 760 | */ |
| 761 | final ProcessStats mProcessStats = new ProcessStats( |
| 762 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 763 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 764 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 765 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | long mLastWriteTime = 0; |
| 767 | |
| 768 | /** |
| 769 | * Set to true after the system has finished booting. |
| 770 | */ |
| 771 | boolean mBooted = false; |
| 772 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 773 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 774 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | |
| 776 | WindowManagerService mWindowManager; |
| 777 | |
| 778 | static ActivityManagerService mSelf; |
| 779 | static ActivityThread mSystemThread; |
| 780 | |
| 781 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 782 | final ProcessRecord mApp; |
| 783 | final int mPid; |
| 784 | final IApplicationThread mAppThread; |
| 785 | |
| 786 | AppDeathRecipient(ProcessRecord app, int pid, |
| 787 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 788 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | TAG, "New death recipient " + this |
| 790 | + " for thread " + thread.asBinder()); |
| 791 | mApp = app; |
| 792 | mPid = pid; |
| 793 | mAppThread = thread; |
| 794 | } |
| 795 | |
| 796 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 797 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | TAG, "Death received in " + this |
| 799 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | synchronized(ActivityManagerService.this) { |
| 801 | appDiedLocked(mApp, mPid, mAppThread); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | static final int SHOW_ERROR_MSG = 1; |
| 807 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 808 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 809 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 810 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 811 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 813 | static final int UPDATE_TIME_ZONE = 13; |
| 814 | static final int SHOW_UID_ERROR_MSG = 14; |
| 815 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 817 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 818 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 819 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 820 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 821 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 822 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 823 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 824 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 825 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 826 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 827 | static final int DISPATCH_PROCESSES_CHANGED = 31; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 828 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 829 | static final int REPORT_MEM_USAGE = 33; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 830 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 831 | static final int FIRST_ACTIVITY_STACK_MSG = 100; |
| 832 | static final int FIRST_BROADCAST_QUEUE_MSG = 200; |
| 833 | static final int FIRST_COMPAT_MODE_MSG = 300; |
| 834 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 836 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 837 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | |
| 839 | final Handler mHandler = new Handler() { |
| 840 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 841 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | //} |
| 843 | |
| 844 | public void handleMessage(Message msg) { |
| 845 | switch (msg.what) { |
| 846 | case SHOW_ERROR_MSG: { |
| 847 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | synchronized (ActivityManagerService.this) { |
| 849 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 850 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 851 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | return; |
| 853 | } |
| 854 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 855 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 856 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | d.show(); |
| 858 | proc.crashDialog = d; |
| 859 | } else { |
| 860 | // The device is asleep, so just pretend that the user |
| 861 | // saw a crash dialog and hit "force quit". |
| 862 | res.set(0); |
| 863 | } |
| 864 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 865 | |
| 866 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | } break; |
| 868 | case SHOW_NOT_RESPONDING_MSG: { |
| 869 | synchronized (ActivityManagerService.this) { |
| 870 | HashMap data = (HashMap) msg.obj; |
| 871 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 872 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 873 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | return; |
| 875 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 876 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 877 | Intent intent = new Intent("android.intent.action.ANR"); |
| 878 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 879 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 880 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 881 | } |
| 882 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 883 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 884 | 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] | 885 | |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 886 | if (mShowDialogs) { |
| 887 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Mike Lockwood | 69ccdbd | 2012-04-03 11:53:47 -0700 | [diff] [blame] | 888 | mContext, proc, (ActivityRecord)data.get("activity")); |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 889 | d.show(); |
| 890 | proc.anrDialog = d; |
| 891 | } else { |
| 892 | // Just kill the app if there is no dialog to be shown. |
| 893 | killAppAtUsersRequest(proc, null); |
| 894 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 896 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 897 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 899 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 900 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 901 | synchronized (ActivityManagerService.this) { |
| 902 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 903 | if (proc == null) { |
| 904 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 905 | break; |
| 906 | } |
| 907 | if (proc.crashDialog != null) { |
| 908 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 909 | return; |
| 910 | } |
| 911 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 912 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 913 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 914 | d.show(); |
| 915 | proc.crashDialog = d; |
| 916 | } else { |
| 917 | // The device is asleep, so just pretend that the user |
| 918 | // saw a crash dialog and hit "force quit". |
| 919 | res.set(0); |
| 920 | } |
| 921 | } |
| 922 | ensureBootCompleted(); |
| 923 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | case SHOW_FACTORY_ERROR_MSG: { |
| 925 | Dialog d = new FactoryErrorDialog( |
| 926 | mContext, msg.getData().getCharSequence("msg")); |
| 927 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 928 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | } break; |
| 930 | case UPDATE_CONFIGURATION_MSG: { |
| 931 | final ContentResolver resolver = mContext.getContentResolver(); |
| 932 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 933 | } break; |
| 934 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 935 | synchronized (ActivityManagerService.this) { |
| 936 | performAppGcsIfAppropriateLocked(); |
| 937 | } |
| 938 | } break; |
| 939 | case WAIT_FOR_DEBUGGER_MSG: { |
| 940 | synchronized (ActivityManagerService.this) { |
| 941 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 942 | if (msg.arg1 != 0) { |
| 943 | if (!app.waitedForDebugger) { |
| 944 | Dialog d = new AppWaitingForDebuggerDialog( |
| 945 | ActivityManagerService.this, |
| 946 | mContext, app); |
| 947 | app.waitDialog = d; |
| 948 | app.waitedForDebugger = true; |
| 949 | d.show(); |
| 950 | } |
| 951 | } else { |
| 952 | if (app.waitDialog != null) { |
| 953 | app.waitDialog.dismiss(); |
| 954 | app.waitDialog = null; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 960 | if (mDidDexOpt) { |
| 961 | mDidDexOpt = false; |
| 962 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 963 | nmsg.obj = msg.obj; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 964 | mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 965 | return; |
| 966 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 967 | mServices.serviceTimeout((ProcessRecord)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | } break; |
| 969 | case UPDATE_TIME_ZONE: { |
| 970 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 971 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 972 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | if (r.thread != null) { |
| 974 | try { |
| 975 | r.thread.updateTimeZone(); |
| 976 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 977 | 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] | 978 | } |
| 979 | } |
| 980 | } |
| 981 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 982 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 983 | case CLEAR_DNS_CACHE: { |
| 984 | synchronized (ActivityManagerService.this) { |
| 985 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 986 | ProcessRecord r = mLruProcesses.get(i); |
| 987 | if (r.thread != null) { |
| 988 | try { |
| 989 | r.thread.clearDnsCache(); |
| 990 | } catch (RemoteException ex) { |
| 991 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 997 | case UPDATE_HTTP_PROXY: { |
| 998 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 999 | String host = ""; |
| 1000 | String port = ""; |
| 1001 | String exclList = ""; |
| 1002 | if (proxy != null) { |
| 1003 | host = proxy.getHost(); |
| 1004 | port = Integer.toString(proxy.getPort()); |
| 1005 | exclList = proxy.getExclusionList(); |
| 1006 | } |
| 1007 | synchronized (ActivityManagerService.this) { |
| 1008 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1009 | ProcessRecord r = mLruProcesses.get(i); |
| 1010 | if (r.thread != null) { |
| 1011 | try { |
| 1012 | r.thread.setHttpProxy(host, port, exclList); |
| 1013 | } catch (RemoteException ex) { |
| 1014 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1015 | r.info.processName); |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1022 | String title = "System UIDs Inconsistent"; |
| 1023 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1024 | + " data partition or your device will be unstable."; |
| 1025 | Log.e(TAG, title + ": " + text); |
| 1026 | if (mShowDialogs) { |
| 1027 | // XXX This is a temporary dialog, no need to localize. |
| 1028 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1029 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1030 | d.setCancelable(false); |
| 1031 | d.setTitle(title); |
| 1032 | d.setMessage(text); |
| 1033 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1034 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1035 | mUidAlert = d; |
| 1036 | d.show(); |
| 1037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } break; |
| 1039 | case IM_FEELING_LUCKY_MSG: { |
| 1040 | if (mUidAlert != null) { |
| 1041 | mUidAlert.dismiss(); |
| 1042 | mUidAlert = null; |
| 1043 | } |
| 1044 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1046 | if (mDidDexOpt) { |
| 1047 | mDidDexOpt = false; |
| 1048 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1049 | nmsg.obj = msg.obj; |
| 1050 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1051 | return; |
| 1052 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1054 | synchronized (ActivityManagerService.this) { |
| 1055 | processStartTimedOutLocked(app); |
| 1056 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1057 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1058 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1059 | synchronized (ActivityManagerService.this) { |
| 1060 | doPendingActivityLaunchesLocked(true); |
| 1061 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1062 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1063 | case KILL_APPLICATION_MSG: { |
| 1064 | synchronized (ActivityManagerService.this) { |
| 1065 | int uid = msg.arg1; |
| 1066 | boolean restart = (msg.arg2 == 1); |
| 1067 | String pkg = (String) msg.obj; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1068 | forceStopPackageLocked(pkg, uid, restart, false, true, false, |
| 1069 | UserId.getUserId(uid)); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1072 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1073 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1074 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1075 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1076 | INotificationManager inm = NotificationManager.getService(); |
| 1077 | if (inm == null) { |
| 1078 | return; |
| 1079 | } |
| 1080 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1081 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1082 | ProcessRecord process = root.app; |
| 1083 | if (process == null) { |
| 1084 | return; |
| 1085 | } |
| 1086 | |
| 1087 | try { |
| 1088 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1089 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1090 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1091 | Notification notification = new Notification(); |
| 1092 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1093 | notification.when = 0; |
| 1094 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1095 | notification.tickerText = text; |
| 1096 | notification.defaults = 0; // please be quiet |
| 1097 | notification.sound = null; |
| 1098 | notification.vibrate = null; |
| 1099 | notification.setLatestEventInfo(context, text, |
| 1100 | mContext.getText(R.string.heavy_weight_notification_detail), |
| 1101 | PendingIntent.getActivity(mContext, 0, root.intent, |
| 1102 | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 1103 | |
| 1104 | try { |
| 1105 | int[] outId = new int[1]; |
| 1106 | inm.enqueueNotification("android", R.string.heavy_weight_notification, |
| 1107 | notification, outId); |
| 1108 | } catch (RuntimeException e) { |
| 1109 | Slog.w(ActivityManagerService.TAG, |
| 1110 | "Error showing notification for heavy-weight app", e); |
| 1111 | } catch (RemoteException e) { |
| 1112 | } |
| 1113 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1114 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1115 | } |
| 1116 | } break; |
| 1117 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1118 | INotificationManager inm = NotificationManager.getService(); |
| 1119 | if (inm == null) { |
| 1120 | return; |
| 1121 | } |
| 1122 | try { |
| 1123 | inm.cancelNotification("android", |
| 1124 | R.string.heavy_weight_notification); |
| 1125 | } catch (RuntimeException e) { |
| 1126 | Slog.w(ActivityManagerService.TAG, |
| 1127 | "Error canceling notification for service", e); |
| 1128 | } catch (RemoteException e) { |
| 1129 | } |
| 1130 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1131 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1132 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1133 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1134 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1135 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1136 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1139 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1140 | synchronized (ActivityManagerService.this) { |
| 1141 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1142 | if (mCompatModeDialog != null) { |
| 1143 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1144 | ar.info.applicationInfo.packageName)) { |
| 1145 | return; |
| 1146 | } |
| 1147 | mCompatModeDialog.dismiss(); |
| 1148 | mCompatModeDialog = null; |
| 1149 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1150 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1151 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1152 | ar.packageName)) { |
| 1153 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1154 | ar.info.applicationInfo); |
| 1155 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1156 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1157 | mCompatModeDialog = new CompatModeDialog( |
| 1158 | ActivityManagerService.this, mContext, |
| 1159 | ar.info.applicationInfo); |
| 1160 | mCompatModeDialog.show(); |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1165 | break; |
| 1166 | } |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 1167 | case DISPATCH_PROCESSES_CHANGED: { |
| 1168 | dispatchProcessesChanged(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1169 | break; |
| 1170 | } |
| 1171 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1172 | final int pid = msg.arg1; |
| 1173 | final int uid = msg.arg2; |
| 1174 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1175 | break; |
| 1176 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1177 | case REPORT_MEM_USAGE: { |
| 1178 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1179 | if (!isDebuggable) { |
| 1180 | return; |
| 1181 | } |
| 1182 | synchronized (ActivityManagerService.this) { |
| 1183 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1184 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1185 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1186 | // avoid spamming. |
| 1187 | return; |
| 1188 | } |
| 1189 | mLastMemUsageReportTime = now; |
| 1190 | } |
| 1191 | Thread thread = new Thread() { |
| 1192 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1193 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1194 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1195 | StringWriter oomSw = new StringWriter(); |
| 1196 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1197 | StringWriter catSw = new StringWriter(); |
| 1198 | PrintWriter catPw = new PrintWriter(catSw); |
| 1199 | String[] emptyArgs = new String[] { }; |
| 1200 | StringBuilder tag = new StringBuilder(128); |
| 1201 | StringBuilder stack = new StringBuilder(128); |
| 1202 | tag.append("Low on memory -- "); |
| 1203 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1204 | tag, stack); |
| 1205 | dropBuilder.append(stack); |
| 1206 | dropBuilder.append('\n'); |
| 1207 | dropBuilder.append('\n'); |
| 1208 | String oomString = oomSw.toString(); |
| 1209 | dropBuilder.append(oomString); |
| 1210 | dropBuilder.append('\n'); |
| 1211 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1212 | try { |
| 1213 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1214 | "procrank", }); |
| 1215 | final InputStreamReader converter = new InputStreamReader( |
| 1216 | proc.getInputStream()); |
| 1217 | BufferedReader in = new BufferedReader(converter); |
| 1218 | String line; |
| 1219 | while (true) { |
| 1220 | line = in.readLine(); |
| 1221 | if (line == null) { |
| 1222 | break; |
| 1223 | } |
| 1224 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1225 | logBuilder.append(line); |
| 1226 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1227 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1228 | dropBuilder.append(line); |
| 1229 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1230 | } |
| 1231 | converter.close(); |
| 1232 | } catch (IOException e) { |
| 1233 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1234 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1235 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1236 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1237 | catPw.println(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1238 | mServices.dumpServicesLocked(null, catPw, emptyArgs, 0, |
| 1239 | false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1240 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1241 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1242 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1243 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1244 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1245 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1246 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1247 | synchronized (ActivityManagerService.this) { |
| 1248 | long now = SystemClock.uptimeMillis(); |
| 1249 | if (mLastMemUsageReportTime < now) { |
| 1250 | mLastMemUsageReportTime = now; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | }; |
| 1255 | thread.start(); |
| 1256 | break; |
| 1257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | }; |
| 1261 | |
| 1262 | public static void setSystemProcess() { |
| 1263 | try { |
| 1264 | ActivityManagerService m = mSelf; |
| 1265 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1266 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1268 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1269 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | if (MONITOR_CPU_USAGE) { |
| 1271 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1272 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1274 | |
| 1275 | ApplicationInfo info = |
| 1276 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1277 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1278 | mSystemThread.installSystemApplicationInfo(info); |
| 1279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | synchronized (mSelf) { |
| 1281 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1282 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1283 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1285 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1286 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1287 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | synchronized (mSelf.mPidsSelfLocked) { |
| 1289 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1290 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1291 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | } |
| 1293 | } catch (PackageManager.NameNotFoundException e) { |
| 1294 | throw new RuntimeException( |
| 1295 | "Unable to find android system package", e); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | public void setWindowManager(WindowManagerService wm) { |
| 1300 | mWindowManager = wm; |
| 1301 | } |
| 1302 | |
| 1303 | public static final Context main(int factoryTest) { |
| 1304 | AThread thr = new AThread(); |
| 1305 | thr.start(); |
| 1306 | |
| 1307 | synchronized (thr) { |
| 1308 | while (thr.mService == null) { |
| 1309 | try { |
| 1310 | thr.wait(); |
| 1311 | } catch (InterruptedException e) { |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | ActivityManagerService m = thr.mService; |
| 1317 | mSelf = m; |
| 1318 | ActivityThread at = ActivityThread.systemMain(); |
| 1319 | mSystemThread = at; |
| 1320 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1321 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1322 | m.mContext = context; |
| 1323 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1324 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | |
| 1326 | m.mBatteryStatsService.publish(context); |
| 1327 | m.mUsageStatsService.publish(context); |
| 1328 | |
| 1329 | synchronized (thr) { |
| 1330 | thr.mReady = true; |
| 1331 | thr.notifyAll(); |
| 1332 | } |
| 1333 | |
| 1334 | m.startRunning(null, null, null, null); |
| 1335 | |
| 1336 | return context; |
| 1337 | } |
| 1338 | |
| 1339 | public static ActivityManagerService self() { |
| 1340 | return mSelf; |
| 1341 | } |
| 1342 | |
| 1343 | static class AThread extends Thread { |
| 1344 | ActivityManagerService mService; |
| 1345 | boolean mReady = false; |
| 1346 | |
| 1347 | public AThread() { |
| 1348 | super("ActivityManager"); |
| 1349 | } |
| 1350 | |
| 1351 | public void run() { |
| 1352 | Looper.prepare(); |
| 1353 | |
| 1354 | android.os.Process.setThreadPriority( |
| 1355 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1356 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1357 | |
| 1358 | ActivityManagerService m = new ActivityManagerService(); |
| 1359 | |
| 1360 | synchronized (this) { |
| 1361 | mService = m; |
| 1362 | notifyAll(); |
| 1363 | } |
| 1364 | |
| 1365 | synchronized (this) { |
| 1366 | while (!mReady) { |
| 1367 | try { |
| 1368 | wait(); |
| 1369 | } catch (InterruptedException e) { |
| 1370 | } |
| 1371 | } |
| 1372 | } |
| 1373 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1374 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1375 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1376 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1377 | } |
| 1378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1379 | Looper.loop(); |
| 1380 | } |
| 1381 | } |
| 1382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | static class MemBinder extends Binder { |
| 1384 | ActivityManagerService mActivityManagerService; |
| 1385 | MemBinder(ActivityManagerService activityManagerService) { |
| 1386 | mActivityManagerService = activityManagerService; |
| 1387 | } |
| 1388 | |
| 1389 | @Override |
| 1390 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1391 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1392 | != PackageManager.PERMISSION_GRANTED) { |
| 1393 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1394 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1395 | + " without permission " + android.Manifest.permission.DUMP); |
| 1396 | return; |
| 1397 | } |
| 1398 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1399 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1400 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1404 | static class GraphicsBinder extends Binder { |
| 1405 | ActivityManagerService mActivityManagerService; |
| 1406 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1407 | mActivityManagerService = activityManagerService; |
| 1408 | } |
| 1409 | |
| 1410 | @Override |
| 1411 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1412 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1413 | != PackageManager.PERMISSION_GRANTED) { |
| 1414 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1415 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1416 | + " without permission " + android.Manifest.permission.DUMP); |
| 1417 | return; |
| 1418 | } |
| 1419 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1420 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1424 | static class DbBinder extends Binder { |
| 1425 | ActivityManagerService mActivityManagerService; |
| 1426 | DbBinder(ActivityManagerService activityManagerService) { |
| 1427 | mActivityManagerService = activityManagerService; |
| 1428 | } |
| 1429 | |
| 1430 | @Override |
| 1431 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1432 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1433 | != PackageManager.PERMISSION_GRANTED) { |
| 1434 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1435 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1436 | + " without permission " + android.Manifest.permission.DUMP); |
| 1437 | return; |
| 1438 | } |
| 1439 | |
| 1440 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1441 | } |
| 1442 | } |
| 1443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | static class CpuBinder extends Binder { |
| 1445 | ActivityManagerService mActivityManagerService; |
| 1446 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1447 | mActivityManagerService = activityManagerService; |
| 1448 | } |
| 1449 | |
| 1450 | @Override |
| 1451 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1452 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1453 | != PackageManager.PERMISSION_GRANTED) { |
| 1454 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1455 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1456 | + " without permission " + android.Manifest.permission.DUMP); |
| 1457 | return; |
| 1458 | } |
| 1459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1461 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1462 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1463 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1469 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1470 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1471 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1472 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1473 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1474 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 1475 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1476 | mServices = new ActiveServices(this); |
| 1477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | File dataDir = Environment.getDataDirectory(); |
| 1479 | File systemDir = new File(dataDir, "system"); |
| 1480 | systemDir.mkdirs(); |
| 1481 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1482 | systemDir, "batterystats.bin").toString()); |
| 1483 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1484 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1485 | mOnBattery = DEBUG_POWER ? true |
| 1486 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1487 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1489 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1490 | systemDir, "usagestats").toString()); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1491 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1493 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1494 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1495 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1496 | mConfiguration.setToDefaults(); |
| 1497 | mConfiguration.locale = Locale.getDefault(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1498 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | mProcessStats.init(); |
| 1500 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1501 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1502 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | // Add ourself to the Watchdog monitors. |
| 1504 | Watchdog.getInstance().addMonitor(this); |
| 1505 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1506 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1507 | public void run() { |
| 1508 | while (true) { |
| 1509 | try { |
| 1510 | try { |
| 1511 | synchronized(this) { |
| 1512 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1513 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1514 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1515 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | // + ", write delay=" + nextWriteDelay); |
| 1517 | if (nextWriteDelay < nextCpuDelay) { |
| 1518 | nextCpuDelay = nextWriteDelay; |
| 1519 | } |
| 1520 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1521 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1522 | this.wait(nextCpuDelay); |
| 1523 | } |
| 1524 | } |
| 1525 | } catch (InterruptedException e) { |
| 1526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | updateCpuStatsNow(); |
| 1528 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1529 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | }; |
| 1534 | mProcessStatsThread.start(); |
| 1535 | } |
| 1536 | |
| 1537 | @Override |
| 1538 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1539 | throws RemoteException { |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 1540 | if (code == SYSPROPS_TRANSACTION) { |
| 1541 | // We need to tell all apps about the system property change. |
| 1542 | ArrayList<IBinder> procs = new ArrayList<IBinder>(); |
| 1543 | synchronized(this) { |
| 1544 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 1545 | final int NA = apps.size(); |
| 1546 | for (int ia=0; ia<NA; ia++) { |
| 1547 | ProcessRecord app = apps.valueAt(ia); |
| 1548 | if (app.thread != null) { |
| 1549 | procs.add(app.thread.asBinder()); |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | int N = procs.size(); |
| 1556 | for (int i=0; i<N; i++) { |
| 1557 | Parcel data2 = Parcel.obtain(); |
| 1558 | try { |
| 1559 | procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0); |
| 1560 | } catch (RemoteException e) { |
| 1561 | } |
| 1562 | data2.recycle(); |
| 1563 | } |
| 1564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | try { |
| 1566 | return super.onTransact(code, data, reply, flags); |
| 1567 | } catch (RuntimeException e) { |
| 1568 | // The activity manager only throws security exceptions, so let's |
| 1569 | // log all others. |
| 1570 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1571 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | } |
| 1573 | throw e; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1578 | final long now = SystemClock.uptimeMillis(); |
| 1579 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1580 | return; |
| 1581 | } |
| 1582 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1583 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | mProcessStatsThread.notify(); |
| 1585 | } |
| 1586 | } |
| 1587 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | void updateCpuStatsNow() { |
| 1590 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1591 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | final long now = SystemClock.uptimeMillis(); |
| 1593 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1596 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1597 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | haveNewCpuStats = true; |
| 1599 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1600 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1601 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1602 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1603 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1604 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1605 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1606 | int user = mProcessStats.getLastUserTime(); |
| 1607 | int system = mProcessStats.getLastSystemTime(); |
| 1608 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1609 | int irq = mProcessStats.getLastIrqTime(); |
| 1610 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1611 | int idle = mProcessStats.getLastIdleTime(); |
| 1612 | |
| 1613 | int total = user + system + iowait + irq + softIrq + idle; |
| 1614 | if (total == 0) total = 1; |
| 1615 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1616 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1617 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1618 | (user * 100) / total, |
| 1619 | (system * 100) / total, |
| 1620 | (iowait * 100) / total, |
| 1621 | (irq * 100) / total, |
| 1622 | (softIrq * 100) / total); |
| 1623 | } |
| 1624 | } |
| 1625 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1626 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1627 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1628 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | synchronized(mPidsSelfLocked) { |
| 1630 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1631 | if (mOnBattery) { |
| 1632 | int perc = bstats.startAddingCpuLocked(); |
| 1633 | int totalUTime = 0; |
| 1634 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1635 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1637 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1638 | if (!st.working) { |
| 1639 | continue; |
| 1640 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1642 | int otherUTime = (st.rel_utime*perc)/100; |
| 1643 | int otherSTime = (st.rel_stime*perc)/100; |
| 1644 | totalUTime += otherUTime; |
| 1645 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | if (pr != null) { |
| 1647 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1648 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1649 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1650 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1651 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1652 | } else { |
| 1653 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1654 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1655 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1656 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1657 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1658 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1660 | } |
| 1661 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1662 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1663 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1667 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1669 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1670 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1676 | @Override |
| 1677 | public void batteryNeedsCpuUpdate() { |
| 1678 | updateCpuStatsNow(); |
| 1679 | } |
| 1680 | |
| 1681 | @Override |
| 1682 | public void batteryPowerChanged(boolean onBattery) { |
| 1683 | // When plugging in, update the CPU stats first before changing |
| 1684 | // the plug state. |
| 1685 | updateCpuStatsNow(); |
| 1686 | synchronized (this) { |
| 1687 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1688 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | /** |
| 1694 | * Initialize the application bind args. These are passed to each |
| 1695 | * process when the bindApplication() IPC is sent to the process. They're |
| 1696 | * lazily setup to make sure the services are running when they're asked for. |
| 1697 | */ |
| 1698 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1699 | if (mAppBindArgs == null) { |
| 1700 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1701 | |
| 1702 | // Setup the application init args |
| 1703 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1704 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1705 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1706 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1707 | } |
| 1708 | return mAppBindArgs; |
| 1709 | } |
| 1710 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1711 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | if (mFocusedActivity != r) { |
| 1713 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1714 | if (r != null) { |
| 1715 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1720 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1721 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | // 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] | 1723 | int lrui = mLruProcesses.indexOf(app); |
| 1724 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1725 | |
| 1726 | int i = mLruProcesses.size()-1; |
| 1727 | int skipTop = 0; |
| 1728 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1729 | app.lruSeq = mLruSeq; |
| 1730 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1731 | // compute the new weight for this process. |
| 1732 | if (updateActivityTime) { |
| 1733 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1734 | } |
| 1735 | if (app.activities.size() > 0) { |
| 1736 | // If this process has activities, we more strongly want to keep |
| 1737 | // it around. |
| 1738 | app.lruWeight = app.lastActivityTime; |
| 1739 | } else if (app.pubProviders.size() > 0) { |
| 1740 | // If this process contains content providers, we want to keep |
| 1741 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1742 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1743 | // 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] | 1744 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1745 | } else { |
| 1746 | // If this process doesn't have activities, we less strongly |
| 1747 | // want to keep it around, and generally want to avoid getting |
| 1748 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1749 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1750 | // 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] | 1751 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1752 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1753 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1754 | while (i >= 0) { |
| 1755 | ProcessRecord p = mLruProcesses.get(i); |
| 1756 | // If this app shouldn't be in front of the first N background |
| 1757 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1758 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1759 | skipTop--; |
| 1760 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1761 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1762 | mLruProcesses.add(i+1, app); |
| 1763 | break; |
| 1764 | } |
| 1765 | i--; |
| 1766 | } |
| 1767 | if (i < 0) { |
| 1768 | mLruProcesses.add(0, app); |
| 1769 | } |
| 1770 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1771 | // If the app is currently using a content provider or service, |
| 1772 | // bump those processes as well. |
| 1773 | if (app.connections.size() > 0) { |
| 1774 | for (ConnectionRecord cr : app.connections) { |
| 1775 | if (cr.binding != null && cr.binding.service != null |
| 1776 | && cr.binding.service.app != null |
| 1777 | && cr.binding.service.app.lruSeq != mLruSeq) { |
Björn Davidsson | 90f9e31 | 2010-11-18 08:26:27 +0100 | [diff] [blame] | 1778 | updateLruProcessInternalLocked(cr.binding.service.app, false, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1779 | updateActivityTime, i+1); |
| 1780 | } |
| 1781 | } |
| 1782 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1783 | for (int j=app.conProviders.size()-1; j>=0; j--) { |
| 1784 | ContentProviderRecord cpr = app.conProviders.get(j).provider; |
| 1785 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
Jean-Baptiste Queru | b8c6405 | 2012-06-11 11:01:19 -0700 | [diff] [blame] | 1786 | updateLruProcessInternalLocked(cpr.proc, false, |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1787 | updateActivityTime, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1791 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | if (oomAdj) { |
| 1793 | updateOomAdjLocked(); |
| 1794 | } |
| 1795 | } |
| 1796 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1797 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1798 | boolean oomAdj, boolean updateActivityTime) { |
| 1799 | mLruSeq++; |
| 1800 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1801 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1802 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1803 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | String processName, int uid) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1805 | if (uid == Process.SYSTEM_UID) { |
Amith Yamasani | 0184ce9 | 2012-03-28 22:41:41 -0700 | [diff] [blame] | 1806 | // The system gets to run in any process. If there are multiple |
| 1807 | // processes with the same uid, just pick the first (this |
| 1808 | // should never happen). |
| 1809 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1810 | processName); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1811 | if (procs == null) return null; |
| 1812 | final int N = procs.size(); |
| 1813 | for (int i = 0; i < N; i++) { |
| 1814 | if (UserId.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i); |
| 1815 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1816 | } |
| 1817 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1818 | return proc; |
| 1819 | } |
| 1820 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1821 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1822 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1823 | try { |
| 1824 | if (pm.performDexOpt(packageName)) { |
| 1825 | mDidDexOpt = true; |
| 1826 | } |
| 1827 | } catch (RemoteException e) { |
| 1828 | } |
| 1829 | } |
| 1830 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1831 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1832 | int transit = mWindowManager.getPendingAppTransition(); |
| 1833 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1834 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1835 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1836 | } |
| 1837 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1838 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1840 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 1841 | boolean isolated) { |
| 1842 | ProcessRecord app; |
| 1843 | if (!isolated) { |
| 1844 | app = getProcessRecordLocked(processName, info.uid); |
| 1845 | } else { |
| 1846 | // If this is an isolated process, it can't re-use an existing process. |
| 1847 | app = null; |
| 1848 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1849 | // We don't have to do anything more if: |
| 1850 | // (1) There is an existing application record; and |
| 1851 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1852 | // object attached to it so we know it couldn't have crashed; and |
| 1853 | // (3) There is a pid assigned to it, so it is either starting or |
| 1854 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1855 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1856 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1857 | + " thread=" + (app != null ? app.thread : null) |
| 1858 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1859 | if (app != null && app.pid > 0) { |
| 1860 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1861 | // We already have the app running, or are waiting for it to |
| 1862 | // 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] | 1863 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1864 | // If this is a new package in the process, add the package to the list |
| 1865 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1866 | return app; |
| 1867 | } else { |
| 1868 | // An application record is attached to a previous process, |
| 1869 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1870 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1871 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1872 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1873 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1874 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | String hostingNameStr = hostingName != null |
| 1876 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1877 | |
| 1878 | if (!isolated) { |
| 1879 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1880 | // If we are in the background, then check to see if this process |
| 1881 | // is bad. If so, we will just silently fail. |
| 1882 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1883 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1884 | + "/" + info.processName); |
| 1885 | return null; |
| 1886 | } |
| 1887 | } else { |
| 1888 | // When the user is explicitly starting a process, then clear its |
| 1889 | // crash count so that we won't make it bad until they see at |
| 1890 | // least one crash dialog again, and make the process good again |
| 1891 | // if it had been bad. |
| 1892 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1893 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1894 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1895 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1896 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
| 1897 | info.processName); |
| 1898 | mBadProcesses.remove(info.processName, info.uid); |
| 1899 | if (app != null) { |
| 1900 | app.bad = false; |
| 1901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1902 | } |
| 1903 | } |
| 1904 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1905 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1906 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1907 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 1908 | if (app == null) { |
| 1909 | Slog.w(TAG, "Failed making new process record for " |
| 1910 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 1911 | return null; |
| 1912 | } |
| 1913 | mProcessNames.put(processName, app.uid, app); |
| 1914 | if (isolated) { |
| 1915 | mIsolatedProcesses.put(app.uid, app); |
| 1916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1917 | } else { |
| 1918 | // If this is a new package in the process, add the package to the list |
| 1919 | app.addPackage(info.packageName); |
| 1920 | } |
| 1921 | |
| 1922 | // If the system is not ready yet, then hold off on starting this |
| 1923 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1924 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1925 | && !isAllowedWhileBooting(info) |
| 1926 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1927 | if (!mProcessesOnHold.contains(app)) { |
| 1928 | mProcessesOnHold.add(app); |
| 1929 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1930 | 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] | 1931 | return app; |
| 1932 | } |
| 1933 | |
| 1934 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1935 | return (app.pid != 0) ? app : null; |
| 1936 | } |
| 1937 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1938 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1939 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1940 | } |
| 1941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1942 | private final void startProcessLocked(ProcessRecord app, |
| 1943 | String hostingType, String hostingNameStr) { |
| 1944 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1945 | synchronized (mPidsSelfLocked) { |
| 1946 | mPidsSelfLocked.remove(app.pid); |
| 1947 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1948 | } |
| 1949 | app.pid = 0; |
| 1950 | } |
| 1951 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1952 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1953 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1954 | mProcessesOnHold.remove(app); |
| 1955 | |
| 1956 | updateCpuStats(); |
| 1957 | |
| 1958 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1959 | mProcDeaths[0] = 0; |
| 1960 | |
| 1961 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1962 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1963 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | int[] gids = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1965 | if (!app.isolated) { |
| 1966 | try { |
| 1967 | gids = mContext.getPackageManager().getPackageGids( |
| 1968 | app.info.packageName); |
| 1969 | } catch (PackageManager.NameNotFoundException e) { |
| 1970 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 1971 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1972 | } |
| 1973 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 1974 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1975 | && mTopComponent != null |
| 1976 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 1977 | uid = 0; |
| 1978 | } |
| 1979 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 1980 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 1981 | uid = 0; |
| 1982 | } |
| 1983 | } |
| 1984 | int debugFlags = 0; |
| 1985 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 1986 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 1987 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 1988 | // awkward to turn on otherwise. |
| 1989 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 1991 | // Run the app in safe mode if its manifest requests so or the |
| 1992 | // system is booted in safe mode. |
| 1993 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 1994 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 1995 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 1996 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1997 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 1998 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 1999 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 2000 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 2001 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 2002 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2003 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 2004 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 2005 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2006 | |
| 2007 | // Start the process. It will either succeed and return a result containing |
| 2008 | // the PID of the new process, or else throw a RuntimeException. |
| 2009 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 2010 | app.processName, uid, uid, gids, debugFlags, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 2011 | app.info.targetSdkVersion, null, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2012 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2013 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2014 | synchronized (bs) { |
| 2015 | if (bs.isOnBattery()) { |
| 2016 | app.batteryStats.incStartsLocked(); |
| 2017 | } |
| 2018 | } |
| 2019 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2020 | EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2021 | app.processName, hostingType, |
| 2022 | hostingNameStr != null ? hostingNameStr : ""); |
| 2023 | |
| 2024 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2025 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2028 | StringBuilder buf = mStringBuilder; |
| 2029 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2030 | buf.append("Start proc "); |
| 2031 | buf.append(app.processName); |
| 2032 | buf.append(" for "); |
| 2033 | buf.append(hostingType); |
| 2034 | if (hostingNameStr != null) { |
| 2035 | buf.append(" "); |
| 2036 | buf.append(hostingNameStr); |
| 2037 | } |
| 2038 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2039 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2040 | buf.append(" uid="); |
| 2041 | buf.append(uid); |
| 2042 | buf.append(" gids={"); |
| 2043 | if (gids != null) { |
| 2044 | for (int gi=0; gi<gids.length; gi++) { |
| 2045 | if (gi != 0) buf.append(", "); |
| 2046 | buf.append(gids[gi]); |
| 2047 | |
| 2048 | } |
| 2049 | } |
| 2050 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2051 | Slog.i(TAG, buf.toString()); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2052 | app.pid = startResult.pid; |
| 2053 | app.usingWrapper = startResult.usingWrapper; |
| 2054 | app.removed = false; |
| 2055 | synchronized (mPidsSelfLocked) { |
| 2056 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2057 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2058 | msg.obj = app; |
| 2059 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2060 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2061 | } |
| 2062 | } catch (RuntimeException e) { |
| 2063 | // XXX do better error recovery. |
| 2064 | app.pid = 0; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2065 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2066 | } |
| 2067 | } |
| 2068 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2069 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2070 | if (resumed) { |
| 2071 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2072 | } else { |
| 2073 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2074 | } |
| 2075 | } |
| 2076 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2077 | boolean startHomeActivityLocked(int userId) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2078 | if (mHeadless) { |
| 2079 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2080 | // when running headless. |
| 2081 | ensureBootCompleted(); |
| 2082 | return false; |
| 2083 | } |
| 2084 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2085 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2086 | && mTopAction == null) { |
| 2087 | // We are running in factory test mode, but unable to find |
| 2088 | // the factory test app, so just sit around displaying the |
| 2089 | // error message and don't try to start anything. |
| 2090 | return false; |
| 2091 | } |
| 2092 | Intent intent = new Intent( |
| 2093 | mTopAction, |
| 2094 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2095 | intent.setComponent(mTopComponent); |
| 2096 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2097 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2098 | } |
| 2099 | ActivityInfo aInfo = |
| 2100 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 2101 | STOCK_PM_FLAGS); |
| 2102 | if (aInfo != null) { |
| 2103 | intent.setComponent(new ComponentName( |
| 2104 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2105 | // Don't do this if the home app is currently being |
| 2106 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2107 | aInfo = new ActivityInfo(aInfo); |
| 2108 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2109 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2110 | aInfo.applicationInfo.uid); |
| 2111 | if (app == null || app.instrumentationClass == null) { |
| 2112 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2113 | mMainStack.startActivityLocked(null, intent, null, aInfo, |
| 2114 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2115 | } |
| 2116 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2117 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2118 | return true; |
| 2119 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2120 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2121 | /** |
| 2122 | * Starts the "new version setup screen" if appropriate. |
| 2123 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2124 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2125 | // Only do this once per boot. |
| 2126 | if (mCheckedForSetup) { |
| 2127 | return; |
| 2128 | } |
| 2129 | |
| 2130 | // We will show this screen if the current one is a different |
| 2131 | // version than the last one shown, and we are not running in |
| 2132 | // low-level factory test mode. |
| 2133 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2134 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 2135 | Settings.Secure.getInt(resolver, |
| 2136 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 2137 | mCheckedForSetup = true; |
| 2138 | |
| 2139 | // See if we should be showing the platform update setup UI. |
| 2140 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2141 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2142 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2143 | |
| 2144 | // We don't allow third party apps to replace this. |
| 2145 | ResolveInfo ri = null; |
| 2146 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2147 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2148 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2149 | ri = ris.get(i); |
| 2150 | break; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | if (ri != null) { |
| 2155 | String vers = ri.activityInfo.metaData != null |
| 2156 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2157 | : null; |
| 2158 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2159 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2160 | Intent.METADATA_SETUP_VERSION); |
| 2161 | } |
| 2162 | String lastVers = Settings.Secure.getString( |
| 2163 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2164 | if (vers != null && !vers.equals(lastVers)) { |
| 2165 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2166 | intent.setComponent(new ComponentName( |
| 2167 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2168 | mMainStack.startActivityLocked(null, intent, null, ri.activityInfo, |
| 2169 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2170 | } |
| 2171 | } |
| 2172 | } |
| 2173 | } |
| 2174 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2175 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2176 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2177 | } |
| 2178 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2179 | void enforceNotIsolatedCaller(String caller) { |
| 2180 | if (UserId.isIsolated(Binder.getCallingUid())) { |
| 2181 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2182 | } |
| 2183 | } |
| 2184 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2185 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2186 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2187 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2188 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2189 | } |
| 2190 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2191 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2192 | public void setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2193 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2194 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2195 | synchronized (this) { |
| 2196 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2197 | } |
| 2198 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2199 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2200 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2201 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2202 | synchronized (this) { |
| 2203 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2204 | } |
| 2205 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2206 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2207 | public void setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2208 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2209 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2210 | synchronized (this) { |
| 2211 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2215 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2216 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2217 | synchronized (this) { |
| 2218 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2223 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2224 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2225 | synchronized (this) { |
| 2226 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2227 | } |
| 2228 | } |
| 2229 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2230 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2231 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2232 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2233 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2234 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2235 | private void dispatchProcessesChanged() { |
| 2236 | int N; |
| 2237 | synchronized (this) { |
| 2238 | N = mPendingProcessChanges.size(); |
| 2239 | if (mActiveProcessChanges.length < N) { |
| 2240 | mActiveProcessChanges = new ProcessChangeItem[N]; |
| 2241 | } |
| 2242 | mPendingProcessChanges.toArray(mActiveProcessChanges); |
| 2243 | mAvailProcessChanges.addAll(mPendingProcessChanges); |
| 2244 | mPendingProcessChanges.clear(); |
| 2245 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes"); |
| 2246 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2247 | int i = mProcessObservers.beginBroadcast(); |
| 2248 | while (i > 0) { |
| 2249 | i--; |
| 2250 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2251 | if (observer != null) { |
| 2252 | try { |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2253 | for (int j=0; j<N; j++) { |
| 2254 | ProcessChangeItem item = mActiveProcessChanges[j]; |
| 2255 | if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) { |
| 2256 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid=" |
| 2257 | + item.pid + " uid=" + item.uid + ": " |
| 2258 | + item.foregroundActivities); |
| 2259 | observer.onForegroundActivitiesChanged(item.pid, item.uid, |
| 2260 | item.foregroundActivities); |
| 2261 | } |
| 2262 | if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) { |
| 2263 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid=" |
| 2264 | + item.pid + " uid=" + item.uid + ": " + item.importance); |
| 2265 | observer.onImportanceChanged(item.pid, item.uid, |
| 2266 | item.importance); |
| 2267 | } |
| 2268 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2269 | } catch (RemoteException e) { |
| 2270 | } |
| 2271 | } |
| 2272 | } |
| 2273 | mProcessObservers.finishBroadcast(); |
| 2274 | } |
| 2275 | |
| 2276 | private void dispatchProcessDied(int pid, int uid) { |
| 2277 | int i = mProcessObservers.beginBroadcast(); |
| 2278 | while (i > 0) { |
| 2279 | i--; |
| 2280 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2281 | if (observer != null) { |
| 2282 | try { |
| 2283 | observer.onProcessDied(pid, uid); |
| 2284 | } catch (RemoteException e) { |
| 2285 | } |
| 2286 | } |
| 2287 | } |
| 2288 | mProcessObservers.finishBroadcast(); |
| 2289 | } |
| 2290 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2291 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2292 | final int N = mPendingActivityLaunches.size(); |
| 2293 | if (N <= 0) { |
| 2294 | return; |
| 2295 | } |
| 2296 | for (int i=0; i<N; i++) { |
| 2297 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2298 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2299 | pal.startFlags, doResume && i == (N-1), null); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2300 | } |
| 2301 | mPendingActivityLaunches.clear(); |
| 2302 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2303 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2304 | public final int startActivity(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2305 | Intent intent, String resolvedType, IBinder resultTo, |
| 2306 | String resultWho, int requestCode, int startFlags, |
| 2307 | String profileFile, ParcelFileDescriptor profileFd, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2308 | enforceNotIsolatedCaller("startActivity"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2309 | int userId = 0; |
| 2310 | if (intent.getCategories() != null && intent.getCategories().contains(Intent.CATEGORY_HOME)) { |
| 2311 | // Requesting home, set the identity to the current user |
| 2312 | // HACK! |
| 2313 | userId = mCurrentUserId; |
| 2314 | } else { |
| 2315 | // TODO: Fix this in a better way - calls coming from SystemUI should probably carry |
| 2316 | // the current user's userId |
| 2317 | if (Binder.getCallingUid() < Process.FIRST_APPLICATION_UID) { |
| 2318 | userId = 0; |
| 2319 | } else { |
| 2320 | userId = Binder.getOrigCallingUser(); |
| 2321 | } |
| 2322 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2323 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2324 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2325 | null, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2329 | Intent intent, String resolvedType, IBinder resultTo, |
| 2330 | String resultWho, int requestCode, int startFlags, String profileFile, |
| 2331 | ParcelFileDescriptor profileFd, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2332 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2333 | WaitResult res = new WaitResult(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2334 | int userId = Binder.getOrigCallingUser(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2335 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2336 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2337 | res, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2338 | return res; |
| 2339 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 2340 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2341 | public final int startActivityWithConfig(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2342 | Intent intent, String resolvedType, IBinder resultTo, |
| 2343 | String resultWho, int requestCode, int startFlags, Configuration config, |
| 2344 | Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2345 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2346 | int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2347 | resultTo, resultWho, requestCode, startFlags, |
| 2348 | null, null, null, config, options, Binder.getOrigCallingUser()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2349 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2352 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2353 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2354 | IBinder resultTo, String resultWho, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2355 | int flagsMask, int flagsValues, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2356 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2357 | // Refuse possible leaked file descriptors |
| 2358 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2359 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2360 | } |
| 2361 | |
| 2362 | IIntentSender sender = intent.getTarget(); |
| 2363 | if (!(sender instanceof PendingIntentRecord)) { |
| 2364 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2365 | } |
| 2366 | |
| 2367 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2368 | |
| 2369 | synchronized (this) { |
| 2370 | // If this is coming from the currently resumed activity, it is |
| 2371 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2372 | if (mMainStack.mResumedActivity != null |
| 2373 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2374 | Binder.getCallingUid()) { |
| 2375 | mAppSwitchesAllowedTime = 0; |
| 2376 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2377 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2378 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null, |
| 2379 | resultTo, resultWho, requestCode, flagsMask, flagsValues, options); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2380 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2383 | public boolean startNextMatchingActivity(IBinder callingActivity, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2384 | Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2385 | // Refuse possible leaked file descriptors |
| 2386 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2387 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2388 | } |
| 2389 | |
| 2390 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2391 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2392 | if (r == null) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2393 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2394 | return false; |
| 2395 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2396 | if (r.app == null || r.app.thread == null) { |
| 2397 | // The caller is not running... d'oh! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2398 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2399 | return false; |
| 2400 | } |
| 2401 | intent = new Intent(intent); |
| 2402 | // The caller is not allowed to change the data. |
| 2403 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2404 | // And we are resetting to find the next component... |
| 2405 | intent.setComponent(null); |
| 2406 | |
| 2407 | ActivityInfo aInfo = null; |
| 2408 | try { |
| 2409 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2410 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2411 | intent, r.resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2412 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS, |
| 2413 | UserId.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2414 | |
| 2415 | // Look for the original activity in the list... |
| 2416 | final int N = resolves != null ? resolves.size() : 0; |
| 2417 | for (int i=0; i<N; i++) { |
| 2418 | ResolveInfo rInfo = resolves.get(i); |
| 2419 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2420 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2421 | // We found the current one... the next matching is |
| 2422 | // after it. |
| 2423 | i++; |
| 2424 | if (i<N) { |
| 2425 | aInfo = resolves.get(i).activityInfo; |
| 2426 | } |
| 2427 | break; |
| 2428 | } |
| 2429 | } |
| 2430 | } catch (RemoteException e) { |
| 2431 | } |
| 2432 | |
| 2433 | if (aInfo == null) { |
| 2434 | // Nobody who is next! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2435 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2436 | return false; |
| 2437 | } |
| 2438 | |
| 2439 | intent.setComponent(new ComponentName( |
| 2440 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2441 | intent.setFlags(intent.getFlags()&~( |
| 2442 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2443 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2444 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2445 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2446 | |
| 2447 | // Okay now we need to start the new activity, replacing the |
| 2448 | // currently running activity. This is a little tricky because |
| 2449 | // we want to start the new one as if the current one is finished, |
| 2450 | // but not finish the current one first so that there is no flicker. |
| 2451 | // And thus... |
| 2452 | final boolean wasFinishing = r.finishing; |
| 2453 | r.finishing = true; |
| 2454 | |
| 2455 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2456 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2457 | final String resultWho = r.resultWho; |
| 2458 | final int requestCode = r.requestCode; |
| 2459 | r.resultTo = null; |
| 2460 | if (resultTo != null) { |
| 2461 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2462 | } |
| 2463 | |
| 2464 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2465 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2466 | r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null, |
| 2467 | resultWho, requestCode, -1, r.launchedFromUid, 0, |
| 2468 | options, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2469 | Binder.restoreCallingIdentity(origId); |
| 2470 | |
| 2471 | r.finishing = wasFinishing; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2472 | if (res != ActivityManager.START_SUCCESS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2473 | return false; |
| 2474 | } |
| 2475 | return true; |
| 2476 | } |
| 2477 | } |
| 2478 | |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2479 | public final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2480 | Intent intent, String resolvedType, IBinder resultTo, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2481 | String resultWho, int requestCode, int startFlags, Bundle options) { |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2482 | |
| 2483 | // This is so super not safe, that only the system (or okay root) |
| 2484 | // can do it. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2485 | int userId = Binder.getOrigCallingUser(); |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2486 | final int callingUid = Binder.getCallingUid(); |
| 2487 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2488 | throw new SecurityException( |
| 2489 | "startActivityInPackage only available to the system"); |
| 2490 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2491 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2492 | int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2493 | resultTo, resultWho, requestCode, startFlags, |
| 2494 | null, null, null, null, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2495 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | public final int startActivities(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2499 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2500 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2501 | int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2502 | options, Binder.getOrigCallingUser()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2503 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | public final int startActivitiesInPackage(int uid, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2507 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, |
| 2508 | Bundle options) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2509 | |
| 2510 | // This is so super not safe, that only the system (or okay root) |
| 2511 | // can do it. |
| 2512 | final int callingUid = Binder.getCallingUid(); |
| 2513 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2514 | throw new SecurityException( |
| 2515 | "startActivityInPackage only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2516 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2517 | int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2518 | options, UserId.getUserId(uid)); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2519 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2520 | } |
| 2521 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2522 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2523 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2524 | // Quick case: check if the top-most recent task is the same. |
| 2525 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2526 | return; |
| 2527 | } |
| 2528 | // 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] | 2529 | for (int i=0; i<N; i++) { |
| 2530 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2531 | if (task.userId == tr.userId |
| 2532 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2533 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2534 | mRecentTasks.remove(i); |
| 2535 | i--; |
| 2536 | N--; |
| 2537 | if (task.intent == null) { |
| 2538 | // If the new recent task we are adding is not fully |
| 2539 | // specified, then replace it with the existing recent task. |
| 2540 | task = tr; |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | if (N >= MAX_RECENT_TASKS) { |
| 2545 | mRecentTasks.remove(N-1); |
| 2546 | } |
| 2547 | mRecentTasks.add(0, task); |
| 2548 | } |
| 2549 | |
| 2550 | public void setRequestedOrientation(IBinder token, |
| 2551 | int requestedOrientation) { |
| 2552 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2553 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2554 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2555 | return; |
| 2556 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2557 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2558 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2559 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2560 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2561 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2562 | if (config != null) { |
| 2563 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2564 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2565 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | Binder.restoreCallingIdentity(origId); |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | public int getRequestedOrientation(IBinder token) { |
| 2573 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2574 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2575 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2576 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2577 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2578 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2579 | } |
| 2580 | } |
| 2581 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2582 | /** |
| 2583 | * This is the internal entry point for handling Activity.finish(). |
| 2584 | * |
| 2585 | * @param token The Binder token referencing the Activity we want to finish. |
| 2586 | * @param resultCode Result code, if any, from this Activity. |
| 2587 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2588 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2589 | * @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] | 2590 | */ |
| 2591 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2592 | // Refuse possible leaked file descriptors |
| 2593 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2594 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2595 | } |
| 2596 | |
| 2597 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2598 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2599 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2600 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2601 | if (next != null) { |
| 2602 | // ask watcher if this is allowed |
| 2603 | boolean resumeOK = true; |
| 2604 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2605 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2606 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2607 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | if (!resumeOK) { |
| 2611 | return false; |
| 2612 | } |
| 2613 | } |
| 2614 | } |
| 2615 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2616 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2617 | resultData, "app-request"); |
| 2618 | Binder.restoreCallingIdentity(origId); |
| 2619 | return res; |
| 2620 | } |
| 2621 | } |
| 2622 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2623 | public final void finishHeavyWeightApp() { |
| 2624 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2625 | != PackageManager.PERMISSION_GRANTED) { |
| 2626 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2627 | + Binder.getCallingPid() |
| 2628 | + ", uid=" + Binder.getCallingUid() |
| 2629 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2630 | Slog.w(TAG, msg); |
| 2631 | throw new SecurityException(msg); |
| 2632 | } |
| 2633 | |
| 2634 | synchronized(this) { |
| 2635 | if (mHeavyWeightProcess == null) { |
| 2636 | return; |
| 2637 | } |
| 2638 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2639 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2640 | mHeavyWeightProcess.activities); |
| 2641 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2642 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2643 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2644 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2645 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2646 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2647 | null, "finish-heavy"); |
| 2648 | } |
| 2649 | } |
| 2650 | } |
| 2651 | |
| 2652 | mHeavyWeightProcess = null; |
| 2653 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 2654 | } |
| 2655 | } |
| 2656 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2657 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2658 | String message) { |
| 2659 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2660 | != PackageManager.PERMISSION_GRANTED) { |
| 2661 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2662 | + Binder.getCallingPid() |
| 2663 | + ", uid=" + Binder.getCallingUid() |
| 2664 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2665 | Slog.w(TAG, msg); |
| 2666 | throw new SecurityException(msg); |
| 2667 | } |
| 2668 | |
| 2669 | synchronized(this) { |
| 2670 | ProcessRecord proc = null; |
| 2671 | |
| 2672 | // Figure out which process to kill. We don't trust that initialPid |
| 2673 | // still has any relation to current pids, so must scan through the |
| 2674 | // list. |
| 2675 | synchronized (mPidsSelfLocked) { |
| 2676 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2677 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2678 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2679 | continue; |
| 2680 | } |
| 2681 | if (p.pid == initialPid) { |
| 2682 | proc = p; |
| 2683 | break; |
| 2684 | } |
| 2685 | for (String str : p.pkgList) { |
| 2686 | if (str.equals(packageName)) { |
| 2687 | proc = p; |
| 2688 | } |
| 2689 | } |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2694 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2695 | + " initialPid=" + initialPid |
| 2696 | + " packageName=" + packageName); |
| 2697 | return; |
| 2698 | } |
| 2699 | |
| 2700 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2701 | if (proc.pid == Process.myPid()) { |
| 2702 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2703 | return; |
| 2704 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2705 | long ident = Binder.clearCallingIdentity(); |
| 2706 | try { |
| 2707 | proc.thread.scheduleCrash(message); |
| 2708 | } catch (RemoteException e) { |
| 2709 | } |
| 2710 | Binder.restoreCallingIdentity(ident); |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2715 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2716 | int requestCode) { |
| 2717 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2718 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2719 | mMainStack.finishSubActivityLocked(token, resultWho, requestCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | Binder.restoreCallingIdentity(origId); |
| 2721 | } |
| 2722 | } |
| 2723 | |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2724 | public boolean finishActivityAffinity(IBinder token) { |
| 2725 | synchronized(this) { |
| 2726 | final long origId = Binder.clearCallingIdentity(); |
| 2727 | boolean res = mMainStack.finishActivityAffinityLocked(token); |
| 2728 | Binder.restoreCallingIdentity(origId); |
| 2729 | return res; |
| 2730 | } |
| 2731 | } |
| 2732 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2733 | public boolean willActivityBeVisible(IBinder token) { |
| 2734 | synchronized(this) { |
| 2735 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2736 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2737 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2738 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2739 | return true; |
| 2740 | } |
| 2741 | if (r.fullscreen && !r.finishing) { |
| 2742 | return false; |
| 2743 | } |
| 2744 | } |
| 2745 | return true; |
| 2746 | } |
| 2747 | } |
| 2748 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2749 | public void overridePendingTransition(IBinder token, String packageName, |
| 2750 | int enterAnim, int exitAnim) { |
| 2751 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2752 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2753 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2754 | return; |
| 2755 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2756 | |
| 2757 | final long origId = Binder.clearCallingIdentity(); |
| 2758 | |
| 2759 | if (self.state == ActivityState.RESUMED |
| 2760 | || self.state == ActivityState.PAUSING) { |
| 2761 | mWindowManager.overridePendingAppTransition(packageName, |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2762 | enterAnim, exitAnim, null); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | Binder.restoreCallingIdentity(origId); |
| 2766 | } |
| 2767 | } |
| 2768 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2769 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2770 | * Main function for removing an existing process from the activity manager |
| 2771 | * as a result of that process going away. Clears out all connections |
| 2772 | * to the process. |
| 2773 | */ |
| 2774 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2775 | boolean restarting, boolean allowRestart) { |
| 2776 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2777 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2778 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2779 | } |
| 2780 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2781 | if (mProfileProc == app) { |
| 2782 | clearProfilerLocked(); |
| 2783 | } |
| 2784 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2785 | // Just in case... |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2786 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2787 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2788 | mMainStack.mPausingActivity = null; |
| 2789 | } |
| 2790 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2791 | mMainStack.mLastPausedActivity = null; |
| 2792 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2793 | |
| 2794 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2795 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2796 | |
| 2797 | boolean atTop = true; |
| 2798 | boolean hasVisibleActivities = false; |
| 2799 | |
| 2800 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2801 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2802 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2803 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2804 | while (i > 0) { |
| 2805 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2806 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2807 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2808 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2809 | if (r.app == app) { |
| 2810 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2811 | if (ActivityStack.DEBUG_ADD_REMOVE) { |
| 2812 | RuntimeException here = new RuntimeException("here"); |
| 2813 | here.fillInStackTrace(); |
| 2814 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 2815 | + ": haveState=" + r.haveState |
| 2816 | + " stateNotNeeded=" + r.stateNotNeeded |
| 2817 | + " finishing=" + r.finishing |
| 2818 | + " state=" + r.state, here); |
| 2819 | } |
| 2820 | if (!r.finishing) { |
| 2821 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 2822 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
| 2823 | System.identityHashCode(r), |
| 2824 | r.task.taskId, r.shortComponentName, |
| 2825 | "proc died without state saved"); |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2826 | } |
Dianne Hackborn | 5c60743 | 2012-02-28 14:44:19 -0800 | [diff] [blame] | 2827 | mMainStack.removeActivityFromHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2828 | |
| 2829 | } else { |
| 2830 | // We have the current state for this activity, so |
| 2831 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2832 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2833 | TAG, "Keeping entry, setting app to null"); |
| 2834 | if (r.visible) { |
| 2835 | hasVisibleActivities = true; |
| 2836 | } |
| 2837 | r.app = null; |
| 2838 | r.nowVisible = false; |
| 2839 | if (!r.haveState) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2840 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 2841 | "App died, clearing saved state of " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2842 | r.icicle = null; |
| 2843 | } |
| 2844 | } |
| 2845 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2846 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2847 | } |
| 2848 | atTop = false; |
| 2849 | } |
| 2850 | |
| 2851 | app.activities.clear(); |
| 2852 | |
| 2853 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2854 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2855 | + " running instrumentation " + app.instrumentationClass); |
| 2856 | Bundle info = new Bundle(); |
| 2857 | info.putString("shortMsg", "Process crashed."); |
| 2858 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2859 | } |
| 2860 | |
| 2861 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2862 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2863 | // If there was nothing to resume, and we are not already |
| 2864 | // restarting this process, but there is a visible activity that |
| 2865 | // is hosted by the process... then make sure all visible |
| 2866 | // activities are running, taking care of restarting this |
| 2867 | // process. |
| 2868 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2869 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2870 | } |
| 2871 | } |
| 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2876 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2877 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2878 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2879 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2880 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2881 | return i; |
| 2882 | } |
| 2883 | } |
| 2884 | return -1; |
| 2885 | } |
| 2886 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2887 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2888 | IApplicationThread thread) { |
| 2889 | if (thread == null) { |
| 2890 | return null; |
| 2891 | } |
| 2892 | |
| 2893 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2894 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2895 | } |
| 2896 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2897 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2898 | IApplicationThread thread) { |
| 2899 | |
| 2900 | mProcDeaths[0]++; |
| 2901 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2902 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2903 | synchronized (stats) { |
| 2904 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2905 | } |
| 2906 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2907 | // Clean up already done if the process has been re-started. |
| 2908 | if (app.pid == pid && app.thread != null && |
| 2909 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2910 | if (!app.killedBackground) { |
| 2911 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2912 | + ") has died."); |
| 2913 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2914 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2915 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2916 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2917 | + ", thread: " + thread.asBinder()); |
| 2918 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2919 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2920 | |
| 2921 | if (doLowMem) { |
| 2922 | // If there are no longer any background processes running, |
| 2923 | // and the app that died was not running instrumentation, |
| 2924 | // then tell everyone we are now low on memory. |
| 2925 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2926 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2927 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2928 | 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] | 2929 | haveBg = true; |
| 2930 | break; |
| 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2935 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2936 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2937 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2938 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2939 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2940 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2941 | // The low memory report is overriding any current |
| 2942 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2943 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2944 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2945 | rec.lastRequestedGc = 0; |
| 2946 | } else { |
| 2947 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2948 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2949 | rec.reportLowMemory = true; |
| 2950 | rec.lastLowMemory = now; |
| 2951 | mProcessesToGc.remove(rec); |
| 2952 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2953 | } |
| 2954 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 2955 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2956 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2957 | } |
| 2958 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2959 | } else if (app.pid != pid) { |
| 2960 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2961 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2962 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 2963 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 2964 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2965 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2966 | + thread.asBinder()); |
| 2967 | } |
| 2968 | } |
| 2969 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2970 | /** |
| 2971 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2972 | * @param clearTraces causes the dump file to be erased prior to the new |
| 2973 | * traces being written, if true; when false, the new traces will be |
| 2974 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2975 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 2976 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 2977 | * @param nativeProcs optional list of native process names to dump stack crawls |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2978 | * @return file containing stack traces, or null if no dump file is configured |
| 2979 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2980 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 2981 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2982 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 2983 | if (tracesPath == null || tracesPath.length() == 0) { |
| 2984 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2985 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2986 | |
| 2987 | File tracesFile = new File(tracesPath); |
| 2988 | try { |
| 2989 | File tracesDir = tracesFile.getParentFile(); |
| 2990 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 2991 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 2992 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2993 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2994 | tracesFile.createNewFile(); |
| 2995 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 2996 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2997 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2998 | return null; |
| 2999 | } |
| 3000 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3001 | dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3002 | return tracesFile; |
| 3003 | } |
| 3004 | |
| 3005 | private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3006 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3007 | // Use a FileObserver to detect when traces finish writing. |
| 3008 | // The order of traces is considered important to maintain for legibility. |
| 3009 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 3010 | public synchronized void onEvent(int event, String path) { notify(); } |
| 3011 | }; |
| 3012 | |
| 3013 | try { |
| 3014 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3015 | |
| 3016 | // First collect all of the stacks of the most important pids. |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3017 | if (firstPids != null) { |
| 3018 | try { |
| 3019 | int num = firstPids.size(); |
| 3020 | for (int i = 0; i < num; i++) { |
| 3021 | synchronized (observer) { |
| 3022 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3023 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3024 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3025 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3026 | } catch (InterruptedException e) { |
| 3027 | Log.wtf(TAG, e); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3028 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | // Next measure CPU usage. |
| 3032 | if (processStats != null) { |
| 3033 | processStats.init(); |
| 3034 | System.gc(); |
| 3035 | processStats.update(); |
| 3036 | try { |
| 3037 | synchronized (processStats) { |
| 3038 | processStats.wait(500); // measure over 1/2 second. |
| 3039 | } |
| 3040 | } catch (InterruptedException e) { |
| 3041 | } |
| 3042 | processStats.update(); |
| 3043 | |
| 3044 | // We'll take the stack crawls of just the top apps using CPU. |
| 3045 | final int N = processStats.countWorkingStats(); |
| 3046 | int numProcs = 0; |
| 3047 | for (int i=0; i<N && numProcs<5; i++) { |
| 3048 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3049 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3050 | numProcs++; |
| 3051 | try { |
| 3052 | synchronized (observer) { |
| 3053 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3054 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3055 | } |
| 3056 | } catch (InterruptedException e) { |
| 3057 | Log.wtf(TAG, e); |
| 3058 | } |
| 3059 | |
| 3060 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3061 | } |
| 3062 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3063 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3064 | } finally { |
| 3065 | observer.stopWatching(); |
| 3066 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3067 | |
| 3068 | if (nativeProcs != null) { |
| 3069 | int[] pids = Process.getPidsForCommands(nativeProcs); |
| 3070 | if (pids != null) { |
| 3071 | for (int pid : pids) { |
| 3072 | Debug.dumpNativeBacktraceToFile(pid, tracesPath); |
| 3073 | } |
| 3074 | } |
| 3075 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3076 | } |
| 3077 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3078 | final void logAppTooSlow(ProcessRecord app, long startTime, String msg) { |
Dianne Hackborn | 69dc66e | 2012-03-26 10:50:54 -0700 | [diff] [blame] | 3079 | if (true || IS_USER_BUILD) { |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3080 | return; |
| 3081 | } |
| 3082 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3083 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3084 | return; |
| 3085 | } |
| 3086 | |
| 3087 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 3088 | StrictMode.allowThreadDiskWrites(); |
| 3089 | try { |
| 3090 | final File tracesFile = new File(tracesPath); |
| 3091 | final File tracesDir = tracesFile.getParentFile(); |
| 3092 | final File tracesTmp = new File(tracesDir, "__tmp__"); |
| 3093 | try { |
| 3094 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 3095 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3096 | |
| 3097 | if (tracesFile.exists()) { |
| 3098 | tracesTmp.delete(); |
| 3099 | tracesFile.renameTo(tracesTmp); |
| 3100 | } |
| 3101 | StringBuilder sb = new StringBuilder(); |
| 3102 | Time tobj = new Time(); |
| 3103 | tobj.set(System.currentTimeMillis()); |
| 3104 | sb.append(tobj.format("%Y-%m-%d %H:%M:%S")); |
| 3105 | sb.append(": "); |
| 3106 | TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb); |
| 3107 | sb.append(" since "); |
| 3108 | sb.append(msg); |
| 3109 | FileOutputStream fos = new FileOutputStream(tracesFile); |
| 3110 | fos.write(sb.toString().getBytes()); |
| 3111 | if (app == null) { |
| 3112 | fos.write("\n*** No application process!".getBytes()); |
| 3113 | } |
| 3114 | fos.close(); |
| 3115 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3116 | } catch (IOException e) { |
| 3117 | Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e); |
| 3118 | return; |
| 3119 | } |
| 3120 | |
| 3121 | if (app != null) { |
| 3122 | ArrayList<Integer> firstPids = new ArrayList<Integer>(); |
| 3123 | firstPids.add(app.pid); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3124 | dumpStackTraces(tracesPath, firstPids, null, null, null); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | File lastTracesFile = null; |
| 3128 | File curTracesFile = null; |
| 3129 | for (int i=9; i>=0; i--) { |
| 3130 | String name = String.format("slow%02d.txt", i); |
| 3131 | curTracesFile = new File(tracesDir, name); |
| 3132 | if (curTracesFile.exists()) { |
| 3133 | if (lastTracesFile != null) { |
| 3134 | curTracesFile.renameTo(lastTracesFile); |
| 3135 | } else { |
| 3136 | curTracesFile.delete(); |
| 3137 | } |
| 3138 | } |
| 3139 | lastTracesFile = curTracesFile; |
| 3140 | } |
| 3141 | tracesFile.renameTo(curTracesFile); |
| 3142 | if (tracesTmp.exists()) { |
| 3143 | tracesTmp.renameTo(tracesFile); |
| 3144 | } |
| 3145 | } finally { |
| 3146 | StrictMode.setThreadPolicy(oldPolicy); |
| 3147 | } |
| 3148 | } |
| 3149 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3150 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 3151 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3152 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3153 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3154 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3155 | if (mController != null) { |
| 3156 | try { |
| 3157 | // 0 == continue, -1 = kill process immediately |
| 3158 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3159 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3160 | } catch (RemoteException e) { |
| 3161 | mController = null; |
| 3162 | } |
| 3163 | } |
| 3164 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3165 | long anrTime = SystemClock.uptimeMillis(); |
| 3166 | if (MONITOR_CPU_USAGE) { |
| 3167 | updateCpuStatsNow(); |
| 3168 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3169 | |
| 3170 | synchronized (this) { |
| 3171 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3172 | if (mShuttingDown) { |
| 3173 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3174 | return; |
| 3175 | } else if (app.notResponding) { |
| 3176 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3177 | return; |
| 3178 | } else if (app.crashing) { |
| 3179 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3180 | return; |
| 3181 | } |
| 3182 | |
| 3183 | // In case we come through here for the same app before completing |
| 3184 | // this one, mark as anring now so we will bail out. |
| 3185 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3186 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3187 | // Log the ANR to the event log. |
| 3188 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 3189 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3190 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3191 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3192 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3193 | |
| 3194 | int parentPid = app.pid; |
| 3195 | 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] | 3196 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3197 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3198 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3199 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3200 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3201 | ProcessRecord r = mLruProcesses.get(i); |
| 3202 | if (r != null && r.thread != null) { |
| 3203 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3204 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3205 | if (r.persistent) { |
| 3206 | firstPids.add(pid); |
| 3207 | } else { |
| 3208 | lastPids.put(pid, Boolean.TRUE); |
| 3209 | } |
| 3210 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3211 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3212 | } |
| 3213 | } |
| 3214 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3215 | // Log the ANR to the main log. |
Jeff Brown | deb6ed8 | 2012-04-10 14:26:26 -0700 | [diff] [blame] | 3216 | StringBuilder info = new StringBuilder(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3217 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3218 | info.append("ANR in ").append(app.processName); |
| 3219 | if (activity != null && activity.shortComponentName != null) { |
| 3220 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3221 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3222 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3223 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3224 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3225 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3226 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3227 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3228 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3229 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3230 | final ProcessStats processStats = new ProcessStats(true); |
| 3231 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3232 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3233 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3234 | String cpuInfo = null; |
| 3235 | if (MONITOR_CPU_USAGE) { |
| 3236 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3237 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3238 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3239 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3240 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3241 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3242 | } |
| 3243 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3244 | info.append(processStats.printCurrentState(anrTime)); |
| 3245 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3246 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3247 | if (tracesFile == null) { |
| 3248 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3249 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3250 | } |
| 3251 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3252 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3253 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3254 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3255 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3256 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3257 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3258 | 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] | 3259 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3260 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3261 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3262 | } |
| 3263 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3264 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3265 | } |
| 3266 | } |
| 3267 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3268 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3269 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3270 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3271 | |
| 3272 | synchronized (this) { |
| 3273 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3274 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 3275 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 3276 | app.processName, app.setAdj, "background ANR"); |
| 3277 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3278 | return; |
| 3279 | } |
| 3280 | |
| 3281 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3282 | makeAppNotRespondingLocked(app, |
| 3283 | activity != null ? activity.shortComponentName : null, |
| 3284 | annotation != null ? "ANR " + annotation : "ANR", |
| 3285 | info.toString()); |
| 3286 | |
| 3287 | // Bring up the infamous App Not Responding dialog |
| 3288 | Message msg = Message.obtain(); |
| 3289 | HashMap map = new HashMap(); |
| 3290 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3291 | msg.obj = map; |
| 3292 | map.put("app", app); |
| 3293 | if (activity != null) { |
| 3294 | map.put("activity", activity); |
| 3295 | } |
| 3296 | |
| 3297 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3298 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3299 | } |
| 3300 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3301 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3302 | if (!mLaunchWarningShown) { |
| 3303 | mLaunchWarningShown = true; |
| 3304 | mHandler.post(new Runnable() { |
| 3305 | @Override |
| 3306 | public void run() { |
| 3307 | synchronized (ActivityManagerService.this) { |
| 3308 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3309 | d.show(); |
| 3310 | mHandler.postDelayed(new Runnable() { |
| 3311 | @Override |
| 3312 | public void run() { |
| 3313 | synchronized (ActivityManagerService.this) { |
| 3314 | d.dismiss(); |
| 3315 | mLaunchWarningShown = false; |
| 3316 | } |
| 3317 | } |
| 3318 | }, 4000); |
| 3319 | } |
| 3320 | } |
| 3321 | }); |
| 3322 | } |
| 3323 | } |
| 3324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3325 | public boolean clearApplicationUserData(final String packageName, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3326 | final IPackageDataObserver observer, final int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3327 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3328 | int uid = Binder.getCallingUid(); |
| 3329 | int pid = Binder.getCallingPid(); |
| 3330 | long callingId = Binder.clearCallingIdentity(); |
| 3331 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3332 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3333 | int pkgUid = -1; |
| 3334 | synchronized(this) { |
| 3335 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3336 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3337 | } catch (RemoteException e) { |
| 3338 | } |
| 3339 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3340 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3341 | return false; |
| 3342 | } |
| 3343 | if (uid == pkgUid || checkComponentPermission( |
| 3344 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3345 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3346 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3347 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3348 | } else { |
| 3349 | throw new SecurityException(pid+" does not have permission:"+ |
| 3350 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3351 | "for process:"+packageName); |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | try { |
| 3356 | //clear application user data |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3357 | pm.clearApplicationUserData(packageName, observer, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3358 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3359 | Uri.fromParts("package", packageName, null)); |
| 3360 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3361 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3362 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3363 | } catch (RemoteException e) { |
| 3364 | } |
| 3365 | } finally { |
| 3366 | Binder.restoreCallingIdentity(callingId); |
| 3367 | } |
| 3368 | return true; |
| 3369 | } |
| 3370 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3371 | public void killBackgroundProcesses(final String packageName) { |
| 3372 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3373 | != PackageManager.PERMISSION_GRANTED && |
| 3374 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3375 | != PackageManager.PERMISSION_GRANTED) { |
| 3376 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3377 | + Binder.getCallingPid() |
| 3378 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3379 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3380 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3381 | throw new SecurityException(msg); |
| 3382 | } |
| 3383 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3384 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3385 | long callingId = Binder.clearCallingIdentity(); |
| 3386 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3387 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3388 | int pkgUid = -1; |
| 3389 | synchronized(this) { |
| 3390 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3391 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3392 | } catch (RemoteException e) { |
| 3393 | } |
| 3394 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3395 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3396 | return; |
| 3397 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3398 | killPackageProcessesLocked(packageName, pkgUid, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3399 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3400 | } |
| 3401 | } finally { |
| 3402 | Binder.restoreCallingIdentity(callingId); |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | public void killAllBackgroundProcesses() { |
| 3407 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3408 | != PackageManager.PERMISSION_GRANTED) { |
| 3409 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3410 | + Binder.getCallingPid() |
| 3411 | + ", uid=" + Binder.getCallingUid() |
| 3412 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3413 | Slog.w(TAG, msg); |
| 3414 | throw new SecurityException(msg); |
| 3415 | } |
| 3416 | |
| 3417 | long callingId = Binder.clearCallingIdentity(); |
| 3418 | try { |
| 3419 | synchronized(this) { |
| 3420 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3421 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3422 | final int NA = apps.size(); |
| 3423 | for (int ia=0; ia<NA; ia++) { |
| 3424 | ProcessRecord app = apps.valueAt(ia); |
| 3425 | if (app.persistent) { |
| 3426 | // we don't kill persistent processes |
| 3427 | continue; |
| 3428 | } |
| 3429 | if (app.removed) { |
| 3430 | procs.add(app); |
| 3431 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3432 | app.removed = true; |
| 3433 | procs.add(app); |
| 3434 | } |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | int N = procs.size(); |
| 3439 | for (int i=0; i<N; i++) { |
| 3440 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3441 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3442 | } |
| 3443 | } finally { |
| 3444 | Binder.restoreCallingIdentity(callingId); |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | public void forceStopPackage(final String packageName) { |
| 3449 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3450 | != PackageManager.PERMISSION_GRANTED) { |
| 3451 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3452 | + Binder.getCallingPid() |
| 3453 | + ", uid=" + Binder.getCallingUid() |
| 3454 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3455 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3456 | throw new SecurityException(msg); |
| 3457 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3458 | final int userId = UserId.getCallingUserId(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3459 | long callingId = Binder.clearCallingIdentity(); |
| 3460 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3461 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3462 | int pkgUid = -1; |
| 3463 | synchronized(this) { |
| 3464 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3465 | pkgUid = pm.getPackageUid(packageName, userId); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3466 | } catch (RemoteException e) { |
| 3467 | } |
| 3468 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3469 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3470 | return; |
| 3471 | } |
| 3472 | forceStopPackageLocked(packageName, pkgUid); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3473 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3474 | pm.setPackageStoppedState(packageName, true, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3475 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 3476 | } catch (IllegalArgumentException e) { |
| 3477 | Slog.w(TAG, "Failed trying to unstop package " |
| 3478 | + packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3480 | } |
| 3481 | } finally { |
| 3482 | Binder.restoreCallingIdentity(callingId); |
| 3483 | } |
| 3484 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3485 | |
| 3486 | /* |
| 3487 | * The pkg name and uid have to be specified. |
| 3488 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3489 | */ |
| 3490 | public void killApplicationWithUid(String pkg, int uid) { |
| 3491 | if (pkg == null) { |
| 3492 | return; |
| 3493 | } |
| 3494 | // Make sure the uid is valid. |
| 3495 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3496 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3497 | return; |
| 3498 | } |
| 3499 | int callerUid = Binder.getCallingUid(); |
| 3500 | // Only the system server can kill an application |
| 3501 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3502 | // Post an aysnc message to kill the application |
| 3503 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3504 | msg.arg1 = uid; |
| 3505 | msg.arg2 = 0; |
| 3506 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3507 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3508 | } else { |
| 3509 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3510 | pkg); |
| 3511 | } |
| 3512 | } |
| 3513 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3514 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3515 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3516 | |
| 3517 | final int uid = Binder.getCallingUid(); |
| 3518 | final long origId = Binder.clearCallingIdentity(); |
| 3519 | synchronized (this) { |
| 3520 | closeSystemDialogsLocked(uid, reason); |
| 3521 | } |
| 3522 | Binder.restoreCallingIdentity(origId); |
| 3523 | } |
| 3524 | |
| 3525 | void closeSystemDialogsLocked(int callingUid, String reason) { |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3526 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3527 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3528 | if (reason != null) { |
| 3529 | intent.putExtra("reason", reason); |
| 3530 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3531 | mWindowManager.closeSystemDialogs(reason); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3532 | |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3533 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3534 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 3535 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3536 | r.stack.finishActivityLocked(r, i, |
| 3537 | Activity.RESULT_CANCELED, null, "close-sys"); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3538 | } |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3539 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3540 | |
| 3541 | broadcastIntentLocked(null, null, intent, null, |
| 3542 | null, 0, null, null, null, false, false, -1, |
| 3543 | callingUid, 0 /* TODO: Verify */); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3544 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3545 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3546 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3547 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3548 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3549 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3550 | for (int i=pids.length-1; i>=0; i--) { |
| 3551 | infos[i] = new Debug.MemoryInfo(); |
| 3552 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3553 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3554 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3555 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3556 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3557 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3558 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3559 | long[] pss = new long[pids.length]; |
| 3560 | for (int i=pids.length-1; i>=0; i--) { |
| 3561 | pss[i] = Debug.getPss(pids[i]); |
| 3562 | } |
| 3563 | return pss; |
| 3564 | } |
| 3565 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3566 | public void killApplicationProcess(String processName, int uid) { |
| 3567 | if (processName == null) { |
| 3568 | return; |
| 3569 | } |
| 3570 | |
| 3571 | int callerUid = Binder.getCallingUid(); |
| 3572 | // Only the system server can kill an application |
| 3573 | if (callerUid == Process.SYSTEM_UID) { |
| 3574 | synchronized (this) { |
| 3575 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3576 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3577 | try { |
| 3578 | app.thread.scheduleSuicide(); |
| 3579 | } catch (RemoteException e) { |
| 3580 | // If the other end already died, then our work here is done. |
| 3581 | } |
| 3582 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3583 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3584 | + processName + " / " + uid); |
| 3585 | } |
| 3586 | } |
| 3587 | } else { |
| 3588 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3589 | processName); |
| 3590 | } |
| 3591 | } |
| 3592 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3593 | private void forceStopPackageLocked(final String packageName, int uid) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3594 | forceStopPackageLocked(packageName, uid, false, false, true, false, UserId.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3595 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3596 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3597 | if (!mProcessesReady) { |
| 3598 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3599 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3600 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3601 | broadcastIntentLocked(null, null, intent, |
| 3602 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3603 | false, false, |
| 3604 | MY_PID, Process.SYSTEM_UID, UserId.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3605 | } |
| 3606 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3607 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3608 | int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3609 | boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3610 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3611 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3612 | // Remove all processes this package may have touched: all with the |
| 3613 | // same UID (except for the system or root user), and all whose name |
| 3614 | // matches the package name. |
| 3615 | final String procNamePrefix = packageName + ":"; |
| 3616 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3617 | final int NA = apps.size(); |
| 3618 | for (int ia=0; ia<NA; ia++) { |
| 3619 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3620 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3621 | // we don't kill persistent processes |
| 3622 | continue; |
| 3623 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3624 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3625 | if (doit) { |
| 3626 | procs.add(app); |
| 3627 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3628 | // If uid is specified and the uid and process name match |
| 3629 | // Or, the uid is not specified and the process name matches |
| 3630 | } else if (((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
Amith Yamasani | 34db3d6 | 2012-04-02 16:35:19 -0700 | [diff] [blame] | 3631 | || ((app.processName.equals(packageName) |
| 3632 | || app.processName.startsWith(procNamePrefix)) |
| 3633 | && uid < 0))) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3634 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3635 | if (!doit) { |
| 3636 | return true; |
| 3637 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3638 | app.removed = true; |
| 3639 | procs.add(app); |
| 3640 | } |
| 3641 | } |
| 3642 | } |
| 3643 | } |
| 3644 | |
| 3645 | int N = procs.size(); |
| 3646 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3647 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3648 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3649 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3650 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3651 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3652 | private final boolean forceStopPackageLocked(String name, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3653 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3654 | boolean evenPersistent, int userId) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3655 | int i; |
| 3656 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3657 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3658 | if (uid < 0) { |
| 3659 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3660 | uid = AppGlobals.getPackageManager().getPackageUid(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3661 | } catch (RemoteException e) { |
| 3662 | } |
| 3663 | } |
| 3664 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3665 | if (doit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3666 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3667 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3668 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3669 | while (badApps.hasNext()) { |
| 3670 | SparseArray<Long> ba = badApps.next(); |
| 3671 | if (ba.get(uid) != null) { |
| 3672 | badApps.remove(); |
| 3673 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3674 | } |
| 3675 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3676 | |
| 3677 | boolean didSomething = killPackageProcessesLocked(name, uid, -100, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3678 | callerWillRestart, false, doit, evenPersistent, "force stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3679 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3680 | TaskRecord lastTask = null; |
| 3681 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3682 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3683 | final boolean samePackage = r.packageName.equals(name); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3684 | if (r.userId == userId |
| 3685 | && (samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3686 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3687 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3688 | if (r.finishing) { |
| 3689 | // If this activity is just finishing, then it is not |
| 3690 | // interesting as far as something to stop. |
| 3691 | continue; |
| 3692 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3693 | return true; |
| 3694 | } |
| 3695 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3696 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3697 | if (samePackage) { |
| 3698 | if (r.app != null) { |
| 3699 | r.app.removed = true; |
| 3700 | } |
| 3701 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3702 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3703 | lastTask = r.task; |
| 3704 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3705 | null, "force-stop", true)) { |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3706 | i--; |
| 3707 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3708 | } |
| 3709 | } |
| 3710 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3711 | if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) { |
| 3712 | if (!doit) { |
| 3713 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3714 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3715 | didSomething = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3716 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3717 | |
| 3718 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3719 | for (ContentProviderRecord provider : mProviderMap.getProvidersByClass(userId).values()) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3720 | if (provider.info.packageName.equals(name) |
| 3721 | && (provider.proc == null || evenPersistent || !provider.proc.persistent)) { |
| 3722 | if (!doit) { |
| 3723 | return true; |
| 3724 | } |
| 3725 | didSomething = true; |
| 3726 | providers.add(provider); |
| 3727 | } |
| 3728 | } |
| 3729 | |
| 3730 | N = providers.size(); |
| 3731 | for (i=0; i<N; i++) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 3732 | removeDyingProviderLocked(null, providers.get(i), true); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3733 | } |
| 3734 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3735 | if (doit) { |
| 3736 | if (purgeCache) { |
| 3737 | AttributeCache ac = AttributeCache.instance(); |
| 3738 | if (ac != null) { |
| 3739 | ac.removePackage(name); |
| 3740 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3741 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 3742 | if (mBooted) { |
| 3743 | mMainStack.resumeTopActivityLocked(null); |
| 3744 | mMainStack.scheduleIdleLocked(); |
| 3745 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3746 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3747 | |
| 3748 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3749 | } |
| 3750 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3751 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3752 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3753 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3754 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3755 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3756 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3757 | + "/" + uid + ")"); |
| 3758 | |
| 3759 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3760 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3761 | if (mHeavyWeightProcess == app) { |
| 3762 | mHeavyWeightProcess = null; |
| 3763 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3764 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3765 | boolean needRestart = false; |
| 3766 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3767 | int pid = app.pid; |
| 3768 | synchronized (mPidsSelfLocked) { |
| 3769 | mPidsSelfLocked.remove(pid); |
| 3770 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3771 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3772 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3773 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3774 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3775 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3776 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3777 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3778 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3779 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3780 | } else { |
| 3781 | needRestart = true; |
| 3782 | } |
| 3783 | } |
| 3784 | } else { |
| 3785 | mRemovedProcesses.add(app); |
| 3786 | } |
| 3787 | |
| 3788 | return needRestart; |
| 3789 | } |
| 3790 | |
| 3791 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3792 | final int pid = app.pid; |
| 3793 | boolean gone = false; |
| 3794 | synchronized (mPidsSelfLocked) { |
| 3795 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3796 | if (knownApp != null && knownApp.thread == null) { |
| 3797 | mPidsSelfLocked.remove(pid); |
| 3798 | gone = true; |
| 3799 | } |
| 3800 | } |
| 3801 | |
| 3802 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3803 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3804 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3805 | app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3806 | mProcessNames.remove(app.processName, app.uid); |
| 3807 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3808 | if (mHeavyWeightProcess == app) { |
| 3809 | mHeavyWeightProcess = null; |
| 3810 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3811 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3812 | // Take care of any launching providers waiting for this process. |
| 3813 | checkAppInLaunchingProvidersLocked(app, true); |
| 3814 | // Take care of any services that are waiting for the process. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3815 | mServices.processStartTimedOutLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3816 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 3817 | app.processName, app.setAdj, "start timeout"); |
| 3818 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3819 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3820 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3821 | try { |
| 3822 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3823 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3824 | bm.agentDisconnected(app.info.packageName); |
| 3825 | } catch (RemoteException e) { |
| 3826 | // Can't happen; the backup manager is local |
| 3827 | } |
| 3828 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3829 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3830 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3831 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3832 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3833 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3834 | 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] | 3835 | } |
| 3836 | } |
| 3837 | |
| 3838 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3839 | int pid) { |
| 3840 | |
| 3841 | // Find the application record that is being attached... either via |
| 3842 | // the pid if we are running in multiple processes, or just pull the |
| 3843 | // next app record if we are emulating process with anonymous threads. |
| 3844 | ProcessRecord app; |
| 3845 | if (pid != MY_PID && pid >= 0) { |
| 3846 | synchronized (mPidsSelfLocked) { |
| 3847 | app = mPidsSelfLocked.get(pid); |
| 3848 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3849 | } else { |
| 3850 | app = null; |
| 3851 | } |
| 3852 | |
| 3853 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3854 | 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] | 3855 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3856 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3857 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3858 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3859 | } else { |
| 3860 | try { |
| 3861 | thread.scheduleExit(); |
| 3862 | } catch (Exception e) { |
| 3863 | // Ignore exceptions. |
| 3864 | } |
| 3865 | } |
| 3866 | return false; |
| 3867 | } |
| 3868 | |
| 3869 | // If this application record is still attached to a previous |
| 3870 | // process, clean it up now. |
| 3871 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3872 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | // Tell the process all about itself. |
| 3876 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3877 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3878 | TAG, "Binding process pid " + pid + " to record " + app); |
| 3879 | |
| 3880 | String processName = app.processName; |
| 3881 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3882 | AppDeathRecipient adr = new AppDeathRecipient( |
| 3883 | app, pid, thread); |
| 3884 | thread.asBinder().linkToDeath(adr, 0); |
| 3885 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3886 | } catch (RemoteException e) { |
| 3887 | app.resetPackageList(); |
| 3888 | startProcessLocked(app, "link fail", processName); |
| 3889 | return false; |
| 3890 | } |
| 3891 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3892 | EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3893 | |
| 3894 | app.thread = thread; |
| 3895 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 3896 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 3897 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3898 | app.forcingToForeground = null; |
| 3899 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 3900 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3901 | app.debugging = false; |
| 3902 | |
| 3903 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3904 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3905 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3906 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3907 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3908 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3909 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3910 | } |
| 3911 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3912 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3913 | TAG, "New app record " + app |
| 3914 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 3915 | try { |
| 3916 | int testMode = IApplicationThread.DEBUG_OFF; |
| 3917 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 3918 | testMode = mWaitForDebugger |
| 3919 | ? IApplicationThread.DEBUG_WAIT |
| 3920 | : IApplicationThread.DEBUG_ON; |
| 3921 | app.debugging = true; |
| 3922 | if (mDebugTransient) { |
| 3923 | mDebugApp = mOrigDebugApp; |
| 3924 | mWaitForDebugger = mOrigWaitForDebugger; |
| 3925 | } |
| 3926 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3927 | String profileFile = app.instrumentationProfileFile; |
| 3928 | ParcelFileDescriptor profileFd = null; |
| 3929 | boolean profileAutoStop = false; |
| 3930 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 3931 | mProfileProc = app; |
| 3932 | profileFile = mProfileFile; |
| 3933 | profileFd = mProfileFd; |
| 3934 | profileAutoStop = mAutoStopProfiler; |
| 3935 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 3936 | boolean enableOpenGlTrace = false; |
| 3937 | if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) { |
| 3938 | enableOpenGlTrace = true; |
| 3939 | mOpenGlTraceApp = null; |
| 3940 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3941 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3942 | // If the app is being launched for restore or full backup, set it up specially |
| 3943 | boolean isRestrictedBackupMode = false; |
| 3944 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 3945 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3946 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3947 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 3948 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3949 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 3950 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 3951 | ? app.instrumentationInfo.packageName |
| 3952 | : app.info.packageName); |
| 3953 | if (app.instrumentationClass != null) { |
| 3954 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3955 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3956 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 3957 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3958 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 3959 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 3960 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3961 | if (profileFd != null) { |
| 3962 | profileFd = profileFd.dup(); |
| 3963 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3964 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3965 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 3966 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
| 3967 | enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 3968 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 3969 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3970 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3971 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3972 | } catch (Exception e) { |
| 3973 | // todo: Yikes! What should we do? For now we will try to |
| 3974 | // start another process, but that could easily get us in |
| 3975 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3976 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3977 | |
| 3978 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3979 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3980 | startProcessLocked(app, "bind fail", processName); |
| 3981 | return false; |
| 3982 | } |
| 3983 | |
| 3984 | // Remove this record from the list of starting applications. |
| 3985 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3986 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 3987 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3988 | mProcessesOnHold.remove(app); |
| 3989 | |
| 3990 | boolean badApp = false; |
| 3991 | boolean didSomething = false; |
| 3992 | |
| 3993 | // 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] | 3994 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 3995 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3996 | 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] | 3997 | && processName.equals(hr.processName)) { |
| 3998 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 3999 | if (mHeadless) { |
| 4000 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 4001 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4002 | didSomething = true; |
| 4003 | } |
| 4004 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4005 | 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] | 4006 | + hr.intent.getComponent().flattenToShortString(), e); |
| 4007 | badApp = true; |
| 4008 | } |
| 4009 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4010 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | // Find any services that should be running in this process... |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4015 | if (!badApp) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4016 | try { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4017 | didSomething |= mServices.attachApplicationLocked(app, processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4018 | } catch (Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4019 | badApp = true; |
| 4020 | } |
| 4021 | } |
| 4022 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4023 | // Check if a next-broadcast receiver is in this process... |
| 4024 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4025 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4026 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4027 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4028 | // 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] | 4029 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4033 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4034 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4035 | 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] | 4036 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4037 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4038 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 4039 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 4040 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4041 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4042 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4043 | e.printStackTrace(); |
| 4044 | } |
| 4045 | } |
| 4046 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4047 | if (badApp) { |
| 4048 | // todo: Also need to kill application to deal with all |
| 4049 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4050 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4051 | return false; |
| 4052 | } |
| 4053 | |
| 4054 | if (!didSomething) { |
| 4055 | updateOomAdjLocked(); |
| 4056 | } |
| 4057 | |
| 4058 | return true; |
| 4059 | } |
| 4060 | |
| 4061 | public final void attachApplication(IApplicationThread thread) { |
| 4062 | synchronized (this) { |
| 4063 | int callingPid = Binder.getCallingPid(); |
| 4064 | final long origId = Binder.clearCallingIdentity(); |
| 4065 | attachApplicationLocked(thread, callingPid); |
| 4066 | Binder.restoreCallingIdentity(origId); |
| 4067 | } |
| 4068 | } |
| 4069 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4070 | 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] | 4071 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4072 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4073 | if (stopProfiling) { |
| 4074 | synchronized (this) { |
| 4075 | if (mProfileProc == r.app) { |
| 4076 | if (mProfileFd != null) { |
| 4077 | try { |
| 4078 | mProfileFd.close(); |
| 4079 | } catch (IOException e) { |
| 4080 | } |
| 4081 | clearProfilerLocked(); |
| 4082 | } |
| 4083 | } |
| 4084 | } |
| 4085 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4086 | Binder.restoreCallingIdentity(origId); |
| 4087 | } |
| 4088 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4089 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4090 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4091 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4092 | mWindowManager.enableScreenAfterBoot(); |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 4093 | |
| 4094 | synchronized (this) { |
| 4095 | updateEventDispatchingLocked(); |
| 4096 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4097 | } |
| 4098 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4099 | public void showBootMessage(final CharSequence msg, final boolean always) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4100 | enforceNotIsolatedCaller("showBootMessage"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4101 | mWindowManager.showBootMessage(msg, always); |
| 4102 | } |
| 4103 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4104 | public void dismissKeyguardOnNextActivity() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4105 | enforceNotIsolatedCaller("dismissKeyguardOnNextActivity"); |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4106 | final long token = Binder.clearCallingIdentity(); |
| 4107 | try { |
| 4108 | synchronized (this) { |
| 4109 | if (mLockScreenShown) { |
| 4110 | mLockScreenShown = false; |
| 4111 | comeOutOfSleepIfNeededLocked(); |
| 4112 | } |
| 4113 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
Dianne Hackborn | 1e88e98 | 2012-04-24 18:35:55 -0700 | [diff] [blame] | 4114 | } |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4115 | } finally { |
| 4116 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4120 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4121 | IntentFilter pkgFilter = new IntentFilter(); |
| 4122 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4123 | pkgFilter.addDataScheme("package"); |
| 4124 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4125 | @Override |
| 4126 | public void onReceive(Context context, Intent intent) { |
| 4127 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4128 | if (pkgs != null) { |
| 4129 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4130 | synchronized (ActivityManagerService.this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4131 | if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) { |
| 4132 | setResultCode(Activity.RESULT_OK); |
| 4133 | return; |
| 4134 | } |
| 4135 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4136 | } |
| 4137 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4138 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4139 | }, pkgFilter); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4140 | |
| 4141 | IntentFilter userFilter = new IntentFilter(); |
| 4142 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 4143 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4144 | @Override |
| 4145 | public void onReceive(Context context, Intent intent) { |
| 4146 | onUserRemoved(intent); |
| 4147 | } |
| 4148 | }, userFilter); |
| 4149 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4150 | synchronized (this) { |
| 4151 | // Ensure that any processes we had put on hold are now started |
| 4152 | // up. |
| 4153 | final int NP = mProcessesOnHold.size(); |
| 4154 | if (NP > 0) { |
| 4155 | ArrayList<ProcessRecord> procs = |
| 4156 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4157 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4158 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4159 | + procs.get(ip)); |
| 4160 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4161 | } |
| 4162 | } |
| 4163 | |
| 4164 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4165 | // Start looking for apps that are abusing wake locks. |
| 4166 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4167 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4168 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4169 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4170 | SystemProperties.set("dev.bootcomplete", "1"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4171 | /* TODO: Send this to all users that are to be logged in on startup */ |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4172 | broadcastIntentLocked(null, null, |
| 4173 | new Intent(Intent.ACTION_BOOT_COMPLETED, null), |
| 4174 | null, null, 0, null, null, |
| 4175 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4176 | false, false, MY_PID, Process.SYSTEM_UID, Binder.getOrigCallingUser()); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4177 | } |
| 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | final void ensureBootCompleted() { |
| 4182 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4183 | boolean enableScreen; |
| 4184 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4185 | booting = mBooting; |
| 4186 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4187 | enableScreen = !mBooted; |
| 4188 | mBooted = true; |
| 4189 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4190 | |
| 4191 | if (booting) { |
| 4192 | finishBooting(); |
| 4193 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4194 | |
| 4195 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4196 | enableScreenAfterBoot(); |
| 4197 | } |
| 4198 | } |
| 4199 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4200 | public final void activityPaused(IBinder token) { |
| 4201 | final long origId = Binder.clearCallingIdentity(); |
| 4202 | mMainStack.activityPaused(token, false); |
| 4203 | Binder.restoreCallingIdentity(origId); |
| 4204 | } |
| 4205 | |
| 4206 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4207 | CharSequence description) { |
| 4208 | if (localLOGV) Slog.v( |
| 4209 | TAG, "Activity stopped: token=" + token); |
| 4210 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4211 | // Refuse possible leaked file descriptors |
| 4212 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4213 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4214 | } |
| 4215 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4216 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4217 | |
| 4218 | final long origId = Binder.clearCallingIdentity(); |
| 4219 | |
| 4220 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4221 | r = mMainStack.isInStackLocked(token); |
| 4222 | if (r != null) { |
| 4223 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | if (r != null) { |
| 4228 | sendPendingThumbnail(r, null, null, null, false); |
| 4229 | } |
| 4230 | |
| 4231 | trimApplications(); |
| 4232 | |
| 4233 | Binder.restoreCallingIdentity(origId); |
| 4234 | } |
| 4235 | |
| 4236 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4237 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4238 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | public String getCallingPackage(IBinder token) { |
| 4242 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4243 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4244 | 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] | 4245 | } |
| 4246 | } |
| 4247 | |
| 4248 | public ComponentName getCallingActivity(IBinder token) { |
| 4249 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4250 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4251 | return r != null ? r.intent.getComponent() : null; |
| 4252 | } |
| 4253 | } |
| 4254 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4255 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4256 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4257 | if (r == null) { |
| 4258 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4259 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4260 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4264 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4265 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4266 | if (r == null) { |
| 4267 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4268 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4269 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4270 | } |
| 4271 | } |
| 4272 | |
| 4273 | public String getPackageForToken(IBinder token) { |
| 4274 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4275 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4276 | if (r == null) { |
| 4277 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4278 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4279 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4280 | } |
| 4281 | } |
| 4282 | |
| 4283 | public IIntentSender getIntentSender(int type, |
| 4284 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4285 | int requestCode, Intent[] intents, String[] resolvedTypes, |
| 4286 | int flags, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4287 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4288 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4289 | if (intents != null) { |
| 4290 | if (intents.length < 1) { |
| 4291 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4292 | } |
| 4293 | for (int i=0; i<intents.length; i++) { |
| 4294 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4295 | if (intent != null) { |
| 4296 | if (intent.hasFileDescriptors()) { |
| 4297 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4298 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4299 | if (type == ActivityManager.INTENT_SENDER_BROADCAST && |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4300 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4301 | throw new IllegalArgumentException( |
| 4302 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4303 | } |
| 4304 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4305 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4306 | } |
| 4307 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4308 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4309 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4310 | } |
| 4311 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4312 | if (options != null) { |
| 4313 | if (options.hasFileDescriptors()) { |
| 4314 | throw new IllegalArgumentException("File descriptors passed in options"); |
| 4315 | } |
| 4316 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4318 | synchronized(this) { |
| 4319 | int callingUid = Binder.getCallingUid(); |
| 4320 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4321 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4322 | int uid = AppGlobals.getPackageManager() |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4323 | .getPackageUid(packageName, UserId.getUserId(callingUid)); |
| 4324 | if (!UserId.isSameApp(callingUid, uid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4325 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4326 | + Binder.getCallingPid() |
| 4327 | + ", uid=" + Binder.getCallingUid() |
| 4328 | + ", (need uid=" + uid + ")" |
| 4329 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4330 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4331 | throw new SecurityException(msg); |
| 4332 | } |
| 4333 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4334 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4335 | if (DEBUG_MU) |
| 4336 | Slog.i(TAG_MU, "Getting intent sender for origCallingUid=" |
| 4337 | + Binder.getOrigCallingUid()); |
| 4338 | return getIntentSenderLocked(type, packageName, Binder.getOrigCallingUid(), |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4339 | token, resultWho, requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4341 | } catch (RemoteException e) { |
| 4342 | throw new SecurityException(e); |
| 4343 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | IIntentSender getIntentSenderLocked(int type, |
| 4348 | String packageName, int callingUid, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4349 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags, |
| 4350 | Bundle options) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4351 | if (DEBUG_MU) |
| 4352 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4353 | ActivityRecord activity = null; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4354 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4355 | activity = mMainStack.isInStackLocked(token); |
| 4356 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4357 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4358 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4359 | if (activity.finishing) { |
| 4360 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4361 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4365 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4366 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4367 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4368 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4369 | |
| 4370 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4371 | type, packageName, activity, resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4372 | requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4373 | WeakReference<PendingIntentRecord> ref; |
| 4374 | ref = mIntentSenderRecords.get(key); |
| 4375 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4376 | if (rec != null) { |
| 4377 | if (!cancelCurrent) { |
| 4378 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4379 | if (rec.key.requestIntent != null) { |
Adam Powell | 501d4a5 | 2012-04-30 15:03:57 -0700 | [diff] [blame] | 4380 | rec.key.requestIntent.replaceExtras(intents != null ? |
| 4381 | intents[intents.length - 1] : null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4382 | } |
| 4383 | if (intents != null) { |
| 4384 | intents[intents.length-1] = rec.key.requestIntent; |
| 4385 | rec.key.allIntents = intents; |
| 4386 | rec.key.allResolvedTypes = resolvedTypes; |
| 4387 | } else { |
| 4388 | rec.key.allIntents = null; |
| 4389 | rec.key.allResolvedTypes = null; |
| 4390 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4392 | return rec; |
| 4393 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4394 | rec.canceled = true; |
| 4395 | mIntentSenderRecords.remove(key); |
| 4396 | } |
| 4397 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4398 | return rec; |
| 4399 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4400 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4401 | mIntentSenderRecords.put(key, rec.ref); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4402 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4403 | if (activity.pendingResults == null) { |
| 4404 | activity.pendingResults |
| 4405 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4406 | } |
| 4407 | activity.pendingResults.add(rec.ref); |
| 4408 | } |
| 4409 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | public void cancelIntentSender(IIntentSender sender) { |
| 4413 | if (!(sender instanceof PendingIntentRecord)) { |
| 4414 | return; |
| 4415 | } |
| 4416 | synchronized(this) { |
| 4417 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4418 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4419 | int uid = AppGlobals.getPackageManager() |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4420 | .getPackageUid(rec.key.packageName, UserId.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4421 | if (!UserId.isSameApp(uid, Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4422 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4423 | + Binder.getCallingPid() |
| 4424 | + ", uid=" + Binder.getCallingUid() |
| 4425 | + " is not allowed to cancel packges " |
| 4426 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4427 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4428 | throw new SecurityException(msg); |
| 4429 | } |
| 4430 | } catch (RemoteException e) { |
| 4431 | throw new SecurityException(e); |
| 4432 | } |
| 4433 | cancelIntentSenderLocked(rec, true); |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4438 | rec.canceled = true; |
| 4439 | mIntentSenderRecords.remove(rec.key); |
| 4440 | if (cleanActivity && rec.key.activity != null) { |
| 4441 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4442 | } |
| 4443 | } |
| 4444 | |
| 4445 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4446 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4447 | return null; |
| 4448 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4449 | try { |
| 4450 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4451 | return res.key.packageName; |
| 4452 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4453 | } |
| 4454 | return null; |
| 4455 | } |
| 4456 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4457 | public int getUidForIntentSender(IIntentSender sender) { |
| 4458 | if (sender instanceof PendingIntentRecord) { |
| 4459 | try { |
| 4460 | PendingIntentRecord res = (PendingIntentRecord)sender; |
| 4461 | return res.uid; |
| 4462 | } catch (ClassCastException e) { |
| 4463 | } |
| 4464 | } |
| 4465 | return -1; |
| 4466 | } |
| 4467 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4468 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4469 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4470 | return false; |
| 4471 | } |
| 4472 | try { |
| 4473 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4474 | if (res.key.allIntents == null) { |
| 4475 | return false; |
| 4476 | } |
| 4477 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4478 | Intent intent = res.key.allIntents[i]; |
| 4479 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4480 | return false; |
| 4481 | } |
| 4482 | } |
| 4483 | return true; |
| 4484 | } catch (ClassCastException e) { |
| 4485 | } |
| 4486 | return false; |
| 4487 | } |
| 4488 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 4489 | public boolean isIntentSenderAnActivity(IIntentSender pendingResult) { |
| 4490 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4491 | return false; |
| 4492 | } |
| 4493 | try { |
| 4494 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4495 | if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) { |
| 4496 | return true; |
| 4497 | } |
| 4498 | return false; |
| 4499 | } catch (ClassCastException e) { |
| 4500 | } |
| 4501 | return false; |
| 4502 | } |
| 4503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4504 | public void setProcessLimit(int max) { |
| 4505 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4506 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4507 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4508 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4509 | mProcessLimitOverride = max; |
| 4510 | } |
| 4511 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4515 | synchronized (this) { |
| 4516 | return mProcessLimitOverride; |
| 4517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | void foregroundTokenDied(ForegroundToken token) { |
| 4521 | synchronized (ActivityManagerService.this) { |
| 4522 | synchronized (mPidsSelfLocked) { |
| 4523 | ForegroundToken cur |
| 4524 | = mForegroundProcesses.get(token.pid); |
| 4525 | if (cur != token) { |
| 4526 | return; |
| 4527 | } |
| 4528 | mForegroundProcesses.remove(token.pid); |
| 4529 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4530 | if (pr == null) { |
| 4531 | return; |
| 4532 | } |
| 4533 | pr.forcingToForeground = null; |
| 4534 | pr.foregroundServices = false; |
| 4535 | } |
| 4536 | updateOomAdjLocked(); |
| 4537 | } |
| 4538 | } |
| 4539 | |
| 4540 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4541 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4542 | "setProcessForeground()"); |
| 4543 | synchronized(this) { |
| 4544 | boolean changed = false; |
| 4545 | |
| 4546 | synchronized (mPidsSelfLocked) { |
| 4547 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4548 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4549 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4550 | return; |
| 4551 | } |
| 4552 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4553 | if (oldToken != null) { |
| 4554 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4555 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4556 | if (pr != null) { |
| 4557 | pr.forcingToForeground = null; |
| 4558 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4559 | changed = true; |
| 4560 | } |
| 4561 | if (isForeground && token != null) { |
| 4562 | ForegroundToken newToken = new ForegroundToken() { |
| 4563 | public void binderDied() { |
| 4564 | foregroundTokenDied(this); |
| 4565 | } |
| 4566 | }; |
| 4567 | newToken.pid = pid; |
| 4568 | newToken.token = token; |
| 4569 | try { |
| 4570 | token.linkToDeath(newToken, 0); |
| 4571 | mForegroundProcesses.put(pid, newToken); |
| 4572 | pr.forcingToForeground = token; |
| 4573 | changed = true; |
| 4574 | } catch (RemoteException e) { |
| 4575 | // If the process died while doing this, we will later |
| 4576 | // do the cleanup with the process death link. |
| 4577 | } |
| 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | if (changed) { |
| 4582 | updateOomAdjLocked(); |
| 4583 | } |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | // ========================================================= |
| 4588 | // PERMISSIONS |
| 4589 | // ========================================================= |
| 4590 | |
| 4591 | static class PermissionController extends IPermissionController.Stub { |
| 4592 | ActivityManagerService mActivityManagerService; |
| 4593 | PermissionController(ActivityManagerService activityManagerService) { |
| 4594 | mActivityManagerService = activityManagerService; |
| 4595 | } |
| 4596 | |
| 4597 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4598 | return mActivityManagerService.checkPermission(permission, pid, |
| 4599 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4600 | } |
| 4601 | } |
| 4602 | |
| 4603 | /** |
| 4604 | * This can be called with or without the global lock held. |
| 4605 | */ |
| 4606 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4607 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4608 | // We might be performing an operation on behalf of an indirect binder |
| 4609 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4610 | // client identity accordingly before proceeding. |
| 4611 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4612 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4613 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4614 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4615 | uid = tlsIdentity.uid; |
| 4616 | pid = tlsIdentity.pid; |
| 4617 | } |
| 4618 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4619 | if (pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4620 | return PackageManager.PERMISSION_GRANTED; |
| 4621 | } |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4622 | |
| 4623 | return ActivityManager.checkComponentPermission(permission, uid, |
| 4624 | owningUid, exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | /** |
| 4628 | * As the only public entry point for permissions checking, this method |
| 4629 | * can enforce the semantic that requesting a check on a null global |
| 4630 | * permission is automatically denied. (Internally a null permission |
| 4631 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4632 | * when only uid-based security is needed.) |
| 4633 | * |
| 4634 | * This can be called with or without the global lock held. |
| 4635 | */ |
| 4636 | public int checkPermission(String permission, int pid, int uid) { |
| 4637 | if (permission == null) { |
| 4638 | return PackageManager.PERMISSION_DENIED; |
| 4639 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4640 | return checkComponentPermission(permission, pid, UserId.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | /** |
| 4644 | * Binder IPC calls go through the public entry point. |
| 4645 | * This can be called with or without the global lock held. |
| 4646 | */ |
| 4647 | int checkCallingPermission(String permission) { |
| 4648 | return checkPermission(permission, |
| 4649 | Binder.getCallingPid(), |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4650 | UserId.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | /** |
| 4654 | * This can be called with or without the global lock held. |
| 4655 | */ |
| 4656 | void enforceCallingPermission(String permission, String func) { |
| 4657 | if (checkCallingPermission(permission) |
| 4658 | == PackageManager.PERMISSION_GRANTED) { |
| 4659 | return; |
| 4660 | } |
| 4661 | |
| 4662 | String msg = "Permission Denial: " + func + " from pid=" |
| 4663 | + Binder.getCallingPid() |
| 4664 | + ", uid=" + Binder.getCallingUid() |
| 4665 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4666 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4667 | throw new SecurityException(msg); |
| 4668 | } |
| 4669 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4670 | /** |
| 4671 | * Determine if UID is holding permissions required to access {@link Uri} in |
| 4672 | * the given {@link ProviderInfo}. Final permission checking is always done |
| 4673 | * in {@link ContentProvider}. |
| 4674 | */ |
| 4675 | private final boolean checkHoldingPermissionsLocked( |
| 4676 | IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4677 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4678 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4679 | |
| 4680 | if (pi.applicationInfo.uid == uid) { |
| 4681 | return true; |
| 4682 | } else if (!pi.exported) { |
| 4683 | return false; |
| 4684 | } |
| 4685 | |
| 4686 | boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4687 | 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] | 4688 | try { |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4689 | // check if target holds top-level <provider> permissions |
| 4690 | if (!readMet && pi.readPermission != null |
| 4691 | && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) { |
| 4692 | readMet = true; |
| 4693 | } |
| 4694 | if (!writeMet && pi.writePermission != null |
| 4695 | && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) { |
| 4696 | writeMet = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4697 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4698 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4699 | // track if unprotected read/write is allowed; any denied |
| 4700 | // <path-permission> below removes this ability |
| 4701 | boolean allowDefaultRead = pi.readPermission == null; |
| 4702 | boolean allowDefaultWrite = pi.writePermission == null; |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4703 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4704 | // check if target holds any <path-permission> that match uri |
| 4705 | final PathPermission[] pps = pi.pathPermissions; |
| 4706 | if (pps != null) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4707 | final String path = uri.getPath(); |
| 4708 | int i = pps.length; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4709 | while (i > 0 && (!readMet || !writeMet)) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4710 | i--; |
| 4711 | PathPermission pp = pps[i]; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4712 | if (pp.match(path)) { |
| 4713 | if (!readMet) { |
| 4714 | final String pprperm = pp.getReadPermission(); |
| 4715 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4716 | + pprperm + " for " + pp.getPath() |
| 4717 | + ": match=" + pp.match(path) |
| 4718 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4719 | if (pprperm != null) { |
| 4720 | if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) { |
| 4721 | readMet = true; |
| 4722 | } else { |
| 4723 | allowDefaultRead = false; |
| 4724 | } |
| 4725 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4726 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4727 | if (!writeMet) { |
| 4728 | final String ppwperm = pp.getWritePermission(); |
| 4729 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4730 | + ppwperm + " for " + pp.getPath() |
| 4731 | + ": match=" + pp.match(path) |
| 4732 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4733 | if (ppwperm != null) { |
| 4734 | if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) { |
| 4735 | writeMet = true; |
| 4736 | } else { |
| 4737 | allowDefaultWrite = false; |
| 4738 | } |
| 4739 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4740 | } |
| 4741 | } |
| 4742 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4743 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4744 | |
| 4745 | // grant unprotected <provider> read/write, if not blocked by |
| 4746 | // <path-permission> above |
| 4747 | if (allowDefaultRead) readMet = true; |
| 4748 | if (allowDefaultWrite) writeMet = true; |
| 4749 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4750 | } catch (RemoteException e) { |
| 4751 | return false; |
| 4752 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4753 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4754 | return readMet && writeMet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4758 | int modeFlags) { |
| 4759 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4760 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4761 | return true; |
| 4762 | } |
| 4763 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4764 | if (perms == null) return false; |
| 4765 | UriPermission perm = perms.get(uri); |
| 4766 | if (perm == null) return false; |
| 4767 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4768 | } |
| 4769 | |
| 4770 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4771 | enforceNotIsolatedCaller("checkUriPermission"); |
| 4772 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4773 | // Another redirected-binder-call permissions check as in |
| 4774 | // {@link checkComponentPermission}. |
| 4775 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4776 | if (tlsIdentity != null) { |
| 4777 | uid = tlsIdentity.uid; |
| 4778 | pid = tlsIdentity.pid; |
| 4779 | } |
| 4780 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4781 | uid = UserId.getAppId(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4782 | // Our own process gets to do everything. |
| 4783 | if (pid == MY_PID) { |
| 4784 | return PackageManager.PERMISSION_GRANTED; |
| 4785 | } |
| 4786 | synchronized(this) { |
| 4787 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4788 | ? PackageManager.PERMISSION_GRANTED |
| 4789 | : PackageManager.PERMISSION_DENIED; |
| 4790 | } |
| 4791 | } |
| 4792 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4793 | /** |
| 4794 | * Check if the targetPkg can be granted permission to access uri by |
| 4795 | * the callingUid using the given modeFlags. Throws a security exception |
| 4796 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4797 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4798 | * needed (for example targetPkg already has permission to access the URI). |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4799 | * If you already know the uid of the target, you can supply it in |
| 4800 | * lastTargetUid else set that to -1. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4801 | */ |
| 4802 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4803 | Uri uri, int modeFlags, int lastTargetUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4804 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4805 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4806 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4807 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4808 | } |
| 4809 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4810 | if (targetPkg != null) { |
| 4811 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4812 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4813 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4814 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4815 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4816 | |
| 4817 | // If this is not a content: uri, we can't do anything with it. |
| 4818 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4819 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4820 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4821 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | String name = uri.getAuthority(); |
| 4825 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4826 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
| 4827 | UserId.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4828 | if (cpr != null) { |
| 4829 | pi = cpr.info; |
| 4830 | } else { |
| 4831 | try { |
| 4832 | pi = pm.resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4833 | PackageManager.GET_URI_PERMISSION_PATTERNS, UserId.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4834 | } catch (RemoteException ex) { |
| 4835 | } |
| 4836 | } |
| 4837 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4838 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4839 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4840 | } |
| 4841 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4842 | int targetUid = lastTargetUid; |
| 4843 | if (targetUid < 0 && targetPkg != null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4844 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4845 | targetUid = pm.getPackageUid(targetPkg, UserId.getUserId(callingUid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4846 | if (targetUid < 0) { |
| 4847 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4848 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4849 | return -1; |
| 4850 | } |
| 4851 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4852 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4853 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4854 | } |
| 4855 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4856 | if (targetUid >= 0) { |
| 4857 | // First... does the target actually need this permission? |
| 4858 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4859 | // No need to grant the target this permission. |
| 4860 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4861 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4862 | return -1; |
| 4863 | } |
| 4864 | } else { |
| 4865 | // First... there is no target package, so can anyone access it? |
| 4866 | boolean allowed = pi.exported; |
| 4867 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4868 | if (pi.readPermission != null) { |
| 4869 | allowed = false; |
| 4870 | } |
| 4871 | } |
| 4872 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4873 | if (pi.writePermission != null) { |
| 4874 | allowed = false; |
| 4875 | } |
| 4876 | } |
| 4877 | if (allowed) { |
| 4878 | return -1; |
| 4879 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4880 | } |
| 4881 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4882 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4883 | if (!pi.grantUriPermissions) { |
| 4884 | throw new SecurityException("Provider " + pi.packageName |
| 4885 | + "/" + pi.name |
| 4886 | + " does not allow granting of Uri permissions (uri " |
| 4887 | + uri + ")"); |
| 4888 | } |
| 4889 | if (pi.uriPermissionPatterns != null) { |
| 4890 | final int N = pi.uriPermissionPatterns.length; |
| 4891 | boolean allowed = false; |
| 4892 | for (int i=0; i<N; i++) { |
| 4893 | if (pi.uriPermissionPatterns[i] != null |
| 4894 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 4895 | allowed = true; |
| 4896 | break; |
| 4897 | } |
| 4898 | } |
| 4899 | if (!allowed) { |
| 4900 | throw new SecurityException("Provider " + pi.packageName |
| 4901 | + "/" + pi.name |
| 4902 | + " does not allow granting of permission to path of Uri " |
| 4903 | + uri); |
| 4904 | } |
| 4905 | } |
| 4906 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4907 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4908 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4909 | if (callingUid != Process.myUid()) { |
| 4910 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 4911 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4912 | throw new SecurityException("Uid " + callingUid |
| 4913 | + " does not have permission to uri " + uri); |
| 4914 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4915 | } |
| 4916 | } |
| 4917 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4918 | return targetUid; |
| 4919 | } |
| 4920 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4921 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 4922 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4923 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4924 | synchronized(this) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4925 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4926 | } |
| 4927 | } |
| 4928 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4929 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 4930 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 4931 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4932 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4933 | if (modeFlags == 0) { |
| 4934 | return; |
| 4935 | } |
| 4936 | |
| 4937 | // 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] | 4938 | // to the uri, and the target doesn't. Let's now give this to |
| 4939 | // the target. |
| 4940 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4941 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4942 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4943 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4944 | HashMap<Uri, UriPermission> targetUris |
| 4945 | = mGrantedUriPermissions.get(targetUid); |
| 4946 | if (targetUris == null) { |
| 4947 | targetUris = new HashMap<Uri, UriPermission>(); |
| 4948 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 4949 | } |
| 4950 | |
| 4951 | UriPermission perm = targetUris.get(uri); |
| 4952 | if (perm == null) { |
| 4953 | perm = new UriPermission(targetUid, uri); |
| 4954 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4955 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4956 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4957 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4958 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4959 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 4960 | } else { |
| 4961 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4962 | perm.readOwners.add(owner); |
| 4963 | owner.addReadPermission(perm); |
| 4964 | } |
| 4965 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4966 | perm.writeOwners.add(owner); |
| 4967 | owner.addWritePermission(perm); |
| 4968 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4969 | } |
| 4970 | } |
| 4971 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4972 | void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri, |
| 4973 | int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4974 | if (targetPkg == null) { |
| 4975 | throw new NullPointerException("targetPkg"); |
| 4976 | } |
| 4977 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4978 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4979 | if (targetUid < 0) { |
| 4980 | return; |
| 4981 | } |
| 4982 | |
| 4983 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 4984 | } |
| 4985 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4986 | static class NeededUriGrants extends ArrayList<Uri> { |
| 4987 | final String targetPkg; |
| 4988 | final int targetUid; |
| 4989 | final int flags; |
| 4990 | |
| 4991 | NeededUriGrants(String _targetPkg, int _targetUid, int _flags) { |
| 4992 | targetPkg = _targetPkg; |
| 4993 | targetUid = _targetUid; |
| 4994 | flags = _flags; |
| 4995 | } |
| 4996 | } |
| 4997 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4998 | /** |
| 4999 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 5000 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5001 | NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 5002 | String targetPkg, Intent intent, int mode, NeededUriGrants needed) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5003 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5004 | "Checking URI perm to data=" + (intent != null ? intent.getData() : null) |
| 5005 | + " clip=" + (intent != null ? intent.getClipData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5006 | + " from " + intent + "; flags=0x" |
| 5007 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 5008 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5009 | if (targetPkg == null) { |
| 5010 | throw new NullPointerException("targetPkg"); |
| 5011 | } |
| 5012 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5013 | if (intent == null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5014 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5015 | } |
| 5016 | Uri data = intent.getData(); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5017 | ClipData clip = intent.getClipData(); |
| 5018 | if (data == null && clip == null) { |
| 5019 | return null; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5020 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5021 | if (data != null) { |
| 5022 | int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 5023 | mode, needed != null ? needed.targetUid : -1); |
| 5024 | if (target > 0) { |
| 5025 | if (needed == null) { |
| 5026 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5027 | } |
| 5028 | needed.add(data); |
| 5029 | } |
| 5030 | } |
| 5031 | if (clip != null) { |
| 5032 | for (int i=0; i<clip.getItemCount(); i++) { |
| 5033 | Uri uri = clip.getItemAt(i).getUri(); |
| 5034 | if (uri != null) { |
| 5035 | int target = -1; |
| 5036 | target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, |
| 5037 | mode, needed != null ? needed.targetUid : -1); |
| 5038 | if (target > 0) { |
| 5039 | if (needed == null) { |
| 5040 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5041 | } |
| 5042 | needed.add(uri); |
| 5043 | } |
| 5044 | } else { |
| 5045 | Intent clipIntent = clip.getItemAt(i).getIntent(); |
| 5046 | if (clipIntent != null) { |
| 5047 | NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked( |
| 5048 | callingUid, targetPkg, clipIntent, mode, needed); |
| 5049 | if (newNeeded != null) { |
| 5050 | needed = newNeeded; |
| 5051 | } |
| 5052 | } |
| 5053 | } |
| 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | return needed; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | /** |
| 5061 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 5062 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5063 | void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed, |
| 5064 | UriPermissionOwner owner) { |
| 5065 | if (needed != null) { |
| 5066 | for (int i=0; i<needed.size(); i++) { |
| 5067 | grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg, |
| 5068 | needed.get(i), needed.flags, owner); |
| 5069 | } |
| 5070 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5071 | } |
| 5072 | |
| 5073 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 5074 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5075 | NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, |
Dianne Hackborn | d9781fe | 2012-03-08 18:04:18 -0800 | [diff] [blame] | 5076 | intent, intent != null ? intent.getFlags() : 0, null); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5077 | if (needed == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5078 | return; |
| 5079 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5080 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5081 | grantUriPermissionUncheckedFromIntentLocked(needed, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 5085 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5086 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5087 | synchronized(this) { |
| 5088 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5089 | if (r == null) { |
| 5090 | throw new SecurityException("Unable to find app for caller " |
| 5091 | + caller |
| 5092 | + " when granting permission to uri " + uri); |
| 5093 | } |
| 5094 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5095 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5096 | } |
| 5097 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5098 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5099 | } |
| 5100 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5101 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5102 | null); |
| 5103 | } |
| 5104 | } |
| 5105 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5106 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5107 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5108 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 5109 | HashMap<Uri, UriPermission> perms |
| 5110 | = mGrantedUriPermissions.get(perm.uid); |
| 5111 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5112 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5113 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5114 | perms.remove(perm.uri); |
| 5115 | if (perms.size() == 0) { |
| 5116 | mGrantedUriPermissions.remove(perm.uid); |
| 5117 | } |
| 5118 | } |
| 5119 | } |
| 5120 | } |
| 5121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5122 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 5123 | int modeFlags) { |
| 5124 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5125 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5126 | if (modeFlags == 0) { |
| 5127 | return; |
| 5128 | } |
| 5129 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5130 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5131 | "Revoking all granted permissions to " + uri); |
| 5132 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5133 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5134 | |
| 5135 | final String authority = uri.getAuthority(); |
| 5136 | ProviderInfo pi = null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5137 | int userId = UserId.getUserId(callingUid); |
| 5138 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5139 | if (cpr != null) { |
| 5140 | pi = cpr.info; |
| 5141 | } else { |
| 5142 | try { |
| 5143 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5144 | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5145 | } catch (RemoteException ex) { |
| 5146 | } |
| 5147 | } |
| 5148 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5149 | 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] | 5150 | return; |
| 5151 | } |
| 5152 | |
| 5153 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5154 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5155 | // Right now, if you are not the original owner of the permission, |
| 5156 | // you are not allowed to revoke it. |
| 5157 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5158 | throw new SecurityException("Uid " + callingUid |
| 5159 | + " does not have permission to uri " + uri); |
| 5160 | //} |
| 5161 | } |
| 5162 | |
| 5163 | // Go through all of the permissions and remove any that match. |
| 5164 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 5165 | if (SEGMENTS != null) { |
| 5166 | final int NS = SEGMENTS.size(); |
| 5167 | int N = mGrantedUriPermissions.size(); |
| 5168 | for (int i=0; i<N; i++) { |
| 5169 | HashMap<Uri, UriPermission> perms |
| 5170 | = mGrantedUriPermissions.valueAt(i); |
| 5171 | Iterator<UriPermission> it = perms.values().iterator(); |
| 5172 | toploop: |
| 5173 | while (it.hasNext()) { |
| 5174 | UriPermission perm = it.next(); |
| 5175 | Uri targetUri = perm.uri; |
| 5176 | if (!authority.equals(targetUri.getAuthority())) { |
| 5177 | continue; |
| 5178 | } |
| 5179 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5180 | if (targetSegments == null) { |
| 5181 | continue; |
| 5182 | } |
| 5183 | if (targetSegments.size() < NS) { |
| 5184 | continue; |
| 5185 | } |
| 5186 | for (int j=0; j<NS; j++) { |
| 5187 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5188 | continue toploop; |
| 5189 | } |
| 5190 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5191 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5192 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5193 | perm.clearModes(modeFlags); |
| 5194 | if (perm.modeFlags == 0) { |
| 5195 | it.remove(); |
| 5196 | } |
| 5197 | } |
| 5198 | if (perms.size() == 0) { |
| 5199 | mGrantedUriPermissions.remove( |
| 5200 | mGrantedUriPermissions.keyAt(i)); |
| 5201 | N--; |
| 5202 | i--; |
| 5203 | } |
| 5204 | } |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5209 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5210 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5211 | synchronized(this) { |
| 5212 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5213 | if (r == null) { |
| 5214 | throw new SecurityException("Unable to find app for caller " |
| 5215 | + caller |
| 5216 | + " when revoking permission to uri " + uri); |
| 5217 | } |
| 5218 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5219 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5220 | return; |
| 5221 | } |
| 5222 | |
| 5223 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5224 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5225 | if (modeFlags == 0) { |
| 5226 | return; |
| 5227 | } |
| 5228 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5229 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5230 | |
| 5231 | final String authority = uri.getAuthority(); |
| 5232 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5233 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5234 | if (cpr != null) { |
| 5235 | pi = cpr.info; |
| 5236 | } else { |
| 5237 | try { |
| 5238 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5239 | PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5240 | } catch (RemoteException ex) { |
| 5241 | } |
| 5242 | } |
| 5243 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5244 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5245 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5246 | return; |
| 5247 | } |
| 5248 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5249 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5250 | } |
| 5251 | } |
| 5252 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5253 | @Override |
| 5254 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5255 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5256 | synchronized(this) { |
| 5257 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5258 | return owner.getExternalTokenLocked(); |
| 5259 | } |
| 5260 | } |
| 5261 | |
| 5262 | @Override |
| 5263 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5264 | Uri uri, int modeFlags) { |
| 5265 | synchronized(this) { |
| 5266 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5267 | if (owner == null) { |
| 5268 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5269 | } |
| 5270 | if (fromUid != Binder.getCallingUid()) { |
| 5271 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5272 | // Only system code can grant URI permissions on behalf |
| 5273 | // of other users. |
| 5274 | throw new SecurityException("nice try"); |
| 5275 | } |
| 5276 | } |
| 5277 | if (targetPkg == null) { |
| 5278 | throw new IllegalArgumentException("null target"); |
| 5279 | } |
| 5280 | if (uri == null) { |
| 5281 | throw new IllegalArgumentException("null uri"); |
| 5282 | } |
| 5283 | |
| 5284 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | @Override |
| 5289 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5290 | synchronized(this) { |
| 5291 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5292 | if (owner == null) { |
| 5293 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5294 | } |
| 5295 | |
| 5296 | if (uri == null) { |
| 5297 | owner.removeUriPermissionsLocked(mode); |
| 5298 | } else { |
| 5299 | owner.removeUriPermissionLocked(uri, mode); |
| 5300 | } |
| 5301 | } |
| 5302 | } |
| 5303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5304 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5305 | synchronized (this) { |
| 5306 | ProcessRecord app = |
| 5307 | who != null ? getRecordForAppLocked(who) : null; |
| 5308 | if (app == null) return; |
| 5309 | |
| 5310 | Message msg = Message.obtain(); |
| 5311 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5312 | msg.obj = app; |
| 5313 | msg.arg1 = waiting ? 1 : 0; |
| 5314 | mHandler.sendMessage(msg); |
| 5315 | } |
| 5316 | } |
| 5317 | |
| 5318 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5319 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5320 | 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] | 5321 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 5322 | outInfo.totalMem = Process.getTotalMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5323 | outInfo.threshold = homeAppMem; |
| 5324 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5325 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5326 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5327 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5328 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5329 | ProcessList.VISIBLE_APP_ADJ); |
| 5330 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5331 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5332 | } |
| 5333 | |
| 5334 | // ========================================================= |
| 5335 | // TASK MANAGEMENT |
| 5336 | // ========================================================= |
| 5337 | |
| 5338 | public List getTasks(int maxNum, int flags, |
| 5339 | IThumbnailReceiver receiver) { |
| 5340 | ArrayList list = new ArrayList(); |
| 5341 | |
| 5342 | PendingThumbnailsRecord pending = null; |
| 5343 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5344 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5345 | |
| 5346 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5347 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5348 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5349 | + ", receiver=" + receiver); |
| 5350 | |
| 5351 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5352 | != PackageManager.PERMISSION_GRANTED) { |
| 5353 | if (receiver != null) { |
| 5354 | // If the caller wants to wait for pending thumbnails, |
| 5355 | // it ain't gonna get them. |
| 5356 | try { |
| 5357 | receiver.finished(); |
| 5358 | } catch (RemoteException ex) { |
| 5359 | } |
| 5360 | } |
| 5361 | String msg = "Permission Denial: getTasks() from pid=" |
| 5362 | + Binder.getCallingPid() |
| 5363 | + ", uid=" + Binder.getCallingUid() |
| 5364 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5365 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5366 | throw new SecurityException(msg); |
| 5367 | } |
| 5368 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5369 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5370 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5371 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5372 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5373 | TaskRecord curTask = null; |
| 5374 | int numActivities = 0; |
| 5375 | int numRunning = 0; |
| 5376 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5377 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5378 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5379 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5380 | |
| 5381 | // Initialize state for next task if needed. |
| 5382 | if (top == null || |
| 5383 | (top.state == ActivityState.INITIALIZING |
| 5384 | && top.task == r.task)) { |
| 5385 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5386 | curTask = r.task; |
| 5387 | numActivities = numRunning = 0; |
| 5388 | } |
| 5389 | |
| 5390 | // Add 'r' into the current task. |
| 5391 | numActivities++; |
| 5392 | if (r.app != null && r.app.thread != null) { |
| 5393 | numRunning++; |
| 5394 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5395 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5396 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5397 | TAG, r.intent.getComponent().flattenToShortString() |
| 5398 | + ": task=" + r.task); |
| 5399 | |
| 5400 | // If the next one is a different task, generate a new |
| 5401 | // TaskInfo entry for what we have. |
| 5402 | if (next == null || next.task != curTask) { |
| 5403 | ActivityManager.RunningTaskInfo ci |
| 5404 | = new ActivityManager.RunningTaskInfo(); |
| 5405 | ci.id = curTask.taskId; |
| 5406 | ci.baseActivity = r.intent.getComponent(); |
| 5407 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5408 | if (top.thumbHolder != null) { |
| 5409 | ci.description = top.thumbHolder.lastDescription; |
| 5410 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5411 | ci.numActivities = numActivities; |
| 5412 | ci.numRunning = numRunning; |
| 5413 | //System.out.println( |
| 5414 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5415 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5416 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5417 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5418 | + " app=" + top.app |
| 5419 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5420 | if (top.state == ActivityState.RESUMED |
| 5421 | || top.state == ActivityState.PAUSING) { |
| 5422 | if (top.idle && top.app != null |
| 5423 | && top.app.thread != null) { |
| 5424 | topRecord = top; |
| 5425 | topThumbnail = top.app.thread; |
| 5426 | } else { |
| 5427 | top.thumbnailNeeded = true; |
| 5428 | } |
| 5429 | } |
| 5430 | if (pending == null) { |
| 5431 | pending = new PendingThumbnailsRecord(receiver); |
| 5432 | } |
| 5433 | pending.pendingRecords.add(top); |
| 5434 | } |
| 5435 | list.add(ci); |
| 5436 | maxNum--; |
| 5437 | top = null; |
| 5438 | } |
| 5439 | } |
| 5440 | |
| 5441 | if (pending != null) { |
| 5442 | mPendingThumbnails.add(pending); |
| 5443 | } |
| 5444 | } |
| 5445 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5446 | 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] | 5447 | |
| 5448 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5449 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5450 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5451 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5452 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5453 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5454 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5455 | } |
| 5456 | } |
| 5457 | |
| 5458 | if (pending == null && receiver != null) { |
| 5459 | // In this case all thumbnails were available and the client |
| 5460 | // is being asked to be told when the remaining ones come in... |
| 5461 | // which is unusually, since the top-most currently running |
| 5462 | // activity should never have a canned thumbnail! Oh well. |
| 5463 | try { |
| 5464 | receiver.finished(); |
| 5465 | } catch (RemoteException ex) { |
| 5466 | } |
| 5467 | } |
| 5468 | |
| 5469 | return list; |
| 5470 | } |
| 5471 | |
| 5472 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
| 5473 | int flags) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5474 | final int callingUid = Binder.getCallingUid(); |
| 5475 | // If it's the system uid asking, then use the current user id. |
| 5476 | // TODO: Make sure that there aren't any other legitimate calls from the system uid that |
| 5477 | // require the entire list. |
| 5478 | final int callingUserId = callingUid == Process.SYSTEM_UID |
| 5479 | ? mCurrentUserId : UserId.getUserId(callingUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5480 | synchronized (this) { |
| 5481 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5482 | "getRecentTasks()"); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5483 | final boolean detailed = checkCallingPermission( |
| 5484 | android.Manifest.permission.GET_DETAILED_TASKS) |
| 5485 | == PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5486 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5487 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5489 | final int N = mRecentTasks.size(); |
| 5490 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5491 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5492 | maxNum < N ? maxNum : N); |
| 5493 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5494 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5495 | // Only add calling user's recent tasks |
| 5496 | if (tr.userId != callingUserId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5497 | // Return the entry if desired by the caller. We always return |
| 5498 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5499 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5500 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5501 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5502 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5503 | if (i == 0 |
| 5504 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5505 | || (tr.intent == null) |
| 5506 | || ((tr.intent.getFlags() |
| 5507 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5508 | ActivityManager.RecentTaskInfo rti |
| 5509 | = new ActivityManager.RecentTaskInfo(); |
| 5510 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5511 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5512 | rti.baseIntent = new Intent( |
| 5513 | tr.intent != null ? tr.intent : tr.affinityIntent); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5514 | if (!detailed) { |
| 5515 | rti.baseIntent.replaceExtras((Bundle)null); |
| 5516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5517 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5518 | rti.description = tr.lastDescription; |
| 5519 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5520 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5521 | // Check whether this activity is currently available. |
| 5522 | try { |
| 5523 | if (rti.origActivity != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5524 | if (pm.getActivityInfo(rti.origActivity, 0, callingUserId) |
| 5525 | == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5526 | continue; |
| 5527 | } |
| 5528 | } else if (rti.baseIntent != null) { |
| 5529 | if (pm.queryIntentActivities(rti.baseIntent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5530 | null, 0, callingUserId) == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5531 | continue; |
| 5532 | } |
| 5533 | } |
| 5534 | } catch (RemoteException e) { |
| 5535 | // Will never happen. |
| 5536 | } |
| 5537 | } |
| 5538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5539 | res.add(rti); |
| 5540 | maxNum--; |
| 5541 | } |
| 5542 | } |
| 5543 | return res; |
| 5544 | } |
| 5545 | } |
| 5546 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5547 | private TaskRecord taskForIdLocked(int id) { |
| 5548 | final int N = mRecentTasks.size(); |
| 5549 | for (int i=0; i<N; i++) { |
| 5550 | TaskRecord tr = mRecentTasks.get(i); |
| 5551 | if (tr.taskId == id) { |
| 5552 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5553 | } |
| 5554 | } |
| 5555 | return null; |
| 5556 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5557 | |
| 5558 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5559 | synchronized (this) { |
| 5560 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5561 | "getTaskThumbnails()"); |
| 5562 | TaskRecord tr = taskForIdLocked(id); |
| 5563 | if (tr != null) { |
| 5564 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5565 | } |
| 5566 | } |
| 5567 | return null; |
| 5568 | } |
| 5569 | |
| 5570 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5571 | synchronized (this) { |
| 5572 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5573 | "removeSubTask()"); |
| 5574 | long ident = Binder.clearCallingIdentity(); |
| 5575 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5576 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex, |
| 5577 | true) != null; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5578 | } finally { |
| 5579 | Binder.restoreCallingIdentity(ident); |
| 5580 | } |
| 5581 | } |
| 5582 | } |
| 5583 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5584 | private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) { |
| 5585 | final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5586 | Intent baseIntent = new Intent( |
| 5587 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5588 | ComponentName component = baseIntent.getComponent(); |
| 5589 | if (component == null) { |
| 5590 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5591 | return; |
| 5592 | } |
| 5593 | |
| 5594 | // Find any running services associated with this app. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 5595 | mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5596 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5597 | if (killProcesses) { |
| 5598 | // Find any running processes associated with this app. |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5599 | final String pkg = component.getPackageName(); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5600 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5601 | HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap(); |
| 5602 | for (SparseArray<ProcessRecord> uids : pmap.values()) { |
| 5603 | for (int i=0; i<uids.size(); i++) { |
| 5604 | ProcessRecord proc = uids.valueAt(i); |
| 5605 | if (proc.userId != tr.userId) { |
| 5606 | continue; |
| 5607 | } |
| 5608 | if (!proc.pkgList.contains(pkg)) { |
| 5609 | continue; |
| 5610 | } |
| 5611 | procs.add(proc); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5612 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5613 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5614 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5615 | // Kill the running processes. |
| 5616 | for (int i=0; i<procs.size(); i++) { |
| 5617 | ProcessRecord pr = procs.get(i); |
| 5618 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5619 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
| 5620 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5621 | pr.processName, pr.setAdj, "remove task"); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5622 | pr.killedBackground = true; |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5623 | Process.killProcessQuiet(pr.pid); |
| 5624 | } else { |
| 5625 | pr.waitingToKill = "remove task"; |
| 5626 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5627 | } |
| 5628 | } |
| 5629 | } |
| 5630 | |
| 5631 | public boolean removeTask(int taskId, int flags) { |
| 5632 | synchronized (this) { |
| 5633 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5634 | "removeTask()"); |
| 5635 | long ident = Binder.clearCallingIdentity(); |
| 5636 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5637 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1, |
| 5638 | false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5639 | if (r != null) { |
| 5640 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5641 | cleanUpRemovedTaskLocked(r.task, flags); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5642 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5643 | } else { |
| 5644 | TaskRecord tr = null; |
| 5645 | int i=0; |
| 5646 | while (i < mRecentTasks.size()) { |
| 5647 | TaskRecord t = mRecentTasks.get(i); |
| 5648 | if (t.taskId == taskId) { |
| 5649 | tr = t; |
| 5650 | break; |
| 5651 | } |
| 5652 | i++; |
| 5653 | } |
| 5654 | if (tr != null) { |
| 5655 | if (tr.numActivities <= 0) { |
| 5656 | // Caller is just removing a recent task that is |
| 5657 | // not actively running. That is easy! |
| 5658 | mRecentTasks.remove(i); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5659 | cleanUpRemovedTaskLocked(tr, flags); |
| 5660 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5661 | } else { |
| 5662 | Slog.w(TAG, "removeTask: task " + taskId |
| 5663 | + " does not have activities to remove, " |
| 5664 | + " but numActivities=" + tr.numActivities |
| 5665 | + ": " + tr); |
| 5666 | } |
| 5667 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5668 | } |
| 5669 | } finally { |
| 5670 | Binder.restoreCallingIdentity(ident); |
| 5671 | } |
| 5672 | } |
| 5673 | return false; |
| 5674 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5675 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5676 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5677 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5678 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5679 | TaskRecord jt = startTask; |
| 5680 | |
| 5681 | // First look backwards |
| 5682 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5683 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5684 | if (r.task != jt) { |
| 5685 | jt = r.task; |
| 5686 | if (affinity.equals(jt.affinity)) { |
| 5687 | return j; |
| 5688 | } |
| 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5693 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5694 | jt = startTask; |
| 5695 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5696 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5697 | if (r.task != jt) { |
| 5698 | if (affinity.equals(jt.affinity)) { |
| 5699 | return j; |
| 5700 | } |
| 5701 | jt = r.task; |
| 5702 | } |
| 5703 | } |
| 5704 | |
| 5705 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5706 | 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] | 5707 | return N-1; |
| 5708 | } |
| 5709 | |
| 5710 | return -1; |
| 5711 | } |
| 5712 | |
| 5713 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5714 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5715 | */ |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5716 | public void moveTaskToFront(int task, int flags, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5717 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5718 | "moveTaskToFront()"); |
| 5719 | |
| 5720 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5721 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5722 | Binder.getCallingUid(), "Task to front")) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5723 | ActivityOptions.abort(options); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5724 | return; |
| 5725 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5726 | final long origId = Binder.clearCallingIdentity(); |
| 5727 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5728 | TaskRecord tr = taskForIdLocked(task); |
| 5729 | if (tr != null) { |
| 5730 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5731 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5732 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5733 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5734 | // Caller wants the home activity moved with it. To accomplish this, |
| 5735 | // we'll just move the home task to the top first. |
| 5736 | mMainStack.moveHomeToFrontLocked(); |
| 5737 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5738 | mMainStack.moveTaskToFrontLocked(tr, null, options); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5739 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5740 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5741 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5742 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5743 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5744 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5745 | mMainStack.mUserLeaving = true; |
| 5746 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5747 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5748 | // Caller wants the home activity moved with it. To accomplish this, |
| 5749 | // we'll just move the home task to the top first. |
| 5750 | mMainStack.moveHomeToFrontLocked(); |
| 5751 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5752 | mMainStack.moveTaskToFrontLocked(hr.task, null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5753 | return; |
| 5754 | } |
| 5755 | } |
| 5756 | } finally { |
| 5757 | Binder.restoreCallingIdentity(origId); |
| 5758 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5759 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5760 | } |
| 5761 | } |
| 5762 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5763 | public void moveTaskToBack(int task) { |
| 5764 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5765 | "moveTaskToBack()"); |
| 5766 | |
| 5767 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5768 | if (mMainStack.mResumedActivity != null |
| 5769 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5770 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5771 | Binder.getCallingUid(), "Task to back")) { |
| 5772 | return; |
| 5773 | } |
| 5774 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5775 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5776 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5777 | Binder.restoreCallingIdentity(origId); |
| 5778 | } |
| 5779 | } |
| 5780 | |
| 5781 | /** |
| 5782 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5783 | * of the history stack. The activity's order within the task is unchanged. |
| 5784 | * |
| 5785 | * @param token A reference to the activity we wish to move |
| 5786 | * @param nonRoot If false then this only works if the activity is the root |
| 5787 | * of a task; if true it will work for any activity in a task. |
| 5788 | * @return Returns true if the move completed, false if not. |
| 5789 | */ |
| 5790 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5791 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5792 | synchronized(this) { |
| 5793 | final long origId = Binder.clearCallingIdentity(); |
| 5794 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5795 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5796 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5797 | } |
| 5798 | Binder.restoreCallingIdentity(origId); |
| 5799 | } |
| 5800 | return false; |
| 5801 | } |
| 5802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5803 | public void moveTaskBackwards(int task) { |
| 5804 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5805 | "moveTaskBackwards()"); |
| 5806 | |
| 5807 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5808 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5809 | Binder.getCallingUid(), "Task backwards")) { |
| 5810 | return; |
| 5811 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5812 | final long origId = Binder.clearCallingIdentity(); |
| 5813 | moveTaskBackwardsLocked(task); |
| 5814 | Binder.restoreCallingIdentity(origId); |
| 5815 | } |
| 5816 | } |
| 5817 | |
| 5818 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5819 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5823 | synchronized(this) { |
| 5824 | return getTaskForActivityLocked(token, onlyRoot); |
| 5825 | } |
| 5826 | } |
| 5827 | |
| 5828 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5829 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5830 | TaskRecord lastTask = null; |
| 5831 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5832 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5833 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5834 | if (!onlyRoot || lastTask != r.task) { |
| 5835 | return r.task.taskId; |
| 5836 | } |
| 5837 | return -1; |
| 5838 | } |
| 5839 | lastTask = r.task; |
| 5840 | } |
| 5841 | |
| 5842 | return -1; |
| 5843 | } |
| 5844 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5845 | // ========================================================= |
| 5846 | // THUMBNAILS |
| 5847 | // ========================================================= |
| 5848 | |
| 5849 | public void reportThumbnail(IBinder token, |
| 5850 | Bitmap thumbnail, CharSequence description) { |
| 5851 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5852 | final long origId = Binder.clearCallingIdentity(); |
| 5853 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5854 | Binder.restoreCallingIdentity(origId); |
| 5855 | } |
| 5856 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5857 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5858 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5859 | TaskRecord task = null; |
| 5860 | ArrayList receivers = null; |
| 5861 | |
| 5862 | //System.out.println("Send pending thumbnail: " + r); |
| 5863 | |
| 5864 | synchronized(this) { |
| 5865 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 5866 | r = mMainStack.isInStackLocked(token); |
| 5867 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5868 | return; |
| 5869 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5870 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5871 | if (thumbnail == null && r.thumbHolder != null) { |
| 5872 | thumbnail = r.thumbHolder.lastThumbnail; |
| 5873 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5874 | } |
| 5875 | if (thumbnail == null && !always) { |
| 5876 | // If there is no thumbnail, and this entry is not actually |
| 5877 | // going away, then abort for now and pick up the next |
| 5878 | // thumbnail we get. |
| 5879 | return; |
| 5880 | } |
| 5881 | task = r.task; |
| 5882 | |
| 5883 | int N = mPendingThumbnails.size(); |
| 5884 | int i=0; |
| 5885 | while (i<N) { |
| 5886 | PendingThumbnailsRecord pr = |
| 5887 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 5888 | //System.out.println("Looking in " + pr.pendingRecords); |
| 5889 | if (pr.pendingRecords.remove(r)) { |
| 5890 | if (receivers == null) { |
| 5891 | receivers = new ArrayList(); |
| 5892 | } |
| 5893 | receivers.add(pr); |
| 5894 | if (pr.pendingRecords.size() == 0) { |
| 5895 | pr.finished = true; |
| 5896 | mPendingThumbnails.remove(i); |
| 5897 | N--; |
| 5898 | continue; |
| 5899 | } |
| 5900 | } |
| 5901 | i++; |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | if (receivers != null) { |
| 5906 | final int N = receivers.size(); |
| 5907 | for (int i=0; i<N; i++) { |
| 5908 | try { |
| 5909 | PendingThumbnailsRecord pr = |
| 5910 | (PendingThumbnailsRecord)receivers.get(i); |
| 5911 | pr.receiver.newThumbnail( |
| 5912 | task != null ? task.taskId : -1, thumbnail, description); |
| 5913 | if (pr.finished) { |
| 5914 | pr.receiver.finished(); |
| 5915 | } |
| 5916 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5917 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5918 | } |
| 5919 | } |
| 5920 | } |
| 5921 | } |
| 5922 | |
| 5923 | // ========================================================= |
| 5924 | // CONTENT PROVIDERS |
| 5925 | // ========================================================= |
| 5926 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5927 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 5928 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5929 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5930 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5931 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5932 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5933 | } catch (RemoteException ex) { |
| 5934 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5935 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5936 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 5937 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5938 | if (providers != null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 5939 | int N = providers.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5940 | for (int i=0; i<N; i++) { |
| 5941 | ProviderInfo cpi = |
| 5942 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 5943 | boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 5944 | cpi.name, cpi.flags); |
| 5945 | if (singleton && UserId.getUserId(app.uid) != 0) { |
| 5946 | // This is a singleton provider, but a user besides the |
| 5947 | // default user is asking to initialize a process it runs |
| 5948 | // in... well, no, it doesn't actually run in this process, |
| 5949 | // it runs in the process of the default user. Get rid of it. |
| 5950 | providers.remove(i); |
| 5951 | N--; |
| 5952 | continue; |
| 5953 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5954 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5955 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5956 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5957 | if (cpr == null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 5958 | cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5959 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5960 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5961 | if (DEBUG_MU) |
| 5962 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5963 | app.pubProviders.put(cpi.name, cpr); |
| 5964 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 5965 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5966 | } |
| 5967 | } |
| 5968 | return providers; |
| 5969 | } |
| 5970 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5971 | /** |
| 5972 | * Check if {@link ProcessRecord} has a possible chance at accessing the |
| 5973 | * given {@link ProviderInfo}. Final permission checking is always done |
| 5974 | * in {@link ContentProvider}. |
| 5975 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5976 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5977 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5978 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5979 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5980 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5981 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5982 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5983 | return null; |
| 5984 | } |
| 5985 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5986 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5987 | == PackageManager.PERMISSION_GRANTED) { |
| 5988 | return null; |
| 5989 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5990 | |
| 5991 | PathPermission[] pps = cpi.pathPermissions; |
| 5992 | if (pps != null) { |
| 5993 | int i = pps.length; |
| 5994 | while (i > 0) { |
| 5995 | i--; |
| 5996 | PathPermission pp = pps[i]; |
| 5997 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5998 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5999 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6000 | return null; |
| 6001 | } |
| 6002 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6003 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6004 | == PackageManager.PERMISSION_GRANTED) { |
| 6005 | return null; |
| 6006 | } |
| 6007 | } |
| 6008 | } |
| 6009 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6010 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 6011 | if (perms != null) { |
| 6012 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 6013 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 6014 | return null; |
| 6015 | } |
| 6016 | } |
| 6017 | } |
| 6018 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6019 | String msg; |
| 6020 | if (!cpi.exported) { |
| 6021 | msg = "Permission Denial: opening provider " + cpi.name |
| 6022 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6023 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 6024 | + cpi.applicationInfo.uid; |
| 6025 | } else { |
| 6026 | msg = "Permission Denial: opening provider " + cpi.name |
| 6027 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6028 | + ", uid=" + callingUid + ") requires " |
| 6029 | + cpi.readPermission + " or " + cpi.writePermission; |
| 6030 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6031 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6032 | return msg; |
| 6033 | } |
| 6034 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6035 | ContentProviderConnection incProviderCountLocked(ProcessRecord r, |
| 6036 | final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6037 | if (r != null) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6038 | for (int i=0; i<r.conProviders.size(); i++) { |
| 6039 | ContentProviderConnection conn = r.conProviders.get(i); |
| 6040 | if (conn.provider == cpr) { |
| 6041 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6042 | "Adding provider requested by " |
| 6043 | + r.processName + " from process " |
| 6044 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6045 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6046 | if (stable) { |
| 6047 | conn.stableCount++; |
| 6048 | conn.numStableIncs++; |
| 6049 | } else { |
| 6050 | conn.unstableCount++; |
| 6051 | conn.numUnstableIncs++; |
| 6052 | } |
| 6053 | return conn; |
| 6054 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6055 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6056 | ContentProviderConnection conn = new ContentProviderConnection(cpr, r); |
| 6057 | if (stable) { |
| 6058 | conn.stableCount = 1; |
| 6059 | conn.numStableIncs = 1; |
| 6060 | } else { |
| 6061 | conn.unstableCount = 1; |
| 6062 | conn.numUnstableIncs = 1; |
| 6063 | } |
| 6064 | cpr.connections.add(conn); |
| 6065 | r.conProviders.add(conn); |
| 6066 | return conn; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6067 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6068 | cpr.addExternalProcessHandleLocked(externalProcessToken); |
| 6069 | return null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6070 | } |
| 6071 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6072 | boolean decProviderCountLocked(ContentProviderConnection conn, |
| 6073 | ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
| 6074 | if (conn != null) { |
| 6075 | cpr = conn.provider; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6076 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6077 | "Removing provider requested by " |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6078 | + conn.client.processName + " from process " |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6079 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6080 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6081 | if (stable) { |
| 6082 | conn.stableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6083 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6084 | conn.unstableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6085 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6086 | if (conn.stableCount == 0 && conn.unstableCount == 0) { |
| 6087 | cpr.connections.remove(conn); |
| 6088 | conn.client.conProviders.remove(conn); |
| 6089 | return true; |
| 6090 | } |
| 6091 | return false; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6092 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6093 | cpr.removeExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6094 | return false; |
| 6095 | } |
| 6096 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6097 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6098 | String name, IBinder token, boolean stable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6099 | ContentProviderRecord cpr; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6100 | ContentProviderConnection conn = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6101 | ProviderInfo cpi = null; |
| 6102 | |
| 6103 | synchronized(this) { |
| 6104 | ProcessRecord r = null; |
| 6105 | if (caller != null) { |
| 6106 | r = getRecordForAppLocked(caller); |
| 6107 | if (r == null) { |
| 6108 | throw new SecurityException( |
| 6109 | "Unable to find app for caller " + caller |
| 6110 | + " (pid=" + Binder.getCallingPid() |
| 6111 | + ") when getting content provider " + name); |
| 6112 | } |
| 6113 | } |
| 6114 | |
| 6115 | // First check if this content provider has been published... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6116 | int userId = UserId.getUserId(r != null ? r.uid : Binder.getCallingUid()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6117 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6118 | boolean providerRunning = cpr != null; |
| 6119 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6120 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6121 | String msg; |
| 6122 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6123 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | if (r != null && cpr.canRunHere(r)) { |
| 6127 | // This provider has been published or is in the process |
| 6128 | // of being published... but it is also allowed to run |
| 6129 | // in the caller's process, so don't make a connection |
| 6130 | // and just let the caller instantiate its own instance. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6131 | ContentProviderHolder holder = cpr.newHolder(null); |
| 6132 | // don't give caller the provider object, it needs |
| 6133 | // to make its own. |
| 6134 | holder.provider = null; |
| 6135 | return holder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | final long origId = Binder.clearCallingIdentity(); |
| 6139 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6140 | // 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] | 6141 | // return it right away. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6142 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6143 | if (conn != null && (conn.stableCount+conn.unstableCount) == 1) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6144 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 6145 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6146 | // make sure to count it as being accessed and thus |
| 6147 | // back up on the LRU list. This is good because |
| 6148 | // content providers are often expensive to start. |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6149 | updateLruProcessLocked(cpr.proc, false, true); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6150 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 6151 | } |
| 6152 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6153 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6154 | if (false) { |
| 6155 | if (cpr.name.flattenToShortString().equals( |
| 6156 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 6157 | Slog.v(TAG, "****************** KILLING " |
| 6158 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6159 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6160 | } |
| 6161 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6162 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6163 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 6164 | // NOTE: there is still a race here where a signal could be |
| 6165 | // pending on the process even though we managed to update its |
| 6166 | // adj level. Not sure what to do about this, but at least |
| 6167 | // the race is now smaller. |
| 6168 | if (!success) { |
| 6169 | // Uh oh... it looks like the provider's process |
| 6170 | // has been killed on us. We need to wait for a new |
| 6171 | // process to be started, and make sure its death |
| 6172 | // doesn't kill our process. |
| 6173 | Slog.i(TAG, |
| 6174 | "Existing provider " + cpr.name.flattenToShortString() |
| 6175 | + " is crashing; detaching " + r); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6176 | boolean lastRef = decProviderCountLocked(conn, cpr, token, stable); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6177 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6178 | if (!lastRef) { |
| 6179 | // This wasn't the last ref our process had on |
| 6180 | // the provider... we have now been killed, bail. |
| 6181 | return null; |
| 6182 | } |
| 6183 | providerRunning = false; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6184 | conn = null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6186 | } |
| 6187 | |
| 6188 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6189 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6190 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 6191 | boolean singleton; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6192 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6193 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6194 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6195 | resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6196 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6197 | } catch (RemoteException ex) { |
| 6198 | } |
| 6199 | if (cpi == null) { |
| 6200 | return null; |
| 6201 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 6202 | singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6203 | cpi.name, cpi.flags); |
| 6204 | if (singleton) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 6205 | userId = 0; |
| 6206 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6207 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6208 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6209 | String msg; |
| 6210 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6211 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6212 | } |
| 6213 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6214 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6215 | && !cpi.processName.equals("system")) { |
| 6216 | // If this content provider does not run in the system |
| 6217 | // process, and the system is not yet ready to run other |
| 6218 | // processes, then fail fast instead of hanging. |
| 6219 | throw new IllegalArgumentException( |
| 6220 | "Attempt to launch content provider before system ready"); |
| 6221 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6222 | |
| 6223 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6224 | cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6225 | final boolean firstClass = cpr == null; |
| 6226 | if (firstClass) { |
| 6227 | try { |
| 6228 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6229 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6230 | getApplicationInfo( |
| 6231 | cpi.applicationInfo.packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6232 | STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6233 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6234 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6235 | + cpi.name); |
| 6236 | return null; |
| 6237 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6238 | ai = getAppInfoForUser(ai, userId); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 6239 | cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6240 | } catch (RemoteException ex) { |
| 6241 | // pm is in same process, this will never happen. |
| 6242 | } |
| 6243 | } |
| 6244 | |
| 6245 | if (r != null && cpr.canRunHere(r)) { |
| 6246 | // If this is a multiprocess provider, then just return its |
| 6247 | // info and allow the caller to instantiate it. Only do |
| 6248 | // this if the provider is the same user as the caller's |
| 6249 | // process, or can run as root (so can be in any process). |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6250 | return cpr.newHolder(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6251 | } |
| 6252 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6253 | if (DEBUG_PROVIDER) { |
| 6254 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6255 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6256 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6257 | } |
| 6258 | |
| 6259 | // This is single process, and our app is now connecting to it. |
| 6260 | // See if we are already in the process of launching this |
| 6261 | // provider. |
| 6262 | final int N = mLaunchingProviders.size(); |
| 6263 | int i; |
| 6264 | for (i=0; i<N; i++) { |
| 6265 | if (mLaunchingProviders.get(i) == cpr) { |
| 6266 | break; |
| 6267 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6268 | } |
| 6269 | |
| 6270 | // If the provider is not already being launched, then get it |
| 6271 | // started. |
| 6272 | if (i >= N) { |
| 6273 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6274 | |
| 6275 | try { |
| 6276 | // Content provider is now in use, its package can't be stopped. |
| 6277 | try { |
| 6278 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6279 | cpr.appInfo.packageName, false, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6280 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6281 | } catch (IllegalArgumentException e) { |
| 6282 | Slog.w(TAG, "Failed trying to unstop package " |
| 6283 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6284 | } |
| 6285 | |
| 6286 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6287 | cpr.appInfo, false, 0, "content provider", |
| 6288 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6289 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6290 | if (proc == null) { |
| 6291 | Slog.w(TAG, "Unable to launch app " |
| 6292 | + cpi.applicationInfo.packageName + "/" |
| 6293 | + cpi.applicationInfo.uid + " for provider " |
| 6294 | + name + ": process is bad"); |
| 6295 | return null; |
| 6296 | } |
| 6297 | cpr.launchingApp = proc; |
| 6298 | mLaunchingProviders.add(cpr); |
| 6299 | } finally { |
| 6300 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6302 | } |
| 6303 | |
| 6304 | // Make sure the provider is published (the same provider class |
| 6305 | // may be published under multiple names). |
| 6306 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6307 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6308 | } |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6309 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6310 | mProviderMap.putProviderByName(name, cpr); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6311 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6312 | if (conn != null) { |
| 6313 | conn.waiting = true; |
| 6314 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6315 | } |
| 6316 | } |
| 6317 | |
| 6318 | // Wait for the provider to be published... |
| 6319 | synchronized (cpr) { |
| 6320 | while (cpr.provider == null) { |
| 6321 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6322 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6323 | + cpi.applicationInfo.packageName + "/" |
| 6324 | + cpi.applicationInfo.uid + " for provider " |
| 6325 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6326 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6327 | cpi.applicationInfo.packageName, |
| 6328 | cpi.applicationInfo.uid, name); |
| 6329 | return null; |
| 6330 | } |
| 6331 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6332 | if (DEBUG_MU) { |
| 6333 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6334 | + cpr.launchingApp); |
| 6335 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6336 | if (conn != null) { |
| 6337 | conn.waiting = true; |
| 6338 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6339 | cpr.wait(); |
| 6340 | } catch (InterruptedException ex) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6341 | } finally { |
| 6342 | if (conn != null) { |
| 6343 | conn.waiting = false; |
| 6344 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6345 | } |
| 6346 | } |
| 6347 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6348 | return cpr != null ? cpr.newHolder(conn) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | public final ContentProviderHolder getContentProvider( |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6352 | IApplicationThread caller, String name, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6353 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6354 | if (caller == null) { |
| 6355 | String msg = "null IApplicationThread when getting content provider " |
| 6356 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6357 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6358 | throw new SecurityException(msg); |
| 6359 | } |
| 6360 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6361 | return getContentProviderImpl(caller, name, null, stable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6362 | } |
| 6363 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6364 | public ContentProviderHolder getContentProviderExternal(String name, IBinder token) { |
| 6365 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6366 | "Do not have permission in call getContentProviderExternal()"); |
| 6367 | return getContentProviderExternalUnchecked(name, token); |
| 6368 | } |
| 6369 | |
| 6370 | private ContentProviderHolder getContentProviderExternalUnchecked(String name,IBinder token) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6371 | return getContentProviderImpl(null, name, token, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | /** |
| 6375 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6376 | * @param cpr |
| 6377 | */ |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6378 | public void removeContentProvider(IBinder connection, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6379 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6380 | synchronized (this) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6381 | ContentProviderConnection conn; |
| 6382 | try { |
| 6383 | conn = (ContentProviderConnection)connection; |
| 6384 | } catch (ClassCastException e) { |
| 6385 | String msg ="removeContentProvider: " + connection |
| 6386 | + " not a ContentProviderConnection"; |
| 6387 | Slog.w(TAG, msg); |
| 6388 | throw new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6389 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6390 | if (conn == null) { |
| 6391 | throw new NullPointerException("connection is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6392 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6393 | if (decProviderCountLocked(conn, null, null, stable)) { |
| 6394 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6395 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6396 | } |
| 6397 | } |
| 6398 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6399 | public void removeContentProviderExternal(String name, IBinder token) { |
| 6400 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6401 | "Do not have permission in call removeContentProviderExternal()"); |
| 6402 | removeContentProviderExternalUnchecked(name, token); |
| 6403 | } |
| 6404 | |
| 6405 | private void removeContentProviderExternalUnchecked(String name, IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6406 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6407 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
| 6408 | Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6409 | if(cpr == null) { |
| 6410 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6411 | 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] | 6412 | return; |
| 6413 | } |
| 6414 | |
| 6415 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6416 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6417 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, |
| 6418 | Binder.getOrigCallingUser()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6419 | if (localCpr.hasExternalProcessHandles()) { |
| 6420 | if (localCpr.removeExternalProcessHandleLocked(token)) { |
| 6421 | updateOomAdjLocked(); |
| 6422 | } else { |
| 6423 | Slog.e(TAG, "Attmpt to remove content provider " + localCpr |
| 6424 | + " with no external reference for token: " |
| 6425 | + token + "."); |
| 6426 | } |
| 6427 | } else { |
| 6428 | Slog.e(TAG, "Attmpt to remove content provider: " + localCpr |
| 6429 | + " with no external references."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6430 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6431 | } |
| 6432 | } |
| 6433 | |
| 6434 | public final void publishContentProviders(IApplicationThread caller, |
| 6435 | List<ContentProviderHolder> providers) { |
| 6436 | if (providers == null) { |
| 6437 | return; |
| 6438 | } |
| 6439 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6440 | enforceNotIsolatedCaller("publishContentProviders"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6441 | synchronized (this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6442 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6443 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6444 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6445 | if (r == null) { |
| 6446 | throw new SecurityException( |
| 6447 | "Unable to find app for caller " + caller |
| 6448 | + " (pid=" + Binder.getCallingPid() |
| 6449 | + ") when publishing content providers"); |
| 6450 | } |
| 6451 | |
| 6452 | final long origId = Binder.clearCallingIdentity(); |
| 6453 | |
| 6454 | final int N = providers.size(); |
| 6455 | for (int i=0; i<N; i++) { |
| 6456 | ContentProviderHolder src = providers.get(i); |
| 6457 | if (src == null || src.info == null || src.provider == null) { |
| 6458 | continue; |
| 6459 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6460 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6461 | if (DEBUG_MU) |
| 6462 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6463 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6464 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6465 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6466 | String names[] = dst.info.authority.split(";"); |
| 6467 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6468 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6469 | } |
| 6470 | |
| 6471 | int NL = mLaunchingProviders.size(); |
| 6472 | int j; |
| 6473 | for (j=0; j<NL; j++) { |
| 6474 | if (mLaunchingProviders.get(j) == dst) { |
| 6475 | mLaunchingProviders.remove(j); |
| 6476 | j--; |
| 6477 | NL--; |
| 6478 | } |
| 6479 | } |
| 6480 | synchronized (dst) { |
| 6481 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6482 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6483 | dst.notifyAll(); |
| 6484 | } |
| 6485 | updateOomAdjLocked(r); |
| 6486 | } |
| 6487 | } |
| 6488 | |
| 6489 | Binder.restoreCallingIdentity(origId); |
| 6490 | } |
| 6491 | } |
| 6492 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6493 | public boolean refContentProvider(IBinder connection, int stable, int unstable) { |
| 6494 | ContentProviderConnection conn; |
| 6495 | try { |
| 6496 | conn = (ContentProviderConnection)connection; |
| 6497 | } catch (ClassCastException e) { |
| 6498 | String msg ="refContentProvider: " + connection |
| 6499 | + " not a ContentProviderConnection"; |
| 6500 | Slog.w(TAG, msg); |
| 6501 | throw new IllegalArgumentException(msg); |
| 6502 | } |
| 6503 | if (conn == null) { |
| 6504 | throw new NullPointerException("connection is null"); |
| 6505 | } |
| 6506 | |
| 6507 | synchronized (this) { |
| 6508 | if (stable > 0) { |
| 6509 | conn.numStableIncs += stable; |
| 6510 | } |
| 6511 | stable = conn.stableCount + stable; |
| 6512 | if (stable < 0) { |
| 6513 | throw new IllegalStateException("stableCount < 0: " + stable); |
| 6514 | } |
| 6515 | |
| 6516 | if (unstable > 0) { |
| 6517 | conn.numUnstableIncs += unstable; |
| 6518 | } |
| 6519 | unstable = conn.unstableCount + unstable; |
| 6520 | if (unstable < 0) { |
| 6521 | throw new IllegalStateException("unstableCount < 0: " + unstable); |
| 6522 | } |
| 6523 | |
| 6524 | if ((stable+unstable) <= 0) { |
| 6525 | throw new IllegalStateException("ref counts can't go to zero here: stable=" |
| 6526 | + stable + " unstable=" + unstable); |
| 6527 | } |
| 6528 | conn.stableCount = stable; |
| 6529 | conn.unstableCount = unstable; |
| 6530 | return !conn.dead; |
| 6531 | } |
| 6532 | } |
| 6533 | |
| 6534 | public void unstableProviderDied(IBinder connection) { |
| 6535 | ContentProviderConnection conn; |
| 6536 | try { |
| 6537 | conn = (ContentProviderConnection)connection; |
| 6538 | } catch (ClassCastException e) { |
| 6539 | String msg ="refContentProvider: " + connection |
| 6540 | + " not a ContentProviderConnection"; |
| 6541 | Slog.w(TAG, msg); |
| 6542 | throw new IllegalArgumentException(msg); |
| 6543 | } |
| 6544 | if (conn == null) { |
| 6545 | throw new NullPointerException("connection is null"); |
| 6546 | } |
| 6547 | |
| 6548 | // Safely retrieve the content provider associated with the connection. |
| 6549 | IContentProvider provider; |
| 6550 | synchronized (this) { |
| 6551 | provider = conn.provider.provider; |
| 6552 | } |
| 6553 | |
| 6554 | if (provider == null) { |
| 6555 | // Um, yeah, we're way ahead of you. |
| 6556 | return; |
| 6557 | } |
| 6558 | |
| 6559 | // Make sure the caller is being honest with us. |
| 6560 | if (provider.asBinder().pingBinder()) { |
| 6561 | // Er, no, still looks good to us. |
| 6562 | synchronized (this) { |
| 6563 | Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid() |
| 6564 | + " says " + conn + " died, but we don't agree"); |
| 6565 | return; |
| 6566 | } |
| 6567 | } |
| 6568 | |
| 6569 | // Well look at that! It's dead! |
| 6570 | synchronized (this) { |
| 6571 | if (conn.provider.provider != provider) { |
| 6572 | // But something changed... good enough. |
| 6573 | return; |
| 6574 | } |
| 6575 | |
| 6576 | ProcessRecord proc = conn.provider.proc; |
| 6577 | if (proc == null || proc.thread == null) { |
| 6578 | // Seems like the process is already cleaned up. |
| 6579 | return; |
| 6580 | } |
| 6581 | |
| 6582 | // As far as we're concerned, this is just like receiving a |
| 6583 | // death notification... just a bit prematurely. |
| 6584 | Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid |
| 6585 | + ") early provider death"); |
Dianne Hackborn | bd145db | 2012-06-05 16:20:46 -0700 | [diff] [blame] | 6586 | final long ident = Binder.clearCallingIdentity(); |
| 6587 | try { |
| 6588 | appDiedLocked(proc, proc.pid, proc.thread); |
| 6589 | } finally { |
| 6590 | Binder.restoreCallingIdentity(ident); |
| 6591 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6592 | } |
| 6593 | } |
| 6594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6595 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6596 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6597 | synchronized (mSelf) { |
| 6598 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6599 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6600 | if (providers != null) { |
| 6601 | for (int i=providers.size()-1; i>=0; i--) { |
| 6602 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6603 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6604 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6605 | + ": not system .apk"); |
| 6606 | providers.remove(i); |
| 6607 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6608 | } |
| 6609 | } |
| 6610 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6611 | if (providers != null) { |
| 6612 | mSystemThread.installSystemProviders(providers); |
| 6613 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6614 | |
| 6615 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6616 | |
| 6617 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6618 | } |
| 6619 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6620 | /** |
| 6621 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6622 | * to access its content provider. |
| 6623 | * |
| 6624 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6625 | * |
| 6626 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6627 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6628 | */ |
| 6629 | public String getProviderMimeType(Uri uri) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6630 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6631 | final String name = uri.getAuthority(); |
| 6632 | final long ident = Binder.clearCallingIdentity(); |
| 6633 | ContentProviderHolder holder = null; |
| 6634 | |
| 6635 | try { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6636 | holder = getContentProviderExternalUnchecked(name, null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6637 | if (holder != null) { |
| 6638 | return holder.provider.getType(uri); |
| 6639 | } |
| 6640 | } catch (RemoteException e) { |
| 6641 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6642 | return null; |
| 6643 | } finally { |
| 6644 | if (holder != null) { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6645 | removeContentProviderExternalUnchecked(name, null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6646 | } |
| 6647 | Binder.restoreCallingIdentity(ident); |
| 6648 | } |
| 6649 | |
| 6650 | return null; |
| 6651 | } |
| 6652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6653 | // ========================================================= |
| 6654 | // GLOBAL MANAGEMENT |
| 6655 | // ========================================================= |
| 6656 | |
| 6657 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6658 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6659 | String proc = customProcess != null ? customProcess : info.processName; |
| 6660 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6661 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6662 | int uid = info.uid; |
| 6663 | if (isolated) { |
| 6664 | int userId = UserId.getUserId(uid); |
| 6665 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 6666 | uid = 0; |
| 6667 | while (true) { |
| 6668 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 6669 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 6670 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 6671 | } |
| 6672 | uid = UserId.getUid(userId, mNextIsolatedProcessUid); |
| 6673 | mNextIsolatedProcessUid++; |
| 6674 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 6675 | // No process for this uid, use it. |
| 6676 | break; |
| 6677 | } |
| 6678 | stepsLeft--; |
| 6679 | if (stepsLeft <= 0) { |
| 6680 | return null; |
| 6681 | } |
| 6682 | } |
| 6683 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6684 | synchronized (stats) { |
| 6685 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6686 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6687 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6688 | } |
| 6689 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6690 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 6691 | ProcessRecord app; |
| 6692 | if (!isolated) { |
| 6693 | app = getProcessRecordLocked(info.processName, info.uid); |
| 6694 | } else { |
| 6695 | app = null; |
| 6696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6697 | |
| 6698 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6699 | app = newProcessRecordLocked(null, info, null, isolated); |
| 6700 | mProcessNames.put(info.processName, app.uid, app); |
| 6701 | if (isolated) { |
| 6702 | mIsolatedProcesses.put(app.uid, app); |
| 6703 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 6704 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6705 | } |
| 6706 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6707 | // This package really, really can not be stopped. |
| 6708 | try { |
| 6709 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6710 | info.packageName, false, UserId.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6711 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6712 | } catch (IllegalArgumentException e) { |
| 6713 | Slog.w(TAG, "Failed trying to unstop package " |
| 6714 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6715 | } |
| 6716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6717 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6718 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6719 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6720 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6721 | } |
| 6722 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6723 | mPersistentStartingProcesses.add(app); |
| 6724 | startProcessLocked(app, "added application", app.processName); |
| 6725 | } |
| 6726 | |
| 6727 | return app; |
| 6728 | } |
| 6729 | |
| 6730 | public void unhandledBack() { |
| 6731 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6732 | "unhandledBack()"); |
| 6733 | |
| 6734 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6735 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6736 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6737 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 6738 | if (count > 1) { |
| 6739 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6740 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6741 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 6742 | Binder.restoreCallingIdentity(origId); |
| 6743 | } |
| 6744 | } |
| 6745 | } |
| 6746 | |
| 6747 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6748 | enforceNotIsolatedCaller("openContentUri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6749 | String name = uri.getAuthority(); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6750 | ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6751 | ParcelFileDescriptor pfd = null; |
| 6752 | if (cph != null) { |
| 6753 | // We record the binder invoker's uid in thread-local storage before |
| 6754 | // going to the content provider to open the file. Later, in the code |
| 6755 | // that handles all permissions checks, we look for this uid and use |
| 6756 | // that rather than the Activity Manager's own uid. The effect is that |
| 6757 | // we do the check against the caller's permissions even though it looks |
| 6758 | // to the content provider like the Activity Manager itself is making |
| 6759 | // the request. |
| 6760 | sCallerIdentity.set(new Identity( |
| 6761 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6762 | try { |
| 6763 | pfd = cph.provider.openFile(uri, "r"); |
| 6764 | } catch (FileNotFoundException e) { |
| 6765 | // do nothing; pfd will be returned null |
| 6766 | } finally { |
| 6767 | // Ensure that whatever happens, we clean up the identity state |
| 6768 | sCallerIdentity.remove(); |
| 6769 | } |
| 6770 | |
| 6771 | // We've got the fd now, so we're done with the provider. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6772 | removeContentProviderExternalUnchecked(name, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6773 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6774 | 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] | 6775 | } |
| 6776 | return pfd; |
| 6777 | } |
| 6778 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6779 | // Actually is sleeping or shutting down or whatever else in the future |
| 6780 | // is an inactive state. |
| 6781 | public boolean isSleeping() { |
| 6782 | return mSleeping || mShuttingDown; |
| 6783 | } |
| 6784 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6785 | public void goingToSleep() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6786 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 6787 | != PackageManager.PERMISSION_GRANTED) { |
| 6788 | throw new SecurityException("Requires permission " |
| 6789 | + android.Manifest.permission.DEVICE_POWER); |
| 6790 | } |
| 6791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6792 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6793 | mWentToSleep = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 6794 | updateEventDispatchingLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6795 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6796 | if (!mSleeping) { |
| 6797 | mSleeping = true; |
| 6798 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6799 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6800 | // Initialize the wake times of all processes. |
| 6801 | checkExcessivePowerUsageLocked(false); |
| 6802 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6803 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6804 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
| 6805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6806 | } |
| 6807 | } |
| 6808 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6809 | public boolean shutdown(int timeout) { |
| 6810 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6811 | != PackageManager.PERMISSION_GRANTED) { |
| 6812 | throw new SecurityException("Requires permission " |
| 6813 | + android.Manifest.permission.SHUTDOWN); |
| 6814 | } |
| 6815 | |
| 6816 | boolean timedout = false; |
| 6817 | |
| 6818 | synchronized(this) { |
| 6819 | mShuttingDown = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 6820 | updateEventDispatchingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6821 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6822 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6823 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6824 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6825 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 6826 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6827 | long delay = endTime - System.currentTimeMillis(); |
| 6828 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6829 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6830 | timedout = true; |
| 6831 | break; |
| 6832 | } |
| 6833 | try { |
| 6834 | this.wait(); |
| 6835 | } catch (InterruptedException e) { |
| 6836 | } |
| 6837 | } |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | mUsageStatsService.shutdown(); |
| 6842 | mBatteryStatsService.shutdown(); |
| 6843 | |
| 6844 | return timedout; |
| 6845 | } |
| 6846 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6847 | public final void activitySlept(IBinder token) { |
| 6848 | if (localLOGV) Slog.v( |
| 6849 | TAG, "Activity slept: token=" + token); |
| 6850 | |
| 6851 | ActivityRecord r = null; |
| 6852 | |
| 6853 | final long origId = Binder.clearCallingIdentity(); |
| 6854 | |
| 6855 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6856 | r = mMainStack.isInStackLocked(token); |
| 6857 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6858 | mMainStack.activitySleptLocked(r); |
| 6859 | } |
| 6860 | } |
| 6861 | |
| 6862 | Binder.restoreCallingIdentity(origId); |
| 6863 | } |
| 6864 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6865 | private void comeOutOfSleepIfNeededLocked() { |
| 6866 | if (!mWentToSleep && !mLockScreenShown) { |
| 6867 | if (mSleeping) { |
| 6868 | mSleeping = false; |
| 6869 | mMainStack.awakeFromSleepingLocked(); |
| 6870 | mMainStack.resumeTopActivityLocked(null); |
| 6871 | } |
| 6872 | } |
| 6873 | } |
| 6874 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6875 | public void wakingUp() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6876 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 6877 | != PackageManager.PERMISSION_GRANTED) { |
| 6878 | throw new SecurityException("Requires permission " |
| 6879 | + android.Manifest.permission.DEVICE_POWER); |
| 6880 | } |
| 6881 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6882 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6883 | mWentToSleep = false; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 6884 | updateEventDispatchingLocked(); |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6885 | comeOutOfSleepIfNeededLocked(); |
| 6886 | } |
| 6887 | } |
| 6888 | |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 6889 | private void updateEventDispatchingLocked() { |
| 6890 | mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown); |
| 6891 | } |
| 6892 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6893 | public void setLockScreenShown(boolean shown) { |
| 6894 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 6895 | != PackageManager.PERMISSION_GRANTED) { |
| 6896 | throw new SecurityException("Requires permission " |
| 6897 | + android.Manifest.permission.DEVICE_POWER); |
| 6898 | } |
| 6899 | |
| 6900 | synchronized(this) { |
| 6901 | mLockScreenShown = shown; |
| 6902 | comeOutOfSleepIfNeededLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6903 | } |
| 6904 | } |
| 6905 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6906 | public void stopAppSwitches() { |
| 6907 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6908 | != PackageManager.PERMISSION_GRANTED) { |
| 6909 | throw new SecurityException("Requires permission " |
| 6910 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6911 | } |
| 6912 | |
| 6913 | synchronized(this) { |
| 6914 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 6915 | + APP_SWITCH_DELAY_TIME; |
| 6916 | mDidAppSwitch = false; |
| 6917 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6918 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6919 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 6920 | } |
| 6921 | } |
| 6922 | |
| 6923 | public void resumeAppSwitches() { |
| 6924 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6925 | != PackageManager.PERMISSION_GRANTED) { |
| 6926 | throw new SecurityException("Requires permission " |
| 6927 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6928 | } |
| 6929 | |
| 6930 | synchronized(this) { |
| 6931 | // Note that we don't execute any pending app switches... we will |
| 6932 | // let those wait until either the timeout, or the next start |
| 6933 | // activity request. |
| 6934 | mAppSwitchesAllowedTime = 0; |
| 6935 | } |
| 6936 | } |
| 6937 | |
| 6938 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 6939 | String name) { |
| 6940 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 6941 | return true; |
| 6942 | } |
| 6943 | |
| 6944 | final int perm = checkComponentPermission( |
| 6945 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6946 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6947 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 6948 | return true; |
| 6949 | } |
| 6950 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6951 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6952 | return false; |
| 6953 | } |
| 6954 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6955 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 6956 | boolean persistent) { |
| 6957 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 6958 | "setDebugApp()"); |
| 6959 | |
| 6960 | // Note that this is not really thread safe if there are multiple |
| 6961 | // callers into it at the same time, but that's not a situation we |
| 6962 | // care about. |
| 6963 | if (persistent) { |
| 6964 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6965 | Settings.System.putString( |
| 6966 | resolver, Settings.System.DEBUG_APP, |
| 6967 | packageName); |
| 6968 | Settings.System.putInt( |
| 6969 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 6970 | waitForDebugger ? 1 : 0); |
| 6971 | } |
| 6972 | |
| 6973 | synchronized (this) { |
| 6974 | if (!persistent) { |
| 6975 | mOrigDebugApp = mDebugApp; |
| 6976 | mOrigWaitForDebugger = mWaitForDebugger; |
| 6977 | } |
| 6978 | mDebugApp = packageName; |
| 6979 | mWaitForDebugger = waitForDebugger; |
| 6980 | mDebugTransient = !persistent; |
| 6981 | if (packageName != null) { |
| 6982 | final long origId = Binder.clearCallingIdentity(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6983 | forceStopPackageLocked(packageName, -1, false, false, true, true, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6984 | Binder.restoreCallingIdentity(origId); |
| 6985 | } |
| 6986 | } |
| 6987 | } |
| 6988 | |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 6989 | void setOpenGlTraceApp(ApplicationInfo app, String processName) { |
| 6990 | synchronized (this) { |
| 6991 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 6992 | if (!isDebuggable) { |
| 6993 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 6994 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 6995 | } |
| 6996 | } |
| 6997 | |
| 6998 | mOpenGlTraceApp = processName; |
| 6999 | } |
| 7000 | } |
| 7001 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 7002 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 7003 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 7004 | synchronized (this) { |
| 7005 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7006 | if (!isDebuggable) { |
| 7007 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7008 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7009 | } |
| 7010 | } |
| 7011 | mProfileApp = processName; |
| 7012 | mProfileFile = profileFile; |
| 7013 | if (mProfileFd != null) { |
| 7014 | try { |
| 7015 | mProfileFd.close(); |
| 7016 | } catch (IOException e) { |
| 7017 | } |
| 7018 | mProfileFd = null; |
| 7019 | } |
| 7020 | mProfileFd = profileFd; |
| 7021 | mProfileType = 0; |
| 7022 | mAutoStopProfiler = autoStopProfiler; |
| 7023 | } |
| 7024 | } |
| 7025 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7026 | public void setAlwaysFinish(boolean enabled) { |
| 7027 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 7028 | "setAlwaysFinish()"); |
| 7029 | |
| 7030 | Settings.System.putInt( |
| 7031 | mContext.getContentResolver(), |
| 7032 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 7033 | |
| 7034 | synchronized (this) { |
| 7035 | mAlwaysFinishActivities = enabled; |
| 7036 | } |
| 7037 | } |
| 7038 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7039 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7040 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7041 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7042 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7043 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7044 | } |
| 7045 | } |
| 7046 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 7047 | public boolean isUserAMonkey() { |
| 7048 | // For now the fact that there is a controller implies |
| 7049 | // we have a monkey. |
| 7050 | synchronized (this) { |
| 7051 | return mController != null; |
| 7052 | } |
| 7053 | } |
| 7054 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7055 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7056 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 7057 | "registerProcessObserver()"); |
| 7058 | synchronized (this) { |
| 7059 | mProcessObservers.register(observer); |
| 7060 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7061 | } |
| 7062 | |
| 7063 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7064 | synchronized (this) { |
| 7065 | mProcessObservers.unregister(observer); |
| 7066 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7067 | } |
| 7068 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7069 | public void setImmersive(IBinder token, boolean immersive) { |
| 7070 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7071 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7072 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7073 | throw new IllegalArgumentException(); |
| 7074 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7075 | r.immersive = immersive; |
| 7076 | } |
| 7077 | } |
| 7078 | |
| 7079 | public boolean isImmersive(IBinder token) { |
| 7080 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7081 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7082 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7083 | throw new IllegalArgumentException(); |
| 7084 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7085 | return r.immersive; |
| 7086 | } |
| 7087 | } |
| 7088 | |
| 7089 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7090 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7091 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7092 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7093 | return (r != null) ? r.immersive : false; |
| 7094 | } |
| 7095 | } |
| 7096 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7097 | public final void enterSafeMode() { |
| 7098 | synchronized(this) { |
| 7099 | // It only makes sense to do this before the system is ready |
| 7100 | // and started launching other packages. |
| 7101 | if (!mSystemReady) { |
| 7102 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7103 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7104 | } catch (RemoteException e) { |
| 7105 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7106 | } |
| 7107 | } |
| 7108 | } |
| 7109 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7110 | public final void showSafeModeOverlay() { |
| 7111 | View v = LayoutInflater.from(mContext).inflate( |
| 7112 | com.android.internal.R.layout.safe_mode, null); |
| 7113 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 7114 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 7115 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 7116 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 7117 | lp.gravity = Gravity.BOTTOM | Gravity.START; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7118 | lp.format = v.getBackground().getOpacity(); |
| 7119 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 7120 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 7121 | ((WindowManager)mContext.getSystemService( |
| 7122 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 7123 | } |
| 7124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7125 | public void noteWakeupAlarm(IIntentSender sender) { |
| 7126 | if (!(sender instanceof PendingIntentRecord)) { |
| 7127 | return; |
| 7128 | } |
| 7129 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 7130 | synchronized (stats) { |
| 7131 | if (mBatteryStatsService.isOnBattery()) { |
| 7132 | mBatteryStatsService.enforceCallingPermission(); |
| 7133 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 7134 | int MY_UID = Binder.getCallingUid(); |
| 7135 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 7136 | BatteryStatsImpl.Uid.Pkg pkg = |
| 7137 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 7138 | pkg.incWakeupsLocked(); |
| 7139 | } |
| 7140 | } |
| 7141 | } |
| 7142 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7143 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7144 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7145 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7146 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7147 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7148 | // XXX Note: don't acquire main activity lock here, because the window |
| 7149 | // manager calls in with its locks held. |
| 7150 | |
| 7151 | boolean killed = false; |
| 7152 | synchronized (mPidsSelfLocked) { |
| 7153 | int[] types = new int[pids.length]; |
| 7154 | int worstType = 0; |
| 7155 | for (int i=0; i<pids.length; i++) { |
| 7156 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7157 | if (proc != null) { |
| 7158 | int type = proc.setAdj; |
| 7159 | types[i] = type; |
| 7160 | if (type > worstType) { |
| 7161 | worstType = type; |
| 7162 | } |
| 7163 | } |
| 7164 | } |
| 7165 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7166 | // 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] | 7167 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7168 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 7169 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7170 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7171 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7172 | |
| 7173 | // If this is not a secure call, don't let it kill processes that |
| 7174 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7175 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 7176 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7177 | } |
| 7178 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7179 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7180 | for (int i=0; i<pids.length; i++) { |
| 7181 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7182 | if (proc == null) { |
| 7183 | continue; |
| 7184 | } |
| 7185 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7186 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7187 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7188 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 7189 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7190 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7191 | proc.killedBackground = true; |
| 7192 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7193 | } |
| 7194 | } |
| 7195 | } |
| 7196 | return killed; |
| 7197 | } |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7198 | |
| 7199 | @Override |
| 7200 | public boolean killProcessesBelowForeground(String reason) { |
| 7201 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7202 | throw new SecurityException("killProcessesBelowForeground() only available to system"); |
| 7203 | } |
| 7204 | |
| 7205 | return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason); |
| 7206 | } |
| 7207 | |
| 7208 | private boolean killProcessesBelowAdj(int belowAdj, String reason) { |
| 7209 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7210 | throw new SecurityException("killProcessesBelowAdj() only available to system"); |
| 7211 | } |
| 7212 | |
| 7213 | boolean killed = false; |
| 7214 | synchronized (mPidsSelfLocked) { |
| 7215 | final int size = mPidsSelfLocked.size(); |
| 7216 | for (int i = 0; i < size; i++) { |
| 7217 | final int pid = mPidsSelfLocked.keyAt(i); |
| 7218 | final ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7219 | if (proc == null) continue; |
| 7220 | |
| 7221 | final int adj = proc.setAdj; |
| 7222 | if (adj > belowAdj && !proc.killedBackground) { |
| 7223 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
| 7224 | EventLog.writeEvent( |
| 7225 | EventLogTags.AM_KILL, proc.pid, proc.processName, adj, reason); |
| 7226 | killed = true; |
| 7227 | proc.killedBackground = true; |
| 7228 | Process.killProcessQuiet(pid); |
| 7229 | } |
| 7230 | } |
| 7231 | } |
| 7232 | return killed; |
| 7233 | } |
| 7234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7235 | public final void startRunning(String pkg, String cls, String action, |
| 7236 | String data) { |
| 7237 | synchronized(this) { |
| 7238 | if (mStartRunning) { |
| 7239 | return; |
| 7240 | } |
| 7241 | mStartRunning = true; |
| 7242 | mTopComponent = pkg != null && cls != null |
| 7243 | ? new ComponentName(pkg, cls) : null; |
| 7244 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 7245 | mTopData = data; |
| 7246 | if (!mSystemReady) { |
| 7247 | return; |
| 7248 | } |
| 7249 | } |
| 7250 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7251 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7252 | } |
| 7253 | |
| 7254 | private void retrieveSettings() { |
| 7255 | final ContentResolver resolver = mContext.getContentResolver(); |
| 7256 | String debugApp = Settings.System.getString( |
| 7257 | resolver, Settings.System.DEBUG_APP); |
| 7258 | boolean waitForDebugger = Settings.System.getInt( |
| 7259 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 7260 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 7261 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 7262 | |
| 7263 | Configuration configuration = new Configuration(); |
| 7264 | Settings.System.getConfiguration(resolver, configuration); |
| 7265 | |
| 7266 | synchronized (this) { |
| 7267 | mDebugApp = mOrigDebugApp = debugApp; |
| 7268 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 7269 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 7270 | // This happens before any activities are started, so we can |
| 7271 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 7272 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7273 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7274 | } |
| 7275 | } |
| 7276 | |
| 7277 | public boolean testIsSystemReady() { |
| 7278 | // no need to synchronize(this) just to read & return the value |
| 7279 | return mSystemReady; |
| 7280 | } |
| 7281 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7282 | private static File getCalledPreBootReceiversFile() { |
| 7283 | File dataDir = Environment.getDataDirectory(); |
| 7284 | File systemDir = new File(dataDir, "system"); |
| 7285 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 7286 | return fname; |
| 7287 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7288 | |
| 7289 | static final int LAST_DONE_VERSION = 10000; |
| 7290 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7291 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 7292 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 7293 | File file = getCalledPreBootReceiversFile(); |
| 7294 | FileInputStream fis = null; |
| 7295 | try { |
| 7296 | fis = new FileInputStream(file); |
| 7297 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7298 | int fvers = dis.readInt(); |
| 7299 | if (fvers == LAST_DONE_VERSION) { |
| 7300 | String vers = dis.readUTF(); |
| 7301 | String codename = dis.readUTF(); |
| 7302 | String build = dis.readUTF(); |
| 7303 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 7304 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 7305 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 7306 | int num = dis.readInt(); |
| 7307 | while (num > 0) { |
| 7308 | num--; |
| 7309 | String pkg = dis.readUTF(); |
| 7310 | String cls = dis.readUTF(); |
| 7311 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 7312 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7313 | } |
| 7314 | } |
| 7315 | } catch (FileNotFoundException e) { |
| 7316 | } catch (IOException e) { |
| 7317 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 7318 | } finally { |
| 7319 | if (fis != null) { |
| 7320 | try { |
| 7321 | fis.close(); |
| 7322 | } catch (IOException e) { |
| 7323 | } |
| 7324 | } |
| 7325 | } |
| 7326 | return lastDoneReceivers; |
| 7327 | } |
| 7328 | |
| 7329 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 7330 | File file = getCalledPreBootReceiversFile(); |
| 7331 | FileOutputStream fos = null; |
| 7332 | DataOutputStream dos = null; |
| 7333 | try { |
| 7334 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 7335 | fos = new FileOutputStream(file); |
| 7336 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7337 | dos.writeInt(LAST_DONE_VERSION); |
| 7338 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7339 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7340 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7341 | dos.writeInt(list.size()); |
| 7342 | for (int i=0; i<list.size(); i++) { |
| 7343 | dos.writeUTF(list.get(i).getPackageName()); |
| 7344 | dos.writeUTF(list.get(i).getClassName()); |
| 7345 | } |
| 7346 | } catch (IOException e) { |
| 7347 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 7348 | file.delete(); |
| 7349 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 7350 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7351 | if (dos != null) { |
| 7352 | try { |
| 7353 | dos.close(); |
| 7354 | } catch (IOException e) { |
| 7355 | // TODO Auto-generated catch block |
| 7356 | e.printStackTrace(); |
| 7357 | } |
| 7358 | } |
| 7359 | } |
| 7360 | } |
| 7361 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7362 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7363 | synchronized(this) { |
| 7364 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7365 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7366 | return; |
| 7367 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7368 | |
| 7369 | // Check to see if there are any update receivers to run. |
| 7370 | if (!mDidUpdate) { |
| 7371 | if (mWaitingUpdate) { |
| 7372 | return; |
| 7373 | } |
| 7374 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 7375 | List<ResolveInfo> ris = null; |
| 7376 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7377 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7378 | intent, null, 0, 0); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7379 | } catch (RemoteException e) { |
| 7380 | } |
| 7381 | if (ris != null) { |
| 7382 | for (int i=ris.size()-1; i>=0; i--) { |
| 7383 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 7384 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7385 | ris.remove(i); |
| 7386 | } |
| 7387 | } |
| 7388 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7389 | |
| 7390 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 7391 | |
| 7392 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7393 | for (int i=0; i<ris.size(); i++) { |
| 7394 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7395 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7396 | if (lastDoneReceivers.contains(comp)) { |
| 7397 | ris.remove(i); |
| 7398 | i--; |
| 7399 | } |
| 7400 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 7401 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7402 | for (int i=0; i<ris.size(); i++) { |
| 7403 | ActivityInfo ai = ris.get(i).activityInfo; |
| 7404 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7405 | doneReceivers.add(comp); |
| 7406 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7407 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 7408 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7409 | finisher = new IIntentReceiver.Stub() { |
| 7410 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 7411 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7412 | boolean sticky) { |
| 7413 | // The raw IIntentReceiver interface is called |
| 7414 | // with the AM lock held, so redispatch to |
| 7415 | // execute our code without the lock. |
| 7416 | mHandler.post(new Runnable() { |
| 7417 | public void run() { |
| 7418 | synchronized (ActivityManagerService.this) { |
| 7419 | mDidUpdate = true; |
| 7420 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7421 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7422 | showBootMessage(mContext.getText( |
| 7423 | R.string.android_upgrading_complete), |
| 7424 | false); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7425 | systemReady(goingCallback); |
| 7426 | } |
| 7427 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7428 | } |
| 7429 | }; |
| 7430 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7431 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 7432 | /* TODO: Send this to all users */ |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7433 | broadcastIntentLocked(null, null, intent, null, finisher, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 7434 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
Amith Yamasani | c600e21f | 2012-02-14 16:08:07 -0800 | [diff] [blame] | 7435 | 0 /* UserId zero */); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 7436 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7437 | mWaitingUpdate = true; |
| 7438 | } |
| 7439 | } |
| 7440 | } |
| 7441 | if (mWaitingUpdate) { |
| 7442 | return; |
| 7443 | } |
| 7444 | mDidUpdate = true; |
| 7445 | } |
| 7446 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7447 | mSystemReady = true; |
| 7448 | if (!mStartRunning) { |
| 7449 | return; |
| 7450 | } |
| 7451 | } |
| 7452 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7453 | ArrayList<ProcessRecord> procsToKill = null; |
| 7454 | synchronized(mPidsSelfLocked) { |
| 7455 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7456 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7457 | if (!isAllowedWhileBooting(proc.info)){ |
| 7458 | if (procsToKill == null) { |
| 7459 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7460 | } |
| 7461 | procsToKill.add(proc); |
| 7462 | } |
| 7463 | } |
| 7464 | } |
| 7465 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7466 | synchronized(this) { |
| 7467 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7468 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7469 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7470 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7471 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7472 | } |
| 7473 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7474 | |
| 7475 | // Now that we have cleaned up any update processes, we |
| 7476 | // are ready to start launching real processes and know that |
| 7477 | // we won't trample on them any more. |
| 7478 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7479 | } |
| 7480 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7481 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7482 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7483 | SystemClock.uptimeMillis()); |
| 7484 | |
| 7485 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7486 | // Make sure we have no pre-ready processes sitting around. |
| 7487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7488 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7489 | ResolveInfo ri = mContext.getPackageManager() |
| 7490 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7491 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7492 | CharSequence errorMsg = null; |
| 7493 | if (ri != null) { |
| 7494 | ActivityInfo ai = ri.activityInfo; |
| 7495 | ApplicationInfo app = ai.applicationInfo; |
| 7496 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7497 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7498 | mTopData = null; |
| 7499 | mTopComponent = new ComponentName(app.packageName, |
| 7500 | ai.name); |
| 7501 | } else { |
| 7502 | errorMsg = mContext.getResources().getText( |
| 7503 | com.android.internal.R.string.factorytest_not_system); |
| 7504 | } |
| 7505 | } else { |
| 7506 | errorMsg = mContext.getResources().getText( |
| 7507 | com.android.internal.R.string.factorytest_no_action); |
| 7508 | } |
| 7509 | if (errorMsg != null) { |
| 7510 | mTopAction = null; |
| 7511 | mTopData = null; |
| 7512 | mTopComponent = null; |
| 7513 | Message msg = Message.obtain(); |
| 7514 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7515 | msg.getData().putCharSequence("msg", errorMsg); |
| 7516 | mHandler.sendMessage(msg); |
| 7517 | } |
| 7518 | } |
| 7519 | } |
| 7520 | |
| 7521 | retrieveSettings(); |
| 7522 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7523 | if (goingCallback != null) goingCallback.run(); |
| 7524 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7525 | synchronized (this) { |
| 7526 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7527 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7528 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7529 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7530 | if (apps != null) { |
| 7531 | int N = apps.size(); |
| 7532 | int i; |
| 7533 | for (i=0; i<N; i++) { |
| 7534 | ApplicationInfo info |
| 7535 | = (ApplicationInfo)apps.get(i); |
| 7536 | if (info != null && |
| 7537 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7538 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7539 | } |
| 7540 | } |
| 7541 | } |
| 7542 | } catch (RemoteException ex) { |
| 7543 | // pm is in same process, this will never happen. |
| 7544 | } |
| 7545 | } |
| 7546 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7547 | // Start up initial activity. |
| 7548 | mBooting = true; |
| 7549 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7550 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7551 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7552 | Message msg = Message.obtain(); |
| 7553 | msg.what = SHOW_UID_ERROR_MSG; |
| 7554 | mHandler.sendMessage(msg); |
| 7555 | } |
| 7556 | } catch (RemoteException e) { |
| 7557 | } |
| 7558 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7559 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7560 | } |
| 7561 | } |
| 7562 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7563 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7564 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7565 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7566 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7567 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7568 | startAppProblemLocked(app); |
| 7569 | app.stopFreezingAllLocked(); |
| 7570 | return handleAppCrashLocked(app); |
| 7571 | } |
| 7572 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7573 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7574 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7575 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7576 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7577 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7578 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7579 | startAppProblemLocked(app); |
| 7580 | app.stopFreezingAllLocked(); |
| 7581 | } |
| 7582 | |
| 7583 | /** |
| 7584 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7585 | * |
| 7586 | * @param app The ProcessRecord in which the error occurred. |
| 7587 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7588 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7589 | * @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] | 7590 | * @param shortMsg Short message describing the crash. |
| 7591 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7592 | * @param stackTrace Full crash stack trace, may be null. |
| 7593 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7594 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7595 | */ |
| 7596 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7597 | 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] | 7598 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7599 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7600 | report.condition = condition; |
| 7601 | report.processName = app.processName; |
| 7602 | report.pid = app.pid; |
| 7603 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7604 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7605 | report.shortMsg = shortMsg; |
| 7606 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7607 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7608 | |
| 7609 | return report; |
| 7610 | } |
| 7611 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7612 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7613 | synchronized (this) { |
| 7614 | app.crashing = false; |
| 7615 | app.crashingReport = null; |
| 7616 | app.notResponding = false; |
| 7617 | app.notRespondingReport = null; |
| 7618 | if (app.anrDialog == fromDialog) { |
| 7619 | app.anrDialog = null; |
| 7620 | } |
| 7621 | if (app.waitDialog == fromDialog) { |
| 7622 | app.waitDialog = null; |
| 7623 | } |
| 7624 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7625 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7626 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7627 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 7628 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7629 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7630 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7631 | } |
| 7632 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7633 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7634 | private boolean handleAppCrashLocked(ProcessRecord app) { |
Mike Lockwood | 86548c4 | 2011-09-13 17:21:46 -0400 | [diff] [blame] | 7635 | if (mHeadless) { |
| 7636 | Log.e(TAG, "handleAppCrashLocked: " + app.processName); |
| 7637 | return false; |
| 7638 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7639 | long now = SystemClock.uptimeMillis(); |
| 7640 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7641 | Long crashTime; |
| 7642 | if (!app.isolated) { |
| 7643 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 7644 | } else { |
| 7645 | crashTime = null; |
| 7646 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7647 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7648 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7649 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7650 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7651 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7652 | app.info.processName, app.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7653 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 7654 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7655 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7656 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7657 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7658 | r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "crashed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7659 | } |
| 7660 | } |
| 7661 | if (!app.persistent) { |
| 7662 | // We don't want to start this process again until the user |
| 7663 | // explicitly does so... but for persistent process, we really |
| 7664 | // need to keep it running. If a persistent process is actually |
| 7665 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7666 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7667 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7668 | if (!app.isolated) { |
| 7669 | // XXX We don't have a way to mark isolated processes |
| 7670 | // as bad, since they don't have a peristent identity. |
| 7671 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 7672 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 7673 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7674 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7675 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 7676 | // Don't let services in this process be restarted and potentially |
| 7677 | // annoy the user repeatedly. Unless it is persistent, since those |
| 7678 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7679 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7680 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7681 | return false; |
| 7682 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7683 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7684 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7685 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Kevin Hester-Chow | d87a9be | 2012-03-05 08:01:00 -0800 | [diff] [blame] | 7686 | if (r != null && r.app == app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7687 | // If the top running activity is from this crashing |
| 7688 | // process, then terminate it to avoid getting in a loop. |
| 7689 | Slog.w(TAG, " Force finishing activity " |
| 7690 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 7691 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7692 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7693 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 7694 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7695 | // stopped, to avoid a situation where one will get |
| 7696 | // re-start our crashing activity once it gets resumed again. |
| 7697 | index--; |
| 7698 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7699 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7700 | if (r.state == ActivityState.RESUMED |
| 7701 | || r.state == ActivityState.PAUSING |
| 7702 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 7703 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7704 | Slog.w(TAG, " Force finishing activity " |
| 7705 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7706 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7707 | Activity.RESULT_CANCELED, null, "crashed"); |
| 7708 | } |
| 7709 | } |
| 7710 | } |
| 7711 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7712 | } |
| 7713 | |
| 7714 | // Bump up the crash count of any services currently running in the proc. |
| 7715 | if (app.services.size() != 0) { |
| 7716 | // Any services running in the application need to be placed |
| 7717 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7718 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7719 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7720 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7721 | sr.crashCount++; |
| 7722 | } |
| 7723 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7724 | |
| 7725 | // If the crashing process is what we consider to be the "home process" and it has been |
| 7726 | // replaced by a third-party app, clear the package preferred activities from packages |
| 7727 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 7728 | // from blocking the user to manually clear the list. |
| 7729 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 7730 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7731 | Iterator it = mHomeProcess.activities.iterator(); |
| 7732 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 7733 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7734 | if (r.isHomeActivity) { |
| 7735 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 7736 | try { |
| 7737 | ActivityThread.getPackageManager() |
| 7738 | .clearPackagePreferredActivities(r.packageName); |
| 7739 | } catch (RemoteException c) { |
| 7740 | // pm is in same process, this will never happen. |
| 7741 | } |
| 7742 | } |
| 7743 | } |
| 7744 | } |
| 7745 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7746 | if (!app.isolated) { |
| 7747 | // XXX Can't keep track of crash times for isolated processes, |
| 7748 | // because they don't have a perisistent identity. |
| 7749 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 7750 | } |
| 7751 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7752 | return true; |
| 7753 | } |
| 7754 | |
| 7755 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7756 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 7757 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7758 | skipCurrentReceiverLocked(app); |
| 7759 | } |
| 7760 | |
| 7761 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 7762 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 7763 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7764 | } |
| 7765 | } |
| 7766 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7767 | /** |
| 7768 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 7769 | * The application process will exit immediately after this call returns. |
| 7770 | * @param app object of the crashing app, null for the system server |
| 7771 | * @param crashInfo describing the exception |
| 7772 | */ |
| 7773 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7774 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7775 | final String processName = app == null ? "system_server" |
| 7776 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7777 | |
| 7778 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7779 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7780 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7781 | crashInfo.exceptionClassName, |
| 7782 | crashInfo.exceptionMessage, |
| 7783 | crashInfo.throwFileName, |
| 7784 | crashInfo.throwLineNumber); |
| 7785 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7786 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7787 | |
| 7788 | crashApplication(r, crashInfo); |
| 7789 | } |
| 7790 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7791 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7792 | IBinder app, |
| 7793 | int violationMask, |
| 7794 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7795 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 7796 | if (r == null) { |
| 7797 | return; |
| 7798 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7799 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7800 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 7801 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7802 | boolean logIt = true; |
| 7803 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 7804 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 7805 | logIt = false; |
| 7806 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7807 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7808 | // the relative pain numbers, without logging all |
| 7809 | // the stack traces repeatedly. We'd want to do |
| 7810 | // likewise in the client code, which also does |
| 7811 | // dup suppression, before the Binder call. |
| 7812 | } else { |
| 7813 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 7814 | mAlreadyLoggedViolatedStacks.clear(); |
| 7815 | } |
| 7816 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 7817 | } |
| 7818 | } |
| 7819 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7820 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7821 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7822 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7823 | |
| 7824 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 7825 | AppErrorResult result = new AppErrorResult(); |
| 7826 | synchronized (this) { |
| 7827 | final long origId = Binder.clearCallingIdentity(); |
| 7828 | |
| 7829 | Message msg = Message.obtain(); |
| 7830 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 7831 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 7832 | data.put("result", result); |
| 7833 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7834 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7835 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7836 | msg.obj = data; |
| 7837 | mHandler.sendMessage(msg); |
| 7838 | |
| 7839 | Binder.restoreCallingIdentity(origId); |
| 7840 | } |
| 7841 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 7842 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7843 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7844 | } |
| 7845 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7846 | // Depending on the policy in effect, there could be a bunch of |
| 7847 | // these in quick succession so we try to batch these together to |
| 7848 | // minimize disk writes, number of dropbox entries, and maximize |
| 7849 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7850 | private void logStrictModeViolationToDropBox( |
| 7851 | ProcessRecord process, |
| 7852 | StrictMode.ViolationInfo info) { |
| 7853 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7854 | return; |
| 7855 | } |
| 7856 | final boolean isSystemApp = process == null || |
| 7857 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 7858 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7859 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7860 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 7861 | final DropBoxManager dbox = (DropBoxManager) |
| 7862 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7863 | |
| 7864 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7865 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7866 | |
| 7867 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7868 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7869 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 7870 | synchronized (sb) { |
| 7871 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7872 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7873 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 7874 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7875 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 7876 | if (info.violationNumThisLoop != 0) { |
| 7877 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 7878 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 7879 | if (info.numAnimationsRunning != 0) { |
| 7880 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 7881 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 7882 | if (info.broadcastIntentAction != null) { |
| 7883 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 7884 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7885 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7886 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7887 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 7888 | if (info.numInstances != -1) { |
| 7889 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 7890 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7891 | if (info.tags != null) { |
| 7892 | for (String tag : info.tags) { |
| 7893 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 7894 | } |
| 7895 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7896 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7897 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 7898 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7899 | } |
| 7900 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7901 | |
| 7902 | // Only buffer up to ~64k. Various logging bits truncate |
| 7903 | // things at 128k. |
| 7904 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7905 | } |
| 7906 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7907 | // Flush immediately if the buffer's grown too large, or this |
| 7908 | // is a non-system app. Non-system apps are isolated with a |
| 7909 | // different tag & policy and not batched. |
| 7910 | // |
| 7911 | // Batching is useful during internal testing with |
| 7912 | // StrictMode settings turned up high. Without batching, |
| 7913 | // thousands of separate files could be created on boot. |
| 7914 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7915 | new Thread("Error dump: " + dropboxTag) { |
| 7916 | @Override |
| 7917 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7918 | String report; |
| 7919 | synchronized (sb) { |
| 7920 | report = sb.toString(); |
| 7921 | sb.delete(0, sb.length()); |
| 7922 | sb.trimToSize(); |
| 7923 | } |
| 7924 | if (report.length() != 0) { |
| 7925 | dbox.addText(dropboxTag, report); |
| 7926 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7927 | } |
| 7928 | }.start(); |
| 7929 | return; |
| 7930 | } |
| 7931 | |
| 7932 | // System app batching: |
| 7933 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7934 | // An existing dropbox-writing thread is outstanding, so |
| 7935 | // we don't need to start it up. The existing thread will |
| 7936 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7937 | return; |
| 7938 | } |
| 7939 | |
| 7940 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 7941 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7942 | new Thread("Error dump: " + dropboxTag) { |
| 7943 | @Override |
| 7944 | public void run() { |
| 7945 | // 5 second sleep to let stacks arrive and be batched together |
| 7946 | try { |
| 7947 | Thread.sleep(5000); // 5 seconds |
| 7948 | } catch (InterruptedException e) {} |
| 7949 | |
| 7950 | String errorReport; |
| 7951 | synchronized (mStrictModeBuffer) { |
| 7952 | errorReport = mStrictModeBuffer.toString(); |
| 7953 | if (errorReport.length() == 0) { |
| 7954 | return; |
| 7955 | } |
| 7956 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 7957 | mStrictModeBuffer.trimToSize(); |
| 7958 | } |
| 7959 | dbox.addText(dropboxTag, errorReport); |
| 7960 | } |
| 7961 | }.start(); |
| 7962 | } |
| 7963 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7964 | /** |
| 7965 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 7966 | * @param app object of the crashing app, null for the system server |
| 7967 | * @param tag reported by the caller |
| 7968 | * @param crashInfo describing the context of the error |
| 7969 | * @return true if the process should exit immediately (WTF is fatal) |
| 7970 | */ |
| 7971 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7972 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7973 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7974 | final String processName = app == null ? "system_server" |
| 7975 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7976 | |
| 7977 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7978 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7979 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7980 | tag, crashInfo.exceptionMessage); |
| 7981 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7982 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7983 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 7984 | if (r != null && r.pid != Process.myPid() && |
| 7985 | Settings.Secure.getInt(mContext.getContentResolver(), |
| 7986 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7987 | crashApplication(r, crashInfo); |
| 7988 | return true; |
| 7989 | } else { |
| 7990 | return false; |
| 7991 | } |
| 7992 | } |
| 7993 | |
| 7994 | /** |
| 7995 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 7996 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 7997 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7998 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7999 | if (app == null) { |
| 8000 | return null; |
| 8001 | } |
| 8002 | |
| 8003 | synchronized (this) { |
| 8004 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 8005 | final int NA = apps.size(); |
| 8006 | for (int ia=0; ia<NA; ia++) { |
| 8007 | ProcessRecord p = apps.valueAt(ia); |
| 8008 | if (p.thread != null && p.thread.asBinder() == app) { |
| 8009 | return p; |
| 8010 | } |
| 8011 | } |
| 8012 | } |
| 8013 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8014 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 8015 | + " from pid=" + Binder.getCallingPid() |
| 8016 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8017 | return null; |
| 8018 | } |
| 8019 | } |
| 8020 | |
| 8021 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8022 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 8023 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8024 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8025 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 8026 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8027 | // Watchdog thread ends up invoking this function (with |
| 8028 | // a null ProcessRecord) to add the stack file to dropbox. |
| 8029 | // Do not acquire a lock on this (am) in such cases, as it |
| 8030 | // could cause a potential deadlock, if and when watchdog |
| 8031 | // is invoked due to unavailability of lock on am and it |
| 8032 | // would prevent watchdog from killing system_server. |
| 8033 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8034 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8035 | return; |
| 8036 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 8037 | // Note: ProcessRecord 'process' is guarded by the service |
| 8038 | // instance. (notably process.pkgList, which could otherwise change |
| 8039 | // concurrently during execution of this method) |
| 8040 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8041 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8042 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8043 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8044 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 8045 | for (String pkg : process.pkgList) { |
| 8046 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8047 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8048 | PackageInfo pi = pm.getPackageInfo(pkg, 0, 0); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8049 | if (pi != null) { |
| 8050 | sb.append(" v").append(pi.versionCode); |
| 8051 | if (pi.versionName != null) { |
| 8052 | sb.append(" (").append(pi.versionName).append(")"); |
| 8053 | } |
| 8054 | } |
| 8055 | } catch (RemoteException e) { |
| 8056 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8057 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8058 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8059 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8060 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8061 | } |
| 8062 | |
| 8063 | private static String processClass(ProcessRecord process) { |
| 8064 | if (process == null || process.pid == MY_PID) { |
| 8065 | return "system_server"; |
| 8066 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 8067 | return "system_app"; |
| 8068 | } else { |
| 8069 | return "data_app"; |
| 8070 | } |
| 8071 | } |
| 8072 | |
| 8073 | /** |
| 8074 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 8075 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 8076 | * @param process which caused the error, null means the system server |
| 8077 | * @param activity which triggered the error, null if unknown |
| 8078 | * @param parent activity related to the error, null if unknown |
| 8079 | * @param subject line related to the error, null if absent |
| 8080 | * @param report in long form describing the error, null if absent |
| 8081 | * @param logFile to include in the report, null if none |
| 8082 | * @param crashInfo giving an application stack trace, null if absent |
| 8083 | */ |
| 8084 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8085 | ProcessRecord process, String processName, ActivityRecord activity, |
| 8086 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8087 | final String report, final File logFile, |
| 8088 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 8089 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 8090 | // otherwise the watchdog may be prevented from resetting the system. |
| 8091 | |
| 8092 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 8093 | final DropBoxManager dbox = (DropBoxManager) |
| 8094 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8095 | |
| 8096 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8097 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8098 | |
| 8099 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8100 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8101 | if (activity != null) { |
| 8102 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 8103 | } |
| 8104 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 8105 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 8106 | } |
| 8107 | if (parent != null && parent != activity) { |
| 8108 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 8109 | } |
| 8110 | if (subject != null) { |
| 8111 | sb.append("Subject: ").append(subject).append("\n"); |
| 8112 | } |
| 8113 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 8114 | if (Debug.isDebuggerConnected()) { |
| 8115 | sb.append("Debugger: Connected\n"); |
| 8116 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8117 | sb.append("\n"); |
| 8118 | |
| 8119 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 8120 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8121 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 8122 | @Override |
| 8123 | public void run() { |
| 8124 | if (report != null) { |
| 8125 | sb.append(report); |
| 8126 | } |
| 8127 | if (logFile != null) { |
| 8128 | try { |
| 8129 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 8130 | } catch (IOException e) { |
| 8131 | Slog.e(TAG, "Error reading " + logFile, e); |
| 8132 | } |
| 8133 | } |
| 8134 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 8135 | sb.append(crashInfo.stackTrace); |
| 8136 | } |
| 8137 | |
| 8138 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 8139 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 8140 | if (lines > 0) { |
| 8141 | sb.append("\n"); |
| 8142 | |
| 8143 | // Merge several logcat streams, and take the last N lines |
| 8144 | InputStreamReader input = null; |
| 8145 | try { |
| 8146 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 8147 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 8148 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 8149 | |
| 8150 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 8151 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 8152 | input = new InputStreamReader(logcat.getInputStream()); |
| 8153 | |
| 8154 | int num; |
| 8155 | char[] buf = new char[8192]; |
| 8156 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 8157 | } catch (IOException e) { |
| 8158 | Slog.e(TAG, "Error running logcat", e); |
| 8159 | } finally { |
| 8160 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 8161 | } |
| 8162 | } |
| 8163 | |
| 8164 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8165 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8166 | }; |
| 8167 | |
Dianne Hackborn | 56385cc | 2012-04-30 15:07:47 -0700 | [diff] [blame] | 8168 | if (process == null) { |
| 8169 | // If process is null, we are being called from some internal code |
| 8170 | // and may be about to die -- run this synchronously. |
| 8171 | worker.run(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8172 | } else { |
| 8173 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8174 | } |
| 8175 | } |
| 8176 | |
| 8177 | /** |
| 8178 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 8179 | * Deal with edge cases (intercepts from instrumented applications, |
| 8180 | * ActivityController, error intent receivers, that sort of thing). |
| 8181 | * @param r the application crashing |
| 8182 | * @param crashInfo describing the failure |
| 8183 | */ |
| 8184 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8185 | long timeMillis = System.currentTimeMillis(); |
| 8186 | String shortMsg = crashInfo.exceptionClassName; |
| 8187 | String longMsg = crashInfo.exceptionMessage; |
| 8188 | String stackTrace = crashInfo.stackTrace; |
| 8189 | if (shortMsg != null && longMsg != null) { |
| 8190 | longMsg = shortMsg + ": " + longMsg; |
| 8191 | } else if (shortMsg != null) { |
| 8192 | longMsg = shortMsg; |
| 8193 | } |
| 8194 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8195 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8196 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8197 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8198 | try { |
| 8199 | String name = r != null ? r.processName : null; |
| 8200 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8201 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8202 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8203 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8204 | + " at watcher's request"); |
| 8205 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8206 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8207 | } |
| 8208 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8209 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8210 | } |
| 8211 | } |
| 8212 | |
| 8213 | final long origId = Binder.clearCallingIdentity(); |
| 8214 | |
| 8215 | // If this process is running instrumentation, finish it. |
| 8216 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8217 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8218 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8219 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 8220 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8221 | Bundle info = new Bundle(); |
| 8222 | info.putString("shortMsg", shortMsg); |
| 8223 | info.putString("longMsg", longMsg); |
| 8224 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 8225 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8226 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8227 | } |
| 8228 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8229 | // If we can't identify the process or it's already exceeded its crash quota, |
| 8230 | // quit right away without showing a crash dialog. |
| 8231 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8232 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8233 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8234 | } |
| 8235 | |
| 8236 | Message msg = Message.obtain(); |
| 8237 | msg.what = SHOW_ERROR_MSG; |
| 8238 | HashMap data = new HashMap(); |
| 8239 | data.put("result", result); |
| 8240 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8241 | msg.obj = data; |
| 8242 | mHandler.sendMessage(msg); |
| 8243 | |
| 8244 | Binder.restoreCallingIdentity(origId); |
| 8245 | } |
| 8246 | |
| 8247 | int res = result.get(); |
| 8248 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8249 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8250 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8251 | if (r != null && !r.isolated) { |
| 8252 | // XXX Can't keep track of crash time for isolated processes, |
| 8253 | // since they don't have a persistent identity. |
| 8254 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8255 | SystemClock.uptimeMillis()); |
| 8256 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8257 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8258 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8259 | } |
| 8260 | } |
| 8261 | |
| 8262 | if (appErrorIntent != null) { |
| 8263 | try { |
| 8264 | mContext.startActivity(appErrorIntent); |
| 8265 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8266 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8267 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8268 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8269 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8270 | |
| 8271 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 8272 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 8273 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8274 | if (report == null) { |
| 8275 | return null; |
| 8276 | } |
| 8277 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 8278 | result.setComponent(r.errorReportReceiver); |
| 8279 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 8280 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 8281 | return result; |
| 8282 | } |
| 8283 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8284 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 8285 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8286 | if (r.errorReportReceiver == null) { |
| 8287 | return null; |
| 8288 | } |
| 8289 | |
| 8290 | if (!r.crashing && !r.notResponding) { |
| 8291 | return null; |
| 8292 | } |
| 8293 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8294 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 8295 | report.packageName = r.info.packageName; |
| 8296 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 8297 | report.processName = r.processName; |
| 8298 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 8299 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8300 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8301 | if (r.crashing) { |
| 8302 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 8303 | report.crashInfo = crashInfo; |
| 8304 | } else if (r.notResponding) { |
| 8305 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 8306 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8307 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8308 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 8309 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 8310 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8311 | } |
| 8312 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8313 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8314 | } |
| 8315 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8316 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8317 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8318 | // assume our apps are happy - lazy create the list |
| 8319 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 8320 | |
| 8321 | synchronized (this) { |
| 8322 | |
| 8323 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8324 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8325 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8326 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 8327 | // This one's in trouble, so we'll generate a report for it |
| 8328 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 8329 | ActivityManager.ProcessErrorStateInfo report = null; |
| 8330 | if (app.crashing) { |
| 8331 | report = app.crashingReport; |
| 8332 | } else if (app.notResponding) { |
| 8333 | report = app.notRespondingReport; |
| 8334 | } |
| 8335 | |
| 8336 | if (report != null) { |
| 8337 | if (errList == null) { |
| 8338 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 8339 | } |
| 8340 | errList.add(report); |
| 8341 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8342 | 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] | 8343 | " crashing = " + app.crashing + |
| 8344 | " notResponding = " + app.notResponding); |
| 8345 | } |
| 8346 | } |
| 8347 | } |
| 8348 | } |
| 8349 | |
| 8350 | return errList; |
| 8351 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8352 | |
| 8353 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8354 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8355 | if (currApp != null) { |
| 8356 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 8357 | } |
| 8358 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8359 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 8360 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8361 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 8362 | if (currApp != null) { |
| 8363 | currApp.lru = 0; |
| 8364 | } |
| 8365 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8366 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8367 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 8368 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8369 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 8370 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8371 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 8372 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8373 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 8374 | } else { |
| 8375 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 8376 | } |
| 8377 | } |
| 8378 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8379 | private void fillInProcMemInfo(ProcessRecord app, |
| 8380 | ActivityManager.RunningAppProcessInfo outInfo) { |
| 8381 | outInfo.pid = app.pid; |
| 8382 | outInfo.uid = app.info.uid; |
| 8383 | if (mHeavyWeightProcess == app) { |
| 8384 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
| 8385 | } |
| 8386 | if (app.persistent) { |
| 8387 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 8388 | } |
| 8389 | outInfo.lastTrimLevel = app.trimMemoryLevel; |
| 8390 | int adj = app.curAdj; |
| 8391 | outInfo.importance = oomAdjToImportance(adj, outInfo); |
| 8392 | outInfo.importanceReasonCode = app.adjTypeCode; |
| 8393 | } |
| 8394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8395 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8396 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8397 | // Lazy instantiation of list |
| 8398 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
| 8399 | synchronized (this) { |
| 8400 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8401 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8402 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8403 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 8404 | // Generate process state info for running application |
| 8405 | ActivityManager.RunningAppProcessInfo currApp = |
| 8406 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 8407 | app.pid, app.getPackageList()); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8408 | fillInProcMemInfo(app, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8409 | if (app.adjSource instanceof ProcessRecord) { |
| 8410 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8411 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 8412 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8413 | } else if (app.adjSource instanceof ActivityRecord) { |
| 8414 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8415 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 8416 | } |
| 8417 | if (app.adjTarget instanceof ComponentName) { |
| 8418 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 8419 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8420 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8421 | // + " lru=" + currApp.lru); |
| 8422 | if (runList == null) { |
| 8423 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 8424 | } |
| 8425 | runList.add(currApp); |
| 8426 | } |
| 8427 | } |
| 8428 | } |
| 8429 | return runList; |
| 8430 | } |
| 8431 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8432 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8433 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8434 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8435 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8436 | if (runningApps != null && runningApps.size() > 0) { |
| 8437 | Set<String> extList = new HashSet<String>(); |
| 8438 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8439 | if (app.pkgList != null) { |
| 8440 | for (String pkg : app.pkgList) { |
| 8441 | extList.add(pkg); |
| 8442 | } |
| 8443 | } |
| 8444 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8445 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8446 | for (String pkg : extList) { |
| 8447 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8448 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserId.getCallingUserId()); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8449 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8450 | retList.add(info); |
| 8451 | } |
| 8452 | } catch (RemoteException e) { |
| 8453 | } |
| 8454 | } |
| 8455 | } |
| 8456 | return retList; |
| 8457 | } |
| 8458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8459 | @Override |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8460 | public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) { |
| 8461 | enforceNotIsolatedCaller("getMyMemoryState"); |
| 8462 | synchronized (this) { |
| 8463 | ProcessRecord proc; |
| 8464 | synchronized (mPidsSelfLocked) { |
| 8465 | proc = mPidsSelfLocked.get(Binder.getCallingPid()); |
| 8466 | } |
| 8467 | fillInProcMemInfo(proc, outInfo); |
| 8468 | } |
| 8469 | } |
| 8470 | |
| 8471 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8472 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8473 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 8474 | != PackageManager.PERMISSION_GRANTED) { |
| 8475 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8476 | + Binder.getCallingPid() |
| 8477 | + ", uid=" + Binder.getCallingUid() |
| 8478 | + " without permission " |
| 8479 | + android.Manifest.permission.DUMP); |
| 8480 | return; |
| 8481 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8482 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8483 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8484 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8485 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8486 | |
| 8487 | int opti = 0; |
| 8488 | while (opti < args.length) { |
| 8489 | String opt = args[opti]; |
| 8490 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8491 | break; |
| 8492 | } |
| 8493 | opti++; |
| 8494 | if ("-a".equals(opt)) { |
| 8495 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8496 | } else if ("-c".equals(opt)) { |
| 8497 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8498 | } else if ("-h".equals(opt)) { |
| 8499 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8500 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8501 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8502 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8503 | pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state"); |
| 8504 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 8505 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8506 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8507 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8508 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8509 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8510 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8511 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8512 | pw.println(" all: dump all activities"); |
| 8513 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8514 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8515 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 8516 | pw.println(" a partial substring in a component name, a"); |
| 8517 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8518 | pw.println(" -a: include all available server state."); |
| 8519 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8520 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8521 | } else { |
| 8522 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8523 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8524 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8525 | |
| 8526 | long origId = Binder.clearCallingIdentity(); |
| 8527 | boolean more = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8528 | // Is the caller requesting to dump a particular piece of data? |
| 8529 | if (opti < args.length) { |
| 8530 | String cmd = args[opti]; |
| 8531 | opti++; |
| 8532 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 8533 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8534 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8535 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8536 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8537 | String[] newArgs; |
| 8538 | String name; |
| 8539 | if (opti >= args.length) { |
| 8540 | name = null; |
| 8541 | newArgs = EMPTY_STRING_ARRAY; |
| 8542 | } else { |
| 8543 | name = args[opti]; |
| 8544 | opti++; |
| 8545 | newArgs = new String[args.length - opti]; |
| 8546 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8547 | args.length - opti); |
| 8548 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8549 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8550 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8551 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8552 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8553 | String[] newArgs; |
| 8554 | String name; |
| 8555 | if (opti >= args.length) { |
| 8556 | name = null; |
| 8557 | newArgs = EMPTY_STRING_ARRAY; |
| 8558 | } else { |
| 8559 | name = args[opti]; |
| 8560 | opti++; |
| 8561 | newArgs = new String[args.length - opti]; |
| 8562 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8563 | args.length - opti); |
| 8564 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8565 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8566 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8567 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8568 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8569 | String[] newArgs; |
| 8570 | String name; |
| 8571 | if (opti >= args.length) { |
| 8572 | name = null; |
| 8573 | newArgs = EMPTY_STRING_ARRAY; |
| 8574 | } else { |
| 8575 | name = args[opti]; |
| 8576 | opti++; |
| 8577 | newArgs = new String[args.length - opti]; |
| 8578 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8579 | args.length - opti); |
| 8580 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8581 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8582 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8583 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8584 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8585 | synchronized (this) { |
| 8586 | dumpOomLocked(fd, pw, args, opti, true); |
| 8587 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8588 | } else if ("provider".equals(cmd)) { |
| 8589 | String[] newArgs; |
| 8590 | String name; |
| 8591 | if (opti >= args.length) { |
| 8592 | name = null; |
| 8593 | newArgs = EMPTY_STRING_ARRAY; |
| 8594 | } else { |
| 8595 | name = args[opti]; |
| 8596 | opti++; |
| 8597 | newArgs = new String[args.length - opti]; |
| 8598 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8599 | } |
| 8600 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8601 | pw.println("No providers match: " + name); |
| 8602 | pw.println("Use -h for help."); |
| 8603 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8604 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8605 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8606 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8607 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8608 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8609 | String[] newArgs; |
| 8610 | String name; |
| 8611 | if (opti >= args.length) { |
| 8612 | name = null; |
| 8613 | newArgs = EMPTY_STRING_ARRAY; |
| 8614 | } else { |
| 8615 | name = args[opti]; |
| 8616 | opti++; |
| 8617 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8618 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8619 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8620 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8621 | if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8622 | pw.println("No services match: " + name); |
| 8623 | pw.println("Use -h for help."); |
| 8624 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8625 | } else if ("package".equals(cmd)) { |
| 8626 | String[] newArgs; |
| 8627 | if (opti >= args.length) { |
| 8628 | pw.println("package: no package name specified"); |
| 8629 | pw.println("Use -h for help."); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8630 | } else { |
| 8631 | dumpPackage = args[opti]; |
| 8632 | opti++; |
| 8633 | newArgs = new String[args.length - opti]; |
| 8634 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8635 | args.length - opti); |
| 8636 | args = newArgs; |
| 8637 | opti = 0; |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8638 | more = true; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8639 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8640 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 8641 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8642 | mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8643 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8644 | } else { |
| 8645 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8646 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 8647 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 8648 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8649 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8650 | } |
| 8651 | if (!more) { |
| 8652 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8653 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8654 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8655 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8656 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8657 | // No piece of data specified, dump everything. |
| 8658 | synchronized (this) { |
| 8659 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8660 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8661 | if (needSep) { |
| 8662 | pw.println(" "); |
| 8663 | } |
| 8664 | if (dumpAll) { |
| 8665 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8666 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8667 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8668 | if (needSep) { |
| 8669 | pw.println(" "); |
| 8670 | } |
| 8671 | if (dumpAll) { |
| 8672 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8673 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8674 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8675 | if (needSep) { |
| 8676 | pw.println(" "); |
| 8677 | } |
| 8678 | if (dumpAll) { |
| 8679 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8680 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8681 | needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8682 | if (needSep) { |
| 8683 | pw.println(" "); |
| 8684 | } |
| 8685 | if (dumpAll) { |
| 8686 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8687 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8688 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8689 | if (needSep) { |
| 8690 | pw.println(" "); |
| 8691 | } |
| 8692 | if (dumpAll) { |
| 8693 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8694 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8695 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8696 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8697 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8698 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8699 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8700 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8701 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8702 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 8703 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8704 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 8705 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8706 | pw.println(" "); |
| 8707 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8708 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 8709 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8710 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8711 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8712 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8713 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8714 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8715 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8716 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8717 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8718 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8719 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8720 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8721 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8722 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 8723 | pw.println(" "); |
| 8724 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8725 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8726 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8727 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8728 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8729 | pw.println(" "); |
| 8730 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8731 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8732 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8733 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8734 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8735 | pw.println(" "); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 8736 | if (mMainStack.mPausingActivity != null) { |
| 8737 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8738 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8739 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8740 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8741 | if (dumpAll) { |
| 8742 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 8743 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8744 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 8745 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8746 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8747 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8748 | if (mRecentTasks.size() > 0) { |
| 8749 | pw.println(); |
| 8750 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8751 | |
| 8752 | final int N = mRecentTasks.size(); |
| 8753 | for (int i=0; i<N; i++) { |
| 8754 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8755 | if (dumpPackage != null) { |
| 8756 | if (tr.realActivity == null || |
| 8757 | !dumpPackage.equals(tr.realActivity)) { |
| 8758 | continue; |
| 8759 | } |
| 8760 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8761 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 8762 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8763 | if (dumpAll) { |
| 8764 | mRecentTasks.get(i).dump(pw, " "); |
| 8765 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8766 | } |
| 8767 | } |
| 8768 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8769 | if (dumpAll) { |
| 8770 | pw.println(" "); |
| 8771 | pw.println(" mCurTask: " + mCurTask); |
| 8772 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8773 | |
| 8774 | return true; |
| 8775 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8776 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8777 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8778 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8779 | boolean needSep = false; |
| 8780 | int numPers = 0; |
| 8781 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8782 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 8783 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8784 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8785 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 8786 | final int NA = procs.size(); |
| 8787 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8788 | ProcessRecord r = procs.valueAt(ia); |
| 8789 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8790 | continue; |
| 8791 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8792 | if (!needSep) { |
| 8793 | pw.println(" All known processes:"); |
| 8794 | needSep = true; |
| 8795 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8796 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 8797 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 8798 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8799 | r.dump(pw, " "); |
| 8800 | if (r.persistent) { |
| 8801 | numPers++; |
| 8802 | } |
| 8803 | } |
| 8804 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8805 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8806 | |
| 8807 | if (mIsolatedProcesses.size() > 0) { |
| 8808 | if (needSep) pw.println(" "); |
| 8809 | needSep = true; |
| 8810 | pw.println(" Isolated process list (sorted by uid):"); |
| 8811 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 8812 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 8813 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8814 | continue; |
| 8815 | } |
| 8816 | pw.println(String.format("%sIsolated #%2d: %s", |
| 8817 | " ", i, r.toString())); |
| 8818 | } |
| 8819 | } |
| 8820 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8821 | if (mLruProcesses.size() > 0) { |
| 8822 | if (needSep) pw.println(" "); |
| 8823 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8824 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8825 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8826 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8827 | needSep = true; |
| 8828 | } |
| 8829 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8830 | if (dumpAll) { |
| 8831 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8832 | boolean printed = false; |
| 8833 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 8834 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 8835 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8836 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8837 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8838 | if (!printed) { |
| 8839 | if (needSep) pw.println(" "); |
| 8840 | needSep = true; |
| 8841 | pw.println(" PID mappings:"); |
| 8842 | printed = true; |
| 8843 | } |
| 8844 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 8845 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8846 | } |
| 8847 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8848 | } |
| 8849 | |
| 8850 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8851 | synchronized (mPidsSelfLocked) { |
| 8852 | boolean printed = false; |
| 8853 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 8854 | ProcessRecord r = mPidsSelfLocked.get( |
| 8855 | mForegroundProcesses.valueAt(i).pid); |
| 8856 | if (dumpPackage != null && (r == null |
| 8857 | || !dumpPackage.equals(r.info.packageName))) { |
| 8858 | continue; |
| 8859 | } |
| 8860 | if (!printed) { |
| 8861 | if (needSep) pw.println(" "); |
| 8862 | needSep = true; |
| 8863 | pw.println(" Foreground Processes:"); |
| 8864 | printed = true; |
| 8865 | } |
| 8866 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 8867 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 8868 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8869 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8870 | } |
| 8871 | |
| 8872 | if (mPersistentStartingProcesses.size() > 0) { |
| 8873 | if (needSep) pw.println(" "); |
| 8874 | needSep = true; |
| 8875 | pw.println(" Persisent processes that are starting:"); |
| 8876 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8877 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8878 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8879 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8880 | if (mRemovedProcesses.size() > 0) { |
| 8881 | if (needSep) pw.println(" "); |
| 8882 | needSep = true; |
| 8883 | pw.println(" Processes that are being removed:"); |
| 8884 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8885 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8886 | } |
| 8887 | |
| 8888 | if (mProcessesOnHold.size() > 0) { |
| 8889 | if (needSep) pw.println(" "); |
| 8890 | needSep = true; |
| 8891 | pw.println(" Processes that are on old until the system is ready:"); |
| 8892 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8893 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8894 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8895 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8896 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8897 | |
| 8898 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8899 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8900 | long now = SystemClock.uptimeMillis(); |
| 8901 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8902 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8903 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8904 | SparseArray<Long> uids = procs.getValue(); |
| 8905 | final int N = uids.size(); |
| 8906 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8907 | int puid = uids.keyAt(i); |
| 8908 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8909 | if (dumpPackage != null && (r == null |
| 8910 | || !dumpPackage.equals(r.info.packageName))) { |
| 8911 | continue; |
| 8912 | } |
| 8913 | if (!printed) { |
| 8914 | if (needSep) pw.println(" "); |
| 8915 | needSep = true; |
| 8916 | pw.println(" Time since processes crashed:"); |
| 8917 | printed = true; |
| 8918 | } |
| 8919 | pw.print(" Process "); pw.print(pname); |
| 8920 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8921 | pw.print(": last crashed "); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8922 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 8923 | pw.println(" ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8924 | } |
| 8925 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8926 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8927 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8928 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8929 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8930 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8931 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8932 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8933 | SparseArray<Long> uids = procs.getValue(); |
| 8934 | final int N = uids.size(); |
| 8935 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8936 | int puid = uids.keyAt(i); |
| 8937 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8938 | if (dumpPackage != null && (r == null |
| 8939 | || !dumpPackage.equals(r.info.packageName))) { |
| 8940 | continue; |
| 8941 | } |
| 8942 | if (!printed) { |
| 8943 | if (needSep) pw.println(" "); |
| 8944 | needSep = true; |
| 8945 | pw.println(" Bad processes:"); |
| 8946 | } |
| 8947 | pw.print(" Bad process "); pw.print(pname); |
| 8948 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8949 | pw.print(": crashed at time "); |
| 8950 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8951 | } |
| 8952 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8953 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8954 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8955 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8956 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8957 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 8958 | if (dumpAll) { |
| 8959 | StringBuilder sb = new StringBuilder(128); |
| 8960 | sb.append(" mPreviousProcessVisibleTime: "); |
| 8961 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 8962 | pw.println(sb); |
| 8963 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8964 | if (mHeavyWeightProcess != null) { |
| 8965 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8966 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8967 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8968 | if (dumpAll) { |
| 8969 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 8970 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8971 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8972 | for (Map.Entry<String, Integer> entry |
| 8973 | : mCompatModePackages.getPackages().entrySet()) { |
| 8974 | String pkg = entry.getKey(); |
| 8975 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8976 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 8977 | continue; |
| 8978 | } |
| 8979 | if (!printed) { |
| 8980 | pw.println(" mScreenCompatPackages:"); |
| 8981 | printed = true; |
| 8982 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8983 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 8984 | pw.print(mode); pw.println(); |
| 8985 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 8986 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8987 | } |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 8988 | if (mSleeping || mWentToSleep || mLockScreenShown) { |
| 8989 | pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep |
| 8990 | + " mLockScreenShown " + mLockScreenShown); |
| 8991 | } |
| 8992 | if (mShuttingDown) { |
| 8993 | pw.println(" mShuttingDown=" + mShuttingDown); |
| 8994 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8995 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 8996 | || mOrigWaitForDebugger) { |
| 8997 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 8998 | + " mDebugTransient=" + mDebugTransient |
| 8999 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 9000 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 9001 | if (mOpenGlTraceApp != null) { |
| 9002 | pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp); |
| 9003 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 9004 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 9005 | || mProfileFd != null) { |
| 9006 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 9007 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 9008 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 9009 | + mAutoStopProfiler); |
| 9010 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9011 | if (mAlwaysFinishActivities || mController != null) { |
| 9012 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 9013 | + " mController=" + mController); |
| 9014 | } |
| 9015 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9016 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9017 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9018 | + " mProcessesReady=" + mProcessesReady |
| 9019 | + " mSystemReady=" + mSystemReady); |
| 9020 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9021 | + " mBooted=" + mBooted |
| 9022 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9023 | pw.print(" mLastPowerCheckRealtime="); |
| 9024 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 9025 | pw.println(""); |
| 9026 | pw.print(" mLastPowerCheckUptime="); |
| 9027 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 9028 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9029 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 9030 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 9031 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9032 | pw.println(" mNumServiceProcs=" + mNumServiceProcs |
| 9033 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9034 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9035 | |
| 9036 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9037 | } |
| 9038 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9039 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9040 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9041 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9042 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9043 | long now = SystemClock.uptimeMillis(); |
| 9044 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 9045 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9046 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 9047 | continue; |
| 9048 | } |
| 9049 | if (!printed) { |
| 9050 | if (needSep) pw.println(" "); |
| 9051 | needSep = true; |
| 9052 | pw.println(" Processes that are waiting to GC:"); |
| 9053 | printed = true; |
| 9054 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9055 | pw.print(" Process "); pw.println(proc); |
| 9056 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 9057 | pw.print(", last gced="); |
| 9058 | pw.print(now-proc.lastRequestedGc); |
| 9059 | pw.print(" ms ago, last lowMem="); |
| 9060 | pw.print(now-proc.lastLowMemory); |
| 9061 | pw.println(" ms ago"); |
| 9062 | |
| 9063 | } |
| 9064 | } |
| 9065 | return needSep; |
| 9066 | } |
| 9067 | |
| 9068 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 9069 | int opti, boolean dumpAll) { |
| 9070 | boolean needSep = false; |
| 9071 | |
| 9072 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9073 | if (needSep) pw.println(" "); |
| 9074 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9075 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9076 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9077 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9078 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 9079 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 9080 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9081 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9082 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9083 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9084 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9085 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9086 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9087 | 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] | 9088 | 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] | 9089 | |
| 9090 | if (needSep) pw.println(" "); |
| 9091 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9092 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9093 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9094 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9095 | needSep = true; |
| 9096 | } |
| 9097 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9098 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9099 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9100 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9101 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9102 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9103 | if (mHeavyWeightProcess != null) { |
| 9104 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9105 | } |
| 9106 | |
| 9107 | return true; |
| 9108 | } |
| 9109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9110 | /** |
| 9111 | * There are three ways to call this: |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9112 | * - no provider specified: dump all the providers |
| 9113 | * - a flattened component name that matched an existing provider was specified as the |
| 9114 | * first arg: dump that one provider |
| 9115 | * - the first arg isn't the flattened component name of an existing provider: |
| 9116 | * dump all providers whose component contains the first arg as a substring |
| 9117 | */ |
| 9118 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9119 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 9120 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9121 | } |
| 9122 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9123 | static class ItemMatcher { |
| 9124 | ArrayList<ComponentName> components; |
| 9125 | ArrayList<String> strings; |
| 9126 | ArrayList<Integer> objects; |
| 9127 | boolean all; |
| 9128 | |
| 9129 | ItemMatcher() { |
| 9130 | all = true; |
| 9131 | } |
| 9132 | |
| 9133 | void build(String name) { |
| 9134 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 9135 | if (componentName != null) { |
| 9136 | if (components == null) { |
| 9137 | components = new ArrayList<ComponentName>(); |
| 9138 | } |
| 9139 | components.add(componentName); |
| 9140 | all = false; |
| 9141 | } else { |
| 9142 | int objectId = 0; |
| 9143 | // Not a '/' separated full component name; maybe an object ID? |
| 9144 | try { |
| 9145 | objectId = Integer.parseInt(name, 16); |
| 9146 | if (objects == null) { |
| 9147 | objects = new ArrayList<Integer>(); |
| 9148 | } |
| 9149 | objects.add(objectId); |
| 9150 | all = false; |
| 9151 | } catch (RuntimeException e) { |
| 9152 | // Not an integer; just do string match. |
| 9153 | if (strings == null) { |
| 9154 | strings = new ArrayList<String>(); |
| 9155 | } |
| 9156 | strings.add(name); |
| 9157 | all = false; |
| 9158 | } |
| 9159 | } |
| 9160 | } |
| 9161 | |
| 9162 | int build(String[] args, int opti) { |
| 9163 | for (; opti<args.length; opti++) { |
| 9164 | String name = args[opti]; |
| 9165 | if ("--".equals(name)) { |
| 9166 | return opti+1; |
| 9167 | } |
| 9168 | build(name); |
| 9169 | } |
| 9170 | return opti; |
| 9171 | } |
| 9172 | |
| 9173 | boolean match(Object object, ComponentName comp) { |
| 9174 | if (all) { |
| 9175 | return true; |
| 9176 | } |
| 9177 | if (components != null) { |
| 9178 | for (int i=0; i<components.size(); i++) { |
| 9179 | if (components.get(i).equals(comp)) { |
| 9180 | return true; |
| 9181 | } |
| 9182 | } |
| 9183 | } |
| 9184 | if (objects != null) { |
| 9185 | for (int i=0; i<objects.size(); i++) { |
| 9186 | if (System.identityHashCode(object) == objects.get(i)) { |
| 9187 | return true; |
| 9188 | } |
| 9189 | } |
| 9190 | } |
| 9191 | if (strings != null) { |
| 9192 | String flat = comp.flattenToString(); |
| 9193 | for (int i=0; i<strings.size(); i++) { |
| 9194 | if (flat.contains(strings.get(i))) { |
| 9195 | return true; |
| 9196 | } |
| 9197 | } |
| 9198 | } |
| 9199 | return false; |
| 9200 | } |
| 9201 | } |
| 9202 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9203 | /** |
| 9204 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9205 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9206 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 9207 | * dump all activity whose component contains the cmd as a substring |
| 9208 | * - A hex number of the ActivityRecord object instance. |
| 9209 | */ |
| 9210 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9211 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9212 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9213 | |
| 9214 | if ("all".equals(name)) { |
| 9215 | synchronized (this) { |
| 9216 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9217 | activities.add(r1); |
| 9218 | } |
| 9219 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 9220 | } else if ("top".equals(name)) { |
| 9221 | synchronized (this) { |
| 9222 | final int N = mMainStack.mHistory.size(); |
| 9223 | if (N > 0) { |
| 9224 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 9225 | } |
| 9226 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9227 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9228 | ItemMatcher matcher = new ItemMatcher(); |
| 9229 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9230 | |
| 9231 | synchronized (this) { |
| 9232 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9233 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9234 | activities.add(r1); |
| 9235 | } |
| 9236 | } |
| 9237 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9238 | } |
| 9239 | |
| 9240 | if (activities.size() <= 0) { |
| 9241 | return false; |
| 9242 | } |
| 9243 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9244 | String[] newArgs = new String[args.length - opti]; |
| 9245 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9246 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9247 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9248 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9249 | for (int i=activities.size()-1; i>=0; i--) { |
| 9250 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9251 | if (needSep) { |
| 9252 | pw.println(); |
| 9253 | } |
| 9254 | needSep = true; |
| 9255 | synchronized (this) { |
| 9256 | if (lastTask != r.task) { |
| 9257 | lastTask = r.task; |
| 9258 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 9259 | pw.print(" id="); pw.println(lastTask.taskId); |
| 9260 | if (dumpAll) { |
| 9261 | lastTask.dump(pw, " "); |
| 9262 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9263 | } |
| 9264 | } |
| 9265 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9266 | } |
| 9267 | return true; |
| 9268 | } |
| 9269 | |
| 9270 | /** |
| 9271 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 9272 | * there is a thread associated with the activity. |
| 9273 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9274 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9275 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 9276 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9277 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9278 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 9279 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9280 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9281 | if (r.app != null) pw.println(r.app.pid); |
| 9282 | else pw.println("(not running)"); |
| 9283 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9284 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9285 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9286 | } |
| 9287 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9288 | // flush anything that is already in the PrintWriter since the thread is going |
| 9289 | // to write to the file descriptor directly |
| 9290 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9291 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9292 | TransferPipe tp = new TransferPipe(); |
| 9293 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9294 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9295 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9296 | tp.go(fd); |
| 9297 | } finally { |
| 9298 | tp.kill(); |
| 9299 | } |
| 9300 | } catch (IOException e) { |
| 9301 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9302 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9303 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9304 | } |
| 9305 | } |
| 9306 | } |
| 9307 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9308 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9309 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9310 | boolean needSep = false; |
| 9311 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9312 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9313 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9314 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9315 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9316 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 9317 | while (it.hasNext()) { |
| 9318 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9319 | if (dumpPackage != null && (r.app == null || |
| 9320 | !dumpPackage.equals(r.app.info.packageName))) { |
| 9321 | continue; |
| 9322 | } |
| 9323 | if (!printed) { |
| 9324 | pw.println(" Registered Receivers:"); |
| 9325 | needSep = true; |
| 9326 | printed = true; |
| 9327 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9328 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9329 | r.dump(pw, " "); |
| 9330 | } |
| 9331 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9332 | |
| 9333 | if (mReceiverResolver.dump(pw, needSep ? |
| 9334 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9335 | " ", dumpPackage, false)) { |
| 9336 | needSep = true; |
| 9337 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9338 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9339 | |
| 9340 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9341 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9342 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9343 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9344 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9345 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9346 | if (mStickyBroadcasts != null && dumpPackage == null) { |
| 9347 | if (needSep) { |
| 9348 | pw.println(); |
| 9349 | } |
| 9350 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9351 | pw.println(" Sticky broadcasts:"); |
| 9352 | StringBuilder sb = new StringBuilder(128); |
| 9353 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9354 | : mStickyBroadcasts.entrySet()) { |
| 9355 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9356 | if (dumpAll) { |
| 9357 | pw.println(":"); |
| 9358 | ArrayList<Intent> intents = ent.getValue(); |
| 9359 | final int N = intents.size(); |
| 9360 | for (int i=0; i<N; i++) { |
| 9361 | sb.setLength(0); |
| 9362 | sb.append(" Intent: "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9363 | intents.get(i).toShortString(sb, false, true, false, false); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9364 | pw.println(sb.toString()); |
| 9365 | Bundle bundle = intents.get(i).getExtras(); |
| 9366 | if (bundle != null) { |
| 9367 | pw.print(" "); |
| 9368 | pw.println(bundle.toString()); |
| 9369 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9370 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9371 | } else { |
| 9372 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9373 | } |
| 9374 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9375 | needSep = true; |
| 9376 | } |
| 9377 | |
| 9378 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9379 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9380 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9381 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9382 | + queue.mBroadcastsScheduled); |
| 9383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9384 | pw.println(" mHandler:"); |
| 9385 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9386 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9387 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9388 | |
| 9389 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9390 | } |
| 9391 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9392 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9393 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 9394 | boolean needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9395 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9396 | ItemMatcher matcher = new ItemMatcher(); |
| 9397 | matcher.build(args, opti); |
| 9398 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9399 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9400 | |
| 9401 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9402 | |
| 9403 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9404 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9405 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9406 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9407 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9408 | continue; |
| 9409 | } |
| 9410 | if (!printed) { |
| 9411 | if (needSep) pw.println(" "); |
| 9412 | needSep = true; |
| 9413 | pw.println(" Launching content providers:"); |
| 9414 | printed = true; |
| 9415 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9416 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9417 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9418 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9419 | } |
| 9420 | |
| 9421 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9422 | if (needSep) pw.println(); |
| 9423 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9424 | pw.println("Granted Uri Permissions:"); |
| 9425 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9426 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9427 | HashMap<Uri, UriPermission> perms |
| 9428 | = mGrantedUriPermissions.valueAt(i); |
| 9429 | pw.print(" * UID "); pw.print(uid); |
| 9430 | pw.println(" holds:"); |
| 9431 | for (UriPermission perm : perms.values()) { |
| 9432 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9433 | if (dumpAll) { |
| 9434 | perm.dump(pw, " "); |
| 9435 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9436 | } |
| 9437 | } |
| 9438 | needSep = true; |
| 9439 | } |
| 9440 | |
| 9441 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9442 | } |
| 9443 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9444 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9445 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9446 | boolean needSep = false; |
| 9447 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9448 | if (mIntentSenderRecords.size() > 0) { |
| 9449 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9450 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9451 | = mIntentSenderRecords.values().iterator(); |
| 9452 | while (it.hasNext()) { |
| 9453 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9454 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9455 | if (dumpPackage != null && (rec == null |
| 9456 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9457 | continue; |
| 9458 | } |
| 9459 | if (!printed) { |
| 9460 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9461 | printed = true; |
| 9462 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9463 | needSep = true; |
| 9464 | if (rec != null) { |
| 9465 | pw.print(" * "); pw.println(rec); |
| 9466 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9467 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9468 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9469 | } else { |
| 9470 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9471 | } |
| 9472 | } |
| 9473 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9474 | |
| 9475 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9476 | } |
| 9477 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9478 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9479 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9480 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9481 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9482 | boolean needNL = false; |
| 9483 | final String innerPrefix = prefix + " "; |
| 9484 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9485 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9486 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9487 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9488 | continue; |
| 9489 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9490 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9491 | if (needNL) { |
| 9492 | pw.println(" "); |
| 9493 | needNL = false; |
| 9494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9495 | if (lastTask != r.task) { |
| 9496 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9497 | pw.print(prefix); |
| 9498 | pw.print(full ? "* " : " "); |
| 9499 | pw.println(lastTask); |
| 9500 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9501 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9502 | } else if (complete) { |
| 9503 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9504 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9505 | pw.print(prefix); pw.print(" "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9506 | pw.println(lastTask.intent.toInsecureStringWithClip()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9507 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9508 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9509 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9510 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9511 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9512 | pw.println(r); |
| 9513 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9514 | r.dump(pw, innerPrefix); |
| 9515 | } else if (complete) { |
| 9516 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9517 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9518 | if (r.app != null) { |
| 9519 | pw.print(innerPrefix); pw.println(r.app); |
| 9520 | } |
| 9521 | } |
| 9522 | if (client && r.app != null && r.app.thread != null) { |
| 9523 | // flush anything that is already in the PrintWriter since the thread is going |
| 9524 | // to write to the file descriptor directly |
| 9525 | pw.flush(); |
| 9526 | try { |
| 9527 | TransferPipe tp = new TransferPipe(); |
| 9528 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9529 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9530 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9531 | // Short timeout, since blocking here can |
| 9532 | // deadlock with the application. |
| 9533 | tp.go(fd, 2000); |
| 9534 | } finally { |
| 9535 | tp.kill(); |
| 9536 | } |
| 9537 | } catch (IOException e) { |
| 9538 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9539 | } catch (RemoteException e) { |
| 9540 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9541 | } |
| 9542 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9543 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9544 | } |
| 9545 | } |
| 9546 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9547 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9548 | if (val == base) { |
| 9549 | if (space == null) return prefix; |
| 9550 | return prefix + " "; |
| 9551 | } |
| 9552 | return prefix + "+" + Integer.toString(val-base); |
| 9553 | } |
| 9554 | |
| 9555 | private static final int dumpProcessList(PrintWriter pw, |
| 9556 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9557 | String prefix, String normalLabel, String persistentLabel, |
| 9558 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9559 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9560 | final int N = list.size()-1; |
| 9561 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9562 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9563 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9564 | continue; |
| 9565 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9566 | pw.println(String.format("%s%s #%2d: %s", |
| 9567 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9568 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9569 | if (r.persistent) { |
| 9570 | numPers++; |
| 9571 | } |
| 9572 | } |
| 9573 | return numPers; |
| 9574 | } |
| 9575 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9576 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9577 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9578 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9579 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9580 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9581 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9582 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9583 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9584 | ProcessRecord r = origList.get(i); |
| 9585 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9586 | continue; |
| 9587 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9588 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9589 | } |
| 9590 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9591 | if (list.size() <= 0) { |
| 9592 | return false; |
| 9593 | } |
| 9594 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9595 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 9596 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 9597 | @Override |
| 9598 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 9599 | Pair<ProcessRecord, Integer> object2) { |
| 9600 | if (object1.first.setAdj != object2.first.setAdj) { |
| 9601 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 9602 | } |
| 9603 | if (object1.second.intValue() != object2.second.intValue()) { |
| 9604 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 9605 | } |
| 9606 | return 0; |
| 9607 | } |
| 9608 | }; |
| 9609 | |
| 9610 | Collections.sort(list, comparator); |
| 9611 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9612 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 9613 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 9614 | final long curUptime = SystemClock.uptimeMillis(); |
| 9615 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 9616 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9617 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9618 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9619 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9620 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9621 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9622 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 9623 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9624 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 9625 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9626 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 9627 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9628 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 9629 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9630 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9631 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9632 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 9633 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9634 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 9635 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9636 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 9637 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 9638 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9639 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9640 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 9641 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9642 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 9643 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9644 | } else { |
| 9645 | oomAdj = Integer.toString(r.setAdj); |
| 9646 | } |
| 9647 | String schedGroup; |
| 9648 | switch (r.setSchedGroup) { |
| 9649 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 9650 | schedGroup = "B"; |
| 9651 | break; |
| 9652 | case Process.THREAD_GROUP_DEFAULT: |
| 9653 | schedGroup = "F"; |
| 9654 | break; |
| 9655 | default: |
| 9656 | schedGroup = Integer.toString(r.setSchedGroup); |
| 9657 | break; |
| 9658 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9659 | String foreground; |
| 9660 | if (r.foregroundActivities) { |
| 9661 | foreground = "A"; |
| 9662 | } else if (r.foregroundServices) { |
| 9663 | foreground = "S"; |
| 9664 | } else { |
| 9665 | foreground = " "; |
| 9666 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9667 | 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] | 9668 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9669 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 9670 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9671 | if (r.adjSource != null || r.adjTarget != null) { |
| 9672 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9673 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9674 | if (r.adjTarget instanceof ComponentName) { |
| 9675 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 9676 | } else if (r.adjTarget != null) { |
| 9677 | pw.print(r.adjTarget.toString()); |
| 9678 | } else { |
| 9679 | pw.print("{null}"); |
| 9680 | } |
| 9681 | pw.print("<="); |
| 9682 | if (r.adjSource instanceof ProcessRecord) { |
| 9683 | pw.print("Proc{"); |
| 9684 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 9685 | pw.println("}"); |
| 9686 | } else if (r.adjSource != null) { |
| 9687 | pw.println(r.adjSource.toString()); |
| 9688 | } else { |
| 9689 | pw.println("{null}"); |
| 9690 | } |
| 9691 | } |
| 9692 | if (inclDetails) { |
| 9693 | pw.print(prefix); |
| 9694 | pw.print(" "); |
| 9695 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 9696 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
| 9697 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 9698 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 9699 | pw.print(" cur="); pw.print(r.curAdj); |
| 9700 | pw.print(" set="); pw.println(r.setAdj); |
| 9701 | pw.print(prefix); |
| 9702 | pw.print(" "); |
| 9703 | pw.print("keeping="); pw.print(r.keeping); |
| 9704 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9705 | pw.print(" empty="); pw.print(r.empty); |
| 9706 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9707 | |
| 9708 | if (!r.keeping) { |
| 9709 | if (r.lastWakeTime != 0) { |
| 9710 | long wtime; |
| 9711 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 9712 | synchronized (stats) { |
| 9713 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 9714 | r.pid, curRealtime); |
| 9715 | } |
| 9716 | long timeUsed = wtime - r.lastWakeTime; |
| 9717 | pw.print(prefix); |
| 9718 | pw.print(" "); |
| 9719 | pw.print("keep awake over "); |
| 9720 | TimeUtils.formatDuration(realtimeSince, pw); |
| 9721 | pw.print(" used "); |
| 9722 | TimeUtils.formatDuration(timeUsed, pw); |
| 9723 | pw.print(" ("); |
| 9724 | pw.print((timeUsed*100)/realtimeSince); |
| 9725 | pw.println("%)"); |
| 9726 | } |
| 9727 | if (r.lastCpuTime != 0) { |
| 9728 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 9729 | pw.print(prefix); |
| 9730 | pw.print(" "); |
| 9731 | pw.print("run cpu over "); |
| 9732 | TimeUtils.formatDuration(uptimeSince, pw); |
| 9733 | pw.print(" used "); |
| 9734 | TimeUtils.formatDuration(timeUsed, pw); |
| 9735 | pw.print(" ("); |
| 9736 | pw.print((timeUsed*100)/uptimeSince); |
| 9737 | pw.println("%)"); |
| 9738 | } |
| 9739 | } |
| 9740 | } |
| 9741 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9742 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9743 | } |
| 9744 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9745 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9746 | ArrayList<ProcessRecord> procs; |
| 9747 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9748 | if (args != null && args.length > start |
| 9749 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9750 | procs = new ArrayList<ProcessRecord>(); |
| 9751 | int pid = -1; |
| 9752 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9753 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9754 | } catch (NumberFormatException e) { |
| 9755 | |
| 9756 | } |
| 9757 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 9758 | ProcessRecord proc = mLruProcesses.get(i); |
| 9759 | if (proc.pid == pid) { |
| 9760 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9761 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9762 | procs.add(proc); |
| 9763 | } |
| 9764 | } |
| 9765 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9766 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9767 | return null; |
| 9768 | } |
| 9769 | } else { |
| 9770 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 9771 | } |
| 9772 | } |
| 9773 | return procs; |
| 9774 | } |
| 9775 | |
| 9776 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 9777 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9778 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9779 | if (procs == null) { |
| 9780 | return; |
| 9781 | } |
| 9782 | |
| 9783 | long uptime = SystemClock.uptimeMillis(); |
| 9784 | long realtime = SystemClock.elapsedRealtime(); |
| 9785 | pw.println("Applications Graphics Acceleration Info:"); |
| 9786 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9787 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9788 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9789 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9790 | if (r.thread != null) { |
| 9791 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9792 | pw.flush(); |
| 9793 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9794 | TransferPipe tp = new TransferPipe(); |
| 9795 | try { |
| 9796 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9797 | tp.go(fd); |
| 9798 | } finally { |
| 9799 | tp.kill(); |
| 9800 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9801 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9802 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9803 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9804 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9805 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9806 | pw.flush(); |
| 9807 | } |
| 9808 | } |
| 9809 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9810 | } |
| 9811 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 9812 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 9813 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 9814 | if (procs == null) { |
| 9815 | return; |
| 9816 | } |
| 9817 | |
| 9818 | pw.println("Applications Database Info:"); |
| 9819 | |
| 9820 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9821 | ProcessRecord r = procs.get(i); |
| 9822 | if (r.thread != null) { |
| 9823 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9824 | pw.flush(); |
| 9825 | try { |
| 9826 | TransferPipe tp = new TransferPipe(); |
| 9827 | try { |
| 9828 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9829 | tp.go(fd); |
| 9830 | } finally { |
| 9831 | tp.kill(); |
| 9832 | } |
| 9833 | } catch (IOException e) { |
| 9834 | pw.println("Failure while dumping the app: " + r); |
| 9835 | pw.flush(); |
| 9836 | } catch (RemoteException e) { |
| 9837 | pw.println("Got a RemoteException while dumping the app " + r); |
| 9838 | pw.flush(); |
| 9839 | } |
| 9840 | } |
| 9841 | } |
| 9842 | } |
| 9843 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9844 | final static class MemItem { |
| 9845 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9846 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9847 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9848 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9849 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9850 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9851 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9852 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9853 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9854 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9855 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9856 | } |
| 9857 | } |
| 9858 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9859 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9860 | boolean sort) { |
| 9861 | if (sort) { |
| 9862 | Collections.sort(items, new Comparator<MemItem>() { |
| 9863 | @Override |
| 9864 | public int compare(MemItem lhs, MemItem rhs) { |
| 9865 | if (lhs.pss < rhs.pss) { |
| 9866 | return 1; |
| 9867 | } else if (lhs.pss > rhs.pss) { |
| 9868 | return -1; |
| 9869 | } |
| 9870 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9871 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9872 | }); |
| 9873 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9874 | |
| 9875 | for (int i=0; i<items.size(); i++) { |
| 9876 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9877 | 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] | 9878 | if (mi.subitems != null) { |
| 9879 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 9880 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9881 | } |
| 9882 | } |
| 9883 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9884 | // These are in KB. |
| 9885 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 9886 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 9887 | 120*1024, 160*1024, 200*1024, |
| 9888 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 9889 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 9890 | }; |
| 9891 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9892 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 9893 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9894 | int start = label.lastIndexOf('.'); |
| 9895 | if (start >= 0) start++; |
| 9896 | else start = 0; |
| 9897 | int end = label.length(); |
| 9898 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 9899 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 9900 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 9901 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9902 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9903 | out.append(label, start, end); |
| 9904 | return; |
| 9905 | } |
| 9906 | } |
| 9907 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9908 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9909 | out.append(label, start, end); |
| 9910 | } |
| 9911 | |
| 9912 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 9913 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 9914 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 9915 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 9916 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 9917 | }; |
| 9918 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 9919 | "System", "Persistent", "Foreground", |
| 9920 | "Visible", "Perceptible", "Heavy Weight", |
| 9921 | "Backup", "A Services", "Home", "Previous", |
| 9922 | "B Services", "Background" |
| 9923 | }; |
| 9924 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9925 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9926 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9927 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9928 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9929 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9930 | |
| 9931 | int opti = 0; |
| 9932 | while (opti < args.length) { |
| 9933 | String opt = args[opti]; |
| 9934 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 9935 | break; |
| 9936 | } |
| 9937 | opti++; |
| 9938 | if ("-a".equals(opt)) { |
| 9939 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9940 | } else if ("--oom".equals(opt)) { |
| 9941 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9942 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9943 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9944 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9945 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9946 | pw.println("If [process] is specified it can be the name or "); |
| 9947 | pw.println("pid of a specific process to dump."); |
| 9948 | return; |
| 9949 | } else { |
| 9950 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 9951 | } |
| 9952 | } |
| 9953 | |
| 9954 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9955 | if (procs == null) { |
| 9956 | return; |
| 9957 | } |
| 9958 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 9959 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9960 | long uptime = SystemClock.uptimeMillis(); |
| 9961 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9962 | |
| 9963 | if (procs.size() == 1 || isCheckinRequest) { |
| 9964 | dumpAll = true; |
| 9965 | } |
| 9966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9967 | if (isCheckinRequest) { |
| 9968 | // short checkin version |
| 9969 | pw.println(uptime + "," + realtime); |
| 9970 | pw.flush(); |
| 9971 | } else { |
| 9972 | pw.println("Applications Memory Usage (kB):"); |
| 9973 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9974 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9975 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9976 | String[] innerArgs = new String[args.length-opti]; |
| 9977 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 9978 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9979 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 9980 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 9981 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 9982 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9983 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 9984 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 9985 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9986 | |
| 9987 | long totalPss = 0; |
| 9988 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9989 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9990 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9991 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9992 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9993 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 9994 | pw.flush(); |
| 9995 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9996 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9997 | if (dumpAll) { |
| 9998 | try { |
| 9999 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 10000 | } catch (RemoteException e) { |
| 10001 | if (!isCheckinRequest) { |
| 10002 | pw.println("Got RemoteException!"); |
| 10003 | pw.flush(); |
| 10004 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10005 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10006 | } else { |
| 10007 | mi = new Debug.MemoryInfo(); |
| 10008 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10009 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10010 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10011 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10012 | long myTotalPss = mi.getTotalPss(); |
| 10013 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10014 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10015 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10016 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10017 | |
| 10018 | nativePss += mi.nativePss; |
| 10019 | dalvikPss += mi.dalvikPss; |
| 10020 | otherPss += mi.otherPss; |
| 10021 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 10022 | long mem = mi.getOtherPss(j); |
| 10023 | miscPss[j] += mem; |
| 10024 | otherPss -= mem; |
| 10025 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10026 | |
| 10027 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10028 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 10029 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10030 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10031 | if (oomProcs[oomIndex] == null) { |
| 10032 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 10033 | } |
| 10034 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10035 | break; |
| 10036 | } |
| 10037 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10038 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10039 | } |
| 10040 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10041 | |
| 10042 | if (!isCheckinRequest && procs.size() > 1) { |
| 10043 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 10044 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10045 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10046 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10047 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10048 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10049 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10050 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10051 | } |
| 10052 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10053 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10054 | for (int j=0; j<oomPss.length; j++) { |
| 10055 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10056 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10057 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10058 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10059 | item.subitems = oomProcs[j]; |
| 10060 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10061 | } |
| 10062 | } |
| 10063 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10064 | if (outTag != null || outStack != null) { |
| 10065 | if (outTag != null) { |
| 10066 | appendMemBucket(outTag, totalPss, "total", false); |
| 10067 | } |
| 10068 | if (outStack != null) { |
| 10069 | appendMemBucket(outStack, totalPss, "total", true); |
| 10070 | } |
| 10071 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10072 | for (int i=0; i<oomMems.size(); i++) { |
| 10073 | MemItem miCat = oomMems.get(i); |
| 10074 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10075 | continue; |
| 10076 | } |
| 10077 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10078 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10079 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10080 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10081 | outTag.append(" / "); |
| 10082 | } |
| 10083 | if (outStack != null) { |
| 10084 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10085 | if (firstLine) { |
| 10086 | outStack.append(":"); |
| 10087 | firstLine = false; |
| 10088 | } |
| 10089 | outStack.append("\n\t at "); |
| 10090 | } else { |
| 10091 | outStack.append("$"); |
| 10092 | } |
| 10093 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10094 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10095 | MemItem mi = miCat.subitems.get(j); |
| 10096 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10097 | if (outTag != null) { |
| 10098 | outTag.append(" "); |
| 10099 | } |
| 10100 | if (outStack != null) { |
| 10101 | outStack.append("$"); |
| 10102 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10103 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10104 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10105 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10106 | } |
| 10107 | if (outStack != null) { |
| 10108 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10109 | } |
| 10110 | } |
| 10111 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10112 | outStack.append("("); |
| 10113 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10114 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10115 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10116 | outStack.append(":"); |
| 10117 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10118 | } |
| 10119 | } |
| 10120 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10121 | } |
| 10122 | } |
| 10123 | } |
| 10124 | } |
| 10125 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10126 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10127 | pw.println(); |
| 10128 | pw.println("Total PSS by process:"); |
| 10129 | dumpMemItems(pw, " ", procMems, true); |
| 10130 | pw.println(); |
| 10131 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10132 | pw.println("Total PSS by OOM adjustment:"); |
| 10133 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10134 | if (!oomOnly) { |
| 10135 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10136 | out.println(); |
| 10137 | out.println("Total PSS by category:"); |
| 10138 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10139 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10140 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10141 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | d3e677b | 2012-04-05 14:58:18 -0700 | [diff] [blame] | 10142 | final int[] SINGLE_LONG_FORMAT = new int[] { |
| 10143 | Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG |
| 10144 | }; |
| 10145 | long[] longOut = new long[1]; |
| 10146 | Process.readProcFile("/sys/kernel/mm/ksm/pages_shared", |
| 10147 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10148 | long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10149 | longOut[0] = 0; |
| 10150 | Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing", |
| 10151 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10152 | long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10153 | longOut[0] = 0; |
| 10154 | Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared", |
| 10155 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10156 | long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10157 | longOut[0] = 0; |
| 10158 | Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile", |
| 10159 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10160 | long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10161 | pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared "); |
| 10162 | pw.print(shared); pw.println(" kB"); |
| 10163 | pw.print(" "); pw.print(unshared); pw.print(" kB unshared; "); |
| 10164 | pw.print(voltile); pw.println(" kB volatile"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10165 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10166 | } |
| 10167 | |
| 10168 | /** |
| 10169 | * Searches array of arguments for the specified string |
| 10170 | * @param args array of argument strings |
| 10171 | * @param value value to search for |
| 10172 | * @return true if the value is contained in the array |
| 10173 | */ |
| 10174 | private static boolean scanArgs(String[] args, String value) { |
| 10175 | if (args != null) { |
| 10176 | for (String arg : args) { |
| 10177 | if (value.equals(arg)) { |
| 10178 | return true; |
| 10179 | } |
| 10180 | } |
| 10181 | } |
| 10182 | return false; |
| 10183 | } |
| 10184 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10185 | private final boolean removeDyingProviderLocked(ProcessRecord proc, |
| 10186 | ContentProviderRecord cpr, boolean always) { |
| 10187 | final boolean inLaunching = mLaunchingProviders.contains(cpr); |
| 10188 | |
| 10189 | if (!inLaunching || always) { |
| 10190 | synchronized (cpr) { |
| 10191 | cpr.launchingApp = null; |
| 10192 | cpr.notifyAll(); |
| 10193 | } |
| 10194 | mProviderMap.removeProviderByClass(cpr.name, UserId.getUserId(cpr.uid)); |
| 10195 | String names[] = cpr.info.authority.split(";"); |
| 10196 | for (int j = 0; j < names.length; j++) { |
| 10197 | mProviderMap.removeProviderByName(names[j], UserId.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10198 | } |
| 10199 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10200 | |
| 10201 | for (int i=0; i<cpr.connections.size(); i++) { |
| 10202 | ContentProviderConnection conn = cpr.connections.get(i); |
| 10203 | if (conn.waiting) { |
| 10204 | // If this connection is waiting for the provider, then we don't |
| 10205 | // need to mess with its process unless we are always removing |
| 10206 | // or for some reason the provider is not currently launching. |
| 10207 | if (inLaunching && !always) { |
| 10208 | continue; |
| 10209 | } |
| 10210 | } |
| 10211 | ProcessRecord capp = conn.client; |
| 10212 | conn.dead = true; |
| 10213 | if (conn.stableCount > 0) { |
| 10214 | if (!capp.persistent && capp.thread != null |
| 10215 | && capp.pid != 0 |
| 10216 | && capp.pid != MY_PID) { |
| 10217 | Slog.i(TAG, "Kill " + capp.processName |
| 10218 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 10219 | + " in dying process " + (proc != null ? proc.processName : "??")); |
| 10220 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
| 10221 | capp.processName, capp.setAdj, "dying provider " |
| 10222 | + cpr.name.toShortString()); |
| 10223 | Process.killProcessQuiet(capp.pid); |
| 10224 | } |
| 10225 | } else if (capp.thread != null && conn.provider.provider != null) { |
| 10226 | try { |
| 10227 | capp.thread.unstableProviderDied(conn.provider.provider.asBinder()); |
| 10228 | } catch (RemoteException e) { |
| 10229 | } |
| 10230 | // In the protocol here, we don't expect the client to correctly |
| 10231 | // clean up this connection, we'll just remove it. |
| 10232 | cpr.connections.remove(i); |
| 10233 | conn.client.conProviders.remove(conn); |
| 10234 | } |
| 10235 | } |
| 10236 | |
| 10237 | if (inLaunching && always) { |
| 10238 | mLaunchingProviders.remove(cpr); |
| 10239 | } |
| 10240 | return inLaunching; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10241 | } |
| 10242 | |
| 10243 | /** |
| 10244 | * Main code for cleaning up a process when it has gone away. This is |
| 10245 | * called both as a result of the process dying, or directly when stopping |
| 10246 | * a process when running in single process mode. |
| 10247 | */ |
| 10248 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10249 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10250 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10251 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10252 | } |
| 10253 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10254 | mProcessesToGc.remove(app); |
| 10255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10256 | // Dismiss any open dialogs. |
| 10257 | if (app.crashDialog != null) { |
| 10258 | app.crashDialog.dismiss(); |
| 10259 | app.crashDialog = null; |
| 10260 | } |
| 10261 | if (app.anrDialog != null) { |
| 10262 | app.anrDialog.dismiss(); |
| 10263 | app.anrDialog = null; |
| 10264 | } |
| 10265 | if (app.waitDialog != null) { |
| 10266 | app.waitDialog.dismiss(); |
| 10267 | app.waitDialog = null; |
| 10268 | } |
| 10269 | |
| 10270 | app.crashing = false; |
| 10271 | app.notResponding = false; |
| 10272 | |
| 10273 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10274 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10275 | app.thread = null; |
| 10276 | app.forcingToForeground = null; |
| 10277 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10278 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10279 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10280 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10281 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10282 | mServices.killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10283 | |
| 10284 | boolean restart = false; |
| 10285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10286 | // Remove published content providers. |
| 10287 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10288 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10289 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10290 | ContentProviderRecord cpr = it.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10291 | |
| 10292 | final boolean always = app.bad || !allowRestart; |
| 10293 | if (removeDyingProviderLocked(app, cpr, always) || always) { |
| 10294 | // We left the provider in the launching list, need to |
| 10295 | // restart it. |
| 10296 | restart = true; |
| 10297 | } |
| 10298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10299 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10300 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10301 | } |
| 10302 | app.pubProviders.clear(); |
| 10303 | } |
| 10304 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10305 | // Take care of any launching providers waiting for this process. |
| 10306 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10307 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10308 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10310 | // Unregister from connected content providers. |
| 10311 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10312 | for (int i=0; i<app.conProviders.size(); i++) { |
| 10313 | ContentProviderConnection conn = app.conProviders.get(i); |
| 10314 | conn.provider.connections.remove(conn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10315 | } |
| 10316 | app.conProviders.clear(); |
| 10317 | } |
| 10318 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10319 | // At this point there may be remaining entries in mLaunchingProviders |
| 10320 | // where we were the only one waiting, so they are no longer of use. |
| 10321 | // Look for these and clean up if found. |
| 10322 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10323 | // the actual situation to identify what is actually going on. |
| 10324 | if (false) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10325 | for (int i=0; i<mLaunchingProviders.size(); i++) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10326 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10327 | mLaunchingProviders.get(i); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10328 | if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10329 | synchronized (cpr) { |
| 10330 | cpr.launchingApp = null; |
| 10331 | cpr.notifyAll(); |
| 10332 | } |
| 10333 | } |
| 10334 | } |
| 10335 | } |
| 10336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10337 | skipCurrentReceiverLocked(app); |
| 10338 | |
| 10339 | // Unregister any receivers. |
| 10340 | if (app.receivers.size() > 0) { |
| 10341 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10342 | while (it.hasNext()) { |
| 10343 | removeReceiverLocked(it.next()); |
| 10344 | } |
| 10345 | app.receivers.clear(); |
| 10346 | } |
| 10347 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10348 | // If the app is undergoing backup, tell the backup manager about it |
| 10349 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10350 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10351 | try { |
| 10352 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10353 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10354 | bm.agentDisconnected(app.info.packageName); |
| 10355 | } catch (RemoteException e) { |
| 10356 | // can't happen; backup manager is local |
| 10357 | } |
| 10358 | } |
| 10359 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 10360 | for (int i = mPendingProcessChanges.size()-1; i>=0; i--) { |
| 10361 | ProcessChangeItem item = mPendingProcessChanges.get(i); |
| 10362 | if (item.pid == app.pid) { |
| 10363 | mPendingProcessChanges.remove(i); |
| 10364 | mAvailProcessChanges.add(item); |
| 10365 | } |
| 10366 | } |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10367 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10368 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10369 | // If the caller is restarting this app, then leave it in its |
| 10370 | // current lists and let the caller take care of it. |
| 10371 | if (restarting) { |
| 10372 | return; |
| 10373 | } |
| 10374 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10375 | if (!app.persistent || app.isolated) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10376 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10377 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10378 | mProcessNames.remove(app.processName, app.uid); |
| 10379 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10380 | if (mHeavyWeightProcess == app) { |
| 10381 | mHeavyWeightProcess = null; |
| 10382 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 10383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10384 | } else if (!app.removed) { |
| 10385 | // This app is persistent, so we need to keep its record around. |
| 10386 | // If it is not already on the pending app list, add it there |
| 10387 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10388 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10389 | mPersistentStartingProcesses.add(app); |
| 10390 | restart = true; |
| 10391 | } |
| 10392 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10393 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 10394 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10395 | mProcessesOnHold.remove(app); |
| 10396 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10397 | if (app == mHomeProcess) { |
| 10398 | mHomeProcess = null; |
| 10399 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10400 | if (app == mPreviousProcess) { |
| 10401 | mPreviousProcess = null; |
| 10402 | } |
| 10403 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10404 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10405 | // We have components that still need to be running in the |
| 10406 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10407 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10408 | startProcessLocked(app, "restart", app.processName); |
| 10409 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10410 | // Goodbye! |
| 10411 | synchronized (mPidsSelfLocked) { |
| 10412 | mPidsSelfLocked.remove(app.pid); |
| 10413 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10414 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10415 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10416 | } |
| 10417 | } |
| 10418 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10419 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10420 | // Look through the content providers we are waiting to have launched, |
| 10421 | // and if any run in this process then either schedule a restart of |
| 10422 | // the process or kill the client waiting for it if this process has |
| 10423 | // gone bad. |
| 10424 | int NL = mLaunchingProviders.size(); |
| 10425 | boolean restart = false; |
| 10426 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10427 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10428 | if (cpr.launchingApp == app) { |
| 10429 | if (!alwaysBad && !app.bad) { |
| 10430 | restart = true; |
| 10431 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10432 | removeDyingProviderLocked(app, cpr, true); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10433 | NL = mLaunchingProviders.size(); |
| 10434 | } |
| 10435 | } |
| 10436 | } |
| 10437 | return restart; |
| 10438 | } |
| 10439 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10440 | // ========================================================= |
| 10441 | // SERVICES |
| 10442 | // ========================================================= |
| 10443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10444 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10445 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10446 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10447 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10448 | return mServices.getRunningServiceInfoLocked(maxNum, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10449 | } |
| 10450 | } |
| 10451 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10452 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10453 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10454 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10455 | return mServices.getRunningServiceControlPanelLocked(name); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10456 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10457 | } |
| 10458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10459 | public ComponentName startService(IApplicationThread caller, Intent service, |
| 10460 | String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10461 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10462 | // Refuse possible leaked file descriptors |
| 10463 | if (service != null && service.hasFileDescriptors() == true) { |
| 10464 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10465 | } |
| 10466 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10467 | if (DEBUG_SERVICE) |
| 10468 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10469 | synchronized(this) { |
| 10470 | final int callingPid = Binder.getCallingPid(); |
| 10471 | final int callingUid = Binder.getCallingUid(); |
| 10472 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10473 | ComponentName res = mServices.startServiceLocked(caller, service, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10474 | resolvedType, callingPid, callingUid); |
| 10475 | Binder.restoreCallingIdentity(origId); |
| 10476 | return res; |
| 10477 | } |
| 10478 | } |
| 10479 | |
| 10480 | ComponentName startServiceInPackage(int uid, |
| 10481 | Intent service, String resolvedType) { |
| 10482 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10483 | if (DEBUG_SERVICE) |
| 10484 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10485 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10486 | ComponentName res = mServices.startServiceLocked(null, service, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10487 | resolvedType, -1, uid); |
| 10488 | Binder.restoreCallingIdentity(origId); |
| 10489 | return res; |
| 10490 | } |
| 10491 | } |
| 10492 | |
| 10493 | public int stopService(IApplicationThread caller, Intent service, |
| 10494 | String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10495 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10496 | // Refuse possible leaked file descriptors |
| 10497 | if (service != null && service.hasFileDescriptors() == true) { |
| 10498 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10499 | } |
| 10500 | |
| 10501 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10502 | return mServices.stopServiceLocked(caller, service, resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10503 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10504 | } |
| 10505 | |
| 10506 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10507 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10508 | // Refuse possible leaked file descriptors |
| 10509 | if (service != null && service.hasFileDescriptors() == true) { |
| 10510 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10511 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10512 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10513 | return mServices.peekServiceLocked(service, resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10514 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10515 | } |
| 10516 | |
| 10517 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 10518 | int startId) { |
| 10519 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10520 | return mServices.stopServiceTokenLocked(className, token, startId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10521 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10522 | } |
| 10523 | |
| 10524 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10525 | int id, Notification notification, boolean removeNotification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10526 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10527 | mServices.setServiceForegroundLocked(className, token, id, notification, |
| 10528 | removeNotification); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10529 | } |
| 10530 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10531 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 10532 | boolean isSingleton(String componentProcessName, ApplicationInfo aInfo, |
| 10533 | String className, int flags) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10534 | boolean result = false; |
| 10535 | if (UserId.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 10536 | if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) { |
| 10537 | if (ActivityManager.checkUidPermission( |
| 10538 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10539 | aInfo.uid) != PackageManager.PERMISSION_GRANTED) { |
| 10540 | ComponentName comp = new ComponentName(aInfo.packageName, className); |
| 10541 | String msg = "Permission Denial: Component " + comp.flattenToShortString() |
| 10542 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 10543 | + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 10544 | Slog.w(TAG, msg); |
| 10545 | throw new SecurityException(msg); |
| 10546 | } |
| 10547 | result = true; |
| 10548 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10549 | } else if (componentProcessName == aInfo.packageName) { |
| 10550 | result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 10551 | } else if ("system".equals(componentProcessName)) { |
| 10552 | result = true; |
| 10553 | } |
| 10554 | if (DEBUG_MU) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 10555 | Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo |
| 10556 | + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10557 | } |
| 10558 | return result; |
| 10559 | } |
| 10560 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10561 | public int bindService(IApplicationThread caller, IBinder token, |
| 10562 | Intent service, String resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10563 | IServiceConnection connection, int flags, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10564 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10565 | // Refuse possible leaked file descriptors |
| 10566 | if (service != null && service.hasFileDescriptors() == true) { |
| 10567 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10568 | } |
| 10569 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10570 | checkValidCaller(Binder.getCallingUid(), userId); |
| 10571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10572 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10573 | return mServices.bindServiceLocked(caller, token, service, resolvedType, |
| 10574 | connection, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10575 | } |
| 10576 | } |
| 10577 | |
| 10578 | public boolean unbindService(IServiceConnection connection) { |
| 10579 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10580 | return mServices.unbindServiceLocked(connection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10581 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10582 | } |
| 10583 | |
| 10584 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 10585 | // Refuse possible leaked file descriptors |
| 10586 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10587 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10588 | } |
| 10589 | |
| 10590 | synchronized(this) { |
| 10591 | if (!(token instanceof ServiceRecord)) { |
| 10592 | throw new IllegalArgumentException("Invalid service token"); |
| 10593 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10594 | mServices.publishServiceLocked((ServiceRecord)token, intent, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10595 | } |
| 10596 | } |
| 10597 | |
| 10598 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 10599 | // Refuse possible leaked file descriptors |
| 10600 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10601 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10602 | } |
| 10603 | |
| 10604 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10605 | mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10606 | } |
| 10607 | } |
| 10608 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10609 | 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] | 10610 | synchronized(this) { |
| 10611 | if (!(token instanceof ServiceRecord)) { |
| 10612 | throw new IllegalArgumentException("Invalid service token"); |
| 10613 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10614 | mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 10615 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10616 | } |
| 10617 | |
| 10618 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10619 | // BACKUP AND RESTORE |
| 10620 | // ========================================================= |
| 10621 | |
| 10622 | // Cause the target app to be launched if necessary and its backup agent |
| 10623 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 10624 | // activity manager to announce its creation. |
| 10625 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10626 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10627 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 10628 | |
| 10629 | synchronized(this) { |
| 10630 | // !!! TODO: currently no check here that we're already bound |
| 10631 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 10632 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 10633 | synchronized (stats) { |
| 10634 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 10635 | } |
| 10636 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10637 | // Backup agent is now in use, its package can't be stopped. |
| 10638 | try { |
| 10639 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 10640 | app.packageName, false, UserId.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10641 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 10642 | } catch (IllegalArgumentException e) { |
| 10643 | Slog.w(TAG, "Failed trying to unstop package " |
| 10644 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10645 | } |
| 10646 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10647 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 10648 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 10649 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 10650 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10651 | // startProcessLocked() returns existing proc's record if it's already running |
| 10652 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10653 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10654 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10655 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10656 | return false; |
| 10657 | } |
| 10658 | |
| 10659 | r.app = proc; |
| 10660 | mBackupTarget = r; |
| 10661 | mBackupAppName = app.packageName; |
| 10662 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 10663 | // Try not to kill the process during backup |
| 10664 | updateOomAdjLocked(proc); |
| 10665 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10666 | // If the process is already attached, schedule the creation of the backup agent now. |
| 10667 | // If it is not yet live, this will be done when it attaches to the framework. |
| 10668 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10669 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10670 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10671 | proc.thread.scheduleCreateBackupAgent(app, |
| 10672 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10673 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 10674 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10675 | } |
| 10676 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10677 | 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] | 10678 | } |
| 10679 | // Invariants: at this point, the target app process exists and the application |
| 10680 | // is either already running or in the process of coming up. mBackupTarget and |
| 10681 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 10682 | // know that it's scheduled for a backup-agent operation. |
| 10683 | } |
| 10684 | |
| 10685 | return true; |
| 10686 | } |
| 10687 | |
| 10688 | // A backup agent has just come up |
| 10689 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10690 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10691 | + " = " + agent); |
| 10692 | |
| 10693 | synchronized(this) { |
| 10694 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10695 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10696 | return; |
| 10697 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 10698 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10699 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 10700 | long oldIdent = Binder.clearCallingIdentity(); |
| 10701 | try { |
| 10702 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10703 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10704 | bm.agentConnected(agentPackageName, agent); |
| 10705 | } catch (RemoteException e) { |
| 10706 | // can't happen; the backup manager service is local |
| 10707 | } catch (Exception e) { |
| 10708 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 10709 | e.printStackTrace(); |
| 10710 | } finally { |
| 10711 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10712 | } |
| 10713 | } |
| 10714 | |
| 10715 | // done with this agent |
| 10716 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10717 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10718 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10719 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10720 | return; |
| 10721 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10722 | |
| 10723 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10724 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10725 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10726 | return; |
| 10727 | } |
| 10728 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10729 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10730 | Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10731 | return; |
| 10732 | } |
| 10733 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 10734 | ProcessRecord proc = mBackupTarget.app; |
| 10735 | mBackupTarget = null; |
| 10736 | mBackupAppName = null; |
| 10737 | |
| 10738 | // Not backing this app up any more; reset its OOM adjustment |
| 10739 | updateOomAdjLocked(proc); |
| 10740 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10741 | // If the app crashed during backup, 'thread' will be null here |
| 10742 | if (proc.thread != null) { |
| 10743 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10744 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 10745 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10746 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10747 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10748 | e.printStackTrace(); |
| 10749 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10750 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10751 | } |
| 10752 | } |
| 10753 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10754 | // BROADCASTS |
| 10755 | // ========================================================= |
| 10756 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10757 | private final List getStickiesLocked(String action, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10758 | List cur) { |
| 10759 | final ContentResolver resolver = mContext.getContentResolver(); |
| 10760 | final ArrayList<Intent> list = mStickyBroadcasts.get(action); |
| 10761 | if (list == null) { |
| 10762 | return cur; |
| 10763 | } |
| 10764 | int N = list.size(); |
| 10765 | for (int i=0; i<N; i++) { |
| 10766 | Intent intent = list.get(i); |
| 10767 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 10768 | if (cur == null) { |
| 10769 | cur = new ArrayList<Intent>(); |
| 10770 | } |
| 10771 | cur.add(intent); |
| 10772 | } |
| 10773 | } |
| 10774 | return cur; |
| 10775 | } |
| 10776 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10777 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 10778 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 10779 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 10780 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10781 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10782 | void skipPendingBroadcastLocked(int pid) { |
| 10783 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 10784 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 10785 | queue.skipPendingBroadcastLocked(pid); |
| 10786 | } |
| 10787 | } |
| 10788 | |
| 10789 | // The app just attached; send any pending broadcasts that it should receive |
| 10790 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 10791 | boolean didSomething = false; |
| 10792 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 10793 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10794 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10795 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10796 | } |
| 10797 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 10798 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10799 | IIntentReceiver receiver, IntentFilter filter, String permission) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10800 | enforceNotIsolatedCaller("registerReceiver"); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 10801 | int callingUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10802 | synchronized(this) { |
| 10803 | ProcessRecord callerApp = null; |
| 10804 | if (caller != null) { |
| 10805 | callerApp = getRecordForAppLocked(caller); |
| 10806 | if (callerApp == null) { |
| 10807 | throw new SecurityException( |
| 10808 | "Unable to find app for caller " + caller |
| 10809 | + " (pid=" + Binder.getCallingPid() |
| 10810 | + ") when registering receiver " + receiver); |
| 10811 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 10812 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 10813 | !callerApp.pkgList.contains(callerPackage)) { |
| 10814 | throw new SecurityException("Given caller package " + callerPackage |
| 10815 | + " is not running in process " + callerApp); |
| 10816 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 10817 | callingUid = callerApp.info.uid; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 10818 | } else { |
| 10819 | callerPackage = null; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 10820 | callingUid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10821 | } |
| 10822 | |
| 10823 | List allSticky = null; |
| 10824 | |
| 10825 | // Look for any matching sticky broadcasts... |
| 10826 | Iterator actions = filter.actionsIterator(); |
| 10827 | if (actions != null) { |
| 10828 | while (actions.hasNext()) { |
| 10829 | String action = (String)actions.next(); |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10830 | allSticky = getStickiesLocked(action, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10831 | } |
| 10832 | } else { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10833 | allSticky = getStickiesLocked(null, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10834 | } |
| 10835 | |
| 10836 | // The first sticky in the list is returned directly back to |
| 10837 | // the client. |
| 10838 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 10839 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10840 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10841 | + ": " + sticky); |
| 10842 | |
| 10843 | if (receiver == null) { |
| 10844 | return sticky; |
| 10845 | } |
| 10846 | |
| 10847 | ReceiverList rl |
| 10848 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 10849 | if (rl == null) { |
| 10850 | rl = new ReceiverList(this, callerApp, |
| 10851 | Binder.getCallingPid(), |
| 10852 | Binder.getCallingUid(), receiver); |
| 10853 | if (rl.app != null) { |
| 10854 | rl.app.receivers.add(rl); |
| 10855 | } else { |
| 10856 | try { |
| 10857 | receiver.asBinder().linkToDeath(rl, 0); |
| 10858 | } catch (RemoteException e) { |
| 10859 | return sticky; |
| 10860 | } |
| 10861 | rl.linkedToDeath = true; |
| 10862 | } |
| 10863 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
| 10864 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 10865 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, |
| 10866 | permission, callingUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10867 | rl.add(bf); |
| 10868 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10869 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10870 | } |
| 10871 | mReceiverResolver.addFilter(bf); |
| 10872 | |
| 10873 | // Enqueue broadcasts for all existing stickies that match |
| 10874 | // this filter. |
| 10875 | if (allSticky != null) { |
| 10876 | ArrayList receivers = new ArrayList(); |
| 10877 | receivers.add(bf); |
| 10878 | |
| 10879 | int N = allSticky.size(); |
| 10880 | for (int i=0; i<N; i++) { |
| 10881 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10882 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 10883 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10884 | null, -1, -1, null, receivers, null, 0, null, null, |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 10885 | false, true, true, false); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10886 | queue.enqueueParallelBroadcastLocked(r); |
| 10887 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10888 | } |
| 10889 | } |
| 10890 | |
| 10891 | return sticky; |
| 10892 | } |
| 10893 | } |
| 10894 | |
| 10895 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10896 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10897 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10898 | final long origId = Binder.clearCallingIdentity(); |
| 10899 | try { |
| 10900 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10901 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10902 | synchronized(this) { |
| 10903 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10904 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10905 | if (rl != null) { |
| 10906 | if (rl.curBroadcast != null) { |
| 10907 | BroadcastRecord r = rl.curBroadcast; |
| 10908 | final boolean doNext = finishReceiverLocked( |
| 10909 | receiver.asBinder(), r.resultCode, r.resultData, |
| 10910 | r.resultExtras, r.resultAbort, true); |
| 10911 | if (doNext) { |
| 10912 | doTrim = true; |
| 10913 | r.queue.processNextBroadcast(false); |
| 10914 | } |
| 10915 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10916 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10917 | if (rl.app != null) { |
| 10918 | rl.app.receivers.remove(rl); |
| 10919 | } |
| 10920 | removeReceiverLocked(rl); |
| 10921 | if (rl.linkedToDeath) { |
| 10922 | rl.linkedToDeath = false; |
| 10923 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 10924 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10925 | } |
| 10926 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10927 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 10928 | // If we actually concluded any broadcasts, we might now be able |
| 10929 | // to trim the recipients' apps from our working set |
| 10930 | if (doTrim) { |
| 10931 | trimApplications(); |
| 10932 | return; |
| 10933 | } |
| 10934 | |
| 10935 | } finally { |
| 10936 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10938 | } |
| 10939 | |
| 10940 | void removeReceiverLocked(ReceiverList rl) { |
| 10941 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 10942 | int N = rl.size(); |
| 10943 | for (int i=0; i<N; i++) { |
| 10944 | mReceiverResolver.removeFilter(rl.get(i)); |
| 10945 | } |
| 10946 | } |
| 10947 | |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 10948 | private final void sendPackageBroadcastLocked(int cmd, String[] packages) { |
| 10949 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 10950 | ProcessRecord r = mLruProcesses.get(i); |
| 10951 | if (r.thread != null) { |
| 10952 | try { |
| 10953 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 10954 | } catch (RemoteException ex) { |
| 10955 | } |
| 10956 | } |
| 10957 | } |
| 10958 | } |
| 10959 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10960 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 10961 | String callerPackage, Intent intent, String resolvedType, |
| 10962 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 10963 | Bundle map, String requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10964 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 10965 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10966 | intent = new Intent(intent); |
| 10967 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10968 | // By default broadcasts do not go to stopped apps. |
| 10969 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 10970 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10971 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10972 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 10973 | + " ordered=" + ordered + " userid=" + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10974 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10975 | 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] | 10976 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 10977 | |
| 10978 | boolean onlySendToCaller = false; |
| 10979 | |
| 10980 | // If the caller is trying to send this broadcast to a different |
| 10981 | // user, verify that is allowed. |
| 10982 | if (UserId.getUserId(callingUid) != userId) { |
| 10983 | if (checkComponentPermission( |
| 10984 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 10985 | callingPid, callingUid, -1, true) |
| 10986 | != PackageManager.PERMISSION_GRANTED) { |
| 10987 | if (checkComponentPermission( |
| 10988 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10989 | callingPid, callingUid, -1, true) |
| 10990 | == PackageManager.PERMISSION_GRANTED) { |
| 10991 | onlySendToCaller = true; |
| 10992 | } else { |
| 10993 | String msg = "Permission Denial: " + intent.getAction() |
| 10994 | + " broadcast from " + callerPackage |
| 10995 | + " asks to send as user " + userId |
| 10996 | + " but is calling from user " + UserId.getUserId(callingUid) |
| 10997 | + "; this requires " |
| 10998 | + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 10999 | Slog.w(TAG, msg); |
| 11000 | throw new SecurityException(msg); |
| 11001 | } |
| 11002 | } |
| 11003 | } |
| 11004 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11005 | // Handle special intents: if this broadcast is from the package |
| 11006 | // manager about a package being removed, we need to remove all of |
| 11007 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11008 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11009 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11010 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11011 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11012 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11013 | || uidRemoved) { |
| 11014 | if (checkComponentPermission( |
| 11015 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11016 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11017 | == PackageManager.PERMISSION_GRANTED) { |
| 11018 | if (uidRemoved) { |
| 11019 | final Bundle intentExtras = intent.getExtras(); |
| 11020 | final int uid = intentExtras != null |
| 11021 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11022 | if (uid >= 0) { |
| 11023 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11024 | synchronized (bs) { |
| 11025 | bs.removeUidStatsLocked(uid); |
| 11026 | } |
| 11027 | } |
| 11028 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11029 | // If resources are unvailble just force stop all |
| 11030 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11031 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11032 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11033 | if (list != null && (list.length > 0)) { |
| 11034 | for (String pkg : list) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11035 | forceStopPackageLocked(pkg, -1, false, true, true, false, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11036 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11037 | sendPackageBroadcastLocked( |
| 11038 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11039 | } |
| 11040 | } else { |
| 11041 | Uri data = intent.getData(); |
| 11042 | String ssp; |
| 11043 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11044 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11045 | forceStopPackageLocked(ssp, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11046 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, |
| 11047 | false, userId); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11048 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11049 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11050 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
| 11051 | new String[] {ssp}); |
| 11052 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11053 | } |
| 11054 | } |
| 11055 | } |
| 11056 | } else { |
| 11057 | String msg = "Permission Denial: " + intent.getAction() |
| 11058 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11059 | + ", uid=" + callingUid + ")" |
| 11060 | + " requires " |
| 11061 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11062 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11063 | throw new SecurityException(msg); |
| 11064 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11065 | |
| 11066 | // Special case for adding a package: by default turn on compatibility |
| 11067 | // mode. |
| 11068 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 11069 | Uri data = intent.getData(); |
| 11070 | String ssp; |
| 11071 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11072 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 11073 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11075 | } |
| 11076 | |
| 11077 | /* |
| 11078 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11079 | * of all currently running processes. This message will get queued up before the broadcast |
| 11080 | * happens. |
| 11081 | */ |
| 11082 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11083 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11084 | } |
| 11085 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11086 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11087 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11088 | } |
| 11089 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11090 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11091 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11092 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11093 | } |
| 11094 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11095 | /* |
| 11096 | * Prevent non-system code (defined here to be non-persistent |
| 11097 | * processes) from sending protected broadcasts. |
| 11098 | */ |
| 11099 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 11100 | || callingUid == Process.SHELL_UID || callingUid == Process.BLUETOOTH_UID || |
| 11101 | callingUid == 0) { |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11102 | // Always okay. |
| 11103 | } else if (callerApp == null || !callerApp.persistent) { |
| 11104 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11105 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11106 | intent.getAction())) { |
| 11107 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11108 | + intent.getAction() + " from pid=" |
| 11109 | + callingPid + ", uid=" + callingUid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11110 | Slog.w(TAG, msg); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11111 | throw new SecurityException(msg); |
| 11112 | } |
| 11113 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11114 | Slog.w(TAG, "Remote exception", e); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11115 | return ActivityManager.BROADCAST_SUCCESS; |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11116 | } |
| 11117 | } |
| 11118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11119 | // Add to the sticky list if requested. |
| 11120 | if (sticky) { |
| 11121 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11122 | callingPid, callingUid) |
| 11123 | != PackageManager.PERMISSION_GRANTED) { |
| 11124 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11125 | + callingPid + ", uid=" + callingUid |
| 11126 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11127 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11128 | throw new SecurityException(msg); |
| 11129 | } |
| 11130 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11131 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11132 | + " and enforce permission " + requiredPermission); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11133 | return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11134 | } |
| 11135 | if (intent.getComponent() != null) { |
| 11136 | throw new SecurityException( |
| 11137 | "Sticky broadcasts can't target a specific component"); |
| 11138 | } |
| 11139 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 11140 | if (list == null) { |
| 11141 | list = new ArrayList<Intent>(); |
| 11142 | mStickyBroadcasts.put(intent.getAction(), list); |
| 11143 | } |
| 11144 | int N = list.size(); |
| 11145 | int i; |
| 11146 | for (i=0; i<N; i++) { |
| 11147 | if (intent.filterEquals(list.get(i))) { |
| 11148 | // This sticky already exists, replace it. |
| 11149 | list.set(i, new Intent(intent)); |
| 11150 | break; |
| 11151 | } |
| 11152 | } |
| 11153 | if (i >= N) { |
| 11154 | list.add(new Intent(intent)); |
| 11155 | } |
| 11156 | } |
| 11157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11158 | // Figure out who all will receive this broadcast. |
| 11159 | List receivers = null; |
| 11160 | List<BroadcastFilter> registeredReceivers = null; |
| 11161 | try { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame^] | 11162 | // Need to resolve the intent to interested receivers... |
| 11163 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11164 | == 0) { |
| 11165 | receivers = AppGlobals.getPackageManager().queryIntentReceivers( |
| 11166 | intent, resolvedType, STOCK_PM_FLAGS, userId); |
| 11167 | } |
| 11168 | if (intent.getComponent() == null) { |
| 11169 | registeredReceivers = mReceiverResolver.queryIntent(intent, |
| 11170 | resolvedType, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11171 | } |
| 11172 | } catch (RemoteException ex) { |
| 11173 | // pm is in same process, this will never happen. |
| 11174 | } |
| 11175 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11176 | final boolean replacePending = |
| 11177 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11178 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11179 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11180 | + " replacePending=" + replacePending); |
| 11181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11182 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11183 | if (!ordered && NR > 0) { |
| 11184 | // If we are not serializing this broadcast, then send the |
| 11185 | // registered receivers separately so they don't wait for the |
| 11186 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11187 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11188 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11189 | callerPackage, callingPid, callingUid, requiredPermission, |
| 11190 | registeredReceivers, resultTo, resultCode, resultData, map, |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11191 | ordered, sticky, false, onlySendToCaller); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11192 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11193 | TAG, "Enqueueing parallel broadcast " + r); |
| 11194 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11195 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11196 | queue.enqueueParallelBroadcastLocked(r); |
| 11197 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11199 | registeredReceivers = null; |
| 11200 | NR = 0; |
| 11201 | } |
| 11202 | |
| 11203 | // Merge into one list. |
| 11204 | int ir = 0; |
| 11205 | if (receivers != null) { |
| 11206 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11207 | // being added to see this broadcast. This prevents them from |
| 11208 | // using this as a back door to get run as soon as they are |
| 11209 | // installed. Maybe in the future we want to have a special install |
| 11210 | // broadcast or such for apps, but we'd like to deliberately make |
| 11211 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11212 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11213 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11214 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11215 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11216 | Uri data = intent.getData(); |
| 11217 | if (data != null) { |
| 11218 | String pkgName = data.getSchemeSpecificPart(); |
| 11219 | if (pkgName != null) { |
| 11220 | skipPackages = new String[] { pkgName }; |
| 11221 | } |
| 11222 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11223 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11224 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11225 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11226 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 11227 | for (String skipPackage : skipPackages) { |
| 11228 | if (skipPackage != null) { |
| 11229 | int NT = receivers.size(); |
| 11230 | for (int it=0; it<NT; it++) { |
| 11231 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 11232 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 11233 | receivers.remove(it); |
| 11234 | it--; |
| 11235 | NT--; |
| 11236 | } |
| 11237 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11238 | } |
| 11239 | } |
| 11240 | } |
| 11241 | |
| 11242 | int NT = receivers != null ? receivers.size() : 0; |
| 11243 | int it = 0; |
| 11244 | ResolveInfo curt = null; |
| 11245 | BroadcastFilter curr = null; |
| 11246 | while (it < NT && ir < NR) { |
| 11247 | if (curt == null) { |
| 11248 | curt = (ResolveInfo)receivers.get(it); |
| 11249 | } |
| 11250 | if (curr == null) { |
| 11251 | curr = registeredReceivers.get(ir); |
| 11252 | } |
| 11253 | if (curr.getPriority() >= curt.priority) { |
| 11254 | // Insert this broadcast record into the final list. |
| 11255 | receivers.add(it, curr); |
| 11256 | ir++; |
| 11257 | curr = null; |
| 11258 | it++; |
| 11259 | NT++; |
| 11260 | } else { |
| 11261 | // Skip to the next ResolveInfo in the final list. |
| 11262 | it++; |
| 11263 | curt = null; |
| 11264 | } |
| 11265 | } |
| 11266 | } |
| 11267 | while (ir < NR) { |
| 11268 | if (receivers == null) { |
| 11269 | receivers = new ArrayList(); |
| 11270 | } |
| 11271 | receivers.add(registeredReceivers.get(ir)); |
| 11272 | ir++; |
| 11273 | } |
| 11274 | |
| 11275 | if ((receivers != null && receivers.size() > 0) |
| 11276 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11277 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11278 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11279 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11280 | receivers, resultTo, resultCode, resultData, map, ordered, |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11281 | sticky, false, onlySendToCaller); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11282 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11283 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11284 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11285 | if (DEBUG_BROADCAST) { |
| 11286 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11287 | 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] | 11288 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11289 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11290 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11291 | queue.enqueueOrderedBroadcastLocked(r); |
| 11292 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11293 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11294 | } |
| 11295 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11296 | return ActivityManager.BROADCAST_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11297 | } |
| 11298 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11299 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11300 | // Refuse possible leaked file descriptors |
| 11301 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11302 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11303 | } |
| 11304 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11305 | int flags = intent.getFlags(); |
| 11306 | |
| 11307 | if (!mProcessesReady) { |
| 11308 | // if the caller really truly claims to know what they're doing, go |
| 11309 | // ahead and allow the broadcast without launching any receivers |
| 11310 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 11311 | intent = new Intent(intent); |
| 11312 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 11313 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 11314 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 11315 | + " before boot completion"); |
| 11316 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 11317 | } |
| 11318 | } |
| 11319 | |
| 11320 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 11321 | throw new IllegalArgumentException( |
| 11322 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 11323 | } |
| 11324 | |
| 11325 | return intent; |
| 11326 | } |
| 11327 | |
| 11328 | public final int broadcastIntent(IApplicationThread caller, |
| 11329 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11330 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11331 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11332 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11333 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11334 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11336 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11337 | final int callingPid = Binder.getCallingPid(); |
| 11338 | final int callingUid = Binder.getCallingUid(); |
| 11339 | final long origId = Binder.clearCallingIdentity(); |
| 11340 | int res = broadcastIntentLocked(callerApp, |
| 11341 | callerApp != null ? callerApp.info.packageName : null, |
| 11342 | intent, resolvedType, resultTo, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11343 | resultCode, resultData, map, requiredPermission, serialized, sticky, |
| 11344 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11345 | Binder.restoreCallingIdentity(origId); |
| 11346 | return res; |
| 11347 | } |
| 11348 | } |
| 11349 | |
| 11350 | int broadcastIntentInPackage(String packageName, int uid, |
| 11351 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11352 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11353 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11354 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11355 | intent = verifyBroadcastLocked(intent); |
| 11356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11357 | final long origId = Binder.clearCallingIdentity(); |
| 11358 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 11359 | resultTo, resultCode, resultData, map, requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11360 | serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11361 | Binder.restoreCallingIdentity(origId); |
| 11362 | return res; |
| 11363 | } |
| 11364 | } |
| 11365 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11366 | // TODO: Use the userId; maybe mStickyBroadcasts need to be tied to the user. |
| 11367 | 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] | 11368 | // Refuse possible leaked file descriptors |
| 11369 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11370 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11371 | } |
| 11372 | |
| 11373 | synchronized(this) { |
| 11374 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 11375 | != PackageManager.PERMISSION_GRANTED) { |
| 11376 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 11377 | + Binder.getCallingPid() |
| 11378 | + ", uid=" + Binder.getCallingUid() |
| 11379 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11380 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11381 | throw new SecurityException(msg); |
| 11382 | } |
| 11383 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 11384 | if (list != null) { |
| 11385 | int N = list.size(); |
| 11386 | int i; |
| 11387 | for (i=0; i<N; i++) { |
| 11388 | if (intent.filterEquals(list.get(i))) { |
| 11389 | list.remove(i); |
| 11390 | break; |
| 11391 | } |
| 11392 | } |
| 11393 | } |
| 11394 | } |
| 11395 | } |
| 11396 | |
| 11397 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 11398 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 11399 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11400 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 11401 | if (r == null) { |
| 11402 | 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] | 11403 | return false; |
| 11404 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11405 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11406 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 11407 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11408 | } |
| 11409 | |
| 11410 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 11411 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11412 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11413 | |
| 11414 | // Refuse possible leaked file descriptors |
| 11415 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 11416 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11417 | } |
| 11418 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11419 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11420 | try { |
| 11421 | boolean doNext = false; |
| 11422 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11423 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11424 | synchronized(this) { |
| 11425 | r = broadcastRecordForReceiverLocked(who); |
| 11426 | if (r != null) { |
| 11427 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 11428 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11429 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11430 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11431 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11432 | if (doNext) { |
| 11433 | r.queue.processNextBroadcast(false); |
| 11434 | } |
| 11435 | trimApplications(); |
| 11436 | } finally { |
| 11437 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11439 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11440 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11441 | // ========================================================= |
| 11442 | // INSTRUMENTATION |
| 11443 | // ========================================================= |
| 11444 | |
| 11445 | public boolean startInstrumentation(ComponentName className, |
| 11446 | String profileFile, int flags, Bundle arguments, |
| 11447 | IInstrumentationWatcher watcher) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11448 | enforceNotIsolatedCaller("startInstrumentation"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11449 | // Refuse possible leaked file descriptors |
| 11450 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 11451 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11452 | } |
| 11453 | |
| 11454 | synchronized(this) { |
| 11455 | InstrumentationInfo ii = null; |
| 11456 | ApplicationInfo ai = null; |
| 11457 | try { |
| 11458 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11459 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11460 | ai = mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11461 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11462 | } catch (PackageManager.NameNotFoundException e) { |
| 11463 | } |
| 11464 | if (ii == null) { |
| 11465 | reportStartInstrumentationFailure(watcher, className, |
| 11466 | "Unable to find instrumentation info for: " + className); |
| 11467 | return false; |
| 11468 | } |
| 11469 | if (ai == null) { |
| 11470 | reportStartInstrumentationFailure(watcher, className, |
| 11471 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 11472 | return false; |
| 11473 | } |
| 11474 | |
| 11475 | int match = mContext.getPackageManager().checkSignatures( |
| 11476 | ii.targetPackage, ii.packageName); |
| 11477 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 11478 | String msg = "Permission Denial: starting instrumentation " |
| 11479 | + className + " from pid=" |
| 11480 | + Binder.getCallingPid() |
| 11481 | + ", uid=" + Binder.getCallingPid() |
| 11482 | + " not allowed because package " + ii.packageName |
| 11483 | + " does not have a signature matching the target " |
| 11484 | + ii.targetPackage; |
| 11485 | reportStartInstrumentationFailure(watcher, className, msg); |
| 11486 | throw new SecurityException(msg); |
| 11487 | } |
| 11488 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11489 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11490 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 11491 | // Instrumentation can kill and relaunch even persistent processes |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11492 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11493 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11494 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11495 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11496 | app.instrumentationProfileFile = profileFile; |
| 11497 | app.instrumentationArguments = arguments; |
| 11498 | app.instrumentationWatcher = watcher; |
| 11499 | app.instrumentationResultClass = className; |
| 11500 | Binder.restoreCallingIdentity(origId); |
| 11501 | } |
| 11502 | |
| 11503 | return true; |
| 11504 | } |
| 11505 | |
| 11506 | /** |
| 11507 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 11508 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 11509 | * the "am" command to report errors with more information. |
| 11510 | * |
| 11511 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 11512 | * @param cn The component name of the instrumentation. |
| 11513 | * @param report The error report. |
| 11514 | */ |
| 11515 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 11516 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11517 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11518 | try { |
| 11519 | if (watcher != null) { |
| 11520 | Bundle results = new Bundle(); |
| 11521 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 11522 | results.putString("Error", report); |
| 11523 | watcher.instrumentationStatus(cn, -1, results); |
| 11524 | } |
| 11525 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11526 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11527 | } |
| 11528 | } |
| 11529 | |
| 11530 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 11531 | if (app.instrumentationWatcher != null) { |
| 11532 | try { |
| 11533 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 11534 | app.instrumentationWatcher.instrumentationFinished( |
| 11535 | app.instrumentationClass, |
| 11536 | resultCode, |
| 11537 | results); |
| 11538 | } catch (RemoteException e) { |
| 11539 | } |
| 11540 | } |
| 11541 | app.instrumentationWatcher = null; |
| 11542 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11543 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11544 | app.instrumentationProfileFile = null; |
| 11545 | app.instrumentationArguments = null; |
| 11546 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11547 | forceStopPackageLocked(app.processName, -1, false, false, true, true, app.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11548 | } |
| 11549 | |
| 11550 | public void finishInstrumentation(IApplicationThread target, |
| 11551 | int resultCode, Bundle results) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11552 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11553 | // Refuse possible leaked file descriptors |
| 11554 | if (results != null && results.hasFileDescriptors()) { |
| 11555 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11556 | } |
| 11557 | |
| 11558 | synchronized(this) { |
| 11559 | ProcessRecord app = getRecordForAppLocked(target); |
| 11560 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11561 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11562 | return; |
| 11563 | } |
| 11564 | final long origId = Binder.clearCallingIdentity(); |
| 11565 | finishInstrumentationLocked(app, resultCode, results); |
| 11566 | Binder.restoreCallingIdentity(origId); |
| 11567 | } |
| 11568 | } |
| 11569 | |
| 11570 | // ========================================================= |
| 11571 | // CONFIGURATION |
| 11572 | // ========================================================= |
| 11573 | |
| 11574 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 11575 | ConfigurationInfo config = new ConfigurationInfo(); |
| 11576 | synchronized (this) { |
| 11577 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 11578 | config.reqKeyboardType = mConfiguration.keyboard; |
| 11579 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 11580 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 11581 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11582 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 11583 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 11584 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 11585 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11586 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 11587 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 11588 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11589 | } |
| 11590 | return config; |
| 11591 | } |
| 11592 | |
| 11593 | public Configuration getConfiguration() { |
| 11594 | Configuration ci; |
| 11595 | synchronized(this) { |
| 11596 | ci = new Configuration(mConfiguration); |
| 11597 | } |
| 11598 | return ci; |
| 11599 | } |
| 11600 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 11601 | public void updatePersistentConfiguration(Configuration values) { |
| 11602 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 11603 | "updateConfiguration()"); |
| 11604 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 11605 | "updateConfiguration()"); |
| 11606 | if (values == null) { |
| 11607 | throw new NullPointerException("Configuration must not be null"); |
| 11608 | } |
| 11609 | |
| 11610 | synchronized(this) { |
| 11611 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11612 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 11613 | Binder.restoreCallingIdentity(origId); |
| 11614 | } |
| 11615 | } |
| 11616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11617 | public void updateConfiguration(Configuration values) { |
| 11618 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 11619 | "updateConfiguration()"); |
| 11620 | |
| 11621 | synchronized(this) { |
| 11622 | if (values == null && mWindowManager != null) { |
| 11623 | // sentinel: fetch the current configuration from the window manager |
| 11624 | values = mWindowManager.computeNewConfiguration(); |
| 11625 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 11626 | |
| 11627 | if (mWindowManager != null) { |
| 11628 | mProcessList.applyDisplaySize(mWindowManager); |
| 11629 | } |
| 11630 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11631 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 11632 | if (values != null) { |
| 11633 | Settings.System.clearConfiguration(values); |
| 11634 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11635 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11636 | Binder.restoreCallingIdentity(origId); |
| 11637 | } |
| 11638 | } |
| 11639 | |
| 11640 | /** |
| 11641 | * Do either or both things: (1) change the current configuration, and (2) |
| 11642 | * make sure the given activity is running with the (now) current |
| 11643 | * configuration. Returns true if the activity has been left running, or |
| 11644 | * false if <var>starting</var> is being destroyed to match the new |
| 11645 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 11646 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11647 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11648 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11649 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 11650 | // do nothing if we are headless |
| 11651 | if (mHeadless) return true; |
| 11652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11653 | int changes = 0; |
| 11654 | |
| 11655 | boolean kept = true; |
| 11656 | |
| 11657 | if (values != null) { |
| 11658 | Configuration newConfig = new Configuration(mConfiguration); |
| 11659 | changes = newConfig.updateFrom(values); |
| 11660 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 11661 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11662 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11663 | } |
| 11664 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11665 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11666 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11667 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11668 | saveLocaleLocked(values.locale, |
| 11669 | !values.locale.equals(mConfiguration.locale), |
| 11670 | values.userSetLocale); |
| 11671 | } |
| 11672 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 11673 | mConfigurationSeq++; |
| 11674 | if (mConfigurationSeq <= 0) { |
| 11675 | mConfigurationSeq = 1; |
| 11676 | } |
| 11677 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11678 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11679 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11680 | |
| 11681 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 11682 | |
| 11683 | // TODO: If our config changes, should we auto dismiss any currently |
| 11684 | // showing dialogs? |
| 11685 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11686 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 11687 | AttributeCache ac = AttributeCache.instance(); |
| 11688 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11689 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 11690 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11691 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 11692 | // Make sure all resources in our process are updated |
| 11693 | // right now, so that anyone who is going to retrieve |
| 11694 | // resource values after we return will be sure to get |
| 11695 | // the new ones. This is especially important during |
| 11696 | // boot, where the first config change needs to guarantee |
| 11697 | // all resources have that config before following boot |
| 11698 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11699 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 11700 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 11701 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 11702 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11703 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 11704 | mHandler.sendMessage(msg); |
| 11705 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11706 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11707 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 11708 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11709 | try { |
| 11710 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11711 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 11712 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11713 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11714 | } |
| 11715 | } catch (Exception e) { |
| 11716 | } |
| 11717 | } |
| 11718 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11719 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 11720 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11721 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11722 | null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 11723 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 11724 | broadcastIntentLocked(null, null, |
| 11725 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 11726 | null, null, 0, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11727 | null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 11728 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11729 | } |
| 11730 | } |
| 11731 | |
| 11732 | if (changes != 0 && starting == null) { |
| 11733 | // If the configuration changed, and the caller is not already |
| 11734 | // in the process of starting an activity, then find the top |
| 11735 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 11736 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11737 | } |
| 11738 | |
| 11739 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 11740 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 11741 | // And we need to make sure at this point that all other activities |
| 11742 | // are made visible with the correct configuration. |
| 11743 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11744 | } |
| 11745 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 11746 | if (values != null && mWindowManager != null) { |
| 11747 | mWindowManager.setNewConfiguration(mConfiguration); |
| 11748 | } |
| 11749 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11750 | return kept; |
| 11751 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 11752 | |
| 11753 | /** |
| 11754 | * Decide based on the configuration whether we should shouw the ANR, |
| 11755 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 11756 | * press the on-screen buttons, we shouldn't show the dialog. |
| 11757 | * |
| 11758 | * A thought: SystemUI might also want to get told about this, the Power |
| 11759 | * dialog / global actions also might want different behaviors. |
| 11760 | */ |
| 11761 | private static final boolean shouldShowDialogs(Configuration config) { |
| 11762 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 11763 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 11764 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11765 | |
| 11766 | /** |
| 11767 | * Save the locale. You must be inside a synchronized (this) block. |
| 11768 | */ |
| 11769 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 11770 | if(isDiff) { |
| 11771 | SystemProperties.set("user.language", l.getLanguage()); |
| 11772 | SystemProperties.set("user.region", l.getCountry()); |
| 11773 | } |
| 11774 | |
| 11775 | if(isPersist) { |
| 11776 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 11777 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 11778 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 11779 | } |
| 11780 | } |
| 11781 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 11782 | @Override |
| 11783 | public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) { |
| 11784 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 11785 | return srec != null && srec.task.affinity != null && |
| 11786 | srec.task.affinity.equals(destAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 11787 | } |
| 11788 | |
| 11789 | public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode, |
| 11790 | Intent resultData) { |
| 11791 | ComponentName dest = destIntent.getComponent(); |
| 11792 | |
| 11793 | synchronized (this) { |
| 11794 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 11795 | if (srec == null) { |
| 11796 | return false; |
| 11797 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 11798 | ArrayList<ActivityRecord> history = srec.stack.mHistory; |
| 11799 | final int start = history.indexOf(srec); |
| 11800 | if (start < 0) { |
| 11801 | // Current activity is not in history stack; do nothing. |
| 11802 | return false; |
| 11803 | } |
| 11804 | int finishTo = start - 1; |
| 11805 | ActivityRecord parent = null; |
| 11806 | boolean foundParentInTask = false; |
| 11807 | if (dest != null) { |
| 11808 | TaskRecord tr = srec.task; |
| 11809 | for (int i = start - 1; i >= 0; i--) { |
| 11810 | ActivityRecord r = history.get(i); |
| 11811 | if (tr != r.task) { |
| 11812 | // Couldn't find parent in the same task; stop at the one above this. |
| 11813 | // (Root of current task; in-app "home" behavior) |
| 11814 | // Always at least finish the current activity. |
| 11815 | finishTo = Math.min(start - 1, i + 1); |
| 11816 | parent = history.get(finishTo); |
| 11817 | break; |
| 11818 | } else if (r.info.packageName.equals(dest.getPackageName()) && |
| 11819 | r.info.name.equals(dest.getClassName())) { |
| 11820 | finishTo = i; |
| 11821 | parent = r; |
| 11822 | foundParentInTask = true; |
| 11823 | break; |
| 11824 | } |
| 11825 | } |
| 11826 | } |
| 11827 | |
| 11828 | if (mController != null) { |
| 11829 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
| 11830 | if (next != null) { |
| 11831 | // ask watcher if this is allowed |
| 11832 | boolean resumeOK = true; |
| 11833 | try { |
| 11834 | resumeOK = mController.activityResuming(next.packageName); |
| 11835 | } catch (RemoteException e) { |
| 11836 | mController = null; |
| 11837 | } |
| 11838 | |
| 11839 | if (!resumeOK) { |
| 11840 | return false; |
| 11841 | } |
| 11842 | } |
| 11843 | } |
| 11844 | final long origId = Binder.clearCallingIdentity(); |
| 11845 | for (int i = start; i > finishTo; i--) { |
| 11846 | ActivityRecord r = history.get(i); |
| 11847 | mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData, |
| 11848 | "navigate-up"); |
| 11849 | // Only return the supplied result for the first activity finished |
| 11850 | resultCode = Activity.RESULT_CANCELED; |
| 11851 | resultData = null; |
| 11852 | } |
| 11853 | |
| 11854 | if (parent != null && foundParentInTask) { |
| 11855 | final int parentLaunchMode = parent.info.launchMode; |
| 11856 | final int destIntentFlags = destIntent.getFlags(); |
| 11857 | if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || |
| 11858 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK || |
| 11859 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP || |
| 11860 | (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
Adam Powell | 69de7e1 | 2012-05-07 18:42:24 -0700 | [diff] [blame] | 11861 | parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 11862 | } else { |
| 11863 | try { |
| 11864 | ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo( |
| 11865 | destIntent.getComponent(), 0, UserId.getCallingUserId()); |
| 11866 | int res = mMainStack.startActivityLocked(srec.app.thread, destIntent, |
| 11867 | null, aInfo, parent.appToken, null, |
| 11868 | 0, -1, parent.launchedFromUid, 0, null, true, null); |
| 11869 | foundParentInTask = res == ActivityManager.START_SUCCESS; |
| 11870 | } catch (RemoteException e) { |
| 11871 | foundParentInTask = false; |
| 11872 | } |
| 11873 | mMainStack.requestFinishActivityLocked(parent.appToken, resultCode, |
| 11874 | resultData, "navigate-up"); |
| 11875 | } |
| 11876 | } |
| 11877 | Binder.restoreCallingIdentity(origId); |
| 11878 | return foundParentInTask; |
| 11879 | } |
| 11880 | } |
| 11881 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 11882 | public int getLaunchedFromUid(IBinder activityToken) { |
| 11883 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 11884 | if (srec == null) { |
| 11885 | return -1; |
| 11886 | } |
| 11887 | return srec.launchedFromUid; |
| 11888 | } |
| 11889 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11890 | // ========================================================= |
| 11891 | // LIFETIME MANAGEMENT |
| 11892 | // ========================================================= |
| 11893 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11894 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 11895 | // on, or 'null' if the app is not an active broadcast recipient. |
| 11896 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 11897 | BroadcastRecord r = app.curReceiver; |
| 11898 | if (r != null) { |
| 11899 | return r.queue; |
| 11900 | } |
| 11901 | |
| 11902 | // It's not the current receiver, but it might be starting up to become one |
| 11903 | synchronized (this) { |
| 11904 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11905 | r = queue.mPendingBroadcast; |
| 11906 | if (r != null && r.curApp == app) { |
| 11907 | // found it; report which queue it's in |
| 11908 | return queue; |
| 11909 | } |
| 11910 | } |
| 11911 | } |
| 11912 | |
| 11913 | return null; |
| 11914 | } |
| 11915 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11916 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 11917 | ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11918 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11919 | // This adjustment has already been computed. If we are calling |
| 11920 | // from the top, we may have already computed our adjustment with |
| 11921 | // an earlier hidden adjustment that isn't really for us... if |
| 11922 | // so, use the new hidden adjustment. |
| 11923 | if (!recursed && app.hidden) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 11924 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11925 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11926 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11927 | } |
| 11928 | |
| 11929 | if (app.thread == null) { |
| 11930 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11931 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 11932 | return (app.curAdj=ProcessList.HIDDEN_APP_MAX_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11933 | } |
| 11934 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11935 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 11936 | app.adjSource = null; |
| 11937 | app.adjTarget = null; |
| 11938 | app.empty = false; |
| 11939 | app.hidden = false; |
| 11940 | |
| 11941 | final int activitiesSize = app.activities.size(); |
| 11942 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 11943 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11944 | // The max adjustment doesn't allow this app to be anything |
| 11945 | // below foreground, so it is not worth doing work for it. |
| 11946 | app.adjType = "fixed"; |
| 11947 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 11948 | app.curRawAdj = app.nonStoppingAdj = app.maxAdj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11949 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11950 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11951 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11952 | // System process can do UI, and when they do we want to have |
| 11953 | // them trim their memory after the user leaves the UI. To |
| 11954 | // facilitate this, here we need to determine whether or not it |
| 11955 | // is currently showing UI. |
| 11956 | app.systemNoUi = true; |
| 11957 | if (app == TOP_APP) { |
| 11958 | app.systemNoUi = false; |
| 11959 | } else if (activitiesSize > 0) { |
| 11960 | for (int j = 0; j < activitiesSize; j++) { |
| 11961 | final ActivityRecord r = app.activities.get(j); |
| 11962 | if (r.visible) { |
| 11963 | app.systemNoUi = false; |
| 11964 | break; |
| 11965 | } |
| 11966 | } |
| 11967 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11968 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 11969 | } |
| 11970 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11971 | app.keeping = false; |
| 11972 | app.systemNoUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11973 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 11974 | // Determine the importance of the process, starting with most |
| 11975 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11976 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11977 | int schedGroup; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 11978 | boolean foregroundActivities = false; |
| 11979 | boolean interesting = false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11980 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11981 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11982 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 11983 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11984 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11985 | app.adjType = "top-activity"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 11986 | foregroundActivities = true; |
| 11987 | interesting = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11988 | } else if (app.instrumentationClass != null) { |
| 11989 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 11990 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11991 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11992 | app.adjType = "instrumentation"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 11993 | interesting = true; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11994 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11995 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11996 | // counts as being in the foreground for OOM killer purposes. |
| 11997 | // It's placed in a sched group based on the nature of the |
| 11998 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 11999 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12000 | schedGroup = (queue == mFgBroadcastQueue) |
| 12001 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12002 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12003 | } else if (app.executingServices.size() > 0) { |
| 12004 | // An app that is currently executing a service callback also |
| 12005 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12006 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12007 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12008 | app.adjType = "exec-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12009 | } else if (activitiesSize > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12010 | // This app is in the background with paused activities. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12011 | // We inspect activities to potentially upgrade adjustment further below. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12012 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12013 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12014 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12015 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12016 | } else { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12017 | // A very not-needed process. If this is lower in the lru list, |
| 12018 | // we will push it in to the empty bucket. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12019 | adj = hiddenAdj; |
| 12020 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12021 | app.hidden = true; |
| 12022 | app.empty = true; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12023 | app.adjType = "bg-empty"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12024 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12025 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12026 | boolean hasStoppingActivities = false; |
| 12027 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12028 | // Examine all activities if not already foreground. |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12029 | if (!foregroundActivities && activitiesSize > 0) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12030 | for (int j = 0; j < activitiesSize; j++) { |
| 12031 | final ActivityRecord r = app.activities.get(j); |
| 12032 | if (r.visible) { |
| 12033 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12034 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12035 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12036 | app.adjType = "visible"; |
| 12037 | } |
| 12038 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12039 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12040 | foregroundActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12041 | break; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12042 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12043 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12044 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12045 | app.adjType = "pausing"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12046 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 12047 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12048 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12049 | } else if (r.state == ActivityState.STOPPING) { |
| 12050 | // We will apply the actual adjustment later, because |
| 12051 | // we want to allow this process to immediately go through |
| 12052 | // any memory trimming that is in effect. |
| 12053 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12054 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12055 | hasStoppingActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12056 | } |
| 12057 | } |
| 12058 | } |
| 12059 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12060 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12061 | if (app.foregroundServices) { |
| 12062 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12063 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12064 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12065 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12066 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12067 | } else if (app.forcingToForeground != null) { |
| 12068 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12069 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12070 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12071 | app.adjType = "force-foreground"; |
| 12072 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12073 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12074 | } |
| 12075 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12076 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12077 | if (app.foregroundServices) { |
| 12078 | interesting = true; |
| 12079 | } |
| 12080 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12081 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12082 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12083 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12084 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12085 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12086 | app.adjType = "heavy"; |
| 12087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12088 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12089 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12090 | // This process is hosting what we currently consider to be the |
| 12091 | // 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] | 12092 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12093 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12094 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12095 | app.adjType = "home"; |
| 12096 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12097 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 12098 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 12099 | && app.activities.size() > 0) { |
| 12100 | // This was the previous process that showed UI to the user. |
| 12101 | // We want to try to keep it around more aggressively, to give |
| 12102 | // a good experience around switching between two apps. |
| 12103 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 12104 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12105 | app.hidden = false; |
| 12106 | app.adjType = "previous"; |
| 12107 | } |
| 12108 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12109 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 12110 | + " reason=" + app.adjType); |
| 12111 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12112 | // 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] | 12113 | // there are applications dependent on our services or providers, but |
| 12114 | // this gives us a baseline and makes sure we don't get into an |
| 12115 | // infinite recursion. |
| 12116 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12117 | app.curRawAdj = app.nonStoppingAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12118 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12119 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 12120 | // 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] | 12121 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12122 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12123 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12124 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12125 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12126 | } |
| 12127 | } |
| 12128 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12129 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12130 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12131 | final long now = SystemClock.uptimeMillis(); |
| 12132 | // This process is more important if the top activity is |
| 12133 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12134 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12135 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12136 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12137 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12138 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12139 | // If this process has shown some UI, let it immediately |
| 12140 | // go to the LRU list because it may be pretty heavy with |
| 12141 | // UI stuff. We'll tag it with a label just to help |
| 12142 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12143 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12144 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12145 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12146 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12147 | if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12148 | // This service has seen some activity within |
| 12149 | // recent memory, so we will keep its process ahead |
| 12150 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12151 | if (adj > ProcessList.SERVICE_ADJ) { |
| 12152 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12153 | app.adjType = "started-services"; |
| 12154 | app.hidden = false; |
| 12155 | } |
| 12156 | } |
| 12157 | // If we have let the service slide into the background |
| 12158 | // state, still have some text describing what it is doing |
| 12159 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12160 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12161 | app.adjType = "started-bg-services"; |
| 12162 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12163 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12164 | // Don't kill this process because it is doing work; it |
| 12165 | // has said it is doing work. |
| 12166 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12167 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12168 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12169 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12170 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12171 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12172 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12173 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12174 | 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] | 12175 | // XXX should compute this based on the max of |
| 12176 | // all connected clients. |
| 12177 | ConnectionRecord cr = clist.get(i); |
| 12178 | if (cr.binding.client == app) { |
| 12179 | // Binding to ourself is not interesting. |
| 12180 | continue; |
| 12181 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12182 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12183 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12184 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12185 | int myHiddenAdj = hiddenAdj; |
| 12186 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12187 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12188 | myHiddenAdj = client.hiddenAdj; |
| 12189 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12190 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12191 | } |
| 12192 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12193 | clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12194 | client, myHiddenAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12195 | String adjType = null; |
| 12196 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 12197 | // Not doing bind OOM management, so treat |
| 12198 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12199 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12200 | // If this process has shown some UI, let it immediately |
| 12201 | // go to the LRU list because it may be pretty heavy with |
| 12202 | // UI stuff. We'll tag it with a label just to help |
| 12203 | // debug and understand what is going on. |
| 12204 | if (adj > clientAdj) { |
| 12205 | adjType = "bound-bg-ui-services"; |
| 12206 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12207 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12208 | clientAdj = adj; |
| 12209 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12210 | if (now >= (s.lastActivity |
| 12211 | + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12212 | // This service has not seen activity within |
| 12213 | // recent memory, so allow it to drop to the |
| 12214 | // LRU list if there is no other reason to keep |
| 12215 | // it around. We'll also tag it with a label just |
| 12216 | // to help debug and undertand what is going on. |
| 12217 | if (adj > clientAdj) { |
| 12218 | adjType = "bound-bg-services"; |
| 12219 | } |
| 12220 | clientAdj = adj; |
| 12221 | } |
| 12222 | } |
| 12223 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12224 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12225 | // If this process has recently shown UI, and |
| 12226 | // the process that is binding to it is less |
| 12227 | // important than being visible, then we don't |
| 12228 | // care about the binding as much as we care |
| 12229 | // about letting this process get into the LRU |
| 12230 | // list to be killed and restarted if needed for |
| 12231 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12232 | if (app.hasShownUi && app != mHomeProcess |
| 12233 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12234 | adjType = "bound-bg-ui-services"; |
| 12235 | } else { |
| 12236 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 12237 | |Context.BIND_IMPORTANT)) != 0) { |
| 12238 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12239 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 12240 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 12241 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12242 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12243 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12244 | adj = clientAdj; |
| 12245 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12246 | app.pendingUiClean = true; |
| 12247 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12248 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 12249 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12250 | } |
| 12251 | if (!client.hidden) { |
| 12252 | app.hidden = false; |
| 12253 | } |
| 12254 | if (client.keeping) { |
| 12255 | app.keeping = true; |
| 12256 | } |
| 12257 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12258 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12259 | } |
| 12260 | if (adjType != null) { |
| 12261 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12262 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12263 | .REASON_SERVICE_IN_USE; |
| 12264 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12265 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12266 | app.adjTarget = s.name; |
| 12267 | } |
| 12268 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12269 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12270 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12272 | } |
| 12273 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12274 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
| 12275 | ActivityRecord a = cr.activity; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12276 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12277 | (a.visible || a.state == ActivityState.RESUMED |
| 12278 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12279 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12280 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12281 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12282 | } |
| 12283 | app.hidden = false; |
| 12284 | app.adjType = "service"; |
| 12285 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12286 | .REASON_SERVICE_IN_USE; |
| 12287 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12288 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12289 | app.adjTarget = s.name; |
| 12290 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12291 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12292 | } |
| 12293 | } |
| 12294 | } |
| 12295 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12296 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12297 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12298 | // would like to avoid killing it unless it would prevent the current |
| 12299 | // application from running. By default we put the process in |
| 12300 | // with the rest of the background processes; as we scan through |
| 12301 | // its services we may bump it up from there. |
| 12302 | if (adj > hiddenAdj) { |
| 12303 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12304 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12305 | app.adjType = "bg-services"; |
| 12306 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12307 | } |
| 12308 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12309 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12310 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12311 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12312 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12313 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12314 | ContentProviderRecord cpr = jt.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12315 | for (int i = cpr.connections.size()-1; |
| 12316 | i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
| 12317 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE); |
| 12318 | i--) { |
| 12319 | ContentProviderConnection conn = cpr.connections.get(i); |
| 12320 | ProcessRecord client = conn.client; |
| 12321 | if (client == app) { |
| 12322 | // Being our own client is not interesting. |
| 12323 | continue; |
| 12324 | } |
| 12325 | int myHiddenAdj = hiddenAdj; |
| 12326 | if (myHiddenAdj > client.hiddenAdj) { |
| 12327 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12328 | myHiddenAdj = client.hiddenAdj; |
| 12329 | } else { |
| 12330 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12331 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12332 | } |
| 12333 | int clientAdj = computeOomAdjLocked( |
| 12334 | client, myHiddenAdj, TOP_APP, true, doingAll); |
| 12335 | if (adj > clientAdj) { |
| 12336 | if (app.hasShownUi && app != mHomeProcess |
| 12337 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12338 | app.adjType = "bg-ui-provider"; |
| 12339 | } else { |
| 12340 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 12341 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
| 12342 | app.adjType = "provider"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12343 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12344 | if (!client.hidden) { |
| 12345 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12346 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12347 | if (client.keeping) { |
| 12348 | app.keeping = true; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12349 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12350 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12351 | .REASON_PROVIDER_IN_USE; |
| 12352 | app.adjSource = client; |
| 12353 | app.adjSourceOom = clientAdj; |
| 12354 | app.adjTarget = cpr.name; |
| 12355 | } |
| 12356 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12357 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12358 | } |
| 12359 | } |
| 12360 | // If the provider has external (non-framework) process |
| 12361 | // dependencies, ensure that its adjustment is at least |
| 12362 | // FOREGROUND_APP_ADJ. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 12363 | if (cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12364 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12365 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12366 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12367 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12368 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12369 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12370 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12371 | } |
| 12372 | } |
| 12373 | } |
| 12374 | } |
| 12375 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12376 | if (adj == ProcessList.SERVICE_ADJ) { |
| 12377 | if (doingAll) { |
| 12378 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 12379 | mNewNumServiceProcs++; |
| 12380 | } |
| 12381 | if (app.serviceb) { |
| 12382 | adj = ProcessList.SERVICE_B_ADJ; |
| 12383 | } |
| 12384 | } else { |
| 12385 | app.serviceb = false; |
| 12386 | } |
| 12387 | |
| 12388 | app.nonStoppingAdj = adj; |
| 12389 | |
| 12390 | if (hasStoppingActivities) { |
| 12391 | // Only upgrade adjustment. |
| 12392 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12393 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12394 | app.adjType = "stopping"; |
| 12395 | } |
| 12396 | } |
| 12397 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12398 | app.curRawAdj = adj; |
| 12399 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12400 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12401 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 12402 | if (adj > app.maxAdj) { |
| 12403 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12404 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12405 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12406 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12407 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12408 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12409 | app.keeping = true; |
| 12410 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12411 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12412 | if (app.hasAboveClient) { |
| 12413 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 12414 | // then we need to drop its adjustment to be lower than the service's |
| 12415 | // in order to honor the request. We want to drop it by one adjustment |
| 12416 | // level... but there is special meaning applied to various levels so |
| 12417 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12418 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12419 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12420 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 12421 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 12422 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12423 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12424 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 12425 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12426 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12427 | adj++; |
| 12428 | } |
| 12429 | } |
| 12430 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12431 | int importance = app.memImportance; |
| 12432 | if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) { |
| 12433 | app.curAdj = adj; |
| 12434 | app.curSchedGroup = schedGroup; |
| 12435 | if (!interesting) { |
| 12436 | // For this reporting, if there is not something explicitly |
| 12437 | // interesting in this process then we will push it to the |
| 12438 | // background importance. |
| 12439 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12440 | } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 12441 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12442 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 12443 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 12444 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 12445 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12446 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
| 12447 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 12448 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 12449 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 12450 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12451 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 12452 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 12453 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 12454 | } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 12455 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 12456 | } else { |
| 12457 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT; |
| 12458 | } |
| 12459 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12460 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12461 | int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0; |
| 12462 | if (foregroundActivities != app.foregroundActivities) { |
| 12463 | changes |= ProcessChangeItem.CHANGE_ACTIVITIES; |
| 12464 | } |
| 12465 | if (changes != 0) { |
| 12466 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes); |
| 12467 | app.memImportance = importance; |
| 12468 | app.foregroundActivities = foregroundActivities; |
| 12469 | int i = mPendingProcessChanges.size()-1; |
| 12470 | ProcessChangeItem item = null; |
| 12471 | while (i >= 0) { |
| 12472 | item = mPendingProcessChanges.get(i); |
| 12473 | if (item.pid == app.pid) { |
| 12474 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item); |
| 12475 | break; |
| 12476 | } |
| 12477 | i--; |
| 12478 | } |
| 12479 | if (i < 0) { |
| 12480 | // No existing item in pending changes; need a new one. |
| 12481 | final int NA = mAvailProcessChanges.size(); |
| 12482 | if (NA > 0) { |
| 12483 | item = mAvailProcessChanges.remove(NA-1); |
| 12484 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item); |
| 12485 | } else { |
| 12486 | item = new ProcessChangeItem(); |
| 12487 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item); |
| 12488 | } |
| 12489 | item.changes = 0; |
| 12490 | item.pid = app.pid; |
| 12491 | item.uid = app.info.uid; |
| 12492 | if (mPendingProcessChanges.size() == 0) { |
| 12493 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, |
| 12494 | "*** Enqueueing dispatch processes changed!"); |
| 12495 | mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget(); |
| 12496 | } |
| 12497 | mPendingProcessChanges.add(item); |
| 12498 | } |
| 12499 | item.changes |= changes; |
| 12500 | item.importance = importance; |
| 12501 | item.foregroundActivities = foregroundActivities; |
| 12502 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item " |
| 12503 | + Integer.toHexString(System.identityHashCode(item)) |
| 12504 | + " " + app.toShortString() + ": changes=" + item.changes |
| 12505 | + " importance=" + item.importance |
| 12506 | + " foreground=" + item.foregroundActivities |
| 12507 | + " type=" + app.adjType + " source=" + app.adjSource |
| 12508 | + " target=" + app.adjTarget); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12509 | } |
| 12510 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12511 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12512 | } |
| 12513 | |
| 12514 | /** |
| 12515 | * Ask a given process to GC right now. |
| 12516 | */ |
| 12517 | final void performAppGcLocked(ProcessRecord app) { |
| 12518 | try { |
| 12519 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 12520 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12521 | if (app.reportLowMemory) { |
| 12522 | app.reportLowMemory = false; |
| 12523 | app.thread.scheduleLowMemory(); |
| 12524 | } else { |
| 12525 | app.thread.processInBackground(); |
| 12526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12527 | } |
| 12528 | } catch (Exception e) { |
| 12529 | // whatever. |
| 12530 | } |
| 12531 | } |
| 12532 | |
| 12533 | /** |
| 12534 | * Returns true if things are idle enough to perform GCs. |
| 12535 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12536 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12537 | boolean processingBroadcasts = false; |
| 12538 | for (BroadcastQueue q : mBroadcastQueues) { |
| 12539 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 12540 | processingBroadcasts = true; |
| 12541 | } |
| 12542 | } |
| 12543 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12544 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 12545 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12546 | } |
| 12547 | |
| 12548 | /** |
| 12549 | * Perform GCs on all processes that are waiting for it, but only |
| 12550 | * if things are idle. |
| 12551 | */ |
| 12552 | final void performAppGcsLocked() { |
| 12553 | final int N = mProcessesToGc.size(); |
| 12554 | if (N <= 0) { |
| 12555 | return; |
| 12556 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12557 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12558 | while (mProcessesToGc.size() > 0) { |
| 12559 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12560 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12561 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 12562 | <= SystemClock.uptimeMillis()) { |
| 12563 | // To avoid spamming the system, we will GC processes one |
| 12564 | // at a time, waiting a few seconds between each. |
| 12565 | performAppGcLocked(proc); |
| 12566 | scheduleAppGcsLocked(); |
| 12567 | return; |
| 12568 | } else { |
| 12569 | // It hasn't been long enough since we last GCed this |
| 12570 | // process... put it in the list to wait for its time. |
| 12571 | addProcessToGcListLocked(proc); |
| 12572 | break; |
| 12573 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12574 | } |
| 12575 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12576 | |
| 12577 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12578 | } |
| 12579 | } |
| 12580 | |
| 12581 | /** |
| 12582 | * If all looks good, perform GCs on all processes waiting for them. |
| 12583 | */ |
| 12584 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12585 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12586 | performAppGcsLocked(); |
| 12587 | return; |
| 12588 | } |
| 12589 | // Still not idle, wait some more. |
| 12590 | scheduleAppGcsLocked(); |
| 12591 | } |
| 12592 | |
| 12593 | /** |
| 12594 | * Schedule the execution of all pending app GCs. |
| 12595 | */ |
| 12596 | final void scheduleAppGcsLocked() { |
| 12597 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12598 | |
| 12599 | if (mProcessesToGc.size() > 0) { |
| 12600 | // Schedule a GC for the time to the next process. |
| 12601 | ProcessRecord proc = mProcessesToGc.get(0); |
| 12602 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 12603 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12604 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12605 | long now = SystemClock.uptimeMillis(); |
| 12606 | if (when < (now+GC_TIMEOUT)) { |
| 12607 | when = now + GC_TIMEOUT; |
| 12608 | } |
| 12609 | mHandler.sendMessageAtTime(msg, when); |
| 12610 | } |
| 12611 | } |
| 12612 | |
| 12613 | /** |
| 12614 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 12615 | * list in sorted order by the last GC time. The process can't already be |
| 12616 | * on the list. |
| 12617 | */ |
| 12618 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 12619 | boolean added = false; |
| 12620 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 12621 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 12622 | proc.lastRequestedGc) { |
| 12623 | added = true; |
| 12624 | mProcessesToGc.add(i+1, proc); |
| 12625 | break; |
| 12626 | } |
| 12627 | } |
| 12628 | if (!added) { |
| 12629 | mProcessesToGc.add(0, proc); |
| 12630 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12631 | } |
| 12632 | |
| 12633 | /** |
| 12634 | * Set up to ask a process to GC itself. This will either do it |
| 12635 | * immediately, or put it on the list of processes to gc the next |
| 12636 | * time things are idle. |
| 12637 | */ |
| 12638 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 12639 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12640 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12641 | return; |
| 12642 | } |
| 12643 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12644 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12645 | scheduleAppGcsLocked(); |
| 12646 | } |
| 12647 | } |
| 12648 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12649 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 12650 | updateCpuStatsNow(); |
| 12651 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12652 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12653 | boolean doWakeKills = doKills; |
| 12654 | boolean doCpuKills = doKills; |
| 12655 | if (mLastPowerCheckRealtime == 0) { |
| 12656 | doWakeKills = false; |
| 12657 | } |
| 12658 | if (mLastPowerCheckUptime == 0) { |
| 12659 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12660 | } |
| 12661 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12662 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12663 | } |
| 12664 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12665 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 12666 | final long curUptime = SystemClock.uptimeMillis(); |
| 12667 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 12668 | mLastPowerCheckRealtime = curRealtime; |
| 12669 | mLastPowerCheckUptime = curUptime; |
| 12670 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 12671 | doWakeKills = false; |
| 12672 | } |
| 12673 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 12674 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12675 | } |
| 12676 | int i = mLruProcesses.size(); |
| 12677 | while (i > 0) { |
| 12678 | i--; |
| 12679 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12680 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12681 | long wtime; |
| 12682 | synchronized (stats) { |
| 12683 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 12684 | app.pid, curRealtime); |
| 12685 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12686 | long wtimeUsed = wtime - app.lastWakeTime; |
| 12687 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 12688 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 12689 | StringBuilder sb = new StringBuilder(128); |
| 12690 | sb.append("Wake for "); |
| 12691 | app.toShortString(sb); |
| 12692 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12693 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 12694 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12695 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 12696 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12697 | sb.append((wtimeUsed*100)/realtimeSince); |
| 12698 | sb.append("%)"); |
| 12699 | Slog.i(TAG, sb.toString()); |
| 12700 | sb.setLength(0); |
| 12701 | sb.append("CPU for "); |
| 12702 | app.toShortString(sb); |
| 12703 | sb.append(": over "); |
| 12704 | TimeUtils.formatDuration(uptimeSince, sb); |
| 12705 | sb.append(" used "); |
| 12706 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 12707 | sb.append(" ("); |
| 12708 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 12709 | sb.append("%)"); |
| 12710 | Slog.i(TAG, sb.toString()); |
| 12711 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12712 | // If a process has held a wake lock for more |
| 12713 | // than 50% of the time during this period, |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12714 | // that sounds bad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12715 | if (doWakeKills && realtimeSince > 0 |
| 12716 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 12717 | synchronized (stats) { |
| 12718 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 12719 | realtimeSince, wtimeUsed); |
| 12720 | } |
| 12721 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 12722 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 12723 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12724 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 12725 | app.processName, app.setAdj, "excessive wake lock"); |
| 12726 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12727 | } else if (doCpuKills && uptimeSince > 0 |
| 12728 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 12729 | synchronized (stats) { |
| 12730 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 12731 | uptimeSince, cputimeUsed); |
| 12732 | } |
| 12733 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 12734 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 12735 | + " during " + uptimeSince); |
| 12736 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 12737 | app.processName, app.setAdj, "excessive cpu"); |
| 12738 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12739 | } else { |
| 12740 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12741 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12742 | } |
| 12743 | } |
| 12744 | } |
| 12745 | } |
| 12746 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 12747 | private final boolean updateOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12748 | ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12749 | app.hiddenAdj = hiddenAdj; |
| 12750 | |
| 12751 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 12752 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12753 | } |
| 12754 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12755 | final boolean wasKeeping = app.keeping; |
| 12756 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 12757 | boolean success = true; |
| 12758 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12759 | computeOomAdjLocked(app, hiddenAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12760 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 12761 | if (app.curRawAdj != app.setRawAdj) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 12762 | if (wasKeeping && !app.keeping) { |
| 12763 | // This app is no longer something we want to keep. Note |
| 12764 | // its current wake lock time to later know to kill it if |
| 12765 | // it is not behaving well. |
| 12766 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 12767 | synchronized (stats) { |
| 12768 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 12769 | app.pid, SystemClock.elapsedRealtime()); |
| 12770 | } |
| 12771 | app.lastCpuTime = app.curCpuTime; |
| 12772 | } |
| 12773 | |
| 12774 | app.setRawAdj = app.curRawAdj; |
| 12775 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12776 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12777 | if (app.curAdj != app.setAdj) { |
| 12778 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 12779 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12780 | TAG, "Set " + app.pid + " " + app.processName + |
| 12781 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12782 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 12783 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 12784 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12785 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 12786 | } |
| 12787 | } |
| 12788 | if (app.setSchedGroup != app.curSchedGroup) { |
| 12789 | app.setSchedGroup = app.curSchedGroup; |
| 12790 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 12791 | "Setting process group of " + app.processName |
| 12792 | + " to " + app.curSchedGroup); |
| 12793 | if (app.waitingToKill != null && |
| 12794 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 12795 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
| 12796 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 12797 | app.processName, app.setAdj, app.waitingToKill); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 12798 | app.killedBackground = true; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 12799 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 12800 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 12801 | } else { |
| 12802 | if (true) { |
| 12803 | long oldId = Binder.clearCallingIdentity(); |
| 12804 | try { |
| 12805 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 12806 | } catch (Exception e) { |
| 12807 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 12808 | + " to " + app.curSchedGroup); |
| 12809 | e.printStackTrace(); |
| 12810 | } finally { |
| 12811 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12812 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12813 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 12814 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 12815 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 12816 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 12817 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 12818 | } |
| 12819 | } |
| 12820 | } |
| 12821 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12822 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 12823 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12824 | } |
| 12825 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12826 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12827 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12828 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 12829 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12830 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12831 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12832 | } |
| 12833 | } |
| 12834 | return resumedActivity; |
| 12835 | } |
| 12836 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12837 | final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12838 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12839 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 12840 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12841 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 12842 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12843 | |
| 12844 | mAdjSeq++; |
| 12845 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12846 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12847 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 12848 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12849 | if (nowHidden != wasHidden) { |
| 12850 | // Changed to/from hidden state, so apps after it in the LRU |
| 12851 | // list may also be changed. |
| 12852 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12853 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 12854 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12855 | } |
| 12856 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12857 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12858 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12859 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 12860 | |
| 12861 | if (false) { |
| 12862 | RuntimeException e = new RuntimeException(); |
| 12863 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12864 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12865 | } |
| 12866 | |
| 12867 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12868 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12869 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12870 | // Let's determine how many processes we have running vs. |
| 12871 | // how many slots we have for background processes; we may want |
| 12872 | // to put multiple processes in a slot of there are enough of |
| 12873 | // them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12874 | int numSlots = ProcessList.HIDDEN_APP_MAX_ADJ - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12875 | int factor = (mLruProcesses.size()-4)/numSlots; |
| 12876 | if (factor < 1) factor = 1; |
| 12877 | int step = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 12878 | int numHidden = 0; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12879 | int numTrimming = 0; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12880 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12881 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12882 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12883 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12884 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12885 | while (i > 0) { |
| 12886 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12887 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12888 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12889 | updateOomAdjLocked(app, curHiddenAdj, TOP_APP, true); |
| 12890 | if (curHiddenAdj < ProcessList.HIDDEN_APP_MAX_ADJ |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12891 | && app.curAdj == curHiddenAdj) { |
| 12892 | step++; |
| 12893 | if (step >= factor) { |
| 12894 | step = 0; |
| 12895 | curHiddenAdj++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12896 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12897 | } |
| 12898 | if (!app.killedBackground) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12899 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12900 | numHidden++; |
| 12901 | if (numHidden > mProcessLimit) { |
| 12902 | Slog.i(TAG, "No longer want " + app.processName |
| 12903 | + " (pid " + app.pid + "): hidden #" + numHidden); |
| 12904 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 12905 | app.processName, app.setAdj, "too many background"); |
| 12906 | app.killedBackground = true; |
| 12907 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 12908 | } |
| 12909 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 12910 | if (!app.killedBackground && app.isolated && app.services.size() <= 0) { |
| 12911 | // If this is an isolated process, and there are no |
| 12912 | // services running in it, then the process is no longer |
| 12913 | // needed. We agressively kill these because we can by |
| 12914 | // definition not re-use the same process again, and it is |
| 12915 | // good to avoid having whatever code was running in them |
| 12916 | // left sitting around after no longer needed. |
| 12917 | Slog.i(TAG, "Isolated process " + app.processName |
| 12918 | + " (pid " + app.pid + ") no longer needed"); |
| 12919 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 12920 | app.processName, app.setAdj, "isolated not needed"); |
| 12921 | app.killedBackground = true; |
| 12922 | Process.killProcessQuiet(app.pid); |
| 12923 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12924 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 12925 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
| 12926 | && !app.killedBackground) { |
| 12927 | numTrimming++; |
| 12928 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12929 | } |
| 12930 | } |
| 12931 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12932 | mNumServiceProcs = mNewNumServiceProcs; |
| 12933 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12934 | // Now determine the memory trimming level of background processes. |
| 12935 | // Unfortunately we need to start at the back of the list to do this |
| 12936 | // properly. We only do this if the number of background apps we |
| 12937 | // are managing to keep around is less than half the maximum we desire; |
| 12938 | // if we are keeping a good number around, we'll let them use whatever |
| 12939 | // memory they want. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12940 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/2)) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12941 | final int N = mLruProcesses.size(); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12942 | factor = numTrimming/3; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 12943 | int minFactor = 2; |
| 12944 | if (mHomeProcess != null) minFactor++; |
| 12945 | if (mPreviousProcess != null) minFactor++; |
| 12946 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12947 | step = 0; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 12948 | int fgTrimLevel; |
| 12949 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/5)) { |
| 12950 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
| 12951 | } else if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/3)) { |
| 12952 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
| 12953 | } else { |
| 12954 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
| 12955 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12956 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12957 | for (i=0; i<N; i++) { |
| 12958 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12959 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 12960 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 12961 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12962 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 12963 | try { |
| 12964 | app.thread.scheduleTrimMemory(curLevel); |
| 12965 | } catch (RemoteException e) { |
| 12966 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 12967 | if (false) { |
| 12968 | // For now we won't do this; our memory trimming seems |
| 12969 | // to be good enough at this point that destroying |
| 12970 | // activities causes more harm than good. |
| 12971 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 12972 | && app != mHomeProcess && app != mPreviousProcess) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 12973 | // Need to do this on its own message because the stack may not |
| 12974 | // be in a consistent state at this point. |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 12975 | // For these apps we will also finish their activities |
| 12976 | // to help them free memory. |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 12977 | mMainStack.scheduleDestroyActivities(app, false, "trim"); |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 12978 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12979 | } |
| 12980 | } |
| 12981 | app.trimMemoryLevel = curLevel; |
| 12982 | step++; |
| 12983 | if (step >= factor) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 12984 | step = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12985 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12986 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 12987 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12988 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12989 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 12990 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12991 | break; |
| 12992 | } |
| 12993 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12994 | } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12995 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12996 | && app.thread != null) { |
| 12997 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12998 | app.thread.scheduleTrimMemory( |
| 12999 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13000 | } catch (RemoteException e) { |
| 13001 | } |
| 13002 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13003 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13004 | } else { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13005 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13006 | && app.pendingUiClean) { |
| 13007 | // If this application is now in the background and it |
| 13008 | // had done UI, then give it the special trim level to |
| 13009 | // have it free UI resources. |
| 13010 | final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13011 | if (app.trimMemoryLevel < level && app.thread != null) { |
| 13012 | try { |
| 13013 | app.thread.scheduleTrimMemory(level); |
| 13014 | } catch (RemoteException e) { |
| 13015 | } |
| 13016 | } |
| 13017 | app.pendingUiClean = false; |
| 13018 | } |
| 13019 | if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13020 | try { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13021 | app.thread.scheduleTrimMemory(fgTrimLevel); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13022 | } catch (RemoteException e) { |
| 13023 | } |
| 13024 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13025 | app.trimMemoryLevel = fgTrimLevel; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13026 | } |
| 13027 | } |
| 13028 | } else { |
| 13029 | final int N = mLruProcesses.size(); |
| 13030 | for (i=0; i<N; i++) { |
| 13031 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13032 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13033 | && app.pendingUiClean) { |
| 13034 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13035 | && app.thread != null) { |
| 13036 | try { |
| 13037 | app.thread.scheduleTrimMemory( |
| 13038 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 13039 | } catch (RemoteException e) { |
| 13040 | } |
| 13041 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13042 | app.pendingUiClean = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13043 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13044 | app.trimMemoryLevel = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13045 | } |
| 13046 | } |
| 13047 | |
| 13048 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13049 | // Need to do this on its own message because the stack may not |
| 13050 | // be in a consistent state at this point. |
| 13051 | mMainStack.scheduleDestroyActivities(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13052 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13053 | } |
| 13054 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13055 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13056 | synchronized (this) { |
| 13057 | int i; |
| 13058 | |
| 13059 | // First remove any unused application processes whose package |
| 13060 | // has been removed. |
| 13061 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 13062 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 13063 | if (app.activities.size() == 0 |
| 13064 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13065 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13066 | TAG, "Exiting empty application process " |
| 13067 | + app.processName + " (" |
| 13068 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13069 | + ")\n"); |
| 13070 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13071 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13072 | app.processName, app.setAdj, "empty"); |
| 13073 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13074 | } else { |
| 13075 | try { |
| 13076 | app.thread.scheduleExit(); |
| 13077 | } catch (Exception e) { |
| 13078 | // Ignore exceptions. |
| 13079 | } |
| 13080 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13081 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13082 | mRemovedProcesses.remove(i); |
| 13083 | |
| 13084 | if (app.persistent) { |
| 13085 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13086 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13087 | } |
| 13088 | } |
| 13089 | } |
| 13090 | } |
| 13091 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13092 | // Now update the oom adj for all processes. |
| 13093 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13094 | } |
| 13095 | } |
| 13096 | |
| 13097 | /** This method sends the specified signal to each of the persistent apps */ |
| 13098 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 13099 | if (sig != Process.SIGNAL_USR1) { |
| 13100 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 13101 | } |
| 13102 | |
| 13103 | synchronized (this) { |
| 13104 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 13105 | != PackageManager.PERMISSION_GRANTED) { |
| 13106 | throw new SecurityException("Requires permission " |
| 13107 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 13108 | } |
| 13109 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13110 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 13111 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13112 | if (r.thread != null && r.persistent) { |
| 13113 | Process.sendSignal(r.pid, sig); |
| 13114 | } |
| 13115 | } |
| 13116 | } |
| 13117 | } |
| 13118 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13119 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 13120 | if (proc == null || proc == mProfileProc) { |
| 13121 | proc = mProfileProc; |
| 13122 | path = mProfileFile; |
| 13123 | profileType = mProfileType; |
| 13124 | clearProfilerLocked(); |
| 13125 | } |
| 13126 | if (proc == null) { |
| 13127 | return; |
| 13128 | } |
| 13129 | try { |
| 13130 | proc.thread.profilerControl(false, path, null, profileType); |
| 13131 | } catch (RemoteException e) { |
| 13132 | throw new IllegalStateException("Process disappeared"); |
| 13133 | } |
| 13134 | } |
| 13135 | |
| 13136 | private void clearProfilerLocked() { |
| 13137 | if (mProfileFd != null) { |
| 13138 | try { |
| 13139 | mProfileFd.close(); |
| 13140 | } catch (IOException e) { |
| 13141 | } |
| 13142 | } |
| 13143 | mProfileApp = null; |
| 13144 | mProfileProc = null; |
| 13145 | mProfileFile = null; |
| 13146 | mProfileType = 0; |
| 13147 | mAutoStopProfiler = false; |
| 13148 | } |
| 13149 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13150 | public boolean profileControl(String process, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 13151 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13152 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13153 | try { |
| 13154 | synchronized (this) { |
| 13155 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13156 | // its own permission. |
| 13157 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13158 | != PackageManager.PERMISSION_GRANTED) { |
| 13159 | throw new SecurityException("Requires permission " |
| 13160 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13161 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13162 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13163 | if (start && fd == null) { |
| 13164 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13165 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13166 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13167 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13168 | if (process != null) { |
| 13169 | try { |
| 13170 | int pid = Integer.parseInt(process); |
| 13171 | synchronized (mPidsSelfLocked) { |
| 13172 | proc = mPidsSelfLocked.get(pid); |
| 13173 | } |
| 13174 | } catch (NumberFormatException e) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13175 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13176 | |
| 13177 | if (proc == null) { |
| 13178 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13179 | = mProcessNames.getMap(); |
| 13180 | SparseArray<ProcessRecord> procs = all.get(process); |
| 13181 | if (procs != null && procs.size() > 0) { |
| 13182 | proc = procs.valueAt(0); |
| 13183 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13184 | } |
| 13185 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13186 | |
| 13187 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13188 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13189 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13190 | |
| 13191 | if (start) { |
| 13192 | stopProfilerLocked(null, null, 0); |
| 13193 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 13194 | mProfileProc = proc; |
| 13195 | mProfileType = profileType; |
| 13196 | try { |
| 13197 | fd = fd.dup(); |
| 13198 | } catch (IOException e) { |
| 13199 | fd = null; |
| 13200 | } |
| 13201 | proc.thread.profilerControl(start, path, fd, profileType); |
| 13202 | fd = null; |
| 13203 | mProfileFd = null; |
| 13204 | } else { |
| 13205 | stopProfilerLocked(proc, path, profileType); |
| 13206 | if (fd != null) { |
| 13207 | try { |
| 13208 | fd.close(); |
| 13209 | } catch (IOException e) { |
| 13210 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13211 | } |
| 13212 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13213 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13214 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13215 | } |
| 13216 | } catch (RemoteException e) { |
| 13217 | throw new IllegalStateException("Process disappeared"); |
| 13218 | } finally { |
| 13219 | if (fd != null) { |
| 13220 | try { |
| 13221 | fd.close(); |
| 13222 | } catch (IOException e) { |
| 13223 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13224 | } |
| 13225 | } |
| 13226 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13227 | |
| 13228 | public boolean dumpHeap(String process, boolean managed, |
| 13229 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 13230 | |
| 13231 | try { |
| 13232 | synchronized (this) { |
| 13233 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13234 | // its own permission (same as profileControl). |
| 13235 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13236 | != PackageManager.PERMISSION_GRANTED) { |
| 13237 | throw new SecurityException("Requires permission " |
| 13238 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 13239 | } |
| 13240 | |
| 13241 | if (fd == null) { |
| 13242 | throw new IllegalArgumentException("null fd"); |
| 13243 | } |
| 13244 | |
| 13245 | ProcessRecord proc = null; |
| 13246 | try { |
| 13247 | int pid = Integer.parseInt(process); |
| 13248 | synchronized (mPidsSelfLocked) { |
| 13249 | proc = mPidsSelfLocked.get(pid); |
| 13250 | } |
| 13251 | } catch (NumberFormatException e) { |
| 13252 | } |
| 13253 | |
| 13254 | if (proc == null) { |
| 13255 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13256 | = mProcessNames.getMap(); |
| 13257 | SparseArray<ProcessRecord> procs = all.get(process); |
| 13258 | if (procs != null && procs.size() > 0) { |
| 13259 | proc = procs.valueAt(0); |
| 13260 | } |
| 13261 | } |
| 13262 | |
| 13263 | if (proc == null || proc.thread == null) { |
| 13264 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13265 | } |
| 13266 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 13267 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 13268 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13269 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 13270 | throw new SecurityException("Process not debuggable: " + proc); |
| 13271 | } |
| 13272 | } |
| 13273 | |
| 13274 | proc.thread.dumpHeap(managed, path, fd); |
| 13275 | fd = null; |
| 13276 | return true; |
| 13277 | } |
| 13278 | } catch (RemoteException e) { |
| 13279 | throw new IllegalStateException("Process disappeared"); |
| 13280 | } finally { |
| 13281 | if (fd != null) { |
| 13282 | try { |
| 13283 | fd.close(); |
| 13284 | } catch (IOException e) { |
| 13285 | } |
| 13286 | } |
| 13287 | } |
| 13288 | } |
| 13289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13290 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 13291 | public void monitor() { |
| 13292 | synchronized (this) { } |
| 13293 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13294 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 13295 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13296 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 13297 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 13298 | try { |
| 13299 | if (processRecord.thread != null) { |
| 13300 | processRecord.thread.setCoreSettings(settings); |
| 13301 | } |
| 13302 | } catch (RemoteException re) { |
| 13303 | /* ignore */ |
| 13304 | } |
| 13305 | } |
| 13306 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 13307 | |
| 13308 | // Multi-user methods |
| 13309 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13310 | private int mCurrentUserId; |
| 13311 | private SparseIntArray mLoggedInUsers = new SparseIntArray(5); |
| 13312 | |
| 13313 | public boolean switchUser(int userId) { |
| 13314 | final int callingUid = Binder.getCallingUid(); |
| 13315 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 13316 | Slog.e(TAG, "Trying to switch user from unauthorized app"); |
| 13317 | return false; |
| 13318 | } |
| 13319 | if (mCurrentUserId == userId) |
| 13320 | return true; |
| 13321 | |
| 13322 | synchronized (this) { |
| 13323 | // Check if user is already logged in, otherwise check if user exists first before |
| 13324 | // adding to the list of logged in users. |
| 13325 | if (mLoggedInUsers.indexOfKey(userId) < 0) { |
| 13326 | if (!userExists(userId)) { |
| 13327 | return false; |
| 13328 | } |
| 13329 | mLoggedInUsers.append(userId, userId); |
| 13330 | } |
| 13331 | |
| 13332 | mCurrentUserId = userId; |
| 13333 | boolean haveActivities = mMainStack.switchUser(userId); |
| 13334 | if (!haveActivities) { |
| 13335 | startHomeActivityLocked(userId); |
| 13336 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 13337 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 13338 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13339 | |
| 13340 | // Inform of user switch |
| 13341 | Intent addedIntent = new Intent(Intent.ACTION_USER_SWITCHED); |
| 13342 | addedIntent.putExtra(Intent.EXTRA_USERID, userId); |
| 13343 | mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_ACCOUNTS); |
| 13344 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 13345 | return true; |
| 13346 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13347 | |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 13348 | @Override |
| 13349 | public UserInfo getCurrentUser() throws RemoteException { |
| 13350 | final int callingUid = Binder.getCallingUid(); |
| 13351 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 13352 | Slog.e(TAG, "Trying to get user from unauthorized app"); |
| 13353 | return null; |
| 13354 | } |
| 13355 | return AppGlobals.getPackageManager().getUser(mCurrentUserId); |
| 13356 | } |
| 13357 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13358 | private void onUserRemoved(Intent intent) { |
| 13359 | int extraUserId = intent.getIntExtra(Intent.EXTRA_USERID, -1); |
| 13360 | if (extraUserId < 1) return; |
| 13361 | |
| 13362 | // Kill all the processes for the user |
| 13363 | ArrayList<Pair<String, Integer>> pkgAndUids = new ArrayList<Pair<String,Integer>>(); |
| 13364 | synchronized (this) { |
| 13365 | HashMap<String,SparseArray<ProcessRecord>> map = mProcessNames.getMap(); |
| 13366 | for (Entry<String, SparseArray<ProcessRecord>> uidMap : map.entrySet()) { |
| 13367 | SparseArray<ProcessRecord> uids = uidMap.getValue(); |
| 13368 | for (int i = 0; i < uids.size(); i++) { |
| 13369 | if (UserId.getUserId(uids.keyAt(i)) == extraUserId) { |
| 13370 | pkgAndUids.add(new Pair<String,Integer>(uidMap.getKey(), uids.keyAt(i))); |
| 13371 | } |
| 13372 | } |
| 13373 | } |
| 13374 | |
| 13375 | for (Pair<String,Integer> pkgAndUid : pkgAndUids) { |
| 13376 | forceStopPackageLocked(pkgAndUid.first, pkgAndUid.second, |
| 13377 | false, false, true, true, extraUserId); |
| 13378 | } |
| 13379 | } |
| 13380 | } |
| 13381 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13382 | private boolean userExists(int userId) { |
| 13383 | try { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13384 | UserInfo user = AppGlobals.getPackageManager().getUser(userId); |
| 13385 | return user != null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13386 | } catch (RemoteException re) { |
| 13387 | // Won't happen, in same process |
| 13388 | } |
| 13389 | |
| 13390 | return false; |
| 13391 | } |
| 13392 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 13393 | private void checkValidCaller(int uid, int userId) { |
| 13394 | if (UserId.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return; |
| 13395 | |
| 13396 | throw new SecurityException("Caller uid=" + uid |
| 13397 | + " is not privileged to communicate with user=" + userId); |
| 13398 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13399 | |
| 13400 | private int applyUserId(int uid, int userId) { |
| 13401 | return UserId.getUid(userId, uid); |
| 13402 | } |
| 13403 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13404 | ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 13405 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13406 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 13407 | newInfo.uid = applyUserId(info.uid, userId); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 13408 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 13409 | + info.packageName; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13410 | return newInfo; |
| 13411 | } |
| 13412 | |
| 13413 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 13414 | if (aInfo == null |
| 13415 | || (userId < 1 && aInfo.applicationInfo.uid < UserId.PER_USER_RANGE)) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13416 | return aInfo; |
| 13417 | } |
| 13418 | |
| 13419 | ActivityInfo info = new ActivityInfo(aInfo); |
| 13420 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 13421 | return info; |
| 13422 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13423 | } |