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; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 65 | import android.content.IIntentReceiver; |
| 66 | import android.content.IIntentSender; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 67 | import android.content.Intent; |
| 68 | import android.content.IntentFilter; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 69 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.content.pm.ActivityInfo; |
| 71 | import android.content.pm.ApplicationInfo; |
| 72 | import android.content.pm.ConfigurationInfo; |
| 73 | import android.content.pm.IPackageDataObserver; |
| 74 | import android.content.pm.IPackageManager; |
| 75 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 76 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import android.content.pm.PackageManager; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 78 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 79 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.content.pm.ProviderInfo; |
| 81 | import android.content.pm.ResolveInfo; |
| 82 | import android.content.pm.ServiceInfo; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 83 | import android.content.pm.UserInfo; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 84 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.content.res.Configuration; |
| 86 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 87 | import android.net.Proxy; |
| 88 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import android.net.Uri; |
| 90 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 91 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 92 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 93 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 94 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 96 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.os.FileUtils; |
| 98 | import android.os.Handler; |
| 99 | import android.os.IBinder; |
| 100 | import android.os.IPermissionController; |
| 101 | import android.os.Looper; |
| 102 | import android.os.Message; |
| 103 | import android.os.Parcel; |
| 104 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 106 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.os.RemoteException; |
| 108 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 109 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.SystemClock; |
| 111 | import android.os.SystemProperties; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 112 | import android.os.UserId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import android.provider.Settings; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 114 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.util.EventLog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 116 | import android.util.Log; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 117 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | import android.util.PrintWriterPrinter; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 119 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import android.util.SparseArray; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 121 | import android.util.SparseIntArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 122 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | import android.view.Gravity; |
| 124 | import android.view.LayoutInflater; |
| 125 | import android.view.View; |
| 126 | import android.view.WindowManager; |
| 127 | import android.view.WindowManagerPolicy; |
| 128 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 129 | import java.io.BufferedInputStream; |
| 130 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 131 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 132 | import java.io.DataInputStream; |
| 133 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | import java.io.File; |
| 135 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 136 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 138 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 139 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 140 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 142 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | import java.lang.ref.WeakReference; |
| 144 | import java.util.ArrayList; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 145 | import java.util.Collection; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 146 | import java.util.Collections; |
| 147 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | import java.util.HashMap; |
| 149 | import java.util.HashSet; |
| 150 | import java.util.Iterator; |
| 151 | import java.util.List; |
| 152 | import java.util.Locale; |
| 153 | import java.util.Map; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 154 | import java.util.Map.Entry; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 155 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 156 | import java.util.concurrent.atomic.AtomicBoolean; |
| 157 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 159 | public final class ActivityManagerService extends ActivityManagerNative |
| 160 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 161 | private static final String USER_DATA_DIR = "/data/user/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | static final String TAG = "ActivityManager"; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 163 | static final String TAG_MU = "ActivityManagerServiceMU"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 165 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 167 | static final boolean DEBUG_TASKS = localLOGV || false; |
| 168 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 169 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 170 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 171 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 172 | static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 173 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 175 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 177 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 178 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 179 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 181 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 182 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 183 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 184 | static final boolean DEBUG_POWER = localLOGV || false; |
| 185 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 186 | static final boolean DEBUG_MU = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | static final boolean VALIDATE_TOKENS = false; |
| 188 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 189 | |
| 190 | // Control over CPU and battery monitoring. |
| 191 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 192 | static final boolean MONITOR_CPU_USAGE = true; |
| 193 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 194 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 195 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 196 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 197 | // 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] | 198 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 199 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 200 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 202 | static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); |
| 203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | // Maximum number of recent tasks that we can remember. |
| 205 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 206 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 207 | // Amount of time after a call to stopAppSwitches() during which we will |
| 208 | // prevent further untrusted switches from happening. |
| 209 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | |
| 211 | // How long we wait for a launched process to attach to the activity manager |
| 212 | // before we decide it's never going to come up for real. |
| 213 | static final int PROC_START_TIMEOUT = 10*1000; |
| 214 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 215 | // How long we wait for a launched process to attach to the activity manager |
| 216 | // before we decide it's never going to come up for real, when the process was |
| 217 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 218 | // could take much longer than usual. |
| 219 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | // How long to wait after going idle before forcing apps to GC. |
| 222 | static final int GC_TIMEOUT = 5*1000; |
| 223 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 224 | // The minimum amount of time between successive GC requests for a process. |
| 225 | static final int GC_MIN_INTERVAL = 60*1000; |
| 226 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 227 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 228 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 229 | |
| 230 | // The minimum sample duration we will allow before deciding we have |
| 231 | // enough data on wake locks to start killing things. |
| 232 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 233 | |
| 234 | // The minimum sample duration we will allow before deciding we have |
| 235 | // enough data on CPU usage to start killing things. |
| 236 | 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] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | // 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] | 239 | static final int BROADCAST_FG_TIMEOUT = 10*1000; |
| 240 | static final int BROADCAST_BG_TIMEOUT = 60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | |
| 242 | // How long we wait for a service to finish executing. |
| 243 | static final int SERVICE_TIMEOUT = 20*1000; |
| 244 | |
| 245 | // How long a service needs to be running until restarting its process |
| 246 | // is no longer considered to be a relaunch of the service. |
| 247 | static final int SERVICE_RESTART_DURATION = 5*1000; |
| 248 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 249 | // How long a service needs to be running until it will start back at |
| 250 | // SERVICE_RESTART_DURATION after being killed. |
| 251 | static final int SERVICE_RESET_RUN_DURATION = 60*1000; |
| 252 | |
| 253 | // Multiplying factor to increase restart duration time by, for each time |
| 254 | // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. |
| 255 | static final int SERVICE_RESTART_DURATION_FACTOR = 4; |
| 256 | |
| 257 | // The minimum amount of time between restarting services that we allow. |
| 258 | // That is, when multiple services are restarting, we won't allow each |
| 259 | // to restart less than this amount of time from the last one. |
| 260 | static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; |
| 261 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | // Maximum amount of time for there to be no activity on a service before |
| 263 | // we consider it non-essential and allow its process to go on the |
| 264 | // LRU background list. |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 265 | static final int MAX_SERVICE_INACTIVITY = 30*60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
| 267 | // How long we wait until we timeout on key dispatching. |
| 268 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 269 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 271 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 272 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 273 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | |
| 275 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 276 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 277 | public ActivityStack mMainStack; |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 278 | |
| 279 | private final boolean mHeadless; |
| 280 | |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 281 | // Whether we should show our dialogs (ANR, crash, etc) or just perform their |
| 282 | // default actuion automatically. Important for devices without direct input |
| 283 | // devices. |
| 284 | private boolean mShowDialogs = true; |
| 285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 287 | * Description of a request to start a new activity, which has been held |
| 288 | * due to app switches being disabled. |
| 289 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 290 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 291 | ActivityRecord r; |
| 292 | ActivityRecord sourceRecord; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 293 | int startFlags; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 297 | = new ArrayList<PendingActivityLaunch>(); |
| 298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 300 | BroadcastQueue mFgBroadcastQueue; |
| 301 | BroadcastQueue mBgBroadcastQueue; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 302 | // Convenient for easy iteration over the queues. Foreground is first |
| 303 | // so that dispatch of foreground broadcasts gets precedence. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 304 | final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 305 | |
| 306 | BroadcastQueue broadcastQueueForIntent(Intent intent) { |
| 307 | final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; |
| 308 | if (DEBUG_BACKGROUND_BROADCAST) { |
| 309 | Slog.i(TAG, "Broadcast intent " + intent + " on " |
| 310 | + (isFg ? "foreground" : "background") |
| 311 | + " queue"); |
| 312 | } |
| 313 | return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; |
| 314 | } |
| 315 | |
| 316 | BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) { |
| 317 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 318 | BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver); |
| 319 | if (r != null) { |
| 320 | return r; |
| 321 | } |
| 322 | } |
| 323 | return null; |
| 324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | |
| 326 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | * Activity we have told the window manager to have key focus. |
| 328 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 329 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 330 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | * List of intents that were used to start the most recent tasks. |
| 332 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 333 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | |
| 335 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 336 | * Process management. |
| 337 | */ |
| 338 | final ProcessList mProcessList = new ProcessList(); |
| 339 | |
| 340 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | * All of the applications we currently have running organized by name. |
| 342 | * The keys are strings of the application package name (as |
| 343 | * returned by the package manager), and the keys are ApplicationRecord |
| 344 | * objects. |
| 345 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 346 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | |
| 348 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 349 | * The currently running isolated processes. |
| 350 | */ |
| 351 | final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>(); |
| 352 | |
| 353 | /** |
| 354 | * Counter for assigning isolated process uids, to avoid frequently reusing the |
| 355 | * same ones. |
| 356 | */ |
| 357 | int mNextIsolatedProcessUid = 0; |
| 358 | |
| 359 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 360 | * The currently running heavy-weight process, if any. |
| 361 | */ |
| 362 | ProcessRecord mHeavyWeightProcess = null; |
| 363 | |
| 364 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | * The last time that various processes have crashed. |
| 366 | */ |
| 367 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 368 | |
| 369 | /** |
| 370 | * Set of applications that we consider to be bad, and will reject |
| 371 | * incoming broadcasts from (which the user has no control over). |
| 372 | * Processes are added to this set when they have crashed twice within |
| 373 | * a minimum amount of time; they are removed from it when they are |
| 374 | * later restarted (hopefully due to some user action). The value is the |
| 375 | * time it was added to the list. |
| 376 | */ |
| 377 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 378 | |
| 379 | /** |
| 380 | * All of the processes we currently have running organized by pid. |
| 381 | * The keys are the pid running the application. |
| 382 | * |
| 383 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 384 | * activity manager lock! |
| 385 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 386 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | |
| 388 | /** |
| 389 | * All of the processes that have been forced to be foreground. The key |
| 390 | * is the pid of the caller who requested it (we hold a death |
| 391 | * link on it). |
| 392 | */ |
| 393 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 394 | int pid; |
| 395 | IBinder token; |
| 396 | } |
| 397 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 398 | = new SparseArray<ForegroundToken>(); |
| 399 | |
| 400 | /** |
| 401 | * List of records for processes that someone had tried to start before the |
| 402 | * system was ready. We don't start them at that point, but ensure they |
| 403 | * are started by the time booting is complete. |
| 404 | */ |
| 405 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 406 | = new ArrayList<ProcessRecord>(); |
| 407 | |
| 408 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | * List of persistent applications that are in the process |
| 410 | * of being started. |
| 411 | */ |
| 412 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 413 | = new ArrayList<ProcessRecord>(); |
| 414 | |
| 415 | /** |
| 416 | * Processes that are being forcibly torn down. |
| 417 | */ |
| 418 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 419 | = new ArrayList<ProcessRecord>(); |
| 420 | |
| 421 | /** |
| 422 | * List of running applications, sorted by recent usage. |
| 423 | * The first entry in the list is the least recently used. |
| 424 | * It contains ApplicationRecord objects. This list does NOT include |
| 425 | * any persistent application records (since we never want to exit them). |
| 426 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 427 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | = new ArrayList<ProcessRecord>(); |
| 429 | |
| 430 | /** |
| 431 | * List of processes that should gc as soon as things are idle. |
| 432 | */ |
| 433 | final ArrayList<ProcessRecord> mProcessesToGc |
| 434 | = new ArrayList<ProcessRecord>(); |
| 435 | |
| 436 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 437 | * This is the process holding what we currently consider to be |
| 438 | * the "home" activity. |
| 439 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 440 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 441 | |
| 442 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 443 | * This is the process holding the activity the user last visited that |
| 444 | * is in a different process from the one they are currently in. |
| 445 | */ |
| 446 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 447 | |
| 448 | /** |
| 449 | * The time at which the previous process was last visible. |
| 450 | */ |
| 451 | long mPreviousProcessVisibleTime; |
| 452 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 453 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 454 | * Packages that the user has asked to have run in screen size |
| 455 | * compatibility mode instead of filling the screen. |
| 456 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 457 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 458 | |
| 459 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | * Set of PendingResultRecord objects that are currently active. |
| 461 | */ |
| 462 | final HashSet mPendingResultRecords = new HashSet(); |
| 463 | |
| 464 | /** |
| 465 | * Set of IntentSenderRecord objects that are currently active. |
| 466 | */ |
| 467 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 468 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 469 | |
| 470 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 471 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 472 | * already logged DropBox entries for. Guarded by itself. If |
| 473 | * something (rogue user app) forces this over |
| 474 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 475 | */ |
| 476 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 477 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 478 | |
| 479 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 480 | * Strict Mode background batched logging state. |
| 481 | * |
| 482 | * The string buffer is guarded by itself, and its lock is also |
| 483 | * used to determine if another batched write is already |
| 484 | * in-flight. |
| 485 | */ |
| 486 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 487 | |
| 488 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | * Keeps track of all IIntentReceivers that have been registered for |
| 490 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 491 | * a ReceiverList. |
| 492 | */ |
| 493 | final HashMap mRegisteredReceivers = new HashMap(); |
| 494 | |
| 495 | /** |
| 496 | * Resolver for broadcast intents to registered receivers. |
| 497 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 498 | */ |
| 499 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 500 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 501 | @Override |
| 502 | protected boolean allowFilterResult( |
| 503 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 504 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 505 | for (int i=dest.size()-1; i>=0; i--) { |
| 506 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 507 | return false; |
| 508 | } |
| 509 | } |
| 510 | return true; |
| 511 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 512 | |
| 513 | @Override |
| 514 | protected String packageForFilter(BroadcastFilter filter) { |
| 515 | return filter.packageName; |
| 516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | /** |
| 520 | * State of all active sticky broadcasts. Keys are the action of the |
| 521 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
| 522 | * that action (which should usually be one). |
| 523 | */ |
| 524 | final HashMap<String, ArrayList<Intent>> mStickyBroadcasts = |
| 525 | new HashMap<String, ArrayList<Intent>>(); |
| 526 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 527 | final ServiceMap mServiceMap = new ServiceMap(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * All currently bound service connections. Keys are the IBinder of |
| 531 | * the client's IServiceConnection. |
| 532 | */ |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 533 | final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections |
| 534 | = new HashMap<IBinder, ArrayList<ConnectionRecord>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | |
| 536 | /** |
| 537 | * List of services that we have been asked to start, |
| 538 | * but haven't yet been able to. It is used to hold start requests |
| 539 | * while waiting for their corresponding application thread to get |
| 540 | * going. |
| 541 | */ |
| 542 | final ArrayList<ServiceRecord> mPendingServices |
| 543 | = new ArrayList<ServiceRecord>(); |
| 544 | |
| 545 | /** |
| 546 | * List of services that are scheduled to restart following a crash. |
| 547 | */ |
| 548 | final ArrayList<ServiceRecord> mRestartingServices |
| 549 | = new ArrayList<ServiceRecord>(); |
| 550 | |
| 551 | /** |
| 552 | * List of services that are in the process of being stopped. |
| 553 | */ |
| 554 | final ArrayList<ServiceRecord> mStoppingServices |
| 555 | = new ArrayList<ServiceRecord>(); |
| 556 | |
| 557 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 558 | * Backup/restore process management |
| 559 | */ |
| 560 | String mBackupAppName = null; |
| 561 | BackupRecord mBackupTarget = null; |
| 562 | |
| 563 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | * List of PendingThumbnailsRecord objects of clients who are still |
| 565 | * waiting to receive all of the thumbnails for a task. |
| 566 | */ |
| 567 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 568 | |
| 569 | /** |
| 570 | * List of HistoryRecord objects that have been finished and must |
| 571 | * still report back to a pending thumbnail receiver. |
| 572 | */ |
| 573 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 574 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 575 | final ProviderMap mProviderMap = new ProviderMap(); |
| 576 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | /** |
| 578 | * List of content providers who have clients waiting for them. The |
| 579 | * application is currently being launched and the provider will be |
| 580 | * removed from this list once it is published. |
| 581 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 582 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 583 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | |
| 585 | /** |
| 586 | * Global set of specific Uri permissions that have been granted. |
| 587 | */ |
| 588 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 589 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 590 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 591 | CoreSettingsObserver mCoreSettingsObserver; |
| 592 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | /** |
| 594 | * Thread-local storage used to carry caller permissions over through |
| 595 | * indirect content-provider access. |
| 596 | * @see #ActivityManagerService.openContentUri() |
| 597 | */ |
| 598 | private class Identity { |
| 599 | public int pid; |
| 600 | public int uid; |
| 601 | |
| 602 | Identity(int _pid, int _uid) { |
| 603 | pid = _pid; |
| 604 | uid = _uid; |
| 605 | } |
| 606 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 609 | |
| 610 | /** |
| 611 | * All information we have collected about the runtime performance of |
| 612 | * any user id that can impact battery performance. |
| 613 | */ |
| 614 | final BatteryStatsService mBatteryStatsService; |
| 615 | |
| 616 | /** |
| 617 | * information about component usage |
| 618 | */ |
| 619 | final UsageStatsService mUsageStatsService; |
| 620 | |
| 621 | /** |
| 622 | * Current configuration information. HistoryRecord objects are given |
| 623 | * a reference to this object to indicate which configuration they are |
| 624 | * currently running in, so this object must be kept immutable. |
| 625 | */ |
| 626 | Configuration mConfiguration = new Configuration(); |
| 627 | |
| 628 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 629 | * Current sequencing integer of the configuration, for skipping old |
| 630 | * configurations. |
| 631 | */ |
| 632 | int mConfigurationSeq = 0; |
| 633 | |
| 634 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 635 | * Hardware-reported OpenGLES version. |
| 636 | */ |
| 637 | final int GL_ES_VERSION; |
| 638 | |
| 639 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 641 | * For example, references to the commonly used services. |
| 642 | */ |
| 643 | HashMap<String, IBinder> mAppBindArgs; |
| 644 | |
| 645 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 646 | * Temporary to avoid allocations. Protected by main lock. |
| 647 | */ |
| 648 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 649 | |
| 650 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | * Used to control how we initialize the service. |
| 652 | */ |
| 653 | boolean mStartRunning = false; |
| 654 | ComponentName mTopComponent; |
| 655 | String mTopAction; |
| 656 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 657 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | boolean mSystemReady = false; |
| 659 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 660 | boolean mWaitingUpdate = false; |
| 661 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 662 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 663 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | |
| 665 | Context mContext; |
| 666 | |
| 667 | int mFactoryTest; |
| 668 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 669 | boolean mCheckedForSetup; |
| 670 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 672 | * The time at which we will allow normal application switches again, |
| 673 | * after a call to {@link #stopAppSwitches()}. |
| 674 | */ |
| 675 | long mAppSwitchesAllowedTime; |
| 676 | |
| 677 | /** |
| 678 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 679 | * is set; any switches after that will clear the time. |
| 680 | */ |
| 681 | boolean mDidAppSwitch; |
| 682 | |
| 683 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 684 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 685 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 686 | long mLastPowerCheckRealtime; |
| 687 | |
| 688 | /** |
| 689 | * Last time (in uptime) at which we checked for power usage. |
| 690 | */ |
| 691 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 692 | |
| 693 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | * Set while we are wanting to sleep, to prevent any |
| 695 | * activities from being started/resumed. |
| 696 | */ |
| 697 | boolean mSleeping = false; |
| 698 | |
| 699 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 700 | * Set if we are shutting down the system, similar to sleeping. |
| 701 | */ |
| 702 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | |
| 704 | /** |
| 705 | * Task identifier that activities are currently being started |
| 706 | * in. Incremented each time a new task is created. |
| 707 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 708 | * integers that won't wrap. |
| 709 | */ |
| 710 | int mCurTask = 1; |
| 711 | |
| 712 | /** |
| 713 | * Current sequence id for oom_adj computation traversal. |
| 714 | */ |
| 715 | int mAdjSeq = 0; |
| 716 | |
| 717 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 718 | * Current sequence id for process LRU updating. |
| 719 | */ |
| 720 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | |
| 722 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 723 | * Keep track of the number of service processes we last found, to |
| 724 | * determine on the next iteration which should be B services. |
| 725 | */ |
| 726 | int mNumServiceProcs = 0; |
| 727 | int mNewNumServiceProcs = 0; |
| 728 | |
| 729 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | * System monitoring: number of processes that died since the last |
| 731 | * N procs were started. |
| 732 | */ |
| 733 | int[] mProcDeaths = new int[20]; |
| 734 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 735 | /** |
| 736 | * This is set if we had to do a delayed dexopt of an app before launching |
| 737 | * it, to increasing the ANR timeouts in that case. |
| 738 | */ |
| 739 | boolean mDidDexOpt; |
| 740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | String mDebugApp = null; |
| 742 | boolean mWaitForDebugger = false; |
| 743 | boolean mDebugTransient = false; |
| 744 | String mOrigDebugApp = null; |
| 745 | boolean mOrigWaitForDebugger = false; |
| 746 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 747 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 748 | String mProfileApp = null; |
| 749 | ProcessRecord mProfileProc = null; |
| 750 | String mProfileFile; |
| 751 | ParcelFileDescriptor mProfileFd; |
| 752 | int mProfileType = 0; |
| 753 | boolean mAutoStopProfiler = false; |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 754 | String mOpenGlTraceApp = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 756 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 757 | = new RemoteCallbackList<IProcessObserver>(); |
| 758 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | /** |
| 760 | * Callback of last caller to {@link #requestPss}. |
| 761 | */ |
| 762 | Runnable mRequestPssCallback; |
| 763 | |
| 764 | /** |
| 765 | * Remaining processes for which we are waiting results from the last |
| 766 | * call to {@link #requestPss}. |
| 767 | */ |
| 768 | final ArrayList<ProcessRecord> mRequestPssList |
| 769 | = new ArrayList<ProcessRecord>(); |
| 770 | |
| 771 | /** |
| 772 | * Runtime statistics collection thread. This object's lock is used to |
| 773 | * protect all related state. |
| 774 | */ |
| 775 | final Thread mProcessStatsThread; |
| 776 | |
| 777 | /** |
| 778 | * Used to collect process stats when showing not responding dialog. |
| 779 | * Protected by mProcessStatsThread. |
| 780 | */ |
| 781 | final ProcessStats mProcessStats = new ProcessStats( |
| 782 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 783 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 784 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 785 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | long mLastWriteTime = 0; |
| 787 | |
| 788 | /** |
| 789 | * Set to true after the system has finished booting. |
| 790 | */ |
| 791 | boolean mBooted = false; |
| 792 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 793 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 794 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | |
| 796 | WindowManagerService mWindowManager; |
| 797 | |
| 798 | static ActivityManagerService mSelf; |
| 799 | static ActivityThread mSystemThread; |
| 800 | |
| 801 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 802 | final ProcessRecord mApp; |
| 803 | final int mPid; |
| 804 | final IApplicationThread mAppThread; |
| 805 | |
| 806 | AppDeathRecipient(ProcessRecord app, int pid, |
| 807 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 808 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | TAG, "New death recipient " + this |
| 810 | + " for thread " + thread.asBinder()); |
| 811 | mApp = app; |
| 812 | mPid = pid; |
| 813 | mAppThread = thread; |
| 814 | } |
| 815 | |
| 816 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 817 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | TAG, "Death received in " + this |
| 819 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | synchronized(ActivityManagerService.this) { |
| 821 | appDiedLocked(mApp, mPid, mAppThread); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | static final int SHOW_ERROR_MSG = 1; |
| 827 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 828 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 829 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 830 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 831 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 833 | static final int UPDATE_TIME_ZONE = 13; |
| 834 | static final int SHOW_UID_ERROR_MSG = 14; |
| 835 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 837 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 838 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 839 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 840 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 841 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 842 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 843 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 844 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 845 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 846 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 847 | static final int DISPATCH_FOREGROUND_ACTIVITIES_CHANGED = 31; |
| 848 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 849 | static final int REPORT_MEM_USAGE = 33; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 851 | static final int FIRST_ACTIVITY_STACK_MSG = 100; |
| 852 | static final int FIRST_BROADCAST_QUEUE_MSG = 200; |
| 853 | static final int FIRST_COMPAT_MODE_MSG = 300; |
| 854 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 856 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 857 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | |
| 859 | final Handler mHandler = new Handler() { |
| 860 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 861 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | //} |
| 863 | |
| 864 | public void handleMessage(Message msg) { |
| 865 | switch (msg.what) { |
| 866 | case SHOW_ERROR_MSG: { |
| 867 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | synchronized (ActivityManagerService.this) { |
| 869 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 870 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 871 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | return; |
| 873 | } |
| 874 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 875 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 876 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | d.show(); |
| 878 | proc.crashDialog = d; |
| 879 | } else { |
| 880 | // The device is asleep, so just pretend that the user |
| 881 | // saw a crash dialog and hit "force quit". |
| 882 | res.set(0); |
| 883 | } |
| 884 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 885 | |
| 886 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | } break; |
| 888 | case SHOW_NOT_RESPONDING_MSG: { |
| 889 | synchronized (ActivityManagerService.this) { |
| 890 | HashMap data = (HashMap) msg.obj; |
| 891 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 892 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 893 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | return; |
| 895 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 896 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 897 | Intent intent = new Intent("android.intent.action.ANR"); |
| 898 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 899 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 900 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 901 | } |
| 902 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 903 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 904 | 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] | 905 | |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 906 | if (mShowDialogs) { |
| 907 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Mike Lockwood | 69ccdbd | 2012-04-03 11:53:47 -0700 | [diff] [blame] | 908 | mContext, proc, (ActivityRecord)data.get("activity")); |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 909 | d.show(); |
| 910 | proc.anrDialog = d; |
| 911 | } else { |
| 912 | // Just kill the app if there is no dialog to be shown. |
| 913 | killAppAtUsersRequest(proc, null); |
| 914 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 916 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 917 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 919 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 920 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 921 | synchronized (ActivityManagerService.this) { |
| 922 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 923 | if (proc == null) { |
| 924 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 925 | break; |
| 926 | } |
| 927 | if (proc.crashDialog != null) { |
| 928 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 929 | return; |
| 930 | } |
| 931 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 932 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 933 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 934 | d.show(); |
| 935 | proc.crashDialog = d; |
| 936 | } else { |
| 937 | // The device is asleep, so just pretend that the user |
| 938 | // saw a crash dialog and hit "force quit". |
| 939 | res.set(0); |
| 940 | } |
| 941 | } |
| 942 | ensureBootCompleted(); |
| 943 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | case SHOW_FACTORY_ERROR_MSG: { |
| 945 | Dialog d = new FactoryErrorDialog( |
| 946 | mContext, msg.getData().getCharSequence("msg")); |
| 947 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 948 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | } break; |
| 950 | case UPDATE_CONFIGURATION_MSG: { |
| 951 | final ContentResolver resolver = mContext.getContentResolver(); |
| 952 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 953 | } break; |
| 954 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 955 | synchronized (ActivityManagerService.this) { |
| 956 | performAppGcsIfAppropriateLocked(); |
| 957 | } |
| 958 | } break; |
| 959 | case WAIT_FOR_DEBUGGER_MSG: { |
| 960 | synchronized (ActivityManagerService.this) { |
| 961 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 962 | if (msg.arg1 != 0) { |
| 963 | if (!app.waitedForDebugger) { |
| 964 | Dialog d = new AppWaitingForDebuggerDialog( |
| 965 | ActivityManagerService.this, |
| 966 | mContext, app); |
| 967 | app.waitDialog = d; |
| 968 | app.waitedForDebugger = true; |
| 969 | d.show(); |
| 970 | } |
| 971 | } else { |
| 972 | if (app.waitDialog != null) { |
| 973 | app.waitDialog.dismiss(); |
| 974 | app.waitDialog = null; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 980 | if (mDidDexOpt) { |
| 981 | mDidDexOpt = false; |
| 982 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 983 | nmsg.obj = msg.obj; |
| 984 | mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT); |
| 985 | return; |
| 986 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | serviceTimeout((ProcessRecord)msg.obj); |
| 988 | } break; |
| 989 | case UPDATE_TIME_ZONE: { |
| 990 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 991 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 992 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | if (r.thread != null) { |
| 994 | try { |
| 995 | r.thread.updateTimeZone(); |
| 996 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 997 | 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] | 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1002 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1003 | case CLEAR_DNS_CACHE: { |
| 1004 | synchronized (ActivityManagerService.this) { |
| 1005 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1006 | ProcessRecord r = mLruProcesses.get(i); |
| 1007 | if (r.thread != null) { |
| 1008 | try { |
| 1009 | r.thread.clearDnsCache(); |
| 1010 | } catch (RemoteException ex) { |
| 1011 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1017 | case UPDATE_HTTP_PROXY: { |
| 1018 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1019 | String host = ""; |
| 1020 | String port = ""; |
| 1021 | String exclList = ""; |
| 1022 | if (proxy != null) { |
| 1023 | host = proxy.getHost(); |
| 1024 | port = Integer.toString(proxy.getPort()); |
| 1025 | exclList = proxy.getExclusionList(); |
| 1026 | } |
| 1027 | synchronized (ActivityManagerService.this) { |
| 1028 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1029 | ProcessRecord r = mLruProcesses.get(i); |
| 1030 | if (r.thread != null) { |
| 1031 | try { |
| 1032 | r.thread.setHttpProxy(host, port, exclList); |
| 1033 | } catch (RemoteException ex) { |
| 1034 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1035 | r.info.processName); |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1042 | String title = "System UIDs Inconsistent"; |
| 1043 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1044 | + " data partition or your device will be unstable."; |
| 1045 | Log.e(TAG, title + ": " + text); |
| 1046 | if (mShowDialogs) { |
| 1047 | // XXX This is a temporary dialog, no need to localize. |
| 1048 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1049 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1050 | d.setCancelable(false); |
| 1051 | d.setTitle(title); |
| 1052 | d.setMessage(text); |
| 1053 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1054 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1055 | mUidAlert = d; |
| 1056 | d.show(); |
| 1057 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | } break; |
| 1059 | case IM_FEELING_LUCKY_MSG: { |
| 1060 | if (mUidAlert != null) { |
| 1061 | mUidAlert.dismiss(); |
| 1062 | mUidAlert = null; |
| 1063 | } |
| 1064 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1066 | if (mDidDexOpt) { |
| 1067 | mDidDexOpt = false; |
| 1068 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1069 | nmsg.obj = msg.obj; |
| 1070 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1071 | return; |
| 1072 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1074 | synchronized (ActivityManagerService.this) { |
| 1075 | processStartTimedOutLocked(app); |
| 1076 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1077 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1078 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1079 | synchronized (ActivityManagerService.this) { |
| 1080 | doPendingActivityLaunchesLocked(true); |
| 1081 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1082 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1083 | case KILL_APPLICATION_MSG: { |
| 1084 | synchronized (ActivityManagerService.this) { |
| 1085 | int uid = msg.arg1; |
| 1086 | boolean restart = (msg.arg2 == 1); |
| 1087 | String pkg = (String) msg.obj; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1088 | forceStopPackageLocked(pkg, uid, restart, false, true, false, |
| 1089 | UserId.getUserId(uid)); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1092 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1093 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1094 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1095 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1096 | INotificationManager inm = NotificationManager.getService(); |
| 1097 | if (inm == null) { |
| 1098 | return; |
| 1099 | } |
| 1100 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1101 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1102 | ProcessRecord process = root.app; |
| 1103 | if (process == null) { |
| 1104 | return; |
| 1105 | } |
| 1106 | |
| 1107 | try { |
| 1108 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1109 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1110 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1111 | Notification notification = new Notification(); |
| 1112 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1113 | notification.when = 0; |
| 1114 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1115 | notification.tickerText = text; |
| 1116 | notification.defaults = 0; // please be quiet |
| 1117 | notification.sound = null; |
| 1118 | notification.vibrate = null; |
| 1119 | notification.setLatestEventInfo(context, text, |
| 1120 | mContext.getText(R.string.heavy_weight_notification_detail), |
| 1121 | PendingIntent.getActivity(mContext, 0, root.intent, |
| 1122 | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 1123 | |
| 1124 | try { |
| 1125 | int[] outId = new int[1]; |
| 1126 | inm.enqueueNotification("android", R.string.heavy_weight_notification, |
| 1127 | notification, outId); |
| 1128 | } catch (RuntimeException e) { |
| 1129 | Slog.w(ActivityManagerService.TAG, |
| 1130 | "Error showing notification for heavy-weight app", e); |
| 1131 | } catch (RemoteException e) { |
| 1132 | } |
| 1133 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1134 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1135 | } |
| 1136 | } break; |
| 1137 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1138 | INotificationManager inm = NotificationManager.getService(); |
| 1139 | if (inm == null) { |
| 1140 | return; |
| 1141 | } |
| 1142 | try { |
| 1143 | inm.cancelNotification("android", |
| 1144 | R.string.heavy_weight_notification); |
| 1145 | } catch (RuntimeException e) { |
| 1146 | Slog.w(ActivityManagerService.TAG, |
| 1147 | "Error canceling notification for service", e); |
| 1148 | } catch (RemoteException e) { |
| 1149 | } |
| 1150 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1151 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1152 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1153 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1154 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1155 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1156 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1157 | } |
| 1158 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1159 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1160 | synchronized (ActivityManagerService.this) { |
| 1161 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1162 | if (mCompatModeDialog != null) { |
| 1163 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1164 | ar.info.applicationInfo.packageName)) { |
| 1165 | return; |
| 1166 | } |
| 1167 | mCompatModeDialog.dismiss(); |
| 1168 | mCompatModeDialog = null; |
| 1169 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1170 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1171 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1172 | ar.packageName)) { |
| 1173 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1174 | ar.info.applicationInfo); |
| 1175 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1176 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1177 | mCompatModeDialog = new CompatModeDialog( |
| 1178 | ActivityManagerService.this, mContext, |
| 1179 | ar.info.applicationInfo); |
| 1180 | mCompatModeDialog.show(); |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1185 | break; |
| 1186 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1187 | case DISPATCH_FOREGROUND_ACTIVITIES_CHANGED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1188 | final int pid = msg.arg1; |
| 1189 | final int uid = msg.arg2; |
| 1190 | final boolean foregroundActivities = (Boolean) msg.obj; |
| 1191 | dispatchForegroundActivitiesChanged(pid, uid, foregroundActivities); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1192 | break; |
| 1193 | } |
| 1194 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1195 | final int pid = msg.arg1; |
| 1196 | final int uid = msg.arg2; |
| 1197 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1198 | break; |
| 1199 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1200 | case REPORT_MEM_USAGE: { |
| 1201 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1202 | if (!isDebuggable) { |
| 1203 | return; |
| 1204 | } |
| 1205 | synchronized (ActivityManagerService.this) { |
| 1206 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1207 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1208 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1209 | // avoid spamming. |
| 1210 | return; |
| 1211 | } |
| 1212 | mLastMemUsageReportTime = now; |
| 1213 | } |
| 1214 | Thread thread = new Thread() { |
| 1215 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1216 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1217 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1218 | StringWriter oomSw = new StringWriter(); |
| 1219 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1220 | StringWriter catSw = new StringWriter(); |
| 1221 | PrintWriter catPw = new PrintWriter(catSw); |
| 1222 | String[] emptyArgs = new String[] { }; |
| 1223 | StringBuilder tag = new StringBuilder(128); |
| 1224 | StringBuilder stack = new StringBuilder(128); |
| 1225 | tag.append("Low on memory -- "); |
| 1226 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1227 | tag, stack); |
| 1228 | dropBuilder.append(stack); |
| 1229 | dropBuilder.append('\n'); |
| 1230 | dropBuilder.append('\n'); |
| 1231 | String oomString = oomSw.toString(); |
| 1232 | dropBuilder.append(oomString); |
| 1233 | dropBuilder.append('\n'); |
| 1234 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1235 | try { |
| 1236 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1237 | "procrank", }); |
| 1238 | final InputStreamReader converter = new InputStreamReader( |
| 1239 | proc.getInputStream()); |
| 1240 | BufferedReader in = new BufferedReader(converter); |
| 1241 | String line; |
| 1242 | while (true) { |
| 1243 | line = in.readLine(); |
| 1244 | if (line == null) { |
| 1245 | break; |
| 1246 | } |
| 1247 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1248 | logBuilder.append(line); |
| 1249 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1250 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1251 | dropBuilder.append(line); |
| 1252 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1253 | } |
| 1254 | converter.close(); |
| 1255 | } catch (IOException e) { |
| 1256 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1257 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1258 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1259 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1260 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1261 | dumpServicesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1262 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1263 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1264 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1265 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1266 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1267 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1268 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1269 | synchronized (ActivityManagerService.this) { |
| 1270 | long now = SystemClock.uptimeMillis(); |
| 1271 | if (mLastMemUsageReportTime < now) { |
| 1272 | mLastMemUsageReportTime = now; |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | }; |
| 1277 | thread.start(); |
| 1278 | break; |
| 1279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | }; |
| 1283 | |
| 1284 | public static void setSystemProcess() { |
| 1285 | try { |
| 1286 | ActivityManagerService m = mSelf; |
| 1287 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1288 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1290 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1291 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | if (MONITOR_CPU_USAGE) { |
| 1293 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1296 | |
| 1297 | ApplicationInfo info = |
| 1298 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1299 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1300 | mSystemThread.installSystemApplicationInfo(info); |
| 1301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1302 | synchronized (mSelf) { |
| 1303 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1304 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1305 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1307 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1308 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1309 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | synchronized (mSelf.mPidsSelfLocked) { |
| 1311 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1312 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1313 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | } |
| 1315 | } catch (PackageManager.NameNotFoundException e) { |
| 1316 | throw new RuntimeException( |
| 1317 | "Unable to find android system package", e); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | public void setWindowManager(WindowManagerService wm) { |
| 1322 | mWindowManager = wm; |
| 1323 | } |
| 1324 | |
| 1325 | public static final Context main(int factoryTest) { |
| 1326 | AThread thr = new AThread(); |
| 1327 | thr.start(); |
| 1328 | |
| 1329 | synchronized (thr) { |
| 1330 | while (thr.mService == null) { |
| 1331 | try { |
| 1332 | thr.wait(); |
| 1333 | } catch (InterruptedException e) { |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | ActivityManagerService m = thr.mService; |
| 1339 | mSelf = m; |
| 1340 | ActivityThread at = ActivityThread.systemMain(); |
| 1341 | mSystemThread = at; |
| 1342 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1343 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | m.mContext = context; |
| 1345 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1346 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1347 | |
| 1348 | m.mBatteryStatsService.publish(context); |
| 1349 | m.mUsageStatsService.publish(context); |
| 1350 | |
| 1351 | synchronized (thr) { |
| 1352 | thr.mReady = true; |
| 1353 | thr.notifyAll(); |
| 1354 | } |
| 1355 | |
| 1356 | m.startRunning(null, null, null, null); |
| 1357 | |
| 1358 | return context; |
| 1359 | } |
| 1360 | |
| 1361 | public static ActivityManagerService self() { |
| 1362 | return mSelf; |
| 1363 | } |
| 1364 | |
| 1365 | static class AThread extends Thread { |
| 1366 | ActivityManagerService mService; |
| 1367 | boolean mReady = false; |
| 1368 | |
| 1369 | public AThread() { |
| 1370 | super("ActivityManager"); |
| 1371 | } |
| 1372 | |
| 1373 | public void run() { |
| 1374 | Looper.prepare(); |
| 1375 | |
| 1376 | android.os.Process.setThreadPriority( |
| 1377 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1378 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1379 | |
| 1380 | ActivityManagerService m = new ActivityManagerService(); |
| 1381 | |
| 1382 | synchronized (this) { |
| 1383 | mService = m; |
| 1384 | notifyAll(); |
| 1385 | } |
| 1386 | |
| 1387 | synchronized (this) { |
| 1388 | while (!mReady) { |
| 1389 | try { |
| 1390 | wait(); |
| 1391 | } catch (InterruptedException e) { |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1396 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1397 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1398 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1399 | } |
| 1400 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1401 | Looper.loop(); |
| 1402 | } |
| 1403 | } |
| 1404 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | static class MemBinder extends Binder { |
| 1406 | ActivityManagerService mActivityManagerService; |
| 1407 | MemBinder(ActivityManagerService activityManagerService) { |
| 1408 | mActivityManagerService = activityManagerService; |
| 1409 | } |
| 1410 | |
| 1411 | @Override |
| 1412 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1413 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1414 | != PackageManager.PERMISSION_GRANTED) { |
| 1415 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1416 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1417 | + " without permission " + android.Manifest.permission.DUMP); |
| 1418 | return; |
| 1419 | } |
| 1420 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1421 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1422 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1426 | static class GraphicsBinder extends Binder { |
| 1427 | ActivityManagerService mActivityManagerService; |
| 1428 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1429 | mActivityManagerService = activityManagerService; |
| 1430 | } |
| 1431 | |
| 1432 | @Override |
| 1433 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1434 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1435 | != PackageManager.PERMISSION_GRANTED) { |
| 1436 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1437 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1438 | + " without permission " + android.Manifest.permission.DUMP); |
| 1439 | return; |
| 1440 | } |
| 1441 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1442 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1446 | static class DbBinder extends Binder { |
| 1447 | ActivityManagerService mActivityManagerService; |
| 1448 | DbBinder(ActivityManagerService activityManagerService) { |
| 1449 | mActivityManagerService = activityManagerService; |
| 1450 | } |
| 1451 | |
| 1452 | @Override |
| 1453 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1454 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1455 | != PackageManager.PERMISSION_GRANTED) { |
| 1456 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1457 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1458 | + " without permission " + android.Manifest.permission.DUMP); |
| 1459 | return; |
| 1460 | } |
| 1461 | |
| 1462 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1463 | } |
| 1464 | } |
| 1465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1466 | static class CpuBinder extends Binder { |
| 1467 | ActivityManagerService mActivityManagerService; |
| 1468 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1469 | mActivityManagerService = activityManagerService; |
| 1470 | } |
| 1471 | |
| 1472 | @Override |
| 1473 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1474 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1475 | != PackageManager.PERMISSION_GRANTED) { |
| 1476 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1477 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1478 | + " without permission " + android.Manifest.permission.DUMP); |
| 1479 | return; |
| 1480 | } |
| 1481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1483 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1484 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1485 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1491 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1492 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1493 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1494 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1495 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1496 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 1497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | File dataDir = Environment.getDataDirectory(); |
| 1499 | File systemDir = new File(dataDir, "system"); |
| 1500 | systemDir.mkdirs(); |
| 1501 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1502 | systemDir, "batterystats.bin").toString()); |
| 1503 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1504 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1505 | mOnBattery = DEBUG_POWER ? true |
| 1506 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1507 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1509 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1510 | systemDir, "usagestats").toString()); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1511 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1513 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1514 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1515 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1516 | mConfiguration.setToDefaults(); |
| 1517 | mConfiguration.locale = Locale.getDefault(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1518 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | mProcessStats.init(); |
| 1520 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1521 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1522 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1523 | // Add ourself to the Watchdog monitors. |
| 1524 | Watchdog.getInstance().addMonitor(this); |
| 1525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1527 | public void run() { |
| 1528 | while (true) { |
| 1529 | try { |
| 1530 | try { |
| 1531 | synchronized(this) { |
| 1532 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1533 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1535 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | // + ", write delay=" + nextWriteDelay); |
| 1537 | if (nextWriteDelay < nextCpuDelay) { |
| 1538 | nextCpuDelay = nextWriteDelay; |
| 1539 | } |
| 1540 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1541 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | this.wait(nextCpuDelay); |
| 1543 | } |
| 1544 | } |
| 1545 | } catch (InterruptedException e) { |
| 1546 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | updateCpuStatsNow(); |
| 1548 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1549 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | }; |
| 1554 | mProcessStatsThread.start(); |
| 1555 | } |
| 1556 | |
| 1557 | @Override |
| 1558 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1559 | throws RemoteException { |
| 1560 | try { |
| 1561 | return super.onTransact(code, data, reply, flags); |
| 1562 | } catch (RuntimeException e) { |
| 1563 | // The activity manager only throws security exceptions, so let's |
| 1564 | // log all others. |
| 1565 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1566 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | } |
| 1568 | throw e; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1573 | final long now = SystemClock.uptimeMillis(); |
| 1574 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1575 | return; |
| 1576 | } |
| 1577 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1578 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | mProcessStatsThread.notify(); |
| 1580 | } |
| 1581 | } |
| 1582 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | void updateCpuStatsNow() { |
| 1585 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1586 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | final long now = SystemClock.uptimeMillis(); |
| 1588 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1591 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1592 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | haveNewCpuStats = true; |
| 1594 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1595 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1596 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1597 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1598 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1599 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1601 | int user = mProcessStats.getLastUserTime(); |
| 1602 | int system = mProcessStats.getLastSystemTime(); |
| 1603 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1604 | int irq = mProcessStats.getLastIrqTime(); |
| 1605 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1606 | int idle = mProcessStats.getLastIdleTime(); |
| 1607 | |
| 1608 | int total = user + system + iowait + irq + softIrq + idle; |
| 1609 | if (total == 0) total = 1; |
| 1610 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1611 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1613 | (user * 100) / total, |
| 1614 | (system * 100) / total, |
| 1615 | (iowait * 100) / total, |
| 1616 | (irq * 100) / total, |
| 1617 | (softIrq * 100) / total); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1621 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1622 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1623 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | synchronized(mPidsSelfLocked) { |
| 1625 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1626 | if (mOnBattery) { |
| 1627 | int perc = bstats.startAddingCpuLocked(); |
| 1628 | int totalUTime = 0; |
| 1629 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1630 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1632 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1633 | if (!st.working) { |
| 1634 | continue; |
| 1635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1637 | int otherUTime = (st.rel_utime*perc)/100; |
| 1638 | int otherSTime = (st.rel_stime*perc)/100; |
| 1639 | totalUTime += otherUTime; |
| 1640 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | if (pr != null) { |
| 1642 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1643 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1644 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1645 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1646 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1647 | } else { |
| 1648 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1649 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1650 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1651 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1652 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1653 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1654 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1655 | } |
| 1656 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1657 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1658 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1659 | } |
| 1660 | } |
| 1661 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1662 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1664 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1665 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1671 | @Override |
| 1672 | public void batteryNeedsCpuUpdate() { |
| 1673 | updateCpuStatsNow(); |
| 1674 | } |
| 1675 | |
| 1676 | @Override |
| 1677 | public void batteryPowerChanged(boolean onBattery) { |
| 1678 | // When plugging in, update the CPU stats first before changing |
| 1679 | // the plug state. |
| 1680 | updateCpuStatsNow(); |
| 1681 | synchronized (this) { |
| 1682 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1683 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | /** |
| 1689 | * Initialize the application bind args. These are passed to each |
| 1690 | * process when the bindApplication() IPC is sent to the process. They're |
| 1691 | * lazily setup to make sure the services are running when they're asked for. |
| 1692 | */ |
| 1693 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1694 | if (mAppBindArgs == null) { |
| 1695 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1696 | |
| 1697 | // Setup the application init args |
| 1698 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1699 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1700 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1701 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1702 | } |
| 1703 | return mAppBindArgs; |
| 1704 | } |
| 1705 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1706 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | if (mFocusedActivity != r) { |
| 1708 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1709 | if (r != null) { |
| 1710 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1711 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1715 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1716 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1717 | // 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] | 1718 | int lrui = mLruProcesses.indexOf(app); |
| 1719 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1720 | |
| 1721 | int i = mLruProcesses.size()-1; |
| 1722 | int skipTop = 0; |
| 1723 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1724 | app.lruSeq = mLruSeq; |
| 1725 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1726 | // compute the new weight for this process. |
| 1727 | if (updateActivityTime) { |
| 1728 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1729 | } |
| 1730 | if (app.activities.size() > 0) { |
| 1731 | // If this process has activities, we more strongly want to keep |
| 1732 | // it around. |
| 1733 | app.lruWeight = app.lastActivityTime; |
| 1734 | } else if (app.pubProviders.size() > 0) { |
| 1735 | // If this process contains content providers, we want to keep |
| 1736 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1737 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1738 | // 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] | 1739 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1740 | } else { |
| 1741 | // If this process doesn't have activities, we less strongly |
| 1742 | // want to keep it around, and generally want to avoid getting |
| 1743 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1744 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1745 | // 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] | 1746 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1747 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1748 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1749 | while (i >= 0) { |
| 1750 | ProcessRecord p = mLruProcesses.get(i); |
| 1751 | // If this app shouldn't be in front of the first N background |
| 1752 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1753 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1754 | skipTop--; |
| 1755 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1756 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1757 | mLruProcesses.add(i+1, app); |
| 1758 | break; |
| 1759 | } |
| 1760 | i--; |
| 1761 | } |
| 1762 | if (i < 0) { |
| 1763 | mLruProcesses.add(0, app); |
| 1764 | } |
| 1765 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1766 | // If the app is currently using a content provider or service, |
| 1767 | // bump those processes as well. |
| 1768 | if (app.connections.size() > 0) { |
| 1769 | for (ConnectionRecord cr : app.connections) { |
| 1770 | if (cr.binding != null && cr.binding.service != null |
| 1771 | && cr.binding.service.app != null |
| 1772 | && cr.binding.service.app.lruSeq != mLruSeq) { |
| 1773 | updateLruProcessInternalLocked(cr.binding.service.app, oomAdj, |
| 1774 | updateActivityTime, i+1); |
| 1775 | } |
| 1776 | } |
| 1777 | } |
| 1778 | if (app.conProviders.size() > 0) { |
| 1779 | for (ContentProviderRecord cpr : app.conProviders.keySet()) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 1780 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
| 1781 | updateLruProcessInternalLocked(cpr.proc, oomAdj, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1782 | updateActivityTime, i+1); |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1787 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1788 | if (oomAdj) { |
| 1789 | updateOomAdjLocked(); |
| 1790 | } |
| 1791 | } |
| 1792 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1793 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1794 | boolean oomAdj, boolean updateActivityTime) { |
| 1795 | mLruSeq++; |
| 1796 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1797 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1798 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1799 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | String processName, int uid) { |
Amith Yamasani | 0184ce9 | 2012-03-28 22:41:41 -0700 | [diff] [blame] | 1801 | // Temporary hack to make Settings run per user |
| 1802 | if (uid == Process.SYSTEM_UID && !processName.equals("com.android.settings")) { |
| 1803 | // The system gets to run in any process. If there are multiple |
| 1804 | // processes with the same uid, just pick the first (this |
| 1805 | // should never happen). |
| 1806 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1807 | processName); |
| 1808 | return procs != null ? procs.valueAt(0) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1810 | // uid = applyUserId(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1812 | return proc; |
| 1813 | } |
| 1814 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1815 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1816 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1817 | try { |
| 1818 | if (pm.performDexOpt(packageName)) { |
| 1819 | mDidDexOpt = true; |
| 1820 | } |
| 1821 | } catch (RemoteException e) { |
| 1822 | } |
| 1823 | } |
| 1824 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1825 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1826 | int transit = mWindowManager.getPendingAppTransition(); |
| 1827 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1828 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1829 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1830 | } |
| 1831 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1832 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1834 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 1835 | boolean isolated) { |
| 1836 | ProcessRecord app; |
| 1837 | if (!isolated) { |
| 1838 | app = getProcessRecordLocked(processName, info.uid); |
| 1839 | } else { |
| 1840 | // If this is an isolated process, it can't re-use an existing process. |
| 1841 | app = null; |
| 1842 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1843 | // We don't have to do anything more if: |
| 1844 | // (1) There is an existing application record; and |
| 1845 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1846 | // object attached to it so we know it couldn't have crashed; and |
| 1847 | // (3) There is a pid assigned to it, so it is either starting or |
| 1848 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1849 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1850 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1851 | + " thread=" + (app != null ? app.thread : null) |
| 1852 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1853 | if (app != null && app.pid > 0) { |
| 1854 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1855 | // We already have the app running, or are waiting for it to |
| 1856 | // 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] | 1857 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1858 | // If this is a new package in the process, add the package to the list |
| 1859 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1860 | return app; |
| 1861 | } else { |
| 1862 | // An application record is attached to a previous process, |
| 1863 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1864 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1865 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1867 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1868 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1869 | String hostingNameStr = hostingName != null |
| 1870 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1871 | |
| 1872 | if (!isolated) { |
| 1873 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1874 | // If we are in the background, then check to see if this process |
| 1875 | // is bad. If so, we will just silently fail. |
| 1876 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1877 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1878 | + "/" + info.processName); |
| 1879 | return null; |
| 1880 | } |
| 1881 | } else { |
| 1882 | // When the user is explicitly starting a process, then clear its |
| 1883 | // crash count so that we won't make it bad until they see at |
| 1884 | // least one crash dialog again, and make the process good again |
| 1885 | // if it had been bad. |
| 1886 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1887 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1888 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1889 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1890 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
| 1891 | info.processName); |
| 1892 | mBadProcesses.remove(info.processName, info.uid); |
| 1893 | if (app != null) { |
| 1894 | app.bad = false; |
| 1895 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1896 | } |
| 1897 | } |
| 1898 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1899 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1900 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1901 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 1902 | if (app == null) { |
| 1903 | Slog.w(TAG, "Failed making new process record for " |
| 1904 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 1905 | return null; |
| 1906 | } |
| 1907 | mProcessNames.put(processName, app.uid, app); |
| 1908 | if (isolated) { |
| 1909 | mIsolatedProcesses.put(app.uid, app); |
| 1910 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1911 | } else { |
| 1912 | // If this is a new package in the process, add the package to the list |
| 1913 | app.addPackage(info.packageName); |
| 1914 | } |
| 1915 | |
| 1916 | // If the system is not ready yet, then hold off on starting this |
| 1917 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1918 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1919 | && !isAllowedWhileBooting(info) |
| 1920 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1921 | if (!mProcessesOnHold.contains(app)) { |
| 1922 | mProcessesOnHold.add(app); |
| 1923 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1924 | 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] | 1925 | return app; |
| 1926 | } |
| 1927 | |
| 1928 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1929 | return (app.pid != 0) ? app : null; |
| 1930 | } |
| 1931 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1932 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1933 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1934 | } |
| 1935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1936 | private final void startProcessLocked(ProcessRecord app, |
| 1937 | String hostingType, String hostingNameStr) { |
| 1938 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1939 | synchronized (mPidsSelfLocked) { |
| 1940 | mPidsSelfLocked.remove(app.pid); |
| 1941 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1942 | } |
| 1943 | app.pid = 0; |
| 1944 | } |
| 1945 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1946 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1947 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1948 | mProcessesOnHold.remove(app); |
| 1949 | |
| 1950 | updateCpuStats(); |
| 1951 | |
| 1952 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1953 | mProcDeaths[0] = 0; |
| 1954 | |
| 1955 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1956 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1957 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1958 | int[] gids = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1959 | if (!app.isolated) { |
| 1960 | try { |
| 1961 | gids = mContext.getPackageManager().getPackageGids( |
| 1962 | app.info.packageName); |
| 1963 | } catch (PackageManager.NameNotFoundException e) { |
| 1964 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 1965 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1966 | } |
| 1967 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 1968 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1969 | && mTopComponent != null |
| 1970 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 1971 | uid = 0; |
| 1972 | } |
| 1973 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 1974 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 1975 | uid = 0; |
| 1976 | } |
| 1977 | } |
| 1978 | int debugFlags = 0; |
| 1979 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 1980 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 1981 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 1982 | // awkward to turn on otherwise. |
| 1983 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1984 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 1985 | // Run the app in safe mode if its manifest requests so or the |
| 1986 | // system is booted in safe mode. |
| 1987 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 1988 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 1989 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 1990 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1991 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 1992 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 1993 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 1994 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 1995 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 1996 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1997 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 1998 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 1999 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2000 | |
| 2001 | // Start the process. It will either succeed and return a result containing |
| 2002 | // the PID of the new process, or else throw a RuntimeException. |
| 2003 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 2004 | app.processName, uid, uid, gids, debugFlags, |
| 2005 | app.info.targetSdkVersion, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2006 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2008 | synchronized (bs) { |
| 2009 | if (bs.isOnBattery()) { |
| 2010 | app.batteryStats.incStartsLocked(); |
| 2011 | } |
| 2012 | } |
| 2013 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2014 | EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2015 | app.processName, hostingType, |
| 2016 | hostingNameStr != null ? hostingNameStr : ""); |
| 2017 | |
| 2018 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2019 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2020 | } |
| 2021 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2022 | StringBuilder buf = mStringBuilder; |
| 2023 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2024 | buf.append("Start proc "); |
| 2025 | buf.append(app.processName); |
| 2026 | buf.append(" for "); |
| 2027 | buf.append(hostingType); |
| 2028 | if (hostingNameStr != null) { |
| 2029 | buf.append(" "); |
| 2030 | buf.append(hostingNameStr); |
| 2031 | } |
| 2032 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2033 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2034 | buf.append(" uid="); |
| 2035 | buf.append(uid); |
| 2036 | buf.append(" gids={"); |
| 2037 | if (gids != null) { |
| 2038 | for (int gi=0; gi<gids.length; gi++) { |
| 2039 | if (gi != 0) buf.append(", "); |
| 2040 | buf.append(gids[gi]); |
| 2041 | |
| 2042 | } |
| 2043 | } |
| 2044 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2045 | Slog.i(TAG, buf.toString()); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2046 | app.pid = startResult.pid; |
| 2047 | app.usingWrapper = startResult.usingWrapper; |
| 2048 | app.removed = false; |
| 2049 | synchronized (mPidsSelfLocked) { |
| 2050 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2051 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2052 | msg.obj = app; |
| 2053 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2054 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2055 | } |
| 2056 | } catch (RuntimeException e) { |
| 2057 | // XXX do better error recovery. |
| 2058 | app.pid = 0; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2059 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | } |
| 2061 | } |
| 2062 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2063 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2064 | if (resumed) { |
| 2065 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2066 | } else { |
| 2067 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2068 | } |
| 2069 | } |
| 2070 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2071 | boolean startHomeActivityLocked(int userId) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2072 | if (mHeadless) { |
| 2073 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2074 | // when running headless. |
| 2075 | ensureBootCompleted(); |
| 2076 | return false; |
| 2077 | } |
| 2078 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2079 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2080 | && mTopAction == null) { |
| 2081 | // We are running in factory test mode, but unable to find |
| 2082 | // the factory test app, so just sit around displaying the |
| 2083 | // error message and don't try to start anything. |
| 2084 | return false; |
| 2085 | } |
| 2086 | Intent intent = new Intent( |
| 2087 | mTopAction, |
| 2088 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2089 | intent.setComponent(mTopComponent); |
| 2090 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2091 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2092 | } |
| 2093 | ActivityInfo aInfo = |
| 2094 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 2095 | STOCK_PM_FLAGS); |
| 2096 | if (aInfo != null) { |
| 2097 | intent.setComponent(new ComponentName( |
| 2098 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2099 | // Don't do this if the home app is currently being |
| 2100 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2101 | aInfo = new ActivityInfo(aInfo); |
| 2102 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2103 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2104 | aInfo.applicationInfo.uid); |
| 2105 | if (app == null || app.instrumentationClass == null) { |
| 2106 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2107 | mMainStack.startActivityLocked(null, intent, null, aInfo, |
| 2108 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2109 | } |
| 2110 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2111 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2112 | return true; |
| 2113 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2114 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2115 | /** |
| 2116 | * Starts the "new version setup screen" if appropriate. |
| 2117 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2118 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2119 | // Only do this once per boot. |
| 2120 | if (mCheckedForSetup) { |
| 2121 | return; |
| 2122 | } |
| 2123 | |
| 2124 | // We will show this screen if the current one is a different |
| 2125 | // version than the last one shown, and we are not running in |
| 2126 | // low-level factory test mode. |
| 2127 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2128 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 2129 | Settings.Secure.getInt(resolver, |
| 2130 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 2131 | mCheckedForSetup = true; |
| 2132 | |
| 2133 | // See if we should be showing the platform update setup UI. |
| 2134 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2135 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2136 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2137 | |
| 2138 | // We don't allow third party apps to replace this. |
| 2139 | ResolveInfo ri = null; |
| 2140 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2141 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2142 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2143 | ri = ris.get(i); |
| 2144 | break; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | if (ri != null) { |
| 2149 | String vers = ri.activityInfo.metaData != null |
| 2150 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2151 | : null; |
| 2152 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2153 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2154 | Intent.METADATA_SETUP_VERSION); |
| 2155 | } |
| 2156 | String lastVers = Settings.Secure.getString( |
| 2157 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2158 | if (vers != null && !vers.equals(lastVers)) { |
| 2159 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2160 | intent.setComponent(new ComponentName( |
| 2161 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2162 | mMainStack.startActivityLocked(null, intent, null, ri.activityInfo, |
| 2163 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2169 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2170 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2171 | } |
| 2172 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2173 | void enforceNotIsolatedCaller(String caller) { |
| 2174 | if (UserId.isIsolated(Binder.getCallingUid())) { |
| 2175 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2176 | } |
| 2177 | } |
| 2178 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2179 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2180 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2181 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2182 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2183 | } |
| 2184 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2185 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2186 | public void setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2187 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2188 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2189 | synchronized (this) { |
| 2190 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2191 | } |
| 2192 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2193 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2194 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2195 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2196 | synchronized (this) { |
| 2197 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2198 | } |
| 2199 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2200 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2201 | public void setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2202 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2203 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2204 | synchronized (this) { |
| 2205 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2209 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2210 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2211 | synchronized (this) { |
| 2212 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2217 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2218 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2219 | synchronized (this) { |
| 2220 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2221 | } |
| 2222 | } |
| 2223 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2224 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2225 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2226 | |
| 2227 | final int identHash = System.identityHashCode(r); |
| 2228 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2229 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2230 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2231 | private void dispatchForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
| 2232 | int i = mProcessObservers.beginBroadcast(); |
| 2233 | while (i > 0) { |
| 2234 | i--; |
| 2235 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2236 | if (observer != null) { |
| 2237 | try { |
| 2238 | observer.onForegroundActivitiesChanged(pid, uid, foregroundActivities); |
| 2239 | } catch (RemoteException e) { |
| 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | mProcessObservers.finishBroadcast(); |
| 2244 | } |
| 2245 | |
| 2246 | private void dispatchProcessDied(int pid, int uid) { |
| 2247 | int i = mProcessObservers.beginBroadcast(); |
| 2248 | while (i > 0) { |
| 2249 | i--; |
| 2250 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2251 | if (observer != null) { |
| 2252 | try { |
| 2253 | observer.onProcessDied(pid, uid); |
| 2254 | } catch (RemoteException e) { |
| 2255 | } |
| 2256 | } |
| 2257 | } |
| 2258 | mProcessObservers.finishBroadcast(); |
| 2259 | } |
| 2260 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2261 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2262 | final int N = mPendingActivityLaunches.size(); |
| 2263 | if (N <= 0) { |
| 2264 | return; |
| 2265 | } |
| 2266 | for (int i=0; i<N; i++) { |
| 2267 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2268 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2269 | pal.startFlags, doResume && i == (N-1), null); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2270 | } |
| 2271 | mPendingActivityLaunches.clear(); |
| 2272 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2273 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2274 | public final int startActivity(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2275 | Intent intent, String resolvedType, IBinder resultTo, |
| 2276 | String resultWho, int requestCode, int startFlags, |
| 2277 | String profileFile, ParcelFileDescriptor profileFd, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2278 | enforceNotIsolatedCaller("startActivity"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2279 | int userId = 0; |
| 2280 | if (intent.getCategories() != null && intent.getCategories().contains(Intent.CATEGORY_HOME)) { |
| 2281 | // Requesting home, set the identity to the current user |
| 2282 | // HACK! |
| 2283 | userId = mCurrentUserId; |
| 2284 | } else { |
| 2285 | // TODO: Fix this in a better way - calls coming from SystemUI should probably carry |
| 2286 | // the current user's userId |
| 2287 | if (Binder.getCallingUid() < Process.FIRST_APPLICATION_UID) { |
| 2288 | userId = 0; |
| 2289 | } else { |
| 2290 | userId = Binder.getOrigCallingUser(); |
| 2291 | } |
| 2292 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2293 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2294 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2295 | null, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2299 | Intent intent, String resolvedType, IBinder resultTo, |
| 2300 | String resultWho, int requestCode, int startFlags, String profileFile, |
| 2301 | ParcelFileDescriptor profileFd, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2302 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2303 | WaitResult res = new WaitResult(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2304 | int userId = Binder.getOrigCallingUser(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2305 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2306 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2307 | res, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2308 | return res; |
| 2309 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 2310 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2311 | public final int startActivityWithConfig(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2312 | Intent intent, String resolvedType, IBinder resultTo, |
| 2313 | String resultWho, int requestCode, int startFlags, Configuration config, |
| 2314 | Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2315 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2316 | int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2317 | resultTo, resultWho, requestCode, startFlags, |
| 2318 | null, null, null, config, options, Binder.getOrigCallingUser()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2319 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2322 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2323 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2324 | IBinder resultTo, String resultWho, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2325 | int flagsMask, int flagsValues, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2326 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2327 | // Refuse possible leaked file descriptors |
| 2328 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2329 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2330 | } |
| 2331 | |
| 2332 | IIntentSender sender = intent.getTarget(); |
| 2333 | if (!(sender instanceof PendingIntentRecord)) { |
| 2334 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2335 | } |
| 2336 | |
| 2337 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2338 | |
| 2339 | synchronized (this) { |
| 2340 | // If this is coming from the currently resumed activity, it is |
| 2341 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2342 | if (mMainStack.mResumedActivity != null |
| 2343 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2344 | Binder.getCallingUid()) { |
| 2345 | mAppSwitchesAllowedTime = 0; |
| 2346 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2347 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2348 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null, |
| 2349 | resultTo, resultWho, requestCode, flagsMask, flagsValues, options); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2350 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2353 | public boolean startNextMatchingActivity(IBinder callingActivity, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2354 | Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2355 | // Refuse possible leaked file descriptors |
| 2356 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2357 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2358 | } |
| 2359 | |
| 2360 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2361 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2362 | if (r == null) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2363 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2364 | return false; |
| 2365 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2366 | if (r.app == null || r.app.thread == null) { |
| 2367 | // The caller is not running... d'oh! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2368 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2369 | return false; |
| 2370 | } |
| 2371 | intent = new Intent(intent); |
| 2372 | // The caller is not allowed to change the data. |
| 2373 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2374 | // And we are resetting to find the next component... |
| 2375 | intent.setComponent(null); |
| 2376 | |
| 2377 | ActivityInfo aInfo = null; |
| 2378 | try { |
| 2379 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2380 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2381 | intent, r.resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2382 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS, |
| 2383 | UserId.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | |
| 2385 | // Look for the original activity in the list... |
| 2386 | final int N = resolves != null ? resolves.size() : 0; |
| 2387 | for (int i=0; i<N; i++) { |
| 2388 | ResolveInfo rInfo = resolves.get(i); |
| 2389 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2390 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2391 | // We found the current one... the next matching is |
| 2392 | // after it. |
| 2393 | i++; |
| 2394 | if (i<N) { |
| 2395 | aInfo = resolves.get(i).activityInfo; |
| 2396 | } |
| 2397 | break; |
| 2398 | } |
| 2399 | } |
| 2400 | } catch (RemoteException e) { |
| 2401 | } |
| 2402 | |
| 2403 | if (aInfo == null) { |
| 2404 | // Nobody who is next! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2405 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2406 | return false; |
| 2407 | } |
| 2408 | |
| 2409 | intent.setComponent(new ComponentName( |
| 2410 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2411 | intent.setFlags(intent.getFlags()&~( |
| 2412 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2413 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2414 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2415 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2416 | |
| 2417 | // Okay now we need to start the new activity, replacing the |
| 2418 | // currently running activity. This is a little tricky because |
| 2419 | // we want to start the new one as if the current one is finished, |
| 2420 | // but not finish the current one first so that there is no flicker. |
| 2421 | // And thus... |
| 2422 | final boolean wasFinishing = r.finishing; |
| 2423 | r.finishing = true; |
| 2424 | |
| 2425 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2426 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2427 | final String resultWho = r.resultWho; |
| 2428 | final int requestCode = r.requestCode; |
| 2429 | r.resultTo = null; |
| 2430 | if (resultTo != null) { |
| 2431 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2432 | } |
| 2433 | |
| 2434 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2435 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2436 | r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null, |
| 2437 | resultWho, requestCode, -1, r.launchedFromUid, 0, |
| 2438 | options, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2439 | Binder.restoreCallingIdentity(origId); |
| 2440 | |
| 2441 | r.finishing = wasFinishing; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2442 | if (res != ActivityManager.START_SUCCESS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2443 | return false; |
| 2444 | } |
| 2445 | return true; |
| 2446 | } |
| 2447 | } |
| 2448 | |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2449 | public final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2450 | Intent intent, String resolvedType, IBinder resultTo, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2451 | String resultWho, int requestCode, int startFlags, Bundle options) { |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2452 | |
| 2453 | // This is so super not safe, that only the system (or okay root) |
| 2454 | // can do it. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2455 | int userId = Binder.getOrigCallingUser(); |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2456 | final int callingUid = Binder.getCallingUid(); |
| 2457 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2458 | throw new SecurityException( |
| 2459 | "startActivityInPackage only available to the system"); |
| 2460 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2461 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2462 | int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2463 | resultTo, resultWho, requestCode, startFlags, |
| 2464 | null, null, null, null, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2465 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | public final int startActivities(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2469 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2470 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2471 | int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2472 | options, Binder.getOrigCallingUser()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2473 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | public final int startActivitiesInPackage(int uid, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2477 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, |
| 2478 | Bundle options) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2479 | |
| 2480 | // This is so super not safe, that only the system (or okay root) |
| 2481 | // can do it. |
| 2482 | final int callingUid = Binder.getCallingUid(); |
| 2483 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2484 | throw new SecurityException( |
| 2485 | "startActivityInPackage only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2486 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2487 | int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2488 | options, UserId.getUserId(uid)); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2489 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2490 | } |
| 2491 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2492 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2493 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2494 | // Quick case: check if the top-most recent task is the same. |
| 2495 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2496 | return; |
| 2497 | } |
| 2498 | // 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] | 2499 | for (int i=0; i<N; i++) { |
| 2500 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2501 | if (task.userId == tr.userId |
| 2502 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2503 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2504 | mRecentTasks.remove(i); |
| 2505 | i--; |
| 2506 | N--; |
| 2507 | if (task.intent == null) { |
| 2508 | // If the new recent task we are adding is not fully |
| 2509 | // specified, then replace it with the existing recent task. |
| 2510 | task = tr; |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | if (N >= MAX_RECENT_TASKS) { |
| 2515 | mRecentTasks.remove(N-1); |
| 2516 | } |
| 2517 | mRecentTasks.add(0, task); |
| 2518 | } |
| 2519 | |
| 2520 | public void setRequestedOrientation(IBinder token, |
| 2521 | int requestedOrientation) { |
| 2522 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2523 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2524 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2525 | return; |
| 2526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2527 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2528 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2529 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2530 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2531 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2532 | if (config != null) { |
| 2533 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2534 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2535 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2536 | } |
| 2537 | } |
| 2538 | Binder.restoreCallingIdentity(origId); |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | public int getRequestedOrientation(IBinder token) { |
| 2543 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2544 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2545 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2546 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2547 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2548 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2549 | } |
| 2550 | } |
| 2551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2552 | /** |
| 2553 | * This is the internal entry point for handling Activity.finish(). |
| 2554 | * |
| 2555 | * @param token The Binder token referencing the Activity we want to finish. |
| 2556 | * @param resultCode Result code, if any, from this Activity. |
| 2557 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2558 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2559 | * @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] | 2560 | */ |
| 2561 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2562 | // Refuse possible leaked file descriptors |
| 2563 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2564 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2565 | } |
| 2566 | |
| 2567 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2568 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2569 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2570 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2571 | if (next != null) { |
| 2572 | // ask watcher if this is allowed |
| 2573 | boolean resumeOK = true; |
| 2574 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2575 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2576 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2577 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | if (!resumeOK) { |
| 2581 | return false; |
| 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2586 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2587 | resultData, "app-request"); |
| 2588 | Binder.restoreCallingIdentity(origId); |
| 2589 | return res; |
| 2590 | } |
| 2591 | } |
| 2592 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2593 | public final void finishHeavyWeightApp() { |
| 2594 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2595 | != PackageManager.PERMISSION_GRANTED) { |
| 2596 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2597 | + Binder.getCallingPid() |
| 2598 | + ", uid=" + Binder.getCallingUid() |
| 2599 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2600 | Slog.w(TAG, msg); |
| 2601 | throw new SecurityException(msg); |
| 2602 | } |
| 2603 | |
| 2604 | synchronized(this) { |
| 2605 | if (mHeavyWeightProcess == null) { |
| 2606 | return; |
| 2607 | } |
| 2608 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2609 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2610 | mHeavyWeightProcess.activities); |
| 2611 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2612 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2613 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2614 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2615 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2616 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2617 | null, "finish-heavy"); |
| 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | mHeavyWeightProcess = null; |
| 2623 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 2624 | } |
| 2625 | } |
| 2626 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2627 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2628 | String message) { |
| 2629 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2630 | != PackageManager.PERMISSION_GRANTED) { |
| 2631 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2632 | + Binder.getCallingPid() |
| 2633 | + ", uid=" + Binder.getCallingUid() |
| 2634 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2635 | Slog.w(TAG, msg); |
| 2636 | throw new SecurityException(msg); |
| 2637 | } |
| 2638 | |
| 2639 | synchronized(this) { |
| 2640 | ProcessRecord proc = null; |
| 2641 | |
| 2642 | // Figure out which process to kill. We don't trust that initialPid |
| 2643 | // still has any relation to current pids, so must scan through the |
| 2644 | // list. |
| 2645 | synchronized (mPidsSelfLocked) { |
| 2646 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2647 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2648 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2649 | continue; |
| 2650 | } |
| 2651 | if (p.pid == initialPid) { |
| 2652 | proc = p; |
| 2653 | break; |
| 2654 | } |
| 2655 | for (String str : p.pkgList) { |
| 2656 | if (str.equals(packageName)) { |
| 2657 | proc = p; |
| 2658 | } |
| 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2664 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2665 | + " initialPid=" + initialPid |
| 2666 | + " packageName=" + packageName); |
| 2667 | return; |
| 2668 | } |
| 2669 | |
| 2670 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2671 | if (proc.pid == Process.myPid()) { |
| 2672 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2673 | return; |
| 2674 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2675 | long ident = Binder.clearCallingIdentity(); |
| 2676 | try { |
| 2677 | proc.thread.scheduleCrash(message); |
| 2678 | } catch (RemoteException e) { |
| 2679 | } |
| 2680 | Binder.restoreCallingIdentity(ident); |
| 2681 | } |
| 2682 | } |
| 2683 | } |
| 2684 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2685 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2686 | int requestCode) { |
| 2687 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2688 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2689 | if (self == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2690 | return; |
| 2691 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2692 | |
| 2693 | final long origId = Binder.clearCallingIdentity(); |
| 2694 | |
| 2695 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2696 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2697 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2698 | if (r.resultTo == self && r.requestCode == requestCode) { |
| 2699 | if ((r.resultWho == null && resultWho == null) || |
| 2700 | (r.resultWho != null && r.resultWho.equals(resultWho))) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2701 | mMainStack.finishActivityLocked(r, i, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2702 | Activity.RESULT_CANCELED, null, "request-sub"); |
| 2703 | } |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | Binder.restoreCallingIdentity(origId); |
| 2708 | } |
| 2709 | } |
| 2710 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2711 | public boolean willActivityBeVisible(IBinder token) { |
| 2712 | synchronized(this) { |
| 2713 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2714 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2715 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2716 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2717 | return true; |
| 2718 | } |
| 2719 | if (r.fullscreen && !r.finishing) { |
| 2720 | return false; |
| 2721 | } |
| 2722 | } |
| 2723 | return true; |
| 2724 | } |
| 2725 | } |
| 2726 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2727 | public void overridePendingTransition(IBinder token, String packageName, |
| 2728 | int enterAnim, int exitAnim) { |
| 2729 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2730 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2731 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2732 | return; |
| 2733 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2734 | |
| 2735 | final long origId = Binder.clearCallingIdentity(); |
| 2736 | |
| 2737 | if (self.state == ActivityState.RESUMED |
| 2738 | || self.state == ActivityState.PAUSING) { |
| 2739 | mWindowManager.overridePendingAppTransition(packageName, |
| 2740 | enterAnim, exitAnim); |
| 2741 | } |
| 2742 | |
| 2743 | Binder.restoreCallingIdentity(origId); |
| 2744 | } |
| 2745 | } |
| 2746 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2747 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2748 | * Main function for removing an existing process from the activity manager |
| 2749 | * as a result of that process going away. Clears out all connections |
| 2750 | * to the process. |
| 2751 | */ |
| 2752 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2753 | boolean restarting, boolean allowRestart) { |
| 2754 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2755 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2756 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2757 | } |
| 2758 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2759 | if (mProfileProc == app) { |
| 2760 | clearProfilerLocked(); |
| 2761 | } |
| 2762 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | // Just in case... |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2764 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2765 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2766 | mMainStack.mPausingActivity = null; |
| 2767 | } |
| 2768 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2769 | mMainStack.mLastPausedActivity = null; |
| 2770 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2771 | |
| 2772 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2773 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2774 | |
| 2775 | boolean atTop = true; |
| 2776 | boolean hasVisibleActivities = false; |
| 2777 | |
| 2778 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2779 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2780 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2781 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2782 | while (i > 0) { |
| 2783 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2784 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2785 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2786 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2787 | if (r.app == app) { |
| 2788 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2789 | if (ActivityStack.DEBUG_ADD_REMOVE) { |
| 2790 | RuntimeException here = new RuntimeException("here"); |
| 2791 | here.fillInStackTrace(); |
| 2792 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 2793 | + ": haveState=" + r.haveState |
| 2794 | + " stateNotNeeded=" + r.stateNotNeeded |
| 2795 | + " finishing=" + r.finishing |
| 2796 | + " state=" + r.state, here); |
| 2797 | } |
| 2798 | if (!r.finishing) { |
| 2799 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 2800 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
| 2801 | System.identityHashCode(r), |
| 2802 | r.task.taskId, r.shortComponentName, |
| 2803 | "proc died without state saved"); |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2804 | } |
Dianne Hackborn | 5c60743 | 2012-02-28 14:44:19 -0800 | [diff] [blame] | 2805 | mMainStack.removeActivityFromHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2806 | |
| 2807 | } else { |
| 2808 | // We have the current state for this activity, so |
| 2809 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2810 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2811 | TAG, "Keeping entry, setting app to null"); |
| 2812 | if (r.visible) { |
| 2813 | hasVisibleActivities = true; |
| 2814 | } |
| 2815 | r.app = null; |
| 2816 | r.nowVisible = false; |
| 2817 | if (!r.haveState) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2818 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 2819 | "App died, clearing saved state of " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2820 | r.icicle = null; |
| 2821 | } |
| 2822 | } |
| 2823 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2824 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2825 | } |
| 2826 | atTop = false; |
| 2827 | } |
| 2828 | |
| 2829 | app.activities.clear(); |
| 2830 | |
| 2831 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2832 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2833 | + " running instrumentation " + app.instrumentationClass); |
| 2834 | Bundle info = new Bundle(); |
| 2835 | info.putString("shortMsg", "Process crashed."); |
| 2836 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2837 | } |
| 2838 | |
| 2839 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2840 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2841 | // If there was nothing to resume, and we are not already |
| 2842 | // restarting this process, but there is a visible activity that |
| 2843 | // is hosted by the process... then make sure all visible |
| 2844 | // activities are running, taking care of restarting this |
| 2845 | // process. |
| 2846 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2847 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2854 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2855 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2856 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2857 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2858 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2859 | return i; |
| 2860 | } |
| 2861 | } |
| 2862 | return -1; |
| 2863 | } |
| 2864 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2865 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2866 | IApplicationThread thread) { |
| 2867 | if (thread == null) { |
| 2868 | return null; |
| 2869 | } |
| 2870 | |
| 2871 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2872 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2873 | } |
| 2874 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2875 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2876 | IApplicationThread thread) { |
| 2877 | |
| 2878 | mProcDeaths[0]++; |
| 2879 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2880 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2881 | synchronized (stats) { |
| 2882 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2883 | } |
| 2884 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2885 | // Clean up already done if the process has been re-started. |
| 2886 | if (app.pid == pid && app.thread != null && |
| 2887 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2888 | if (!app.killedBackground) { |
| 2889 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2890 | + ") has died."); |
| 2891 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2892 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2893 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2894 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2895 | + ", thread: " + thread.asBinder()); |
| 2896 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2897 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2898 | |
| 2899 | if (doLowMem) { |
| 2900 | // If there are no longer any background processes running, |
| 2901 | // and the app that died was not running instrumentation, |
| 2902 | // then tell everyone we are now low on memory. |
| 2903 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2904 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2905 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2906 | 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] | 2907 | haveBg = true; |
| 2908 | break; |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2913 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2914 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2915 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2916 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2917 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2918 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2919 | // The low memory report is overriding any current |
| 2920 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2921 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2922 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2923 | rec.lastRequestedGc = 0; |
| 2924 | } else { |
| 2925 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2926 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2927 | rec.reportLowMemory = true; |
| 2928 | rec.lastLowMemory = now; |
| 2929 | mProcessesToGc.remove(rec); |
| 2930 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2931 | } |
| 2932 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 2933 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2934 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2935 | } |
| 2936 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2937 | } else if (app.pid != pid) { |
| 2938 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2939 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2940 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 2941 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 2942 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2943 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2944 | + thread.asBinder()); |
| 2945 | } |
| 2946 | } |
| 2947 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2948 | /** |
| 2949 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2950 | * @param clearTraces causes the dump file to be erased prior to the new |
| 2951 | * traces being written, if true; when false, the new traces will be |
| 2952 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2953 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 2954 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2955 | * @return file containing stack traces, or null if no dump file is configured |
| 2956 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2957 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
| 2958 | ProcessStats processStats, SparseArray<Boolean> lastPids) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2959 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 2960 | if (tracesPath == null || tracesPath.length() == 0) { |
| 2961 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2962 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2963 | |
| 2964 | File tracesFile = new File(tracesPath); |
| 2965 | try { |
| 2966 | File tracesDir = tracesFile.getParentFile(); |
| 2967 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 2968 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 2969 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2970 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2971 | tracesFile.createNewFile(); |
| 2972 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 2973 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2974 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2975 | return null; |
| 2976 | } |
| 2977 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 2978 | dumpStackTraces(tracesPath, firstPids, processStats, lastPids); |
| 2979 | return tracesFile; |
| 2980 | } |
| 2981 | |
| 2982 | private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, |
| 2983 | ProcessStats processStats, SparseArray<Boolean> lastPids) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2984 | // Use a FileObserver to detect when traces finish writing. |
| 2985 | // The order of traces is considered important to maintain for legibility. |
| 2986 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 2987 | public synchronized void onEvent(int event, String path) { notify(); } |
| 2988 | }; |
| 2989 | |
| 2990 | try { |
| 2991 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2992 | |
| 2993 | // First collect all of the stacks of the most important pids. |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 2994 | if (firstPids != null) { |
| 2995 | try { |
| 2996 | int num = firstPids.size(); |
| 2997 | for (int i = 0; i < num; i++) { |
| 2998 | synchronized (observer) { |
| 2999 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3000 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3001 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3002 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3003 | } catch (InterruptedException e) { |
| 3004 | Log.wtf(TAG, e); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3005 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | // Next measure CPU usage. |
| 3009 | if (processStats != null) { |
| 3010 | processStats.init(); |
| 3011 | System.gc(); |
| 3012 | processStats.update(); |
| 3013 | try { |
| 3014 | synchronized (processStats) { |
| 3015 | processStats.wait(500); // measure over 1/2 second. |
| 3016 | } |
| 3017 | } catch (InterruptedException e) { |
| 3018 | } |
| 3019 | processStats.update(); |
| 3020 | |
| 3021 | // We'll take the stack crawls of just the top apps using CPU. |
| 3022 | final int N = processStats.countWorkingStats(); |
| 3023 | int numProcs = 0; |
| 3024 | for (int i=0; i<N && numProcs<5; i++) { |
| 3025 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3026 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3027 | numProcs++; |
| 3028 | try { |
| 3029 | synchronized (observer) { |
| 3030 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3031 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3032 | } |
| 3033 | } catch (InterruptedException e) { |
| 3034 | Log.wtf(TAG, e); |
| 3035 | } |
| 3036 | |
| 3037 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3038 | } |
| 3039 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3040 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3041 | } finally { |
| 3042 | observer.stopWatching(); |
| 3043 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3044 | } |
| 3045 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3046 | final void logAppTooSlow(ProcessRecord app, long startTime, String msg) { |
Dianne Hackborn | 69dc66e | 2012-03-26 10:50:54 -0700 | [diff] [blame] | 3047 | if (true || IS_USER_BUILD) { |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3048 | return; |
| 3049 | } |
| 3050 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3051 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3052 | return; |
| 3053 | } |
| 3054 | |
| 3055 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 3056 | StrictMode.allowThreadDiskWrites(); |
| 3057 | try { |
| 3058 | final File tracesFile = new File(tracesPath); |
| 3059 | final File tracesDir = tracesFile.getParentFile(); |
| 3060 | final File tracesTmp = new File(tracesDir, "__tmp__"); |
| 3061 | try { |
| 3062 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 3063 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3064 | |
| 3065 | if (tracesFile.exists()) { |
| 3066 | tracesTmp.delete(); |
| 3067 | tracesFile.renameTo(tracesTmp); |
| 3068 | } |
| 3069 | StringBuilder sb = new StringBuilder(); |
| 3070 | Time tobj = new Time(); |
| 3071 | tobj.set(System.currentTimeMillis()); |
| 3072 | sb.append(tobj.format("%Y-%m-%d %H:%M:%S")); |
| 3073 | sb.append(": "); |
| 3074 | TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb); |
| 3075 | sb.append(" since "); |
| 3076 | sb.append(msg); |
| 3077 | FileOutputStream fos = new FileOutputStream(tracesFile); |
| 3078 | fos.write(sb.toString().getBytes()); |
| 3079 | if (app == null) { |
| 3080 | fos.write("\n*** No application process!".getBytes()); |
| 3081 | } |
| 3082 | fos.close(); |
| 3083 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3084 | } catch (IOException e) { |
| 3085 | Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e); |
| 3086 | return; |
| 3087 | } |
| 3088 | |
| 3089 | if (app != null) { |
| 3090 | ArrayList<Integer> firstPids = new ArrayList<Integer>(); |
| 3091 | firstPids.add(app.pid); |
| 3092 | dumpStackTraces(tracesPath, firstPids, null, null); |
| 3093 | } |
| 3094 | |
| 3095 | File lastTracesFile = null; |
| 3096 | File curTracesFile = null; |
| 3097 | for (int i=9; i>=0; i--) { |
| 3098 | String name = String.format("slow%02d.txt", i); |
| 3099 | curTracesFile = new File(tracesDir, name); |
| 3100 | if (curTracesFile.exists()) { |
| 3101 | if (lastTracesFile != null) { |
| 3102 | curTracesFile.renameTo(lastTracesFile); |
| 3103 | } else { |
| 3104 | curTracesFile.delete(); |
| 3105 | } |
| 3106 | } |
| 3107 | lastTracesFile = curTracesFile; |
| 3108 | } |
| 3109 | tracesFile.renameTo(curTracesFile); |
| 3110 | if (tracesTmp.exists()) { |
| 3111 | tracesTmp.renameTo(tracesFile); |
| 3112 | } |
| 3113 | } finally { |
| 3114 | StrictMode.setThreadPolicy(oldPolicy); |
| 3115 | } |
| 3116 | } |
| 3117 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3118 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 3119 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3120 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3121 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3122 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3123 | if (mController != null) { |
| 3124 | try { |
| 3125 | // 0 == continue, -1 = kill process immediately |
| 3126 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3127 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3128 | } catch (RemoteException e) { |
| 3129 | mController = null; |
| 3130 | } |
| 3131 | } |
| 3132 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3133 | long anrTime = SystemClock.uptimeMillis(); |
| 3134 | if (MONITOR_CPU_USAGE) { |
| 3135 | updateCpuStatsNow(); |
| 3136 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3137 | |
| 3138 | synchronized (this) { |
| 3139 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3140 | if (mShuttingDown) { |
| 3141 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3142 | return; |
| 3143 | } else if (app.notResponding) { |
| 3144 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3145 | return; |
| 3146 | } else if (app.crashing) { |
| 3147 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3148 | return; |
| 3149 | } |
| 3150 | |
| 3151 | // In case we come through here for the same app before completing |
| 3152 | // this one, mark as anring now so we will bail out. |
| 3153 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3154 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3155 | // Log the ANR to the event log. |
| 3156 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 3157 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3158 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3159 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3160 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3161 | |
| 3162 | int parentPid = app.pid; |
| 3163 | 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] | 3164 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3165 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3166 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3167 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3168 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3169 | ProcessRecord r = mLruProcesses.get(i); |
| 3170 | if (r != null && r.thread != null) { |
| 3171 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3172 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3173 | if (r.persistent) { |
| 3174 | firstPids.add(pid); |
| 3175 | } else { |
| 3176 | lastPids.put(pid, Boolean.TRUE); |
| 3177 | } |
| 3178 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3183 | // Log the ANR to the main log. |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3184 | StringBuilder info = mStringBuilder; |
| 3185 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3186 | info.append("ANR in ").append(app.processName); |
| 3187 | if (activity != null && activity.shortComponentName != null) { |
| 3188 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3189 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3190 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3191 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3192 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3193 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3194 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3195 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3196 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3197 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3198 | final ProcessStats processStats = new ProcessStats(true); |
| 3199 | |
| 3200 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids); |
| 3201 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3202 | String cpuInfo = null; |
| 3203 | if (MONITOR_CPU_USAGE) { |
| 3204 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3205 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3206 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3207 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3208 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3209 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3210 | } |
| 3211 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3212 | info.append(processStats.printCurrentState(anrTime)); |
| 3213 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3214 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3215 | if (tracesFile == null) { |
| 3216 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3217 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3218 | } |
| 3219 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3220 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3221 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3222 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3223 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3224 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3225 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3226 | 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] | 3227 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3228 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3229 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3230 | } |
| 3231 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3232 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3236 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3237 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3238 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3239 | |
| 3240 | synchronized (this) { |
| 3241 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3242 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 3243 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 3244 | app.processName, app.setAdj, "background ANR"); |
| 3245 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3246 | return; |
| 3247 | } |
| 3248 | |
| 3249 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3250 | makeAppNotRespondingLocked(app, |
| 3251 | activity != null ? activity.shortComponentName : null, |
| 3252 | annotation != null ? "ANR " + annotation : "ANR", |
| 3253 | info.toString()); |
| 3254 | |
| 3255 | // Bring up the infamous App Not Responding dialog |
| 3256 | Message msg = Message.obtain(); |
| 3257 | HashMap map = new HashMap(); |
| 3258 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3259 | msg.obj = map; |
| 3260 | map.put("app", app); |
| 3261 | if (activity != null) { |
| 3262 | map.put("activity", activity); |
| 3263 | } |
| 3264 | |
| 3265 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3266 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3267 | } |
| 3268 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3269 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3270 | if (!mLaunchWarningShown) { |
| 3271 | mLaunchWarningShown = true; |
| 3272 | mHandler.post(new Runnable() { |
| 3273 | @Override |
| 3274 | public void run() { |
| 3275 | synchronized (ActivityManagerService.this) { |
| 3276 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3277 | d.show(); |
| 3278 | mHandler.postDelayed(new Runnable() { |
| 3279 | @Override |
| 3280 | public void run() { |
| 3281 | synchronized (ActivityManagerService.this) { |
| 3282 | d.dismiss(); |
| 3283 | mLaunchWarningShown = false; |
| 3284 | } |
| 3285 | } |
| 3286 | }, 4000); |
| 3287 | } |
| 3288 | } |
| 3289 | }); |
| 3290 | } |
| 3291 | } |
| 3292 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3293 | public boolean clearApplicationUserData(final String packageName, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3294 | final IPackageDataObserver observer, final int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3295 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3296 | int uid = Binder.getCallingUid(); |
| 3297 | int pid = Binder.getCallingPid(); |
| 3298 | long callingId = Binder.clearCallingIdentity(); |
| 3299 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3300 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3301 | int pkgUid = -1; |
| 3302 | synchronized(this) { |
| 3303 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3304 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3305 | } catch (RemoteException e) { |
| 3306 | } |
| 3307 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3308 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3309 | return false; |
| 3310 | } |
| 3311 | if (uid == pkgUid || checkComponentPermission( |
| 3312 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3313 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3314 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3315 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3316 | } else { |
| 3317 | throw new SecurityException(pid+" does not have permission:"+ |
| 3318 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3319 | "for process:"+packageName); |
| 3320 | } |
| 3321 | } |
| 3322 | |
| 3323 | try { |
| 3324 | //clear application user data |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3325 | pm.clearApplicationUserData(packageName, observer, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3326 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3327 | Uri.fromParts("package", packageName, null)); |
| 3328 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3329 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3330 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3331 | } catch (RemoteException e) { |
| 3332 | } |
| 3333 | } finally { |
| 3334 | Binder.restoreCallingIdentity(callingId); |
| 3335 | } |
| 3336 | return true; |
| 3337 | } |
| 3338 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3339 | public void killBackgroundProcesses(final String packageName) { |
| 3340 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3341 | != PackageManager.PERMISSION_GRANTED && |
| 3342 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3343 | != PackageManager.PERMISSION_GRANTED) { |
| 3344 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3345 | + Binder.getCallingPid() |
| 3346 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3347 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3348 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3349 | throw new SecurityException(msg); |
| 3350 | } |
| 3351 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3352 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3353 | long callingId = Binder.clearCallingIdentity(); |
| 3354 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3355 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3356 | int pkgUid = -1; |
| 3357 | synchronized(this) { |
| 3358 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3359 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3360 | } catch (RemoteException e) { |
| 3361 | } |
| 3362 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3363 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3364 | return; |
| 3365 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3366 | killPackageProcessesLocked(packageName, pkgUid, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3367 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3368 | } |
| 3369 | } finally { |
| 3370 | Binder.restoreCallingIdentity(callingId); |
| 3371 | } |
| 3372 | } |
| 3373 | |
| 3374 | public void killAllBackgroundProcesses() { |
| 3375 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3376 | != PackageManager.PERMISSION_GRANTED) { |
| 3377 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3378 | + Binder.getCallingPid() |
| 3379 | + ", uid=" + Binder.getCallingUid() |
| 3380 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3381 | Slog.w(TAG, msg); |
| 3382 | throw new SecurityException(msg); |
| 3383 | } |
| 3384 | |
| 3385 | long callingId = Binder.clearCallingIdentity(); |
| 3386 | try { |
| 3387 | synchronized(this) { |
| 3388 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3389 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3390 | final int NA = apps.size(); |
| 3391 | for (int ia=0; ia<NA; ia++) { |
| 3392 | ProcessRecord app = apps.valueAt(ia); |
| 3393 | if (app.persistent) { |
| 3394 | // we don't kill persistent processes |
| 3395 | continue; |
| 3396 | } |
| 3397 | if (app.removed) { |
| 3398 | procs.add(app); |
| 3399 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3400 | app.removed = true; |
| 3401 | procs.add(app); |
| 3402 | } |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | int N = procs.size(); |
| 3407 | for (int i=0; i<N; i++) { |
| 3408 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3409 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3410 | } |
| 3411 | } finally { |
| 3412 | Binder.restoreCallingIdentity(callingId); |
| 3413 | } |
| 3414 | } |
| 3415 | |
| 3416 | public void forceStopPackage(final String packageName) { |
| 3417 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3418 | != PackageManager.PERMISSION_GRANTED) { |
| 3419 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3420 | + Binder.getCallingPid() |
| 3421 | + ", uid=" + Binder.getCallingUid() |
| 3422 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3423 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3424 | throw new SecurityException(msg); |
| 3425 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3426 | final int userId = UserId.getCallingUserId(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3427 | long callingId = Binder.clearCallingIdentity(); |
| 3428 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3429 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3430 | int pkgUid = -1; |
| 3431 | synchronized(this) { |
| 3432 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3433 | pkgUid = pm.getPackageUid(packageName, userId); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3434 | } catch (RemoteException e) { |
| 3435 | } |
| 3436 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3437 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3438 | return; |
| 3439 | } |
| 3440 | forceStopPackageLocked(packageName, pkgUid); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3441 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3442 | pm.setPackageStoppedState(packageName, true, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3443 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 3444 | } catch (IllegalArgumentException e) { |
| 3445 | Slog.w(TAG, "Failed trying to unstop package " |
| 3446 | + packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3447 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3448 | } |
| 3449 | } finally { |
| 3450 | Binder.restoreCallingIdentity(callingId); |
| 3451 | } |
| 3452 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3453 | |
| 3454 | /* |
| 3455 | * The pkg name and uid have to be specified. |
| 3456 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3457 | */ |
| 3458 | public void killApplicationWithUid(String pkg, int uid) { |
| 3459 | if (pkg == null) { |
| 3460 | return; |
| 3461 | } |
| 3462 | // Make sure the uid is valid. |
| 3463 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3464 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3465 | return; |
| 3466 | } |
| 3467 | int callerUid = Binder.getCallingUid(); |
| 3468 | // Only the system server can kill an application |
| 3469 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3470 | // Post an aysnc message to kill the application |
| 3471 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3472 | msg.arg1 = uid; |
| 3473 | msg.arg2 = 0; |
| 3474 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3475 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3476 | } else { |
| 3477 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3478 | pkg); |
| 3479 | } |
| 3480 | } |
| 3481 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3482 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3483 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3484 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3485 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3486 | if (reason != null) { |
| 3487 | intent.putExtra("reason", reason); |
| 3488 | } |
| 3489 | |
| 3490 | final int uid = Binder.getCallingUid(); |
| 3491 | final long origId = Binder.clearCallingIdentity(); |
| 3492 | synchronized (this) { |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3493 | mWindowManager.closeSystemDialogs(reason); |
| 3494 | |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 3495 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3496 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3497 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3498 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3499 | Activity.RESULT_CANCELED, null, "close-sys"); |
| 3500 | } |
| 3501 | } |
| 3502 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3503 | broadcastIntentLocked(null, null, intent, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3504 | null, 0, null, null, null, false, false, -1, uid, 0 /* TODO: Verify */); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3505 | } |
| 3506 | Binder.restoreCallingIdentity(origId); |
| 3507 | } |
| 3508 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3509 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3510 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3511 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3512 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3513 | for (int i=pids.length-1; i>=0; i--) { |
| 3514 | infos[i] = new Debug.MemoryInfo(); |
| 3515 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3516 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3517 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3518 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3519 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3520 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3521 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3522 | long[] pss = new long[pids.length]; |
| 3523 | for (int i=pids.length-1; i>=0; i--) { |
| 3524 | pss[i] = Debug.getPss(pids[i]); |
| 3525 | } |
| 3526 | return pss; |
| 3527 | } |
| 3528 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3529 | public void killApplicationProcess(String processName, int uid) { |
| 3530 | if (processName == null) { |
| 3531 | return; |
| 3532 | } |
| 3533 | |
| 3534 | int callerUid = Binder.getCallingUid(); |
| 3535 | // Only the system server can kill an application |
| 3536 | if (callerUid == Process.SYSTEM_UID) { |
| 3537 | synchronized (this) { |
| 3538 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3539 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3540 | try { |
| 3541 | app.thread.scheduleSuicide(); |
| 3542 | } catch (RemoteException e) { |
| 3543 | // If the other end already died, then our work here is done. |
| 3544 | } |
| 3545 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3546 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3547 | + processName + " / " + uid); |
| 3548 | } |
| 3549 | } |
| 3550 | } else { |
| 3551 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3552 | processName); |
| 3553 | } |
| 3554 | } |
| 3555 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3556 | private void forceStopPackageLocked(final String packageName, int uid) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3557 | 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] | 3558 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3559 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3560 | if (!mProcessesReady) { |
| 3561 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3562 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3563 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3564 | broadcastIntentLocked(null, null, intent, |
| 3565 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3566 | false, false, |
| 3567 | MY_PID, Process.SYSTEM_UID, UserId.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3568 | } |
| 3569 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3570 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3571 | int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3572 | boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3573 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3574 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3575 | // Remove all processes this package may have touched: all with the |
| 3576 | // same UID (except for the system or root user), and all whose name |
| 3577 | // matches the package name. |
| 3578 | final String procNamePrefix = packageName + ":"; |
| 3579 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3580 | final int NA = apps.size(); |
| 3581 | for (int ia=0; ia<NA; ia++) { |
| 3582 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3583 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3584 | // we don't kill persistent processes |
| 3585 | continue; |
| 3586 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3587 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3588 | if (doit) { |
| 3589 | procs.add(app); |
| 3590 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3591 | // If uid is specified and the uid and process name match |
| 3592 | // Or, the uid is not specified and the process name matches |
| 3593 | } else if (((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
Amith Yamasani | 34db3d6 | 2012-04-02 16:35:19 -0700 | [diff] [blame] | 3594 | || ((app.processName.equals(packageName) |
| 3595 | || app.processName.startsWith(procNamePrefix)) |
| 3596 | && uid < 0))) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3597 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3598 | if (!doit) { |
| 3599 | return true; |
| 3600 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3601 | app.removed = true; |
| 3602 | procs.add(app); |
| 3603 | } |
| 3604 | } |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | int N = procs.size(); |
| 3609 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3610 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3611 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3612 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3613 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3614 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3615 | private final boolean forceStopPackageLocked(String name, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3616 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3617 | boolean evenPersistent, int userId) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3618 | int i; |
| 3619 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3621 | if (uid < 0) { |
| 3622 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3623 | uid = AppGlobals.getPackageManager().getPackageUid(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3624 | } catch (RemoteException e) { |
| 3625 | } |
| 3626 | } |
| 3627 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3628 | if (doit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3629 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3630 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3631 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3632 | while (badApps.hasNext()) { |
| 3633 | SparseArray<Long> ba = badApps.next(); |
| 3634 | if (ba.get(uid) != null) { |
| 3635 | badApps.remove(); |
| 3636 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3637 | } |
| 3638 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3639 | |
| 3640 | boolean didSomething = killPackageProcessesLocked(name, uid, -100, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3641 | callerWillRestart, false, doit, evenPersistent, "force stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3642 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3643 | TaskRecord lastTask = null; |
| 3644 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3645 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3646 | final boolean samePackage = r.packageName.equals(name); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3647 | if (r.userId == userId |
| 3648 | && (samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3649 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3650 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3651 | if (r.finishing) { |
| 3652 | // If this activity is just finishing, then it is not |
| 3653 | // interesting as far as something to stop. |
| 3654 | continue; |
| 3655 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3656 | return true; |
| 3657 | } |
| 3658 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3659 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3660 | if (samePackage) { |
| 3661 | if (r.app != null) { |
| 3662 | r.app.removed = true; |
| 3663 | } |
| 3664 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3665 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3666 | lastTask = r.task; |
| 3667 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3668 | null, "force-stop", true)) { |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3669 | i--; |
| 3670 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3675 | for (ServiceRecord service : mServiceMap.getAllServices(userId)) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3676 | if (service.packageName.equals(name) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3677 | && (service.app == null || evenPersistent || !service.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3678 | if (!doit) { |
| 3679 | return true; |
| 3680 | } |
| 3681 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3682 | Slog.i(TAG, " Force stopping service " + service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3683 | if (service.app != null) { |
| 3684 | service.app.removed = true; |
| 3685 | } |
| 3686 | service.app = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3687 | service.isolatedProc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3688 | services.add(service); |
| 3689 | } |
| 3690 | } |
| 3691 | |
| 3692 | N = services.size(); |
| 3693 | for (i=0; i<N; i++) { |
| 3694 | bringDownServiceLocked(services.get(i), true); |
| 3695 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3696 | |
| 3697 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3698 | for (ContentProviderRecord provider : mProviderMap.getProvidersByClass(userId).values()) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3699 | if (provider.info.packageName.equals(name) |
| 3700 | && (provider.proc == null || evenPersistent || !provider.proc.persistent)) { |
| 3701 | if (!doit) { |
| 3702 | return true; |
| 3703 | } |
| 3704 | didSomething = true; |
| 3705 | providers.add(provider); |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | N = providers.size(); |
| 3710 | for (i=0; i<N; i++) { |
| 3711 | removeDyingProviderLocked(null, providers.get(i)); |
| 3712 | } |
| 3713 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3714 | if (doit) { |
| 3715 | if (purgeCache) { |
| 3716 | AttributeCache ac = AttributeCache.instance(); |
| 3717 | if (ac != null) { |
| 3718 | ac.removePackage(name); |
| 3719 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3720 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 3721 | if (mBooted) { |
| 3722 | mMainStack.resumeTopActivityLocked(null); |
| 3723 | mMainStack.scheduleIdleLocked(); |
| 3724 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3725 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3726 | |
| 3727 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3728 | } |
| 3729 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3730 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3731 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3732 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3733 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3734 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3735 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3736 | + "/" + uid + ")"); |
| 3737 | |
| 3738 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3739 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3740 | if (mHeavyWeightProcess == app) { |
| 3741 | mHeavyWeightProcess = null; |
| 3742 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3743 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3744 | boolean needRestart = false; |
| 3745 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3746 | int pid = app.pid; |
| 3747 | synchronized (mPidsSelfLocked) { |
| 3748 | mPidsSelfLocked.remove(pid); |
| 3749 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3750 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3751 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3752 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3753 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3754 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3755 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3756 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3757 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3758 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3759 | } else { |
| 3760 | needRestart = true; |
| 3761 | } |
| 3762 | } |
| 3763 | } else { |
| 3764 | mRemovedProcesses.add(app); |
| 3765 | } |
| 3766 | |
| 3767 | return needRestart; |
| 3768 | } |
| 3769 | |
| 3770 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3771 | final int pid = app.pid; |
| 3772 | boolean gone = false; |
| 3773 | synchronized (mPidsSelfLocked) { |
| 3774 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3775 | if (knownApp != null && knownApp.thread == null) { |
| 3776 | mPidsSelfLocked.remove(pid); |
| 3777 | gone = true; |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3782 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3783 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3784 | app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3785 | mProcessNames.remove(app.processName, app.uid); |
| 3786 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3787 | if (mHeavyWeightProcess == app) { |
| 3788 | mHeavyWeightProcess = null; |
| 3789 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3790 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3791 | // Take care of any launching providers waiting for this process. |
| 3792 | checkAppInLaunchingProvidersLocked(app, true); |
| 3793 | // Take care of any services that are waiting for the process. |
| 3794 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3795 | ServiceRecord sr = mPendingServices.get(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3796 | if ((app.uid == sr.appInfo.uid |
| 3797 | && app.processName.equals(sr.processName)) |
| 3798 | || sr.isolatedProc == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3799 | Slog.w(TAG, "Forcing bringing down service: " + sr); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3800 | sr.isolatedProc = null; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3801 | mPendingServices.remove(i); |
| 3802 | i--; |
| 3803 | bringDownServiceLocked(sr, true); |
| 3804 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3805 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3806 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 3807 | app.processName, app.setAdj, "start timeout"); |
| 3808 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3809 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3810 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3811 | try { |
| 3812 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3813 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3814 | bm.agentDisconnected(app.info.packageName); |
| 3815 | } catch (RemoteException e) { |
| 3816 | // Can't happen; the backup manager is local |
| 3817 | } |
| 3818 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3819 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3820 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3821 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3822 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3823 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3824 | 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] | 3825 | } |
| 3826 | } |
| 3827 | |
| 3828 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3829 | int pid) { |
| 3830 | |
| 3831 | // Find the application record that is being attached... either via |
| 3832 | // the pid if we are running in multiple processes, or just pull the |
| 3833 | // next app record if we are emulating process with anonymous threads. |
| 3834 | ProcessRecord app; |
| 3835 | if (pid != MY_PID && pid >= 0) { |
| 3836 | synchronized (mPidsSelfLocked) { |
| 3837 | app = mPidsSelfLocked.get(pid); |
| 3838 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3839 | } else { |
| 3840 | app = null; |
| 3841 | } |
| 3842 | |
| 3843 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3844 | 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] | 3845 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3846 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3847 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3848 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3849 | } else { |
| 3850 | try { |
| 3851 | thread.scheduleExit(); |
| 3852 | } catch (Exception e) { |
| 3853 | // Ignore exceptions. |
| 3854 | } |
| 3855 | } |
| 3856 | return false; |
| 3857 | } |
| 3858 | |
| 3859 | // If this application record is still attached to a previous |
| 3860 | // process, clean it up now. |
| 3861 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3862 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | // Tell the process all about itself. |
| 3866 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3867 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3868 | TAG, "Binding process pid " + pid + " to record " + app); |
| 3869 | |
| 3870 | String processName = app.processName; |
| 3871 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3872 | AppDeathRecipient adr = new AppDeathRecipient( |
| 3873 | app, pid, thread); |
| 3874 | thread.asBinder().linkToDeath(adr, 0); |
| 3875 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3876 | } catch (RemoteException e) { |
| 3877 | app.resetPackageList(); |
| 3878 | startProcessLocked(app, "link fail", processName); |
| 3879 | return false; |
| 3880 | } |
| 3881 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3882 | 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] | 3883 | |
| 3884 | app.thread = thread; |
| 3885 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 3886 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 3887 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3888 | app.forcingToForeground = null; |
| 3889 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 3890 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3891 | app.debugging = false; |
| 3892 | |
| 3893 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3894 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3895 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3896 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3897 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3898 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3899 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3900 | } |
| 3901 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3902 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3903 | TAG, "New app record " + app |
| 3904 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 3905 | try { |
| 3906 | int testMode = IApplicationThread.DEBUG_OFF; |
| 3907 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 3908 | testMode = mWaitForDebugger |
| 3909 | ? IApplicationThread.DEBUG_WAIT |
| 3910 | : IApplicationThread.DEBUG_ON; |
| 3911 | app.debugging = true; |
| 3912 | if (mDebugTransient) { |
| 3913 | mDebugApp = mOrigDebugApp; |
| 3914 | mWaitForDebugger = mOrigWaitForDebugger; |
| 3915 | } |
| 3916 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3917 | String profileFile = app.instrumentationProfileFile; |
| 3918 | ParcelFileDescriptor profileFd = null; |
| 3919 | boolean profileAutoStop = false; |
| 3920 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 3921 | mProfileProc = app; |
| 3922 | profileFile = mProfileFile; |
| 3923 | profileFd = mProfileFd; |
| 3924 | profileAutoStop = mAutoStopProfiler; |
| 3925 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 3926 | boolean enableOpenGlTrace = false; |
| 3927 | if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) { |
| 3928 | enableOpenGlTrace = true; |
| 3929 | mOpenGlTraceApp = null; |
| 3930 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3931 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3932 | // If the app is being launched for restore or full backup, set it up specially |
| 3933 | boolean isRestrictedBackupMode = false; |
| 3934 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 3935 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3936 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3937 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 3938 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3939 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 3940 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 3941 | ? app.instrumentationInfo.packageName |
| 3942 | : app.info.packageName); |
| 3943 | if (app.instrumentationClass != null) { |
| 3944 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3945 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3946 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 3947 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3948 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 3949 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 3950 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3951 | if (profileFd != null) { |
| 3952 | profileFd = profileFd.dup(); |
| 3953 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3954 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3955 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 3956 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
| 3957 | enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 3958 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 3959 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3960 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3961 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3962 | } catch (Exception e) { |
| 3963 | // todo: Yikes! What should we do? For now we will try to |
| 3964 | // start another process, but that could easily get us in |
| 3965 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3966 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3967 | |
| 3968 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3969 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3970 | startProcessLocked(app, "bind fail", processName); |
| 3971 | return false; |
| 3972 | } |
| 3973 | |
| 3974 | // Remove this record from the list of starting applications. |
| 3975 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3976 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 3977 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3978 | mProcessesOnHold.remove(app); |
| 3979 | |
| 3980 | boolean badApp = false; |
| 3981 | boolean didSomething = false; |
| 3982 | |
| 3983 | // 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] | 3984 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 3985 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3986 | 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] | 3987 | && processName.equals(hr.processName)) { |
| 3988 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 3989 | if (mHeadless) { |
| 3990 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 3991 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3992 | didSomething = true; |
| 3993 | } |
| 3994 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3995 | 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] | 3996 | + hr.intent.getComponent().flattenToShortString(), e); |
| 3997 | badApp = true; |
| 3998 | } |
| 3999 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4000 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4001 | } |
| 4002 | } |
| 4003 | |
| 4004 | // Find any services that should be running in this process... |
| 4005 | if (!badApp && mPendingServices.size() > 0) { |
| 4006 | ServiceRecord sr = null; |
| 4007 | try { |
| 4008 | for (int i=0; i<mPendingServices.size(); i++) { |
| 4009 | sr = mPendingServices.get(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4010 | if (app != sr.isolatedProc && (app.uid != sr.appInfo.uid |
| 4011 | || !processName.equals(sr.processName))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4012 | continue; |
| 4013 | } |
| 4014 | |
| 4015 | mPendingServices.remove(i); |
| 4016 | i--; |
| 4017 | realStartServiceLocked(sr, app); |
| 4018 | didSomething = true; |
| 4019 | } |
| 4020 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4021 | Slog.w(TAG, "Exception in new application when starting service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4022 | + sr.shortName, e); |
| 4023 | badApp = true; |
| 4024 | } |
| 4025 | } |
| 4026 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4027 | // Check if a next-broadcast receiver is in this process... |
| 4028 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4029 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4030 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4031 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4032 | // 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] | 4033 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4037 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4038 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4039 | 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] | 4040 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4041 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4042 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 4043 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 4044 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4045 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4046 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4047 | e.printStackTrace(); |
| 4048 | } |
| 4049 | } |
| 4050 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4051 | if (badApp) { |
| 4052 | // todo: Also need to kill application to deal with all |
| 4053 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4054 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4055 | return false; |
| 4056 | } |
| 4057 | |
| 4058 | if (!didSomething) { |
| 4059 | updateOomAdjLocked(); |
| 4060 | } |
| 4061 | |
| 4062 | return true; |
| 4063 | } |
| 4064 | |
| 4065 | public final void attachApplication(IApplicationThread thread) { |
| 4066 | synchronized (this) { |
| 4067 | int callingPid = Binder.getCallingPid(); |
| 4068 | final long origId = Binder.clearCallingIdentity(); |
| 4069 | attachApplicationLocked(thread, callingPid); |
| 4070 | Binder.restoreCallingIdentity(origId); |
| 4071 | } |
| 4072 | } |
| 4073 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4074 | 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] | 4075 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4076 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4077 | if (stopProfiling) { |
| 4078 | synchronized (this) { |
| 4079 | if (mProfileProc == r.app) { |
| 4080 | if (mProfileFd != null) { |
| 4081 | try { |
| 4082 | mProfileFd.close(); |
| 4083 | } catch (IOException e) { |
| 4084 | } |
| 4085 | clearProfilerLocked(); |
| 4086 | } |
| 4087 | } |
| 4088 | } |
| 4089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4090 | Binder.restoreCallingIdentity(origId); |
| 4091 | } |
| 4092 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4093 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4094 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4095 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4096 | mWindowManager.enableScreenAfterBoot(); |
| 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"); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4106 | synchronized (this) { |
| 4107 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
| 4108 | } |
| 4109 | } |
| 4110 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4111 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4112 | IntentFilter pkgFilter = new IntentFilter(); |
| 4113 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4114 | pkgFilter.addDataScheme("package"); |
| 4115 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4116 | @Override |
| 4117 | public void onReceive(Context context, Intent intent) { |
| 4118 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4119 | if (pkgs != null) { |
| 4120 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4121 | synchronized (ActivityManagerService.this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4122 | if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) { |
| 4123 | setResultCode(Activity.RESULT_OK); |
| 4124 | return; |
| 4125 | } |
| 4126 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4127 | } |
| 4128 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4129 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4130 | }, pkgFilter); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4131 | |
| 4132 | IntentFilter userFilter = new IntentFilter(); |
| 4133 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 4134 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4135 | @Override |
| 4136 | public void onReceive(Context context, Intent intent) { |
| 4137 | onUserRemoved(intent); |
| 4138 | } |
| 4139 | }, userFilter); |
| 4140 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4141 | synchronized (this) { |
| 4142 | // Ensure that any processes we had put on hold are now started |
| 4143 | // up. |
| 4144 | final int NP = mProcessesOnHold.size(); |
| 4145 | if (NP > 0) { |
| 4146 | ArrayList<ProcessRecord> procs = |
| 4147 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4148 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4149 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4150 | + procs.get(ip)); |
| 4151 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4156 | // Start looking for apps that are abusing wake locks. |
| 4157 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4158 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4159 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4160 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4161 | SystemProperties.set("dev.bootcomplete", "1"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4162 | /* 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] | 4163 | broadcastIntentLocked(null, null, |
| 4164 | new Intent(Intent.ACTION_BOOT_COMPLETED, null), |
| 4165 | null, null, 0, null, null, |
| 4166 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4167 | false, false, MY_PID, Process.SYSTEM_UID, Binder.getOrigCallingUser()); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4168 | } |
| 4169 | } |
| 4170 | } |
| 4171 | |
| 4172 | final void ensureBootCompleted() { |
| 4173 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4174 | boolean enableScreen; |
| 4175 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4176 | booting = mBooting; |
| 4177 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4178 | enableScreen = !mBooted; |
| 4179 | mBooted = true; |
| 4180 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4181 | |
| 4182 | if (booting) { |
| 4183 | finishBooting(); |
| 4184 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4185 | |
| 4186 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4187 | enableScreenAfterBoot(); |
| 4188 | } |
| 4189 | } |
| 4190 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4191 | public final void activityPaused(IBinder token) { |
| 4192 | final long origId = Binder.clearCallingIdentity(); |
| 4193 | mMainStack.activityPaused(token, false); |
| 4194 | Binder.restoreCallingIdentity(origId); |
| 4195 | } |
| 4196 | |
| 4197 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4198 | CharSequence description) { |
| 4199 | if (localLOGV) Slog.v( |
| 4200 | TAG, "Activity stopped: token=" + token); |
| 4201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4202 | // Refuse possible leaked file descriptors |
| 4203 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4204 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4205 | } |
| 4206 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4207 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4208 | |
| 4209 | final long origId = Binder.clearCallingIdentity(); |
| 4210 | |
| 4211 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4212 | r = mMainStack.isInStackLocked(token); |
| 4213 | if (r != null) { |
| 4214 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | if (r != null) { |
| 4219 | sendPendingThumbnail(r, null, null, null, false); |
| 4220 | } |
| 4221 | |
| 4222 | trimApplications(); |
| 4223 | |
| 4224 | Binder.restoreCallingIdentity(origId); |
| 4225 | } |
| 4226 | |
| 4227 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4228 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4229 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | public String getCallingPackage(IBinder token) { |
| 4233 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4234 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4235 | 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] | 4236 | } |
| 4237 | } |
| 4238 | |
| 4239 | public ComponentName getCallingActivity(IBinder token) { |
| 4240 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4241 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4242 | return r != null ? r.intent.getComponent() : null; |
| 4243 | } |
| 4244 | } |
| 4245 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4246 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4247 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4248 | if (r == null) { |
| 4249 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4250 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4251 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4255 | synchronized(this) { |
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.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4261 | } |
| 4262 | } |
| 4263 | |
| 4264 | public String getPackageForToken(IBinder token) { |
| 4265 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4266 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4267 | if (r == null) { |
| 4268 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4269 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4270 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4271 | } |
| 4272 | } |
| 4273 | |
| 4274 | public IIntentSender getIntentSender(int type, |
| 4275 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4276 | int requestCode, Intent[] intents, String[] resolvedTypes, |
| 4277 | int flags, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4278 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4279 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4280 | if (intents != null) { |
| 4281 | if (intents.length < 1) { |
| 4282 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4283 | } |
| 4284 | for (int i=0; i<intents.length; i++) { |
| 4285 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4286 | if (intent != null) { |
| 4287 | if (intent.hasFileDescriptors()) { |
| 4288 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4289 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4290 | if (type == ActivityManager.INTENT_SENDER_BROADCAST && |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4291 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4292 | throw new IllegalArgumentException( |
| 4293 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4294 | } |
| 4295 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4296 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4297 | } |
| 4298 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4299 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4300 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4301 | } |
| 4302 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4303 | if (options != null) { |
| 4304 | if (options.hasFileDescriptors()) { |
| 4305 | throw new IllegalArgumentException("File descriptors passed in options"); |
| 4306 | } |
| 4307 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4309 | synchronized(this) { |
| 4310 | int callingUid = Binder.getCallingUid(); |
| 4311 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4312 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4313 | int uid = AppGlobals.getPackageManager() |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4314 | .getPackageUid(packageName, UserId.getUserId(callingUid)); |
| 4315 | if (!UserId.isSameApp(callingUid, uid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4316 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4317 | + Binder.getCallingPid() |
| 4318 | + ", uid=" + Binder.getCallingUid() |
| 4319 | + ", (need uid=" + uid + ")" |
| 4320 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4321 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4322 | throw new SecurityException(msg); |
| 4323 | } |
| 4324 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4325 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4326 | if (DEBUG_MU) |
| 4327 | Slog.i(TAG_MU, "Getting intent sender for origCallingUid=" |
| 4328 | + Binder.getOrigCallingUid()); |
| 4329 | return getIntentSenderLocked(type, packageName, Binder.getOrigCallingUid(), |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4330 | token, resultWho, requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4332 | } catch (RemoteException e) { |
| 4333 | throw new SecurityException(e); |
| 4334 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4335 | } |
| 4336 | } |
| 4337 | |
| 4338 | IIntentSender getIntentSenderLocked(int type, |
| 4339 | String packageName, int callingUid, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4340 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags, |
| 4341 | Bundle options) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4342 | if (DEBUG_MU) |
| 4343 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4344 | ActivityRecord activity = null; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4345 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4346 | activity = mMainStack.isInStackLocked(token); |
| 4347 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4348 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4349 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4350 | if (activity.finishing) { |
| 4351 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4352 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4356 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4357 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4358 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4359 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4360 | |
| 4361 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4362 | type, packageName, activity, resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4363 | requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4364 | WeakReference<PendingIntentRecord> ref; |
| 4365 | ref = mIntentSenderRecords.get(key); |
| 4366 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4367 | if (rec != null) { |
| 4368 | if (!cancelCurrent) { |
| 4369 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4370 | if (rec.key.requestIntent != null) { |
| 4371 | rec.key.requestIntent.replaceExtras(intents != null ? intents[0] : null); |
| 4372 | } |
| 4373 | if (intents != null) { |
| 4374 | intents[intents.length-1] = rec.key.requestIntent; |
| 4375 | rec.key.allIntents = intents; |
| 4376 | rec.key.allResolvedTypes = resolvedTypes; |
| 4377 | } else { |
| 4378 | rec.key.allIntents = null; |
| 4379 | rec.key.allResolvedTypes = null; |
| 4380 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4381 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4382 | return rec; |
| 4383 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4384 | rec.canceled = true; |
| 4385 | mIntentSenderRecords.remove(key); |
| 4386 | } |
| 4387 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4388 | return rec; |
| 4389 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4390 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4391 | mIntentSenderRecords.put(key, rec.ref); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4392 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4393 | if (activity.pendingResults == null) { |
| 4394 | activity.pendingResults |
| 4395 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4396 | } |
| 4397 | activity.pendingResults.add(rec.ref); |
| 4398 | } |
| 4399 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | public void cancelIntentSender(IIntentSender sender) { |
| 4403 | if (!(sender instanceof PendingIntentRecord)) { |
| 4404 | return; |
| 4405 | } |
| 4406 | synchronized(this) { |
| 4407 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4408 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4409 | int uid = AppGlobals.getPackageManager() |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4410 | .getPackageUid(rec.key.packageName, UserId.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4411 | if (!UserId.isSameApp(uid, Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4412 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4413 | + Binder.getCallingPid() |
| 4414 | + ", uid=" + Binder.getCallingUid() |
| 4415 | + " is not allowed to cancel packges " |
| 4416 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4417 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4418 | throw new SecurityException(msg); |
| 4419 | } |
| 4420 | } catch (RemoteException e) { |
| 4421 | throw new SecurityException(e); |
| 4422 | } |
| 4423 | cancelIntentSenderLocked(rec, true); |
| 4424 | } |
| 4425 | } |
| 4426 | |
| 4427 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4428 | rec.canceled = true; |
| 4429 | mIntentSenderRecords.remove(rec.key); |
| 4430 | if (cleanActivity && rec.key.activity != null) { |
| 4431 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4432 | } |
| 4433 | } |
| 4434 | |
| 4435 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4436 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4437 | return null; |
| 4438 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4439 | try { |
| 4440 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4441 | return res.key.packageName; |
| 4442 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4443 | } |
| 4444 | return null; |
| 4445 | } |
| 4446 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame^] | 4447 | public int getUidForIntentSender(IIntentSender sender) { |
| 4448 | if (sender instanceof PendingIntentRecord) { |
| 4449 | try { |
| 4450 | PendingIntentRecord res = (PendingIntentRecord)sender; |
| 4451 | return res.uid; |
| 4452 | } catch (ClassCastException e) { |
| 4453 | } |
| 4454 | } |
| 4455 | return -1; |
| 4456 | } |
| 4457 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4458 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4459 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4460 | return false; |
| 4461 | } |
| 4462 | try { |
| 4463 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4464 | if (res.key.allIntents == null) { |
| 4465 | return false; |
| 4466 | } |
| 4467 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4468 | Intent intent = res.key.allIntents[i]; |
| 4469 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4470 | return false; |
| 4471 | } |
| 4472 | } |
| 4473 | return true; |
| 4474 | } catch (ClassCastException e) { |
| 4475 | } |
| 4476 | return false; |
| 4477 | } |
| 4478 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4479 | public void setProcessLimit(int max) { |
| 4480 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4481 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4482 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4483 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4484 | mProcessLimitOverride = max; |
| 4485 | } |
| 4486 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4490 | synchronized (this) { |
| 4491 | return mProcessLimitOverride; |
| 4492 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4493 | } |
| 4494 | |
| 4495 | void foregroundTokenDied(ForegroundToken token) { |
| 4496 | synchronized (ActivityManagerService.this) { |
| 4497 | synchronized (mPidsSelfLocked) { |
| 4498 | ForegroundToken cur |
| 4499 | = mForegroundProcesses.get(token.pid); |
| 4500 | if (cur != token) { |
| 4501 | return; |
| 4502 | } |
| 4503 | mForegroundProcesses.remove(token.pid); |
| 4504 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4505 | if (pr == null) { |
| 4506 | return; |
| 4507 | } |
| 4508 | pr.forcingToForeground = null; |
| 4509 | pr.foregroundServices = false; |
| 4510 | } |
| 4511 | updateOomAdjLocked(); |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4516 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4517 | "setProcessForeground()"); |
| 4518 | synchronized(this) { |
| 4519 | boolean changed = false; |
| 4520 | |
| 4521 | synchronized (mPidsSelfLocked) { |
| 4522 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4523 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4524 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4525 | return; |
| 4526 | } |
| 4527 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4528 | if (oldToken != null) { |
| 4529 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4530 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4531 | if (pr != null) { |
| 4532 | pr.forcingToForeground = null; |
| 4533 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4534 | changed = true; |
| 4535 | } |
| 4536 | if (isForeground && token != null) { |
| 4537 | ForegroundToken newToken = new ForegroundToken() { |
| 4538 | public void binderDied() { |
| 4539 | foregroundTokenDied(this); |
| 4540 | } |
| 4541 | }; |
| 4542 | newToken.pid = pid; |
| 4543 | newToken.token = token; |
| 4544 | try { |
| 4545 | token.linkToDeath(newToken, 0); |
| 4546 | mForegroundProcesses.put(pid, newToken); |
| 4547 | pr.forcingToForeground = token; |
| 4548 | changed = true; |
| 4549 | } catch (RemoteException e) { |
| 4550 | // If the process died while doing this, we will later |
| 4551 | // do the cleanup with the process death link. |
| 4552 | } |
| 4553 | } |
| 4554 | } |
| 4555 | |
| 4556 | if (changed) { |
| 4557 | updateOomAdjLocked(); |
| 4558 | } |
| 4559 | } |
| 4560 | } |
| 4561 | |
| 4562 | // ========================================================= |
| 4563 | // PERMISSIONS |
| 4564 | // ========================================================= |
| 4565 | |
| 4566 | static class PermissionController extends IPermissionController.Stub { |
| 4567 | ActivityManagerService mActivityManagerService; |
| 4568 | PermissionController(ActivityManagerService activityManagerService) { |
| 4569 | mActivityManagerService = activityManagerService; |
| 4570 | } |
| 4571 | |
| 4572 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4573 | return mActivityManagerService.checkPermission(permission, pid, |
| 4574 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | /** |
| 4579 | * This can be called with or without the global lock held. |
| 4580 | */ |
| 4581 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4582 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4583 | // We might be performing an operation on behalf of an indirect binder |
| 4584 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4585 | // client identity accordingly before proceeding. |
| 4586 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4587 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4588 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4589 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4590 | uid = tlsIdentity.uid; |
| 4591 | pid = tlsIdentity.pid; |
| 4592 | } |
| 4593 | |
| 4594 | // Root, system server and our own process get to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4595 | if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4596 | return PackageManager.PERMISSION_GRANTED; |
| 4597 | } |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4598 | // Isolated processes don't get any permissions. |
| 4599 | if (UserId.isIsolated(uid)) { |
| 4600 | return PackageManager.PERMISSION_DENIED; |
| 4601 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4602 | // If there is a uid that owns whatever is being accessed, it has |
| 4603 | // blanket access to it regardless of the permissions it requires. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4604 | if (owningUid >= 0 && UserId.isSameApp(uid, owningUid)) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4605 | return PackageManager.PERMISSION_GRANTED; |
| 4606 | } |
| 4607 | // If the target is not exported, then nobody else can get to it. |
| 4608 | if (!exported) { |
| 4609 | Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4610 | return PackageManager.PERMISSION_DENIED; |
| 4611 | } |
| 4612 | if (permission == null) { |
| 4613 | return PackageManager.PERMISSION_GRANTED; |
| 4614 | } |
| 4615 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4616 | return AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4617 | .checkUidPermission(permission, uid); |
| 4618 | } catch (RemoteException e) { |
| 4619 | // Should never happen, but if it does... deny! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4620 | Slog.e(TAG, "PackageManager is dead?!?", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4621 | } |
| 4622 | return PackageManager.PERMISSION_DENIED; |
| 4623 | } |
| 4624 | |
| 4625 | /** |
| 4626 | * As the only public entry point for permissions checking, this method |
| 4627 | * can enforce the semantic that requesting a check on a null global |
| 4628 | * permission is automatically denied. (Internally a null permission |
| 4629 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4630 | * when only uid-based security is needed.) |
| 4631 | * |
| 4632 | * This can be called with or without the global lock held. |
| 4633 | */ |
| 4634 | public int checkPermission(String permission, int pid, int uid) { |
| 4635 | if (permission == null) { |
| 4636 | return PackageManager.PERMISSION_DENIED; |
| 4637 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4638 | return checkComponentPermission(permission, pid, UserId.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | /** |
| 4642 | * Binder IPC calls go through the public entry point. |
| 4643 | * This can be called with or without the global lock held. |
| 4644 | */ |
| 4645 | int checkCallingPermission(String permission) { |
| 4646 | return checkPermission(permission, |
| 4647 | Binder.getCallingPid(), |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4648 | UserId.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4649 | } |
| 4650 | |
| 4651 | /** |
| 4652 | * This can be called with or without the global lock held. |
| 4653 | */ |
| 4654 | void enforceCallingPermission(String permission, String func) { |
| 4655 | if (checkCallingPermission(permission) |
| 4656 | == PackageManager.PERMISSION_GRANTED) { |
| 4657 | return; |
| 4658 | } |
| 4659 | |
| 4660 | String msg = "Permission Denial: " + func + " from pid=" |
| 4661 | + Binder.getCallingPid() |
| 4662 | + ", uid=" + Binder.getCallingUid() |
| 4663 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4664 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4665 | throw new SecurityException(msg); |
| 4666 | } |
| 4667 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4668 | /** |
| 4669 | * Determine if UID is holding permissions required to access {@link Uri} in |
| 4670 | * the given {@link ProviderInfo}. Final permission checking is always done |
| 4671 | * in {@link ContentProvider}. |
| 4672 | */ |
| 4673 | private final boolean checkHoldingPermissionsLocked( |
| 4674 | IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4675 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4676 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4677 | |
| 4678 | if (pi.applicationInfo.uid == uid) { |
| 4679 | return true; |
| 4680 | } else if (!pi.exported) { |
| 4681 | return false; |
| 4682 | } |
| 4683 | |
| 4684 | boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4685 | 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] | 4686 | try { |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4687 | // check if target holds top-level <provider> permissions |
| 4688 | if (!readMet && pi.readPermission != null |
| 4689 | && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) { |
| 4690 | readMet = true; |
| 4691 | } |
| 4692 | if (!writeMet && pi.writePermission != null |
| 4693 | && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) { |
| 4694 | writeMet = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4695 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4696 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4697 | // track if unprotected read/write is allowed; any denied |
| 4698 | // <path-permission> below removes this ability |
| 4699 | boolean allowDefaultRead = pi.readPermission == null; |
| 4700 | boolean allowDefaultWrite = pi.writePermission == null; |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4701 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4702 | // check if target holds any <path-permission> that match uri |
| 4703 | final PathPermission[] pps = pi.pathPermissions; |
| 4704 | if (pps != null) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4705 | final String path = uri.getPath(); |
| 4706 | int i = pps.length; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4707 | while (i > 0 && (!readMet || !writeMet)) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4708 | i--; |
| 4709 | PathPermission pp = pps[i]; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4710 | if (pp.match(path)) { |
| 4711 | if (!readMet) { |
| 4712 | final String pprperm = pp.getReadPermission(); |
| 4713 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4714 | + pprperm + " for " + pp.getPath() |
| 4715 | + ": match=" + pp.match(path) |
| 4716 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4717 | if (pprperm != null) { |
| 4718 | if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) { |
| 4719 | readMet = true; |
| 4720 | } else { |
| 4721 | allowDefaultRead = false; |
| 4722 | } |
| 4723 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4724 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4725 | if (!writeMet) { |
| 4726 | final String ppwperm = pp.getWritePermission(); |
| 4727 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4728 | + ppwperm + " for " + pp.getPath() |
| 4729 | + ": match=" + pp.match(path) |
| 4730 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4731 | if (ppwperm != null) { |
| 4732 | if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) { |
| 4733 | writeMet = true; |
| 4734 | } else { |
| 4735 | allowDefaultWrite = false; |
| 4736 | } |
| 4737 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4738 | } |
| 4739 | } |
| 4740 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4741 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4742 | |
| 4743 | // grant unprotected <provider> read/write, if not blocked by |
| 4744 | // <path-permission> above |
| 4745 | if (allowDefaultRead) readMet = true; |
| 4746 | if (allowDefaultWrite) writeMet = true; |
| 4747 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4748 | } catch (RemoteException e) { |
| 4749 | return false; |
| 4750 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4751 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4752 | return readMet && writeMet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4756 | int modeFlags) { |
| 4757 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4758 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4759 | return true; |
| 4760 | } |
| 4761 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4762 | if (perms == null) return false; |
| 4763 | UriPermission perm = perms.get(uri); |
| 4764 | if (perm == null) return false; |
| 4765 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4766 | } |
| 4767 | |
| 4768 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4769 | enforceNotIsolatedCaller("checkUriPermission"); |
| 4770 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4771 | // Another redirected-binder-call permissions check as in |
| 4772 | // {@link checkComponentPermission}. |
| 4773 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4774 | if (tlsIdentity != null) { |
| 4775 | uid = tlsIdentity.uid; |
| 4776 | pid = tlsIdentity.pid; |
| 4777 | } |
| 4778 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4779 | uid = UserId.getAppId(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4780 | // Our own process gets to do everything. |
| 4781 | if (pid == MY_PID) { |
| 4782 | return PackageManager.PERMISSION_GRANTED; |
| 4783 | } |
| 4784 | synchronized(this) { |
| 4785 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4786 | ? PackageManager.PERMISSION_GRANTED |
| 4787 | : PackageManager.PERMISSION_DENIED; |
| 4788 | } |
| 4789 | } |
| 4790 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4791 | /** |
| 4792 | * Check if the targetPkg can be granted permission to access uri by |
| 4793 | * the callingUid using the given modeFlags. Throws a security exception |
| 4794 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4795 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4796 | * needed (for example targetPkg already has permission to access the URI). |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4797 | * If you already know the uid of the target, you can supply it in |
| 4798 | * lastTargetUid else set that to -1. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4799 | */ |
| 4800 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4801 | Uri uri, int modeFlags, int lastTargetUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4802 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4803 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4804 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4805 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4806 | } |
| 4807 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4808 | if (targetPkg != null) { |
| 4809 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4810 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4811 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4812 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4813 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4814 | |
| 4815 | // If this is not a content: uri, we can't do anything with it. |
| 4816 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4817 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4818 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4819 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | String name = uri.getAuthority(); |
| 4823 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4824 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
| 4825 | UserId.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4826 | if (cpr != null) { |
| 4827 | pi = cpr.info; |
| 4828 | } else { |
| 4829 | try { |
| 4830 | pi = pm.resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4831 | PackageManager.GET_URI_PERMISSION_PATTERNS, UserId.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4832 | } catch (RemoteException ex) { |
| 4833 | } |
| 4834 | } |
| 4835 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4836 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4837 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4838 | } |
| 4839 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4840 | int targetUid = lastTargetUid; |
| 4841 | if (targetUid < 0 && targetPkg != null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4842 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4843 | targetUid = pm.getPackageUid(targetPkg, UserId.getUserId(callingUid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4844 | if (targetUid < 0) { |
| 4845 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4846 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4847 | return -1; |
| 4848 | } |
| 4849 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4850 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4851 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4852 | } |
| 4853 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4854 | if (targetUid >= 0) { |
| 4855 | // First... does the target actually need this permission? |
| 4856 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4857 | // No need to grant the target this permission. |
| 4858 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4859 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4860 | return -1; |
| 4861 | } |
| 4862 | } else { |
| 4863 | // First... there is no target package, so can anyone access it? |
| 4864 | boolean allowed = pi.exported; |
| 4865 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4866 | if (pi.readPermission != null) { |
| 4867 | allowed = false; |
| 4868 | } |
| 4869 | } |
| 4870 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4871 | if (pi.writePermission != null) { |
| 4872 | allowed = false; |
| 4873 | } |
| 4874 | } |
| 4875 | if (allowed) { |
| 4876 | return -1; |
| 4877 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4878 | } |
| 4879 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4880 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4881 | if (!pi.grantUriPermissions) { |
| 4882 | throw new SecurityException("Provider " + pi.packageName |
| 4883 | + "/" + pi.name |
| 4884 | + " does not allow granting of Uri permissions (uri " |
| 4885 | + uri + ")"); |
| 4886 | } |
| 4887 | if (pi.uriPermissionPatterns != null) { |
| 4888 | final int N = pi.uriPermissionPatterns.length; |
| 4889 | boolean allowed = false; |
| 4890 | for (int i=0; i<N; i++) { |
| 4891 | if (pi.uriPermissionPatterns[i] != null |
| 4892 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 4893 | allowed = true; |
| 4894 | break; |
| 4895 | } |
| 4896 | } |
| 4897 | if (!allowed) { |
| 4898 | throw new SecurityException("Provider " + pi.packageName |
| 4899 | + "/" + pi.name |
| 4900 | + " does not allow granting of permission to path of Uri " |
| 4901 | + uri); |
| 4902 | } |
| 4903 | } |
| 4904 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4905 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4906 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4907 | if (callingUid != Process.myUid()) { |
| 4908 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 4909 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4910 | throw new SecurityException("Uid " + callingUid |
| 4911 | + " does not have permission to uri " + uri); |
| 4912 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4913 | } |
| 4914 | } |
| 4915 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4916 | return targetUid; |
| 4917 | } |
| 4918 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4919 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 4920 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4921 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4922 | synchronized(this) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4923 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4924 | } |
| 4925 | } |
| 4926 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4927 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 4928 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 4929 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4930 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4931 | if (modeFlags == 0) { |
| 4932 | return; |
| 4933 | } |
| 4934 | |
| 4935 | // 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] | 4936 | // to the uri, and the target doesn't. Let's now give this to |
| 4937 | // the target. |
| 4938 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4939 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4940 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4942 | HashMap<Uri, UriPermission> targetUris |
| 4943 | = mGrantedUriPermissions.get(targetUid); |
| 4944 | if (targetUris == null) { |
| 4945 | targetUris = new HashMap<Uri, UriPermission>(); |
| 4946 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 4947 | } |
| 4948 | |
| 4949 | UriPermission perm = targetUris.get(uri); |
| 4950 | if (perm == null) { |
| 4951 | perm = new UriPermission(targetUid, uri); |
| 4952 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4953 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4954 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4955 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4956 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4957 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 4958 | } else { |
| 4959 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4960 | perm.readOwners.add(owner); |
| 4961 | owner.addReadPermission(perm); |
| 4962 | } |
| 4963 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4964 | perm.writeOwners.add(owner); |
| 4965 | owner.addWritePermission(perm); |
| 4966 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4967 | } |
| 4968 | } |
| 4969 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4970 | void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri, |
| 4971 | int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4972 | if (targetPkg == null) { |
| 4973 | throw new NullPointerException("targetPkg"); |
| 4974 | } |
| 4975 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4976 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4977 | if (targetUid < 0) { |
| 4978 | return; |
| 4979 | } |
| 4980 | |
| 4981 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 4982 | } |
| 4983 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4984 | static class NeededUriGrants extends ArrayList<Uri> { |
| 4985 | final String targetPkg; |
| 4986 | final int targetUid; |
| 4987 | final int flags; |
| 4988 | |
| 4989 | NeededUriGrants(String _targetPkg, int _targetUid, int _flags) { |
| 4990 | targetPkg = _targetPkg; |
| 4991 | targetUid = _targetUid; |
| 4992 | flags = _flags; |
| 4993 | } |
| 4994 | } |
| 4995 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4996 | /** |
| 4997 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 4998 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4999 | NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 5000 | String targetPkg, Intent intent, int mode, NeededUriGrants needed) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5001 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5002 | "Checking URI perm to data=" + (intent != null ? intent.getData() : null) |
| 5003 | + " clip=" + (intent != null ? intent.getClipData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5004 | + " from " + intent + "; flags=0x" |
| 5005 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 5006 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5007 | if (targetPkg == null) { |
| 5008 | throw new NullPointerException("targetPkg"); |
| 5009 | } |
| 5010 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5011 | if (intent == null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5012 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5013 | } |
| 5014 | Uri data = intent.getData(); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5015 | ClipData clip = intent.getClipData(); |
| 5016 | if (data == null && clip == null) { |
| 5017 | return null; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5018 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5019 | if (data != null) { |
| 5020 | int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 5021 | mode, needed != null ? needed.targetUid : -1); |
| 5022 | if (target > 0) { |
| 5023 | if (needed == null) { |
| 5024 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5025 | } |
| 5026 | needed.add(data); |
| 5027 | } |
| 5028 | } |
| 5029 | if (clip != null) { |
| 5030 | for (int i=0; i<clip.getItemCount(); i++) { |
| 5031 | Uri uri = clip.getItemAt(i).getUri(); |
| 5032 | if (uri != null) { |
| 5033 | int target = -1; |
| 5034 | target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, |
| 5035 | mode, needed != null ? needed.targetUid : -1); |
| 5036 | if (target > 0) { |
| 5037 | if (needed == null) { |
| 5038 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5039 | } |
| 5040 | needed.add(uri); |
| 5041 | } |
| 5042 | } else { |
| 5043 | Intent clipIntent = clip.getItemAt(i).getIntent(); |
| 5044 | if (clipIntent != null) { |
| 5045 | NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked( |
| 5046 | callingUid, targetPkg, clipIntent, mode, needed); |
| 5047 | if (newNeeded != null) { |
| 5048 | needed = newNeeded; |
| 5049 | } |
| 5050 | } |
| 5051 | } |
| 5052 | } |
| 5053 | } |
| 5054 | |
| 5055 | return needed; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | /** |
| 5059 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 5060 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5061 | void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed, |
| 5062 | UriPermissionOwner owner) { |
| 5063 | if (needed != null) { |
| 5064 | for (int i=0; i<needed.size(); i++) { |
| 5065 | grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg, |
| 5066 | needed.get(i), needed.flags, owner); |
| 5067 | } |
| 5068 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 5072 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5073 | NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, |
Dianne Hackborn | d9781fe | 2012-03-08 18:04:18 -0800 | [diff] [blame] | 5074 | intent, intent != null ? intent.getFlags() : 0, null); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5075 | if (needed == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5076 | return; |
| 5077 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5078 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5079 | grantUriPermissionUncheckedFromIntentLocked(needed, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 5083 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5084 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5085 | synchronized(this) { |
| 5086 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5087 | if (r == null) { |
| 5088 | throw new SecurityException("Unable to find app for caller " |
| 5089 | + caller |
| 5090 | + " when granting permission to uri " + uri); |
| 5091 | } |
| 5092 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5093 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5094 | } |
| 5095 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5096 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5097 | } |
| 5098 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5099 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5100 | null); |
| 5101 | } |
| 5102 | } |
| 5103 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5104 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5105 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5106 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 5107 | HashMap<Uri, UriPermission> perms |
| 5108 | = mGrantedUriPermissions.get(perm.uid); |
| 5109 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5110 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5111 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5112 | perms.remove(perm.uri); |
| 5113 | if (perms.size() == 0) { |
| 5114 | mGrantedUriPermissions.remove(perm.uid); |
| 5115 | } |
| 5116 | } |
| 5117 | } |
| 5118 | } |
| 5119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5120 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 5121 | int modeFlags) { |
| 5122 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5123 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5124 | if (modeFlags == 0) { |
| 5125 | return; |
| 5126 | } |
| 5127 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5128 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5129 | "Revoking all granted permissions to " + uri); |
| 5130 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5131 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5132 | |
| 5133 | final String authority = uri.getAuthority(); |
| 5134 | ProviderInfo pi = null; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5135 | int userId = UserId.getUserId(callingUid); |
| 5136 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5137 | if (cpr != null) { |
| 5138 | pi = cpr.info; |
| 5139 | } else { |
| 5140 | try { |
| 5141 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5142 | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5143 | } catch (RemoteException ex) { |
| 5144 | } |
| 5145 | } |
| 5146 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5147 | 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] | 5148 | return; |
| 5149 | } |
| 5150 | |
| 5151 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5152 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5153 | // Right now, if you are not the original owner of the permission, |
| 5154 | // you are not allowed to revoke it. |
| 5155 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5156 | throw new SecurityException("Uid " + callingUid |
| 5157 | + " does not have permission to uri " + uri); |
| 5158 | //} |
| 5159 | } |
| 5160 | |
| 5161 | // Go through all of the permissions and remove any that match. |
| 5162 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 5163 | if (SEGMENTS != null) { |
| 5164 | final int NS = SEGMENTS.size(); |
| 5165 | int N = mGrantedUriPermissions.size(); |
| 5166 | for (int i=0; i<N; i++) { |
| 5167 | HashMap<Uri, UriPermission> perms |
| 5168 | = mGrantedUriPermissions.valueAt(i); |
| 5169 | Iterator<UriPermission> it = perms.values().iterator(); |
| 5170 | toploop: |
| 5171 | while (it.hasNext()) { |
| 5172 | UriPermission perm = it.next(); |
| 5173 | Uri targetUri = perm.uri; |
| 5174 | if (!authority.equals(targetUri.getAuthority())) { |
| 5175 | continue; |
| 5176 | } |
| 5177 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5178 | if (targetSegments == null) { |
| 5179 | continue; |
| 5180 | } |
| 5181 | if (targetSegments.size() < NS) { |
| 5182 | continue; |
| 5183 | } |
| 5184 | for (int j=0; j<NS; j++) { |
| 5185 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5186 | continue toploop; |
| 5187 | } |
| 5188 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5189 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5190 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5191 | perm.clearModes(modeFlags); |
| 5192 | if (perm.modeFlags == 0) { |
| 5193 | it.remove(); |
| 5194 | } |
| 5195 | } |
| 5196 | if (perms.size() == 0) { |
| 5197 | mGrantedUriPermissions.remove( |
| 5198 | mGrantedUriPermissions.keyAt(i)); |
| 5199 | N--; |
| 5200 | i--; |
| 5201 | } |
| 5202 | } |
| 5203 | } |
| 5204 | } |
| 5205 | |
| 5206 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5207 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5208 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5209 | synchronized(this) { |
| 5210 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5211 | if (r == null) { |
| 5212 | throw new SecurityException("Unable to find app for caller " |
| 5213 | + caller |
| 5214 | + " when revoking permission to uri " + uri); |
| 5215 | } |
| 5216 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5217 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5218 | return; |
| 5219 | } |
| 5220 | |
| 5221 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5222 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5223 | if (modeFlags == 0) { |
| 5224 | return; |
| 5225 | } |
| 5226 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5227 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5228 | |
| 5229 | final String authority = uri.getAuthority(); |
| 5230 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5231 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5232 | if (cpr != null) { |
| 5233 | pi = cpr.info; |
| 5234 | } else { |
| 5235 | try { |
| 5236 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5237 | PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5238 | } catch (RemoteException ex) { |
| 5239 | } |
| 5240 | } |
| 5241 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5242 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5243 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5244 | return; |
| 5245 | } |
| 5246 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5247 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5248 | } |
| 5249 | } |
| 5250 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5251 | @Override |
| 5252 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5253 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5254 | synchronized(this) { |
| 5255 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5256 | return owner.getExternalTokenLocked(); |
| 5257 | } |
| 5258 | } |
| 5259 | |
| 5260 | @Override |
| 5261 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5262 | Uri uri, int modeFlags) { |
| 5263 | synchronized(this) { |
| 5264 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5265 | if (owner == null) { |
| 5266 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5267 | } |
| 5268 | if (fromUid != Binder.getCallingUid()) { |
| 5269 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5270 | // Only system code can grant URI permissions on behalf |
| 5271 | // of other users. |
| 5272 | throw new SecurityException("nice try"); |
| 5273 | } |
| 5274 | } |
| 5275 | if (targetPkg == null) { |
| 5276 | throw new IllegalArgumentException("null target"); |
| 5277 | } |
| 5278 | if (uri == null) { |
| 5279 | throw new IllegalArgumentException("null uri"); |
| 5280 | } |
| 5281 | |
| 5282 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | @Override |
| 5287 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5288 | synchronized(this) { |
| 5289 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5290 | if (owner == null) { |
| 5291 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5292 | } |
| 5293 | |
| 5294 | if (uri == null) { |
| 5295 | owner.removeUriPermissionsLocked(mode); |
| 5296 | } else { |
| 5297 | owner.removeUriPermissionLocked(uri, mode); |
| 5298 | } |
| 5299 | } |
| 5300 | } |
| 5301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5302 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5303 | synchronized (this) { |
| 5304 | ProcessRecord app = |
| 5305 | who != null ? getRecordForAppLocked(who) : null; |
| 5306 | if (app == null) return; |
| 5307 | |
| 5308 | Message msg = Message.obtain(); |
| 5309 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5310 | msg.obj = app; |
| 5311 | msg.arg1 = waiting ? 1 : 0; |
| 5312 | mHandler.sendMessage(msg); |
| 5313 | } |
| 5314 | } |
| 5315 | |
| 5316 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5317 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5318 | 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] | 5319 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5320 | outInfo.threshold = homeAppMem; |
| 5321 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5322 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5323 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5324 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5325 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5326 | ProcessList.VISIBLE_APP_ADJ); |
| 5327 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5328 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5329 | } |
| 5330 | |
| 5331 | // ========================================================= |
| 5332 | // TASK MANAGEMENT |
| 5333 | // ========================================================= |
| 5334 | |
| 5335 | public List getTasks(int maxNum, int flags, |
| 5336 | IThumbnailReceiver receiver) { |
| 5337 | ArrayList list = new ArrayList(); |
| 5338 | |
| 5339 | PendingThumbnailsRecord pending = null; |
| 5340 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5341 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5342 | |
| 5343 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5344 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5345 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5346 | + ", receiver=" + receiver); |
| 5347 | |
| 5348 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5349 | != PackageManager.PERMISSION_GRANTED) { |
| 5350 | if (receiver != null) { |
| 5351 | // If the caller wants to wait for pending thumbnails, |
| 5352 | // it ain't gonna get them. |
| 5353 | try { |
| 5354 | receiver.finished(); |
| 5355 | } catch (RemoteException ex) { |
| 5356 | } |
| 5357 | } |
| 5358 | String msg = "Permission Denial: getTasks() from pid=" |
| 5359 | + Binder.getCallingPid() |
| 5360 | + ", uid=" + Binder.getCallingUid() |
| 5361 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5362 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5363 | throw new SecurityException(msg); |
| 5364 | } |
| 5365 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5366 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5367 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5368 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5369 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5370 | TaskRecord curTask = null; |
| 5371 | int numActivities = 0; |
| 5372 | int numRunning = 0; |
| 5373 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5374 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5375 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5376 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5377 | |
| 5378 | // Initialize state for next task if needed. |
| 5379 | if (top == null || |
| 5380 | (top.state == ActivityState.INITIALIZING |
| 5381 | && top.task == r.task)) { |
| 5382 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5383 | curTask = r.task; |
| 5384 | numActivities = numRunning = 0; |
| 5385 | } |
| 5386 | |
| 5387 | // Add 'r' into the current task. |
| 5388 | numActivities++; |
| 5389 | if (r.app != null && r.app.thread != null) { |
| 5390 | numRunning++; |
| 5391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5392 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5393 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5394 | TAG, r.intent.getComponent().flattenToShortString() |
| 5395 | + ": task=" + r.task); |
| 5396 | |
| 5397 | // If the next one is a different task, generate a new |
| 5398 | // TaskInfo entry for what we have. |
| 5399 | if (next == null || next.task != curTask) { |
| 5400 | ActivityManager.RunningTaskInfo ci |
| 5401 | = new ActivityManager.RunningTaskInfo(); |
| 5402 | ci.id = curTask.taskId; |
| 5403 | ci.baseActivity = r.intent.getComponent(); |
| 5404 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5405 | if (top.thumbHolder != null) { |
| 5406 | ci.description = top.thumbHolder.lastDescription; |
| 5407 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5408 | ci.numActivities = numActivities; |
| 5409 | ci.numRunning = numRunning; |
| 5410 | //System.out.println( |
| 5411 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5412 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5413 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5414 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5415 | + " app=" + top.app |
| 5416 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5417 | if (top.state == ActivityState.RESUMED |
| 5418 | || top.state == ActivityState.PAUSING) { |
| 5419 | if (top.idle && top.app != null |
| 5420 | && top.app.thread != null) { |
| 5421 | topRecord = top; |
| 5422 | topThumbnail = top.app.thread; |
| 5423 | } else { |
| 5424 | top.thumbnailNeeded = true; |
| 5425 | } |
| 5426 | } |
| 5427 | if (pending == null) { |
| 5428 | pending = new PendingThumbnailsRecord(receiver); |
| 5429 | } |
| 5430 | pending.pendingRecords.add(top); |
| 5431 | } |
| 5432 | list.add(ci); |
| 5433 | maxNum--; |
| 5434 | top = null; |
| 5435 | } |
| 5436 | } |
| 5437 | |
| 5438 | if (pending != null) { |
| 5439 | mPendingThumbnails.add(pending); |
| 5440 | } |
| 5441 | } |
| 5442 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5443 | 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] | 5444 | |
| 5445 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5446 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5447 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5448 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5449 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5450 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5451 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5452 | } |
| 5453 | } |
| 5454 | |
| 5455 | if (pending == null && receiver != null) { |
| 5456 | // In this case all thumbnails were available and the client |
| 5457 | // is being asked to be told when the remaining ones come in... |
| 5458 | // which is unusually, since the top-most currently running |
| 5459 | // activity should never have a canned thumbnail! Oh well. |
| 5460 | try { |
| 5461 | receiver.finished(); |
| 5462 | } catch (RemoteException ex) { |
| 5463 | } |
| 5464 | } |
| 5465 | |
| 5466 | return list; |
| 5467 | } |
| 5468 | |
| 5469 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
| 5470 | int flags) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5471 | final int callingUid = Binder.getCallingUid(); |
| 5472 | // If it's the system uid asking, then use the current user id. |
| 5473 | // TODO: Make sure that there aren't any other legitimate calls from the system uid that |
| 5474 | // require the entire list. |
| 5475 | final int callingUserId = callingUid == Process.SYSTEM_UID |
| 5476 | ? mCurrentUserId : UserId.getUserId(callingUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5477 | synchronized (this) { |
| 5478 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5479 | "getRecentTasks()"); |
| 5480 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5481 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5482 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5483 | final int N = mRecentTasks.size(); |
| 5484 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5485 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5486 | maxNum < N ? maxNum : N); |
| 5487 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5488 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5489 | // Only add calling user's recent tasks |
| 5490 | if (tr.userId != callingUserId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5491 | // Return the entry if desired by the caller. We always return |
| 5492 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5493 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5494 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5495 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5496 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5497 | if (i == 0 |
| 5498 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5499 | || (tr.intent == null) |
| 5500 | || ((tr.intent.getFlags() |
| 5501 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5502 | ActivityManager.RecentTaskInfo rti |
| 5503 | = new ActivityManager.RecentTaskInfo(); |
| 5504 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5505 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5506 | rti.baseIntent = new Intent( |
| 5507 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5508 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5509 | rti.description = tr.lastDescription; |
| 5510 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5511 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5512 | // Check whether this activity is currently available. |
| 5513 | try { |
| 5514 | if (rti.origActivity != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5515 | if (pm.getActivityInfo(rti.origActivity, 0, callingUserId) |
| 5516 | == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5517 | continue; |
| 5518 | } |
| 5519 | } else if (rti.baseIntent != null) { |
| 5520 | if (pm.queryIntentActivities(rti.baseIntent, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5521 | null, 0, callingUserId) == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5522 | continue; |
| 5523 | } |
| 5524 | } |
| 5525 | } catch (RemoteException e) { |
| 5526 | // Will never happen. |
| 5527 | } |
| 5528 | } |
| 5529 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5530 | res.add(rti); |
| 5531 | maxNum--; |
| 5532 | } |
| 5533 | } |
| 5534 | return res; |
| 5535 | } |
| 5536 | } |
| 5537 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5538 | private TaskRecord taskForIdLocked(int id) { |
| 5539 | final int N = mRecentTasks.size(); |
| 5540 | for (int i=0; i<N; i++) { |
| 5541 | TaskRecord tr = mRecentTasks.get(i); |
| 5542 | if (tr.taskId == id) { |
| 5543 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5544 | } |
| 5545 | } |
| 5546 | return null; |
| 5547 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5548 | |
| 5549 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5550 | synchronized (this) { |
| 5551 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5552 | "getTaskThumbnails()"); |
| 5553 | TaskRecord tr = taskForIdLocked(id); |
| 5554 | if (tr != null) { |
| 5555 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5556 | } |
| 5557 | } |
| 5558 | return null; |
| 5559 | } |
| 5560 | |
| 5561 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5562 | synchronized (this) { |
| 5563 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5564 | "removeSubTask()"); |
| 5565 | long ident = Binder.clearCallingIdentity(); |
| 5566 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5567 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex, |
| 5568 | true) != null; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5569 | } finally { |
| 5570 | Binder.restoreCallingIdentity(ident); |
| 5571 | } |
| 5572 | } |
| 5573 | } |
| 5574 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5575 | private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) { |
| 5576 | final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5577 | Intent baseIntent = new Intent( |
| 5578 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5579 | ComponentName component = baseIntent.getComponent(); |
| 5580 | if (component == null) { |
| 5581 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5582 | return; |
| 5583 | } |
| 5584 | |
| 5585 | // Find any running services associated with this app. |
| 5586 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5587 | for (ServiceRecord sr : mServiceMap.getAllServices(tr.userId)) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5588 | if (sr.packageName.equals(component.getPackageName())) { |
| 5589 | services.add(sr); |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | // Take care of any running services associated with the app. |
| 5594 | for (int i=0; i<services.size(); i++) { |
| 5595 | ServiceRecord sr = services.get(i); |
| 5596 | if (sr.startRequested) { |
| 5597 | if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5598 | Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5599 | stopServiceLocked(sr); |
| 5600 | } else { |
| 5601 | sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5602 | sr.makeNextStartId(), baseIntent, null)); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5603 | if (sr.app != null && sr.app.thread != null) { |
| 5604 | sendServiceArgsLocked(sr, false); |
| 5605 | } |
| 5606 | } |
| 5607 | } |
| 5608 | } |
| 5609 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5610 | if (killProcesses) { |
| 5611 | // Find any running processes associated with this app. |
| 5612 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 5613 | SparseArray<ProcessRecord> appProcs |
| 5614 | = mProcessNames.getMap().get(component.getPackageName()); |
| 5615 | if (appProcs != null) { |
| 5616 | for (int i=0; i<appProcs.size(); i++) { |
| 5617 | procs.add(appProcs.valueAt(i)); |
| 5618 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5619 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5620 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5621 | // Kill the running processes. |
| 5622 | for (int i=0; i<procs.size(); i++) { |
| 5623 | ProcessRecord pr = procs.get(i); |
| 5624 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5625 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
| 5626 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5627 | pr.processName, pr.setAdj, "remove task"); |
| 5628 | Process.killProcessQuiet(pr.pid); |
| 5629 | } else { |
| 5630 | pr.waitingToKill = "remove task"; |
| 5631 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5632 | } |
| 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | public boolean removeTask(int taskId, int flags) { |
| 5637 | synchronized (this) { |
| 5638 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5639 | "removeTask()"); |
| 5640 | long ident = Binder.clearCallingIdentity(); |
| 5641 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5642 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1, |
| 5643 | false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5644 | if (r != null) { |
| 5645 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5646 | cleanUpRemovedTaskLocked(r.task, flags); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5647 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5648 | } else { |
| 5649 | TaskRecord tr = null; |
| 5650 | int i=0; |
| 5651 | while (i < mRecentTasks.size()) { |
| 5652 | TaskRecord t = mRecentTasks.get(i); |
| 5653 | if (t.taskId == taskId) { |
| 5654 | tr = t; |
| 5655 | break; |
| 5656 | } |
| 5657 | i++; |
| 5658 | } |
| 5659 | if (tr != null) { |
| 5660 | if (tr.numActivities <= 0) { |
| 5661 | // Caller is just removing a recent task that is |
| 5662 | // not actively running. That is easy! |
| 5663 | mRecentTasks.remove(i); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5664 | cleanUpRemovedTaskLocked(tr, flags); |
| 5665 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5666 | } else { |
| 5667 | Slog.w(TAG, "removeTask: task " + taskId |
| 5668 | + " does not have activities to remove, " |
| 5669 | + " but numActivities=" + tr.numActivities |
| 5670 | + ": " + tr); |
| 5671 | } |
| 5672 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5673 | } |
| 5674 | } finally { |
| 5675 | Binder.restoreCallingIdentity(ident); |
| 5676 | } |
| 5677 | } |
| 5678 | return false; |
| 5679 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5680 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5681 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5682 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5683 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5684 | TaskRecord jt = startTask; |
| 5685 | |
| 5686 | // First look backwards |
| 5687 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5688 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5689 | if (r.task != jt) { |
| 5690 | jt = r.task; |
| 5691 | if (affinity.equals(jt.affinity)) { |
| 5692 | return j; |
| 5693 | } |
| 5694 | } |
| 5695 | } |
| 5696 | |
| 5697 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5698 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5699 | jt = startTask; |
| 5700 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5701 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5702 | if (r.task != jt) { |
| 5703 | if (affinity.equals(jt.affinity)) { |
| 5704 | return j; |
| 5705 | } |
| 5706 | jt = r.task; |
| 5707 | } |
| 5708 | } |
| 5709 | |
| 5710 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5711 | 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] | 5712 | return N-1; |
| 5713 | } |
| 5714 | |
| 5715 | return -1; |
| 5716 | } |
| 5717 | |
| 5718 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5719 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5720 | */ |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5721 | public void moveTaskToFront(int task, int flags, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5722 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5723 | "moveTaskToFront()"); |
| 5724 | |
| 5725 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5726 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5727 | Binder.getCallingUid(), "Task to front")) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5728 | ActivityOptions.abort(options); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5729 | return; |
| 5730 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5731 | final long origId = Binder.clearCallingIdentity(); |
| 5732 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5733 | TaskRecord tr = taskForIdLocked(task); |
| 5734 | if (tr != null) { |
| 5735 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5736 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5737 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5738 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5739 | // Caller wants the home activity moved with it. To accomplish this, |
| 5740 | // we'll just move the home task to the top first. |
| 5741 | mMainStack.moveHomeToFrontLocked(); |
| 5742 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5743 | mMainStack.moveTaskToFrontLocked(tr, null, options); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5744 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5745 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5746 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5747 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5748 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5749 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5750 | mMainStack.mUserLeaving = true; |
| 5751 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5752 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5753 | // Caller wants the home activity moved with it. To accomplish this, |
| 5754 | // we'll just move the home task to the top first. |
| 5755 | mMainStack.moveHomeToFrontLocked(); |
| 5756 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5757 | mMainStack.moveTaskToFrontLocked(hr.task, null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5758 | return; |
| 5759 | } |
| 5760 | } |
| 5761 | } finally { |
| 5762 | Binder.restoreCallingIdentity(origId); |
| 5763 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5764 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5765 | } |
| 5766 | } |
| 5767 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5768 | public void moveTaskToBack(int task) { |
| 5769 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5770 | "moveTaskToBack()"); |
| 5771 | |
| 5772 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5773 | if (mMainStack.mResumedActivity != null |
| 5774 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5775 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5776 | Binder.getCallingUid(), "Task to back")) { |
| 5777 | return; |
| 5778 | } |
| 5779 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5780 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5781 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5782 | Binder.restoreCallingIdentity(origId); |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | /** |
| 5787 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5788 | * of the history stack. The activity's order within the task is unchanged. |
| 5789 | * |
| 5790 | * @param token A reference to the activity we wish to move |
| 5791 | * @param nonRoot If false then this only works if the activity is the root |
| 5792 | * of a task; if true it will work for any activity in a task. |
| 5793 | * @return Returns true if the move completed, false if not. |
| 5794 | */ |
| 5795 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5796 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5797 | synchronized(this) { |
| 5798 | final long origId = Binder.clearCallingIdentity(); |
| 5799 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5800 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5801 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5802 | } |
| 5803 | Binder.restoreCallingIdentity(origId); |
| 5804 | } |
| 5805 | return false; |
| 5806 | } |
| 5807 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5808 | public void moveTaskBackwards(int task) { |
| 5809 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5810 | "moveTaskBackwards()"); |
| 5811 | |
| 5812 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5813 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5814 | Binder.getCallingUid(), "Task backwards")) { |
| 5815 | return; |
| 5816 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5817 | final long origId = Binder.clearCallingIdentity(); |
| 5818 | moveTaskBackwardsLocked(task); |
| 5819 | Binder.restoreCallingIdentity(origId); |
| 5820 | } |
| 5821 | } |
| 5822 | |
| 5823 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5824 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5828 | synchronized(this) { |
| 5829 | return getTaskForActivityLocked(token, onlyRoot); |
| 5830 | } |
| 5831 | } |
| 5832 | |
| 5833 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5834 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5835 | TaskRecord lastTask = null; |
| 5836 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5837 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5838 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5839 | if (!onlyRoot || lastTask != r.task) { |
| 5840 | return r.task.taskId; |
| 5841 | } |
| 5842 | return -1; |
| 5843 | } |
| 5844 | lastTask = r.task; |
| 5845 | } |
| 5846 | |
| 5847 | return -1; |
| 5848 | } |
| 5849 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5850 | // ========================================================= |
| 5851 | // THUMBNAILS |
| 5852 | // ========================================================= |
| 5853 | |
| 5854 | public void reportThumbnail(IBinder token, |
| 5855 | Bitmap thumbnail, CharSequence description) { |
| 5856 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5857 | final long origId = Binder.clearCallingIdentity(); |
| 5858 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5859 | Binder.restoreCallingIdentity(origId); |
| 5860 | } |
| 5861 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5862 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5863 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5864 | TaskRecord task = null; |
| 5865 | ArrayList receivers = null; |
| 5866 | |
| 5867 | //System.out.println("Send pending thumbnail: " + r); |
| 5868 | |
| 5869 | synchronized(this) { |
| 5870 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 5871 | r = mMainStack.isInStackLocked(token); |
| 5872 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5873 | return; |
| 5874 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5875 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5876 | if (thumbnail == null && r.thumbHolder != null) { |
| 5877 | thumbnail = r.thumbHolder.lastThumbnail; |
| 5878 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5879 | } |
| 5880 | if (thumbnail == null && !always) { |
| 5881 | // If there is no thumbnail, and this entry is not actually |
| 5882 | // going away, then abort for now and pick up the next |
| 5883 | // thumbnail we get. |
| 5884 | return; |
| 5885 | } |
| 5886 | task = r.task; |
| 5887 | |
| 5888 | int N = mPendingThumbnails.size(); |
| 5889 | int i=0; |
| 5890 | while (i<N) { |
| 5891 | PendingThumbnailsRecord pr = |
| 5892 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 5893 | //System.out.println("Looking in " + pr.pendingRecords); |
| 5894 | if (pr.pendingRecords.remove(r)) { |
| 5895 | if (receivers == null) { |
| 5896 | receivers = new ArrayList(); |
| 5897 | } |
| 5898 | receivers.add(pr); |
| 5899 | if (pr.pendingRecords.size() == 0) { |
| 5900 | pr.finished = true; |
| 5901 | mPendingThumbnails.remove(i); |
| 5902 | N--; |
| 5903 | continue; |
| 5904 | } |
| 5905 | } |
| 5906 | i++; |
| 5907 | } |
| 5908 | } |
| 5909 | |
| 5910 | if (receivers != null) { |
| 5911 | final int N = receivers.size(); |
| 5912 | for (int i=0; i<N; i++) { |
| 5913 | try { |
| 5914 | PendingThumbnailsRecord pr = |
| 5915 | (PendingThumbnailsRecord)receivers.get(i); |
| 5916 | pr.receiver.newThumbnail( |
| 5917 | task != null ? task.taskId : -1, thumbnail, description); |
| 5918 | if (pr.finished) { |
| 5919 | pr.receiver.finished(); |
| 5920 | } |
| 5921 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5922 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5923 | } |
| 5924 | } |
| 5925 | } |
| 5926 | } |
| 5927 | |
| 5928 | // ========================================================= |
| 5929 | // CONTENT PROVIDERS |
| 5930 | // ========================================================= |
| 5931 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5932 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 5933 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5934 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5935 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5936 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5937 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5938 | } catch (RemoteException ex) { |
| 5939 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5940 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5941 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 5942 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5943 | if (providers != null) { |
| 5944 | final int N = providers.size(); |
| 5945 | for (int i=0; i<N; i++) { |
| 5946 | ProviderInfo cpi = |
| 5947 | (ProviderInfo)providers.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5948 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5949 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5950 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5951 | if (cpr == null) { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 5952 | cpr = new ContentProviderRecord(this, cpi, app.info, comp); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5953 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5954 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5955 | if (DEBUG_MU) |
| 5956 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5957 | app.pubProviders.put(cpi.name, cpr); |
| 5958 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 5959 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5960 | } |
| 5961 | } |
| 5962 | return providers; |
| 5963 | } |
| 5964 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5965 | /** |
| 5966 | * Check if {@link ProcessRecord} has a possible chance at accessing the |
| 5967 | * given {@link ProviderInfo}. Final permission checking is always done |
| 5968 | * in {@link ContentProvider}. |
| 5969 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5970 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5971 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5972 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5973 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5974 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5975 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5976 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5977 | return null; |
| 5978 | } |
| 5979 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5980 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5981 | == PackageManager.PERMISSION_GRANTED) { |
| 5982 | return null; |
| 5983 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5984 | |
| 5985 | PathPermission[] pps = cpi.pathPermissions; |
| 5986 | if (pps != null) { |
| 5987 | int i = pps.length; |
| 5988 | while (i > 0) { |
| 5989 | i--; |
| 5990 | PathPermission pp = pps[i]; |
| 5991 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5992 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5993 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5994 | return null; |
| 5995 | } |
| 5996 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5997 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5998 | == PackageManager.PERMISSION_GRANTED) { |
| 5999 | return null; |
| 6000 | } |
| 6001 | } |
| 6002 | } |
| 6003 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6004 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 6005 | if (perms != null) { |
| 6006 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 6007 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 6008 | return null; |
| 6009 | } |
| 6010 | } |
| 6011 | } |
| 6012 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6013 | String msg; |
| 6014 | if (!cpi.exported) { |
| 6015 | msg = "Permission Denial: opening provider " + cpi.name |
| 6016 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6017 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 6018 | + cpi.applicationInfo.uid; |
| 6019 | } else { |
| 6020 | msg = "Permission Denial: opening provider " + cpi.name |
| 6021 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6022 | + ", uid=" + callingUid + ") requires " |
| 6023 | + cpi.readPermission + " or " + cpi.writePermission; |
| 6024 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6025 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6026 | return msg; |
| 6027 | } |
| 6028 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6029 | boolean incProviderCount(ProcessRecord r, final ContentProviderRecord cpr, |
| 6030 | IBinder externalProcessToken) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6031 | if (r != null) { |
| 6032 | Integer cnt = r.conProviders.get(cpr); |
| 6033 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6034 | "Adding provider requested by " |
| 6035 | + r.processName + " from process " |
| 6036 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6037 | + " cnt=" + (cnt == null ? 1 : cnt)); |
| 6038 | if (cnt == null) { |
| 6039 | cpr.clients.add(r); |
| 6040 | r.conProviders.put(cpr, new Integer(1)); |
| 6041 | return true; |
| 6042 | } else { |
| 6043 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 6044 | } |
| 6045 | } else { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6046 | cpr.addExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6047 | } |
| 6048 | return false; |
| 6049 | } |
| 6050 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6051 | boolean decProviderCount(ProcessRecord r, final ContentProviderRecord cpr, |
| 6052 | IBinder externalProcessToken) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6053 | if (r != null) { |
| 6054 | Integer cnt = r.conProviders.get(cpr); |
| 6055 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6056 | "Removing provider requested by " |
| 6057 | + r.processName + " from process " |
| 6058 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6059 | + " cnt=" + cnt); |
| 6060 | if (cnt == null || cnt.intValue() <= 1) { |
| 6061 | cpr.clients.remove(r); |
| 6062 | r.conProviders.remove(cpr); |
| 6063 | return true; |
| 6064 | } else { |
| 6065 | r.conProviders.put(cpr, new Integer(cnt.intValue()-1)); |
| 6066 | } |
| 6067 | } else { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6068 | cpr.removeExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6069 | } |
| 6070 | return false; |
| 6071 | } |
| 6072 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6073 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6074 | String name, IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6075 | ContentProviderRecord cpr; |
| 6076 | ProviderInfo cpi = null; |
| 6077 | |
| 6078 | synchronized(this) { |
| 6079 | ProcessRecord r = null; |
| 6080 | if (caller != null) { |
| 6081 | r = getRecordForAppLocked(caller); |
| 6082 | if (r == null) { |
| 6083 | throw new SecurityException( |
| 6084 | "Unable to find app for caller " + caller |
| 6085 | + " (pid=" + Binder.getCallingPid() |
| 6086 | + ") when getting content provider " + name); |
| 6087 | } |
| 6088 | } |
| 6089 | |
| 6090 | // First check if this content provider has been published... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6091 | int userId = UserId.getUserId(r != null ? r.uid : Binder.getCallingUid()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6092 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6093 | boolean providerRunning = cpr != null; |
| 6094 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6095 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6096 | String msg; |
| 6097 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6098 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6099 | } |
| 6100 | |
| 6101 | if (r != null && cpr.canRunHere(r)) { |
| 6102 | // This provider has been published or is in the process |
| 6103 | // of being published... but it is also allowed to run |
| 6104 | // in the caller's process, so don't make a connection |
| 6105 | // and just let the caller instantiate its own instance. |
| 6106 | if (cpr.provider != null) { |
| 6107 | // don't give caller the provider object, it needs |
| 6108 | // to make its own. |
| 6109 | cpr = new ContentProviderRecord(cpr); |
| 6110 | } |
| 6111 | return cpr; |
| 6112 | } |
| 6113 | |
| 6114 | final long origId = Binder.clearCallingIdentity(); |
| 6115 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6116 | // 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] | 6117 | // return it right away. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6118 | final boolean countChanged = incProviderCount(r, cpr, token); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6119 | if (countChanged) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6120 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 6121 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6122 | // make sure to count it as being accessed and thus |
| 6123 | // back up on the LRU list. This is good because |
| 6124 | // content providers are often expensive to start. |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6125 | updateLruProcessLocked(cpr.proc, false, true); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6126 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 6127 | } |
| 6128 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6129 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6130 | if (false) { |
| 6131 | if (cpr.name.flattenToShortString().equals( |
| 6132 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 6133 | Slog.v(TAG, "****************** KILLING " |
| 6134 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6135 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6136 | } |
| 6137 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6138 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6139 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 6140 | // NOTE: there is still a race here where a signal could be |
| 6141 | // pending on the process even though we managed to update its |
| 6142 | // adj level. Not sure what to do about this, but at least |
| 6143 | // the race is now smaller. |
| 6144 | if (!success) { |
| 6145 | // Uh oh... it looks like the provider's process |
| 6146 | // has been killed on us. We need to wait for a new |
| 6147 | // process to be started, and make sure its death |
| 6148 | // doesn't kill our process. |
| 6149 | Slog.i(TAG, |
| 6150 | "Existing provider " + cpr.name.flattenToShortString() |
| 6151 | + " is crashing; detaching " + r); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6152 | boolean lastRef = decProviderCount(r, cpr, token); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6153 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6154 | if (!lastRef) { |
| 6155 | // This wasn't the last ref our process had on |
| 6156 | // the provider... we have now been killed, bail. |
| 6157 | return null; |
| 6158 | } |
| 6159 | providerRunning = false; |
| 6160 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6164 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6165 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6166 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6167 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6168 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6169 | resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6170 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6171 | } catch (RemoteException ex) { |
| 6172 | } |
| 6173 | if (cpi == null) { |
| 6174 | return null; |
| 6175 | } |
| 6176 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6177 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6178 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6179 | String msg; |
| 6180 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6181 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6182 | } |
| 6183 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6184 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6185 | && !cpi.processName.equals("system")) { |
| 6186 | // If this content provider does not run in the system |
| 6187 | // process, and the system is not yet ready to run other |
| 6188 | // processes, then fail fast instead of hanging. |
| 6189 | throw new IllegalArgumentException( |
| 6190 | "Attempt to launch content provider before system ready"); |
| 6191 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6192 | |
| 6193 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6194 | cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6195 | final boolean firstClass = cpr == null; |
| 6196 | if (firstClass) { |
| 6197 | try { |
| 6198 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6199 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6200 | getApplicationInfo( |
| 6201 | cpi.applicationInfo.packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6202 | STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6203 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6204 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6205 | + cpi.name); |
| 6206 | return null; |
| 6207 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6208 | ai = getAppInfoForUser(ai, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6209 | cpr = new ContentProviderRecord(this, cpi, ai, comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6210 | } catch (RemoteException ex) { |
| 6211 | // pm is in same process, this will never happen. |
| 6212 | } |
| 6213 | } |
| 6214 | |
| 6215 | if (r != null && cpr.canRunHere(r)) { |
| 6216 | // If this is a multiprocess provider, then just return its |
| 6217 | // info and allow the caller to instantiate it. Only do |
| 6218 | // this if the provider is the same user as the caller's |
| 6219 | // process, or can run as root (so can be in any process). |
| 6220 | return cpr; |
| 6221 | } |
| 6222 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6223 | if (DEBUG_PROVIDER) { |
| 6224 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6225 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6226 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6227 | } |
| 6228 | |
| 6229 | // This is single process, and our app is now connecting to it. |
| 6230 | // See if we are already in the process of launching this |
| 6231 | // provider. |
| 6232 | final int N = mLaunchingProviders.size(); |
| 6233 | int i; |
| 6234 | for (i=0; i<N; i++) { |
| 6235 | if (mLaunchingProviders.get(i) == cpr) { |
| 6236 | break; |
| 6237 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6238 | } |
| 6239 | |
| 6240 | // If the provider is not already being launched, then get it |
| 6241 | // started. |
| 6242 | if (i >= N) { |
| 6243 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6244 | |
| 6245 | try { |
| 6246 | // Content provider is now in use, its package can't be stopped. |
| 6247 | try { |
| 6248 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6249 | cpr.appInfo.packageName, false, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6250 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6251 | } catch (IllegalArgumentException e) { |
| 6252 | Slog.w(TAG, "Failed trying to unstop package " |
| 6253 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6257 | cpr.appInfo, false, 0, "content provider", |
| 6258 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6259 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6260 | if (proc == null) { |
| 6261 | Slog.w(TAG, "Unable to launch app " |
| 6262 | + cpi.applicationInfo.packageName + "/" |
| 6263 | + cpi.applicationInfo.uid + " for provider " |
| 6264 | + name + ": process is bad"); |
| 6265 | return null; |
| 6266 | } |
| 6267 | cpr.launchingApp = proc; |
| 6268 | mLaunchingProviders.add(cpr); |
| 6269 | } finally { |
| 6270 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | // Make sure the provider is published (the same provider class |
| 6275 | // may be published under multiple names). |
| 6276 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6277 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6278 | } |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6279 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6280 | mProviderMap.putProviderByName(name, cpr); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6281 | incProviderCount(r, cpr, token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | // Wait for the provider to be published... |
| 6286 | synchronized (cpr) { |
| 6287 | while (cpr.provider == null) { |
| 6288 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6289 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6290 | + cpi.applicationInfo.packageName + "/" |
| 6291 | + cpi.applicationInfo.uid + " for provider " |
| 6292 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6293 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6294 | cpi.applicationInfo.packageName, |
| 6295 | cpi.applicationInfo.uid, name); |
| 6296 | return null; |
| 6297 | } |
| 6298 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6299 | if (DEBUG_MU) { |
| 6300 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6301 | + cpr.launchingApp); |
| 6302 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6303 | cpr.wait(); |
| 6304 | } catch (InterruptedException ex) { |
| 6305 | } |
| 6306 | } |
| 6307 | } |
| 6308 | return cpr; |
| 6309 | } |
| 6310 | |
| 6311 | public final ContentProviderHolder getContentProvider( |
| 6312 | IApplicationThread caller, String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6313 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6314 | if (caller == null) { |
| 6315 | String msg = "null IApplicationThread when getting content provider " |
| 6316 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6317 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6318 | throw new SecurityException(msg); |
| 6319 | } |
| 6320 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6321 | return getContentProviderImpl(caller, name, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6322 | } |
| 6323 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6324 | public ContentProviderHolder getContentProviderExternal(String name, IBinder token) { |
| 6325 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6326 | "Do not have permission in call getContentProviderExternal()"); |
| 6327 | return getContentProviderExternalUnchecked(name, token); |
| 6328 | } |
| 6329 | |
| 6330 | private ContentProviderHolder getContentProviderExternalUnchecked(String name,IBinder token) { |
| 6331 | return getContentProviderImpl(null, name, token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6332 | } |
| 6333 | |
| 6334 | /** |
| 6335 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6336 | * @param cpr |
| 6337 | */ |
| 6338 | public void removeContentProvider(IApplicationThread caller, String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6339 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6340 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6341 | int userId = UserId.getUserId(Binder.getCallingUid()); |
| 6342 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6343 | if(cpr == null) { |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6344 | // remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6345 | if (DEBUG_PROVIDER) Slog.v(TAG, name + |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6346 | " provider not found in providers list"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6347 | return; |
| 6348 | } |
| 6349 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 6350 | if (r == null) { |
| 6351 | throw new SecurityException( |
| 6352 | "Unable to find app for caller " + caller + |
| 6353 | " when removing content provider " + name); |
| 6354 | } |
| 6355 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6356 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6357 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId); |
| 6358 | if (DEBUG_PROVIDER) Slog.v(TAG, "Removing provider requested by " |
| 6359 | + r.info.processName + " from process " |
| 6360 | + localCpr.appInfo.processName); |
| 6361 | if (localCpr.launchingApp == r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6362 | //should not happen. taken care of as a local provider |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6363 | Slog.w(TAG, "removeContentProvider called on local provider: " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6364 | + cpr.info.name + " in process " + r.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6365 | return; |
| 6366 | } else { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6367 | if (decProviderCount(r, localCpr, null)) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6368 | updateOomAdjLocked(); |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 6369 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6370 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6371 | } |
| 6372 | } |
| 6373 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6374 | public void removeContentProviderExternal(String name, IBinder token) { |
| 6375 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6376 | "Do not have permission in call removeContentProviderExternal()"); |
| 6377 | removeContentProviderExternalUnchecked(name, token); |
| 6378 | } |
| 6379 | |
| 6380 | private void removeContentProviderExternalUnchecked(String name, IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6381 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6382 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
| 6383 | Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6384 | if(cpr == null) { |
| 6385 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6386 | 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] | 6387 | return; |
| 6388 | } |
| 6389 | |
| 6390 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6391 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6392 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, |
| 6393 | Binder.getOrigCallingUser()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6394 | if (localCpr.hasExternalProcessHandles()) { |
| 6395 | if (localCpr.removeExternalProcessHandleLocked(token)) { |
| 6396 | updateOomAdjLocked(); |
| 6397 | } else { |
| 6398 | Slog.e(TAG, "Attmpt to remove content provider " + localCpr |
| 6399 | + " with no external reference for token: " |
| 6400 | + token + "."); |
| 6401 | } |
| 6402 | } else { |
| 6403 | Slog.e(TAG, "Attmpt to remove content provider: " + localCpr |
| 6404 | + " with no external references."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6406 | } |
| 6407 | } |
| 6408 | |
| 6409 | public final void publishContentProviders(IApplicationThread caller, |
| 6410 | List<ContentProviderHolder> providers) { |
| 6411 | if (providers == null) { |
| 6412 | return; |
| 6413 | } |
| 6414 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6415 | enforceNotIsolatedCaller("publishContentProviders"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6416 | synchronized(this) { |
| 6417 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6418 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6419 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6420 | if (r == null) { |
| 6421 | throw new SecurityException( |
| 6422 | "Unable to find app for caller " + caller |
| 6423 | + " (pid=" + Binder.getCallingPid() |
| 6424 | + ") when publishing content providers"); |
| 6425 | } |
| 6426 | |
| 6427 | final long origId = Binder.clearCallingIdentity(); |
| 6428 | |
| 6429 | final int N = providers.size(); |
| 6430 | for (int i=0; i<N; i++) { |
| 6431 | ContentProviderHolder src = providers.get(i); |
| 6432 | if (src == null || src.info == null || src.provider == null) { |
| 6433 | continue; |
| 6434 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6435 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6436 | if (DEBUG_MU) |
| 6437 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6438 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6439 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6440 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6441 | String names[] = dst.info.authority.split(";"); |
| 6442 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6443 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6444 | } |
| 6445 | |
| 6446 | int NL = mLaunchingProviders.size(); |
| 6447 | int j; |
| 6448 | for (j=0; j<NL; j++) { |
| 6449 | if (mLaunchingProviders.get(j) == dst) { |
| 6450 | mLaunchingProviders.remove(j); |
| 6451 | j--; |
| 6452 | NL--; |
| 6453 | } |
| 6454 | } |
| 6455 | synchronized (dst) { |
| 6456 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6457 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6458 | dst.notifyAll(); |
| 6459 | } |
| 6460 | updateOomAdjLocked(r); |
| 6461 | } |
| 6462 | } |
| 6463 | |
| 6464 | Binder.restoreCallingIdentity(origId); |
| 6465 | } |
| 6466 | } |
| 6467 | |
| 6468 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6469 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6470 | synchronized (mSelf) { |
| 6471 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6472 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6473 | if (providers != null) { |
| 6474 | for (int i=providers.size()-1; i>=0; i--) { |
| 6475 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6476 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6477 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6478 | + ": not system .apk"); |
| 6479 | providers.remove(i); |
| 6480 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6481 | } |
| 6482 | } |
| 6483 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6484 | if (providers != null) { |
| 6485 | mSystemThread.installSystemProviders(providers); |
| 6486 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6487 | |
| 6488 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6489 | |
| 6490 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6491 | } |
| 6492 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6493 | /** |
| 6494 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6495 | * to access its content provider. |
| 6496 | * |
| 6497 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6498 | * |
| 6499 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6500 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6501 | */ |
| 6502 | public String getProviderMimeType(Uri uri) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6503 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6504 | final String name = uri.getAuthority(); |
| 6505 | final long ident = Binder.clearCallingIdentity(); |
| 6506 | ContentProviderHolder holder = null; |
| 6507 | |
| 6508 | try { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6509 | holder = getContentProviderExternalUnchecked(name, null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6510 | if (holder != null) { |
| 6511 | return holder.provider.getType(uri); |
| 6512 | } |
| 6513 | } catch (RemoteException e) { |
| 6514 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6515 | return null; |
| 6516 | } finally { |
| 6517 | if (holder != null) { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6518 | removeContentProviderExternalUnchecked(name, null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6519 | } |
| 6520 | Binder.restoreCallingIdentity(ident); |
| 6521 | } |
| 6522 | |
| 6523 | return null; |
| 6524 | } |
| 6525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6526 | // ========================================================= |
| 6527 | // GLOBAL MANAGEMENT |
| 6528 | // ========================================================= |
| 6529 | |
| 6530 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6531 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6532 | String proc = customProcess != null ? customProcess : info.processName; |
| 6533 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6534 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6535 | int uid = info.uid; |
| 6536 | if (isolated) { |
| 6537 | int userId = UserId.getUserId(uid); |
| 6538 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 6539 | uid = 0; |
| 6540 | while (true) { |
| 6541 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 6542 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 6543 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 6544 | } |
| 6545 | uid = UserId.getUid(userId, mNextIsolatedProcessUid); |
| 6546 | mNextIsolatedProcessUid++; |
| 6547 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 6548 | // No process for this uid, use it. |
| 6549 | break; |
| 6550 | } |
| 6551 | stepsLeft--; |
| 6552 | if (stepsLeft <= 0) { |
| 6553 | return null; |
| 6554 | } |
| 6555 | } |
| 6556 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6557 | synchronized (stats) { |
| 6558 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6559 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6560 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6561 | } |
| 6562 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6563 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 6564 | ProcessRecord app; |
| 6565 | if (!isolated) { |
| 6566 | app = getProcessRecordLocked(info.processName, info.uid); |
| 6567 | } else { |
| 6568 | app = null; |
| 6569 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6570 | |
| 6571 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6572 | app = newProcessRecordLocked(null, info, null, isolated); |
| 6573 | mProcessNames.put(info.processName, app.uid, app); |
| 6574 | if (isolated) { |
| 6575 | mIsolatedProcesses.put(app.uid, app); |
| 6576 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 6577 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6578 | } |
| 6579 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6580 | // This package really, really can not be stopped. |
| 6581 | try { |
| 6582 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6583 | info.packageName, false, UserId.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6584 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6585 | } catch (IllegalArgumentException e) { |
| 6586 | Slog.w(TAG, "Failed trying to unstop package " |
| 6587 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6588 | } |
| 6589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6590 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6591 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6592 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6593 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6594 | } |
| 6595 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6596 | mPersistentStartingProcesses.add(app); |
| 6597 | startProcessLocked(app, "added application", app.processName); |
| 6598 | } |
| 6599 | |
| 6600 | return app; |
| 6601 | } |
| 6602 | |
| 6603 | public void unhandledBack() { |
| 6604 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6605 | "unhandledBack()"); |
| 6606 | |
| 6607 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6608 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6609 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6610 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 6611 | if (count > 1) { |
| 6612 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6613 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6614 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 6615 | Binder.restoreCallingIdentity(origId); |
| 6616 | } |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6621 | enforceNotIsolatedCaller("openContentUri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6622 | String name = uri.getAuthority(); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6623 | ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6624 | ParcelFileDescriptor pfd = null; |
| 6625 | if (cph != null) { |
| 6626 | // We record the binder invoker's uid in thread-local storage before |
| 6627 | // going to the content provider to open the file. Later, in the code |
| 6628 | // that handles all permissions checks, we look for this uid and use |
| 6629 | // that rather than the Activity Manager's own uid. The effect is that |
| 6630 | // we do the check against the caller's permissions even though it looks |
| 6631 | // to the content provider like the Activity Manager itself is making |
| 6632 | // the request. |
| 6633 | sCallerIdentity.set(new Identity( |
| 6634 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6635 | try { |
| 6636 | pfd = cph.provider.openFile(uri, "r"); |
| 6637 | } catch (FileNotFoundException e) { |
| 6638 | // do nothing; pfd will be returned null |
| 6639 | } finally { |
| 6640 | // Ensure that whatever happens, we clean up the identity state |
| 6641 | sCallerIdentity.remove(); |
| 6642 | } |
| 6643 | |
| 6644 | // 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] | 6645 | removeContentProviderExternalUnchecked(name, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6646 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6647 | 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] | 6648 | } |
| 6649 | return pfd; |
| 6650 | } |
| 6651 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6652 | // Actually is sleeping or shutting down or whatever else in the future |
| 6653 | // is an inactive state. |
| 6654 | public boolean isSleeping() { |
| 6655 | return mSleeping || mShuttingDown; |
| 6656 | } |
| 6657 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6658 | public void goingToSleep() { |
| 6659 | synchronized(this) { |
| 6660 | mSleeping = true; |
| 6661 | mWindowManager.setEventDispatching(false); |
| 6662 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6663 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6664 | |
| 6665 | // Initialize the wake times of all processes. |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6666 | checkExcessivePowerUsageLocked(false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6667 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6668 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6669 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6670 | } |
| 6671 | } |
| 6672 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6673 | public boolean shutdown(int timeout) { |
| 6674 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6675 | != PackageManager.PERMISSION_GRANTED) { |
| 6676 | throw new SecurityException("Requires permission " |
| 6677 | + android.Manifest.permission.SHUTDOWN); |
| 6678 | } |
| 6679 | |
| 6680 | boolean timedout = false; |
| 6681 | |
| 6682 | synchronized(this) { |
| 6683 | mShuttingDown = true; |
| 6684 | mWindowManager.setEventDispatching(false); |
| 6685 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6686 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6687 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6688 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6689 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 6690 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6691 | long delay = endTime - System.currentTimeMillis(); |
| 6692 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6693 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6694 | timedout = true; |
| 6695 | break; |
| 6696 | } |
| 6697 | try { |
| 6698 | this.wait(); |
| 6699 | } catch (InterruptedException e) { |
| 6700 | } |
| 6701 | } |
| 6702 | } |
| 6703 | } |
| 6704 | |
| 6705 | mUsageStatsService.shutdown(); |
| 6706 | mBatteryStatsService.shutdown(); |
| 6707 | |
| 6708 | return timedout; |
| 6709 | } |
| 6710 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6711 | public final void activitySlept(IBinder token) { |
| 6712 | if (localLOGV) Slog.v( |
| 6713 | TAG, "Activity slept: token=" + token); |
| 6714 | |
| 6715 | ActivityRecord r = null; |
| 6716 | |
| 6717 | final long origId = Binder.clearCallingIdentity(); |
| 6718 | |
| 6719 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6720 | r = mMainStack.isInStackLocked(token); |
| 6721 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6722 | mMainStack.activitySleptLocked(r); |
| 6723 | } |
| 6724 | } |
| 6725 | |
| 6726 | Binder.restoreCallingIdentity(origId); |
| 6727 | } |
| 6728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6729 | public void wakingUp() { |
| 6730 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6731 | mWindowManager.setEventDispatching(true); |
| 6732 | mSleeping = false; |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6733 | mMainStack.awakeFromSleepingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6734 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6735 | } |
| 6736 | } |
| 6737 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6738 | public void stopAppSwitches() { |
| 6739 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6740 | != PackageManager.PERMISSION_GRANTED) { |
| 6741 | throw new SecurityException("Requires permission " |
| 6742 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6743 | } |
| 6744 | |
| 6745 | synchronized(this) { |
| 6746 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 6747 | + APP_SWITCH_DELAY_TIME; |
| 6748 | mDidAppSwitch = false; |
| 6749 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6750 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6751 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 6752 | } |
| 6753 | } |
| 6754 | |
| 6755 | public void resumeAppSwitches() { |
| 6756 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6757 | != PackageManager.PERMISSION_GRANTED) { |
| 6758 | throw new SecurityException("Requires permission " |
| 6759 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6760 | } |
| 6761 | |
| 6762 | synchronized(this) { |
| 6763 | // Note that we don't execute any pending app switches... we will |
| 6764 | // let those wait until either the timeout, or the next start |
| 6765 | // activity request. |
| 6766 | mAppSwitchesAllowedTime = 0; |
| 6767 | } |
| 6768 | } |
| 6769 | |
| 6770 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 6771 | String name) { |
| 6772 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 6773 | return true; |
| 6774 | } |
| 6775 | |
| 6776 | final int perm = checkComponentPermission( |
| 6777 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6778 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6779 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 6780 | return true; |
| 6781 | } |
| 6782 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6783 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6784 | return false; |
| 6785 | } |
| 6786 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6787 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 6788 | boolean persistent) { |
| 6789 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 6790 | "setDebugApp()"); |
| 6791 | |
| 6792 | // Note that this is not really thread safe if there are multiple |
| 6793 | // callers into it at the same time, but that's not a situation we |
| 6794 | // care about. |
| 6795 | if (persistent) { |
| 6796 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6797 | Settings.System.putString( |
| 6798 | resolver, Settings.System.DEBUG_APP, |
| 6799 | packageName); |
| 6800 | Settings.System.putInt( |
| 6801 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 6802 | waitForDebugger ? 1 : 0); |
| 6803 | } |
| 6804 | |
| 6805 | synchronized (this) { |
| 6806 | if (!persistent) { |
| 6807 | mOrigDebugApp = mDebugApp; |
| 6808 | mOrigWaitForDebugger = mWaitForDebugger; |
| 6809 | } |
| 6810 | mDebugApp = packageName; |
| 6811 | mWaitForDebugger = waitForDebugger; |
| 6812 | mDebugTransient = !persistent; |
| 6813 | if (packageName != null) { |
| 6814 | final long origId = Binder.clearCallingIdentity(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6815 | forceStopPackageLocked(packageName, -1, false, false, true, true, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6816 | Binder.restoreCallingIdentity(origId); |
| 6817 | } |
| 6818 | } |
| 6819 | } |
| 6820 | |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 6821 | void setOpenGlTraceApp(ApplicationInfo app, String processName) { |
| 6822 | synchronized (this) { |
| 6823 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 6824 | if (!isDebuggable) { |
| 6825 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 6826 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 6827 | } |
| 6828 | } |
| 6829 | |
| 6830 | mOpenGlTraceApp = processName; |
| 6831 | } |
| 6832 | } |
| 6833 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 6834 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 6835 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 6836 | synchronized (this) { |
| 6837 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 6838 | if (!isDebuggable) { |
| 6839 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 6840 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 6841 | } |
| 6842 | } |
| 6843 | mProfileApp = processName; |
| 6844 | mProfileFile = profileFile; |
| 6845 | if (mProfileFd != null) { |
| 6846 | try { |
| 6847 | mProfileFd.close(); |
| 6848 | } catch (IOException e) { |
| 6849 | } |
| 6850 | mProfileFd = null; |
| 6851 | } |
| 6852 | mProfileFd = profileFd; |
| 6853 | mProfileType = 0; |
| 6854 | mAutoStopProfiler = autoStopProfiler; |
| 6855 | } |
| 6856 | } |
| 6857 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6858 | public void setAlwaysFinish(boolean enabled) { |
| 6859 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 6860 | "setAlwaysFinish()"); |
| 6861 | |
| 6862 | Settings.System.putInt( |
| 6863 | mContext.getContentResolver(), |
| 6864 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 6865 | |
| 6866 | synchronized (this) { |
| 6867 | mAlwaysFinishActivities = enabled; |
| 6868 | } |
| 6869 | } |
| 6870 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6871 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6872 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6873 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6874 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6875 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6876 | } |
| 6877 | } |
| 6878 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 6879 | public boolean isUserAMonkey() { |
| 6880 | // For now the fact that there is a controller implies |
| 6881 | // we have a monkey. |
| 6882 | synchronized (this) { |
| 6883 | return mController != null; |
| 6884 | } |
| 6885 | } |
| 6886 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6887 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 6888 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 6889 | "registerProcessObserver()"); |
| 6890 | synchronized (this) { |
| 6891 | mProcessObservers.register(observer); |
| 6892 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 6896 | synchronized (this) { |
| 6897 | mProcessObservers.unregister(observer); |
| 6898 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6899 | } |
| 6900 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6901 | public void setImmersive(IBinder token, boolean immersive) { |
| 6902 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6903 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6904 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6905 | throw new IllegalArgumentException(); |
| 6906 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6907 | r.immersive = immersive; |
| 6908 | } |
| 6909 | } |
| 6910 | |
| 6911 | public boolean isImmersive(IBinder token) { |
| 6912 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6913 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6914 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6915 | throw new IllegalArgumentException(); |
| 6916 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6917 | return r.immersive; |
| 6918 | } |
| 6919 | } |
| 6920 | |
| 6921 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6922 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6923 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6924 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6925 | return (r != null) ? r.immersive : false; |
| 6926 | } |
| 6927 | } |
| 6928 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6929 | public final void enterSafeMode() { |
| 6930 | synchronized(this) { |
| 6931 | // It only makes sense to do this before the system is ready |
| 6932 | // and started launching other packages. |
| 6933 | if (!mSystemReady) { |
| 6934 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6935 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6936 | } catch (RemoteException e) { |
| 6937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6938 | } |
| 6939 | } |
| 6940 | } |
| 6941 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 6942 | public final void showSafeModeOverlay() { |
| 6943 | View v = LayoutInflater.from(mContext).inflate( |
| 6944 | com.android.internal.R.layout.safe_mode, null); |
| 6945 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 6946 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 6947 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6948 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6949 | lp.gravity = Gravity.BOTTOM | Gravity.LEFT; |
| 6950 | lp.format = v.getBackground().getOpacity(); |
| 6951 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 6952 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 6953 | ((WindowManager)mContext.getSystemService( |
| 6954 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 6955 | } |
| 6956 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6957 | public void noteWakeupAlarm(IIntentSender sender) { |
| 6958 | if (!(sender instanceof PendingIntentRecord)) { |
| 6959 | return; |
| 6960 | } |
| 6961 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6962 | synchronized (stats) { |
| 6963 | if (mBatteryStatsService.isOnBattery()) { |
| 6964 | mBatteryStatsService.enforceCallingPermission(); |
| 6965 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 6966 | int MY_UID = Binder.getCallingUid(); |
| 6967 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 6968 | BatteryStatsImpl.Uid.Pkg pkg = |
| 6969 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 6970 | pkg.incWakeupsLocked(); |
| 6971 | } |
| 6972 | } |
| 6973 | } |
| 6974 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6975 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6976 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6977 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6978 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6979 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6980 | // XXX Note: don't acquire main activity lock here, because the window |
| 6981 | // manager calls in with its locks held. |
| 6982 | |
| 6983 | boolean killed = false; |
| 6984 | synchronized (mPidsSelfLocked) { |
| 6985 | int[] types = new int[pids.length]; |
| 6986 | int worstType = 0; |
| 6987 | for (int i=0; i<pids.length; i++) { |
| 6988 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6989 | if (proc != null) { |
| 6990 | int type = proc.setAdj; |
| 6991 | types[i] = type; |
| 6992 | if (type > worstType) { |
| 6993 | worstType = type; |
| 6994 | } |
| 6995 | } |
| 6996 | } |
| 6997 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6998 | // 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] | 6999 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7000 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 7001 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7002 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7003 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7004 | |
| 7005 | // If this is not a secure call, don't let it kill processes that |
| 7006 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7007 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 7008 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7009 | } |
| 7010 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7011 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7012 | for (int i=0; i<pids.length; i++) { |
| 7013 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7014 | if (proc == null) { |
| 7015 | continue; |
| 7016 | } |
| 7017 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7018 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7019 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7020 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 7021 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7022 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7023 | proc.killedBackground = true; |
| 7024 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7025 | } |
| 7026 | } |
| 7027 | } |
| 7028 | return killed; |
| 7029 | } |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7030 | |
| 7031 | @Override |
| 7032 | public boolean killProcessesBelowForeground(String reason) { |
| 7033 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7034 | throw new SecurityException("killProcessesBelowForeground() only available to system"); |
| 7035 | } |
| 7036 | |
| 7037 | return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason); |
| 7038 | } |
| 7039 | |
| 7040 | private boolean killProcessesBelowAdj(int belowAdj, String reason) { |
| 7041 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7042 | throw new SecurityException("killProcessesBelowAdj() only available to system"); |
| 7043 | } |
| 7044 | |
| 7045 | boolean killed = false; |
| 7046 | synchronized (mPidsSelfLocked) { |
| 7047 | final int size = mPidsSelfLocked.size(); |
| 7048 | for (int i = 0; i < size; i++) { |
| 7049 | final int pid = mPidsSelfLocked.keyAt(i); |
| 7050 | final ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7051 | if (proc == null) continue; |
| 7052 | |
| 7053 | final int adj = proc.setAdj; |
| 7054 | if (adj > belowAdj && !proc.killedBackground) { |
| 7055 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
| 7056 | EventLog.writeEvent( |
| 7057 | EventLogTags.AM_KILL, proc.pid, proc.processName, adj, reason); |
| 7058 | killed = true; |
| 7059 | proc.killedBackground = true; |
| 7060 | Process.killProcessQuiet(pid); |
| 7061 | } |
| 7062 | } |
| 7063 | } |
| 7064 | return killed; |
| 7065 | } |
| 7066 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7067 | public final void startRunning(String pkg, String cls, String action, |
| 7068 | String data) { |
| 7069 | synchronized(this) { |
| 7070 | if (mStartRunning) { |
| 7071 | return; |
| 7072 | } |
| 7073 | mStartRunning = true; |
| 7074 | mTopComponent = pkg != null && cls != null |
| 7075 | ? new ComponentName(pkg, cls) : null; |
| 7076 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 7077 | mTopData = data; |
| 7078 | if (!mSystemReady) { |
| 7079 | return; |
| 7080 | } |
| 7081 | } |
| 7082 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7083 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7084 | } |
| 7085 | |
| 7086 | private void retrieveSettings() { |
| 7087 | final ContentResolver resolver = mContext.getContentResolver(); |
| 7088 | String debugApp = Settings.System.getString( |
| 7089 | resolver, Settings.System.DEBUG_APP); |
| 7090 | boolean waitForDebugger = Settings.System.getInt( |
| 7091 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 7092 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 7093 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 7094 | |
| 7095 | Configuration configuration = new Configuration(); |
| 7096 | Settings.System.getConfiguration(resolver, configuration); |
| 7097 | |
| 7098 | synchronized (this) { |
| 7099 | mDebugApp = mOrigDebugApp = debugApp; |
| 7100 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 7101 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 7102 | // This happens before any activities are started, so we can |
| 7103 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 7104 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7105 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7106 | } |
| 7107 | } |
| 7108 | |
| 7109 | public boolean testIsSystemReady() { |
| 7110 | // no need to synchronize(this) just to read & return the value |
| 7111 | return mSystemReady; |
| 7112 | } |
| 7113 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7114 | private static File getCalledPreBootReceiversFile() { |
| 7115 | File dataDir = Environment.getDataDirectory(); |
| 7116 | File systemDir = new File(dataDir, "system"); |
| 7117 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 7118 | return fname; |
| 7119 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7120 | |
| 7121 | static final int LAST_DONE_VERSION = 10000; |
| 7122 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7123 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 7124 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 7125 | File file = getCalledPreBootReceiversFile(); |
| 7126 | FileInputStream fis = null; |
| 7127 | try { |
| 7128 | fis = new FileInputStream(file); |
| 7129 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7130 | int fvers = dis.readInt(); |
| 7131 | if (fvers == LAST_DONE_VERSION) { |
| 7132 | String vers = dis.readUTF(); |
| 7133 | String codename = dis.readUTF(); |
| 7134 | String build = dis.readUTF(); |
| 7135 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 7136 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 7137 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 7138 | int num = dis.readInt(); |
| 7139 | while (num > 0) { |
| 7140 | num--; |
| 7141 | String pkg = dis.readUTF(); |
| 7142 | String cls = dis.readUTF(); |
| 7143 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 7144 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7145 | } |
| 7146 | } |
| 7147 | } catch (FileNotFoundException e) { |
| 7148 | } catch (IOException e) { |
| 7149 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 7150 | } finally { |
| 7151 | if (fis != null) { |
| 7152 | try { |
| 7153 | fis.close(); |
| 7154 | } catch (IOException e) { |
| 7155 | } |
| 7156 | } |
| 7157 | } |
| 7158 | return lastDoneReceivers; |
| 7159 | } |
| 7160 | |
| 7161 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 7162 | File file = getCalledPreBootReceiversFile(); |
| 7163 | FileOutputStream fos = null; |
| 7164 | DataOutputStream dos = null; |
| 7165 | try { |
| 7166 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 7167 | fos = new FileOutputStream(file); |
| 7168 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7169 | dos.writeInt(LAST_DONE_VERSION); |
| 7170 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7171 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7172 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7173 | dos.writeInt(list.size()); |
| 7174 | for (int i=0; i<list.size(); i++) { |
| 7175 | dos.writeUTF(list.get(i).getPackageName()); |
| 7176 | dos.writeUTF(list.get(i).getClassName()); |
| 7177 | } |
| 7178 | } catch (IOException e) { |
| 7179 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 7180 | file.delete(); |
| 7181 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 7182 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7183 | if (dos != null) { |
| 7184 | try { |
| 7185 | dos.close(); |
| 7186 | } catch (IOException e) { |
| 7187 | // TODO Auto-generated catch block |
| 7188 | e.printStackTrace(); |
| 7189 | } |
| 7190 | } |
| 7191 | } |
| 7192 | } |
| 7193 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7194 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7195 | synchronized(this) { |
| 7196 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7197 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7198 | return; |
| 7199 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7200 | |
| 7201 | // Check to see if there are any update receivers to run. |
| 7202 | if (!mDidUpdate) { |
| 7203 | if (mWaitingUpdate) { |
| 7204 | return; |
| 7205 | } |
| 7206 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 7207 | List<ResolveInfo> ris = null; |
| 7208 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7209 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7210 | intent, null, 0, 0); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7211 | } catch (RemoteException e) { |
| 7212 | } |
| 7213 | if (ris != null) { |
| 7214 | for (int i=ris.size()-1; i>=0; i--) { |
| 7215 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 7216 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7217 | ris.remove(i); |
| 7218 | } |
| 7219 | } |
| 7220 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7221 | |
| 7222 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 7223 | |
| 7224 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7225 | for (int i=0; i<ris.size(); i++) { |
| 7226 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7227 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7228 | if (lastDoneReceivers.contains(comp)) { |
| 7229 | ris.remove(i); |
| 7230 | i--; |
| 7231 | } |
| 7232 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 7233 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7234 | for (int i=0; i<ris.size(); i++) { |
| 7235 | ActivityInfo ai = ris.get(i).activityInfo; |
| 7236 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7237 | doneReceivers.add(comp); |
| 7238 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7239 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 7240 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7241 | finisher = new IIntentReceiver.Stub() { |
| 7242 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 7243 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7244 | boolean sticky) { |
| 7245 | // The raw IIntentReceiver interface is called |
| 7246 | // with the AM lock held, so redispatch to |
| 7247 | // execute our code without the lock. |
| 7248 | mHandler.post(new Runnable() { |
| 7249 | public void run() { |
| 7250 | synchronized (ActivityManagerService.this) { |
| 7251 | mDidUpdate = true; |
| 7252 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7253 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7254 | showBootMessage(mContext.getText( |
| 7255 | R.string.android_upgrading_complete), |
| 7256 | false); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7257 | systemReady(goingCallback); |
| 7258 | } |
| 7259 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7260 | } |
| 7261 | }; |
| 7262 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7263 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 7264 | /* TODO: Send this to all users */ |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7265 | broadcastIntentLocked(null, null, intent, null, finisher, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 7266 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
Amith Yamasani | c600e21f | 2012-02-14 16:08:07 -0800 | [diff] [blame] | 7267 | 0 /* UserId zero */); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 7268 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7269 | mWaitingUpdate = true; |
| 7270 | } |
| 7271 | } |
| 7272 | } |
| 7273 | if (mWaitingUpdate) { |
| 7274 | return; |
| 7275 | } |
| 7276 | mDidUpdate = true; |
| 7277 | } |
| 7278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7279 | mSystemReady = true; |
| 7280 | if (!mStartRunning) { |
| 7281 | return; |
| 7282 | } |
| 7283 | } |
| 7284 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7285 | ArrayList<ProcessRecord> procsToKill = null; |
| 7286 | synchronized(mPidsSelfLocked) { |
| 7287 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7288 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7289 | if (!isAllowedWhileBooting(proc.info)){ |
| 7290 | if (procsToKill == null) { |
| 7291 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7292 | } |
| 7293 | procsToKill.add(proc); |
| 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7298 | synchronized(this) { |
| 7299 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7300 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7301 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7302 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7303 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7304 | } |
| 7305 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7306 | |
| 7307 | // Now that we have cleaned up any update processes, we |
| 7308 | // are ready to start launching real processes and know that |
| 7309 | // we won't trample on them any more. |
| 7310 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7311 | } |
| 7312 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7313 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7314 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7315 | SystemClock.uptimeMillis()); |
| 7316 | |
| 7317 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7318 | // Make sure we have no pre-ready processes sitting around. |
| 7319 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7320 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7321 | ResolveInfo ri = mContext.getPackageManager() |
| 7322 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7323 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7324 | CharSequence errorMsg = null; |
| 7325 | if (ri != null) { |
| 7326 | ActivityInfo ai = ri.activityInfo; |
| 7327 | ApplicationInfo app = ai.applicationInfo; |
| 7328 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7329 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7330 | mTopData = null; |
| 7331 | mTopComponent = new ComponentName(app.packageName, |
| 7332 | ai.name); |
| 7333 | } else { |
| 7334 | errorMsg = mContext.getResources().getText( |
| 7335 | com.android.internal.R.string.factorytest_not_system); |
| 7336 | } |
| 7337 | } else { |
| 7338 | errorMsg = mContext.getResources().getText( |
| 7339 | com.android.internal.R.string.factorytest_no_action); |
| 7340 | } |
| 7341 | if (errorMsg != null) { |
| 7342 | mTopAction = null; |
| 7343 | mTopData = null; |
| 7344 | mTopComponent = null; |
| 7345 | Message msg = Message.obtain(); |
| 7346 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7347 | msg.getData().putCharSequence("msg", errorMsg); |
| 7348 | mHandler.sendMessage(msg); |
| 7349 | } |
| 7350 | } |
| 7351 | } |
| 7352 | |
| 7353 | retrieveSettings(); |
| 7354 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7355 | if (goingCallback != null) goingCallback.run(); |
| 7356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7357 | synchronized (this) { |
| 7358 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7359 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7360 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7361 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7362 | if (apps != null) { |
| 7363 | int N = apps.size(); |
| 7364 | int i; |
| 7365 | for (i=0; i<N; i++) { |
| 7366 | ApplicationInfo info |
| 7367 | = (ApplicationInfo)apps.get(i); |
| 7368 | if (info != null && |
| 7369 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7370 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7371 | } |
| 7372 | } |
| 7373 | } |
| 7374 | } catch (RemoteException ex) { |
| 7375 | // pm is in same process, this will never happen. |
| 7376 | } |
| 7377 | } |
| 7378 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7379 | // Start up initial activity. |
| 7380 | mBooting = true; |
| 7381 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7382 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7383 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7384 | Message msg = Message.obtain(); |
| 7385 | msg.what = SHOW_UID_ERROR_MSG; |
| 7386 | mHandler.sendMessage(msg); |
| 7387 | } |
| 7388 | } catch (RemoteException e) { |
| 7389 | } |
| 7390 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7391 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7392 | } |
| 7393 | } |
| 7394 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7395 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7396 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7397 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7398 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7399 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7400 | startAppProblemLocked(app); |
| 7401 | app.stopFreezingAllLocked(); |
| 7402 | return handleAppCrashLocked(app); |
| 7403 | } |
| 7404 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7405 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7406 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7407 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7408 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7409 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7410 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7411 | startAppProblemLocked(app); |
| 7412 | app.stopFreezingAllLocked(); |
| 7413 | } |
| 7414 | |
| 7415 | /** |
| 7416 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7417 | * |
| 7418 | * @param app The ProcessRecord in which the error occurred. |
| 7419 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7420 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7421 | * @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] | 7422 | * @param shortMsg Short message describing the crash. |
| 7423 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7424 | * @param stackTrace Full crash stack trace, may be null. |
| 7425 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7426 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7427 | */ |
| 7428 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7429 | 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] | 7430 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7431 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7432 | report.condition = condition; |
| 7433 | report.processName = app.processName; |
| 7434 | report.pid = app.pid; |
| 7435 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7436 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7437 | report.shortMsg = shortMsg; |
| 7438 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7439 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7440 | |
| 7441 | return report; |
| 7442 | } |
| 7443 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7444 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7445 | synchronized (this) { |
| 7446 | app.crashing = false; |
| 7447 | app.crashingReport = null; |
| 7448 | app.notResponding = false; |
| 7449 | app.notRespondingReport = null; |
| 7450 | if (app.anrDialog == fromDialog) { |
| 7451 | app.anrDialog = null; |
| 7452 | } |
| 7453 | if (app.waitDialog == fromDialog) { |
| 7454 | app.waitDialog = null; |
| 7455 | } |
| 7456 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7457 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7458 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7459 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 7460 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7461 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7462 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7463 | } |
| 7464 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7465 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7466 | private boolean handleAppCrashLocked(ProcessRecord app) { |
Mike Lockwood | 86548c4 | 2011-09-13 17:21:46 -0400 | [diff] [blame] | 7467 | if (mHeadless) { |
| 7468 | Log.e(TAG, "handleAppCrashLocked: " + app.processName); |
| 7469 | return false; |
| 7470 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7471 | long now = SystemClock.uptimeMillis(); |
| 7472 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7473 | Long crashTime; |
| 7474 | if (!app.isolated) { |
| 7475 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 7476 | } else { |
| 7477 | crashTime = null; |
| 7478 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7479 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7480 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7481 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7482 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7483 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7484 | app.info.processName, app.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7485 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 7486 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7487 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7488 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7489 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7490 | 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] | 7491 | } |
| 7492 | } |
| 7493 | if (!app.persistent) { |
| 7494 | // We don't want to start this process again until the user |
| 7495 | // explicitly does so... but for persistent process, we really |
| 7496 | // need to keep it running. If a persistent process is actually |
| 7497 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7498 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7499 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7500 | if (!app.isolated) { |
| 7501 | // XXX We don't have a way to mark isolated processes |
| 7502 | // as bad, since they don't have a peristent identity. |
| 7503 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 7504 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 7505 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7506 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7507 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 7508 | // Don't let services in this process be restarted and potentially |
| 7509 | // annoy the user repeatedly. Unless it is persistent, since those |
| 7510 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7511 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7512 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7513 | return false; |
| 7514 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7515 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7516 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7517 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Kevin Hester-Chow | d87a9be | 2012-03-05 08:01:00 -0800 | [diff] [blame] | 7518 | if (r != null && r.app == app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7519 | // If the top running activity is from this crashing |
| 7520 | // process, then terminate it to avoid getting in a loop. |
| 7521 | Slog.w(TAG, " Force finishing activity " |
| 7522 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 7523 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7524 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7525 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 7526 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7527 | // stopped, to avoid a situation where one will get |
| 7528 | // re-start our crashing activity once it gets resumed again. |
| 7529 | index--; |
| 7530 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7531 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7532 | if (r.state == ActivityState.RESUMED |
| 7533 | || r.state == ActivityState.PAUSING |
| 7534 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 7535 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7536 | Slog.w(TAG, " Force finishing activity " |
| 7537 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7538 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7539 | Activity.RESULT_CANCELED, null, "crashed"); |
| 7540 | } |
| 7541 | } |
| 7542 | } |
| 7543 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7544 | } |
| 7545 | |
| 7546 | // Bump up the crash count of any services currently running in the proc. |
| 7547 | if (app.services.size() != 0) { |
| 7548 | // Any services running in the application need to be placed |
| 7549 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7550 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7551 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7552 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7553 | sr.crashCount++; |
| 7554 | } |
| 7555 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7556 | |
| 7557 | // If the crashing process is what we consider to be the "home process" and it has been |
| 7558 | // replaced by a third-party app, clear the package preferred activities from packages |
| 7559 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 7560 | // from blocking the user to manually clear the list. |
| 7561 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 7562 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7563 | Iterator it = mHomeProcess.activities.iterator(); |
| 7564 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 7565 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7566 | if (r.isHomeActivity) { |
| 7567 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 7568 | try { |
| 7569 | ActivityThread.getPackageManager() |
| 7570 | .clearPackagePreferredActivities(r.packageName); |
| 7571 | } catch (RemoteException c) { |
| 7572 | // pm is in same process, this will never happen. |
| 7573 | } |
| 7574 | } |
| 7575 | } |
| 7576 | } |
| 7577 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7578 | if (!app.isolated) { |
| 7579 | // XXX Can't keep track of crash times for isolated processes, |
| 7580 | // because they don't have a perisistent identity. |
| 7581 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 7582 | } |
| 7583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7584 | return true; |
| 7585 | } |
| 7586 | |
| 7587 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7588 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 7589 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7590 | skipCurrentReceiverLocked(app); |
| 7591 | } |
| 7592 | |
| 7593 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 7594 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 7595 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7596 | } |
| 7597 | } |
| 7598 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7599 | /** |
| 7600 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 7601 | * The application process will exit immediately after this call returns. |
| 7602 | * @param app object of the crashing app, null for the system server |
| 7603 | * @param crashInfo describing the exception |
| 7604 | */ |
| 7605 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7606 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7607 | final String processName = app == null ? "system_server" |
| 7608 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7609 | |
| 7610 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7611 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7612 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7613 | crashInfo.exceptionClassName, |
| 7614 | crashInfo.exceptionMessage, |
| 7615 | crashInfo.throwFileName, |
| 7616 | crashInfo.throwLineNumber); |
| 7617 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7618 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7619 | |
| 7620 | crashApplication(r, crashInfo); |
| 7621 | } |
| 7622 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7623 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7624 | IBinder app, |
| 7625 | int violationMask, |
| 7626 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7627 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 7628 | if (r == null) { |
| 7629 | return; |
| 7630 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7631 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7632 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 7633 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7634 | boolean logIt = true; |
| 7635 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 7636 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 7637 | logIt = false; |
| 7638 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7639 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7640 | // the relative pain numbers, without logging all |
| 7641 | // the stack traces repeatedly. We'd want to do |
| 7642 | // likewise in the client code, which also does |
| 7643 | // dup suppression, before the Binder call. |
| 7644 | } else { |
| 7645 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 7646 | mAlreadyLoggedViolatedStacks.clear(); |
| 7647 | } |
| 7648 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 7649 | } |
| 7650 | } |
| 7651 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7652 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7653 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7654 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7655 | |
| 7656 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 7657 | AppErrorResult result = new AppErrorResult(); |
| 7658 | synchronized (this) { |
| 7659 | final long origId = Binder.clearCallingIdentity(); |
| 7660 | |
| 7661 | Message msg = Message.obtain(); |
| 7662 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 7663 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 7664 | data.put("result", result); |
| 7665 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7666 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7667 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7668 | msg.obj = data; |
| 7669 | mHandler.sendMessage(msg); |
| 7670 | |
| 7671 | Binder.restoreCallingIdentity(origId); |
| 7672 | } |
| 7673 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 7674 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7675 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7676 | } |
| 7677 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7678 | // Depending on the policy in effect, there could be a bunch of |
| 7679 | // these in quick succession so we try to batch these together to |
| 7680 | // minimize disk writes, number of dropbox entries, and maximize |
| 7681 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7682 | private void logStrictModeViolationToDropBox( |
| 7683 | ProcessRecord process, |
| 7684 | StrictMode.ViolationInfo info) { |
| 7685 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7686 | return; |
| 7687 | } |
| 7688 | final boolean isSystemApp = process == null || |
| 7689 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 7690 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7691 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7692 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 7693 | final DropBoxManager dbox = (DropBoxManager) |
| 7694 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7695 | |
| 7696 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7697 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7698 | |
| 7699 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7700 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7701 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 7702 | synchronized (sb) { |
| 7703 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7704 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7705 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 7706 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7707 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 7708 | if (info.violationNumThisLoop != 0) { |
| 7709 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 7710 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 7711 | if (info.numAnimationsRunning != 0) { |
| 7712 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 7713 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 7714 | if (info.broadcastIntentAction != null) { |
| 7715 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 7716 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7717 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7718 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7719 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 7720 | if (info.numInstances != -1) { |
| 7721 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 7722 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7723 | if (info.tags != null) { |
| 7724 | for (String tag : info.tags) { |
| 7725 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 7726 | } |
| 7727 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7728 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7729 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 7730 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7731 | } |
| 7732 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7733 | |
| 7734 | // Only buffer up to ~64k. Various logging bits truncate |
| 7735 | // things at 128k. |
| 7736 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7737 | } |
| 7738 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7739 | // Flush immediately if the buffer's grown too large, or this |
| 7740 | // is a non-system app. Non-system apps are isolated with a |
| 7741 | // different tag & policy and not batched. |
| 7742 | // |
| 7743 | // Batching is useful during internal testing with |
| 7744 | // StrictMode settings turned up high. Without batching, |
| 7745 | // thousands of separate files could be created on boot. |
| 7746 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7747 | new Thread("Error dump: " + dropboxTag) { |
| 7748 | @Override |
| 7749 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7750 | String report; |
| 7751 | synchronized (sb) { |
| 7752 | report = sb.toString(); |
| 7753 | sb.delete(0, sb.length()); |
| 7754 | sb.trimToSize(); |
| 7755 | } |
| 7756 | if (report.length() != 0) { |
| 7757 | dbox.addText(dropboxTag, report); |
| 7758 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7759 | } |
| 7760 | }.start(); |
| 7761 | return; |
| 7762 | } |
| 7763 | |
| 7764 | // System app batching: |
| 7765 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7766 | // An existing dropbox-writing thread is outstanding, so |
| 7767 | // we don't need to start it up. The existing thread will |
| 7768 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7769 | return; |
| 7770 | } |
| 7771 | |
| 7772 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 7773 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7774 | new Thread("Error dump: " + dropboxTag) { |
| 7775 | @Override |
| 7776 | public void run() { |
| 7777 | // 5 second sleep to let stacks arrive and be batched together |
| 7778 | try { |
| 7779 | Thread.sleep(5000); // 5 seconds |
| 7780 | } catch (InterruptedException e) {} |
| 7781 | |
| 7782 | String errorReport; |
| 7783 | synchronized (mStrictModeBuffer) { |
| 7784 | errorReport = mStrictModeBuffer.toString(); |
| 7785 | if (errorReport.length() == 0) { |
| 7786 | return; |
| 7787 | } |
| 7788 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 7789 | mStrictModeBuffer.trimToSize(); |
| 7790 | } |
| 7791 | dbox.addText(dropboxTag, errorReport); |
| 7792 | } |
| 7793 | }.start(); |
| 7794 | } |
| 7795 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7796 | /** |
| 7797 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 7798 | * @param app object of the crashing app, null for the system server |
| 7799 | * @param tag reported by the caller |
| 7800 | * @param crashInfo describing the context of the error |
| 7801 | * @return true if the process should exit immediately (WTF is fatal) |
| 7802 | */ |
| 7803 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7804 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7805 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7806 | final String processName = app == null ? "system_server" |
| 7807 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7808 | |
| 7809 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7810 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7811 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7812 | tag, crashInfo.exceptionMessage); |
| 7813 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7814 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7815 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 7816 | if (r != null && r.pid != Process.myPid() && |
| 7817 | Settings.Secure.getInt(mContext.getContentResolver(), |
| 7818 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7819 | crashApplication(r, crashInfo); |
| 7820 | return true; |
| 7821 | } else { |
| 7822 | return false; |
| 7823 | } |
| 7824 | } |
| 7825 | |
| 7826 | /** |
| 7827 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 7828 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 7829 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7830 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7831 | if (app == null) { |
| 7832 | return null; |
| 7833 | } |
| 7834 | |
| 7835 | synchronized (this) { |
| 7836 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 7837 | final int NA = apps.size(); |
| 7838 | for (int ia=0; ia<NA; ia++) { |
| 7839 | ProcessRecord p = apps.valueAt(ia); |
| 7840 | if (p.thread != null && p.thread.asBinder() == app) { |
| 7841 | return p; |
| 7842 | } |
| 7843 | } |
| 7844 | } |
| 7845 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7846 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 7847 | + " from pid=" + Binder.getCallingPid() |
| 7848 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7849 | return null; |
| 7850 | } |
| 7851 | } |
| 7852 | |
| 7853 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7854 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 7855 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7856 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7857 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 7858 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7859 | // Watchdog thread ends up invoking this function (with |
| 7860 | // a null ProcessRecord) to add the stack file to dropbox. |
| 7861 | // Do not acquire a lock on this (am) in such cases, as it |
| 7862 | // could cause a potential deadlock, if and when watchdog |
| 7863 | // is invoked due to unavailability of lock on am and it |
| 7864 | // would prevent watchdog from killing system_server. |
| 7865 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7866 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7867 | return; |
| 7868 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7869 | // Note: ProcessRecord 'process' is guarded by the service |
| 7870 | // instance. (notably process.pkgList, which could otherwise change |
| 7871 | // concurrently during execution of this method) |
| 7872 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7873 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7874 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7875 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7876 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 7877 | for (String pkg : process.pkgList) { |
| 7878 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7879 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7880 | PackageInfo pi = pm.getPackageInfo(pkg, 0, 0); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7881 | if (pi != null) { |
| 7882 | sb.append(" v").append(pi.versionCode); |
| 7883 | if (pi.versionName != null) { |
| 7884 | sb.append(" (").append(pi.versionName).append(")"); |
| 7885 | } |
| 7886 | } |
| 7887 | } catch (RemoteException e) { |
| 7888 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7889 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7890 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7891 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7892 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7893 | } |
| 7894 | |
| 7895 | private static String processClass(ProcessRecord process) { |
| 7896 | if (process == null || process.pid == MY_PID) { |
| 7897 | return "system_server"; |
| 7898 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7899 | return "system_app"; |
| 7900 | } else { |
| 7901 | return "data_app"; |
| 7902 | } |
| 7903 | } |
| 7904 | |
| 7905 | /** |
| 7906 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 7907 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 7908 | * @param process which caused the error, null means the system server |
| 7909 | * @param activity which triggered the error, null if unknown |
| 7910 | * @param parent activity related to the error, null if unknown |
| 7911 | * @param subject line related to the error, null if absent |
| 7912 | * @param report in long form describing the error, null if absent |
| 7913 | * @param logFile to include in the report, null if none |
| 7914 | * @param crashInfo giving an application stack trace, null if absent |
| 7915 | */ |
| 7916 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7917 | ProcessRecord process, String processName, ActivityRecord activity, |
| 7918 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7919 | final String report, final File logFile, |
| 7920 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 7921 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 7922 | // otherwise the watchdog may be prevented from resetting the system. |
| 7923 | |
| 7924 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 7925 | final DropBoxManager dbox = (DropBoxManager) |
| 7926 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7927 | |
| 7928 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7929 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7930 | |
| 7931 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7932 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7933 | if (activity != null) { |
| 7934 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 7935 | } |
| 7936 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 7937 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 7938 | } |
| 7939 | if (parent != null && parent != activity) { |
| 7940 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 7941 | } |
| 7942 | if (subject != null) { |
| 7943 | sb.append("Subject: ").append(subject).append("\n"); |
| 7944 | } |
| 7945 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 7946 | if (Debug.isDebuggerConnected()) { |
| 7947 | sb.append("Debugger: Connected\n"); |
| 7948 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7949 | sb.append("\n"); |
| 7950 | |
| 7951 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 7952 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7953 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 7954 | @Override |
| 7955 | public void run() { |
| 7956 | if (report != null) { |
| 7957 | sb.append(report); |
| 7958 | } |
| 7959 | if (logFile != null) { |
| 7960 | try { |
| 7961 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 7962 | } catch (IOException e) { |
| 7963 | Slog.e(TAG, "Error reading " + logFile, e); |
| 7964 | } |
| 7965 | } |
| 7966 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 7967 | sb.append(crashInfo.stackTrace); |
| 7968 | } |
| 7969 | |
| 7970 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 7971 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 7972 | if (lines > 0) { |
| 7973 | sb.append("\n"); |
| 7974 | |
| 7975 | // Merge several logcat streams, and take the last N lines |
| 7976 | InputStreamReader input = null; |
| 7977 | try { |
| 7978 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 7979 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 7980 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 7981 | |
| 7982 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 7983 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 7984 | input = new InputStreamReader(logcat.getInputStream()); |
| 7985 | |
| 7986 | int num; |
| 7987 | char[] buf = new char[8192]; |
| 7988 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 7989 | } catch (IOException e) { |
| 7990 | Slog.e(TAG, "Error running logcat", e); |
| 7991 | } finally { |
| 7992 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 7993 | } |
| 7994 | } |
| 7995 | |
| 7996 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7997 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7998 | }; |
| 7999 | |
| 8000 | if (process == null || process.pid == MY_PID) { |
| 8001 | worker.run(); // We may be about to die -- need to run this synchronously |
| 8002 | } else { |
| 8003 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8004 | } |
| 8005 | } |
| 8006 | |
| 8007 | /** |
| 8008 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 8009 | * Deal with edge cases (intercepts from instrumented applications, |
| 8010 | * ActivityController, error intent receivers, that sort of thing). |
| 8011 | * @param r the application crashing |
| 8012 | * @param crashInfo describing the failure |
| 8013 | */ |
| 8014 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8015 | long timeMillis = System.currentTimeMillis(); |
| 8016 | String shortMsg = crashInfo.exceptionClassName; |
| 8017 | String longMsg = crashInfo.exceptionMessage; |
| 8018 | String stackTrace = crashInfo.stackTrace; |
| 8019 | if (shortMsg != null && longMsg != null) { |
| 8020 | longMsg = shortMsg + ": " + longMsg; |
| 8021 | } else if (shortMsg != null) { |
| 8022 | longMsg = shortMsg; |
| 8023 | } |
| 8024 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8025 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8026 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8027 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8028 | try { |
| 8029 | String name = r != null ? r.processName : null; |
| 8030 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8031 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8032 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8033 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8034 | + " at watcher's request"); |
| 8035 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8036 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8037 | } |
| 8038 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8039 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8040 | } |
| 8041 | } |
| 8042 | |
| 8043 | final long origId = Binder.clearCallingIdentity(); |
| 8044 | |
| 8045 | // If this process is running instrumentation, finish it. |
| 8046 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8047 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8048 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8049 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 8050 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8051 | Bundle info = new Bundle(); |
| 8052 | info.putString("shortMsg", shortMsg); |
| 8053 | info.putString("longMsg", longMsg); |
| 8054 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 8055 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8056 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8057 | } |
| 8058 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8059 | // If we can't identify the process or it's already exceeded its crash quota, |
| 8060 | // quit right away without showing a crash dialog. |
| 8061 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8062 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8063 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8064 | } |
| 8065 | |
| 8066 | Message msg = Message.obtain(); |
| 8067 | msg.what = SHOW_ERROR_MSG; |
| 8068 | HashMap data = new HashMap(); |
| 8069 | data.put("result", result); |
| 8070 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8071 | msg.obj = data; |
| 8072 | mHandler.sendMessage(msg); |
| 8073 | |
| 8074 | Binder.restoreCallingIdentity(origId); |
| 8075 | } |
| 8076 | |
| 8077 | int res = result.get(); |
| 8078 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8079 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8080 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8081 | if (r != null && !r.isolated) { |
| 8082 | // XXX Can't keep track of crash time for isolated processes, |
| 8083 | // since they don't have a persistent identity. |
| 8084 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8085 | SystemClock.uptimeMillis()); |
| 8086 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8087 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8088 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8089 | } |
| 8090 | } |
| 8091 | |
| 8092 | if (appErrorIntent != null) { |
| 8093 | try { |
| 8094 | mContext.startActivity(appErrorIntent); |
| 8095 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8096 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8097 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8098 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8099 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8100 | |
| 8101 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 8102 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 8103 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8104 | if (report == null) { |
| 8105 | return null; |
| 8106 | } |
| 8107 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 8108 | result.setComponent(r.errorReportReceiver); |
| 8109 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 8110 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 8111 | return result; |
| 8112 | } |
| 8113 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8114 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 8115 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8116 | if (r.errorReportReceiver == null) { |
| 8117 | return null; |
| 8118 | } |
| 8119 | |
| 8120 | if (!r.crashing && !r.notResponding) { |
| 8121 | return null; |
| 8122 | } |
| 8123 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8124 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 8125 | report.packageName = r.info.packageName; |
| 8126 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 8127 | report.processName = r.processName; |
| 8128 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 8129 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8130 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8131 | if (r.crashing) { |
| 8132 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 8133 | report.crashInfo = crashInfo; |
| 8134 | } else if (r.notResponding) { |
| 8135 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 8136 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8137 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8138 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 8139 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 8140 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8141 | } |
| 8142 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8143 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8144 | } |
| 8145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8146 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8147 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8148 | // assume our apps are happy - lazy create the list |
| 8149 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 8150 | |
| 8151 | synchronized (this) { |
| 8152 | |
| 8153 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8154 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8155 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8156 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 8157 | // This one's in trouble, so we'll generate a report for it |
| 8158 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 8159 | ActivityManager.ProcessErrorStateInfo report = null; |
| 8160 | if (app.crashing) { |
| 8161 | report = app.crashingReport; |
| 8162 | } else if (app.notResponding) { |
| 8163 | report = app.notRespondingReport; |
| 8164 | } |
| 8165 | |
| 8166 | if (report != null) { |
| 8167 | if (errList == null) { |
| 8168 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 8169 | } |
| 8170 | errList.add(report); |
| 8171 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8172 | 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] | 8173 | " crashing = " + app.crashing + |
| 8174 | " notResponding = " + app.notResponding); |
| 8175 | } |
| 8176 | } |
| 8177 | } |
| 8178 | } |
| 8179 | |
| 8180 | return errList; |
| 8181 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8182 | |
| 8183 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8184 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8185 | if (currApp != null) { |
| 8186 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 8187 | } |
| 8188 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8189 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 8190 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8191 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 8192 | if (currApp != null) { |
| 8193 | currApp.lru = 0; |
| 8194 | } |
| 8195 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8196 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8197 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 8198 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8199 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 8200 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8201 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 8202 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8203 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 8204 | } else { |
| 8205 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 8206 | } |
| 8207 | } |
| 8208 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8209 | private void fillInProcMemInfo(ProcessRecord app, |
| 8210 | ActivityManager.RunningAppProcessInfo outInfo) { |
| 8211 | outInfo.pid = app.pid; |
| 8212 | outInfo.uid = app.info.uid; |
| 8213 | if (mHeavyWeightProcess == app) { |
| 8214 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
| 8215 | } |
| 8216 | if (app.persistent) { |
| 8217 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 8218 | } |
| 8219 | outInfo.lastTrimLevel = app.trimMemoryLevel; |
| 8220 | int adj = app.curAdj; |
| 8221 | outInfo.importance = oomAdjToImportance(adj, outInfo); |
| 8222 | outInfo.importanceReasonCode = app.adjTypeCode; |
| 8223 | } |
| 8224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8225 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8226 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8227 | // Lazy instantiation of list |
| 8228 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
| 8229 | synchronized (this) { |
| 8230 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8231 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8232 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8233 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 8234 | // Generate process state info for running application |
| 8235 | ActivityManager.RunningAppProcessInfo currApp = |
| 8236 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 8237 | app.pid, app.getPackageList()); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8238 | fillInProcMemInfo(app, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8239 | if (app.adjSource instanceof ProcessRecord) { |
| 8240 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8241 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 8242 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8243 | } else if (app.adjSource instanceof ActivityRecord) { |
| 8244 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8245 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 8246 | } |
| 8247 | if (app.adjTarget instanceof ComponentName) { |
| 8248 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 8249 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8250 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8251 | // + " lru=" + currApp.lru); |
| 8252 | if (runList == null) { |
| 8253 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 8254 | } |
| 8255 | runList.add(currApp); |
| 8256 | } |
| 8257 | } |
| 8258 | } |
| 8259 | return runList; |
| 8260 | } |
| 8261 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8262 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8263 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8264 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8265 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8266 | if (runningApps != null && runningApps.size() > 0) { |
| 8267 | Set<String> extList = new HashSet<String>(); |
| 8268 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8269 | if (app.pkgList != null) { |
| 8270 | for (String pkg : app.pkgList) { |
| 8271 | extList.add(pkg); |
| 8272 | } |
| 8273 | } |
| 8274 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8275 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8276 | for (String pkg : extList) { |
| 8277 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 8278 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserId.getCallingUserId()); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8279 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8280 | retList.add(info); |
| 8281 | } |
| 8282 | } catch (RemoteException e) { |
| 8283 | } |
| 8284 | } |
| 8285 | } |
| 8286 | return retList; |
| 8287 | } |
| 8288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8289 | @Override |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8290 | public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) { |
| 8291 | enforceNotIsolatedCaller("getMyMemoryState"); |
| 8292 | synchronized (this) { |
| 8293 | ProcessRecord proc; |
| 8294 | synchronized (mPidsSelfLocked) { |
| 8295 | proc = mPidsSelfLocked.get(Binder.getCallingPid()); |
| 8296 | } |
| 8297 | fillInProcMemInfo(proc, outInfo); |
| 8298 | } |
| 8299 | } |
| 8300 | |
| 8301 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8302 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8303 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 8304 | != PackageManager.PERMISSION_GRANTED) { |
| 8305 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8306 | + Binder.getCallingPid() |
| 8307 | + ", uid=" + Binder.getCallingUid() |
| 8308 | + " without permission " |
| 8309 | + android.Manifest.permission.DUMP); |
| 8310 | return; |
| 8311 | } |
| 8312 | |
| 8313 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8314 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8315 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8316 | |
| 8317 | int opti = 0; |
| 8318 | while (opti < args.length) { |
| 8319 | String opt = args[opti]; |
| 8320 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8321 | break; |
| 8322 | } |
| 8323 | opti++; |
| 8324 | if ("-a".equals(opt)) { |
| 8325 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8326 | } else if ("-c".equals(opt)) { |
| 8327 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8328 | } else if ("-h".equals(opt)) { |
| 8329 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8330 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8331 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8332 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8333 | pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state"); |
| 8334 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 8335 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8336 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8337 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8338 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8339 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8340 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8341 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8342 | pw.println(" all: dump all activities"); |
| 8343 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8344 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8345 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 8346 | pw.println(" a partial substring in a component name, a"); |
| 8347 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8348 | pw.println(" -a: include all available server state."); |
| 8349 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8350 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8351 | } else { |
| 8352 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8353 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8354 | } |
| 8355 | |
| 8356 | // Is the caller requesting to dump a particular piece of data? |
| 8357 | if (opti < args.length) { |
| 8358 | String cmd = args[opti]; |
| 8359 | opti++; |
| 8360 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 8361 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8362 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8363 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8364 | return; |
| 8365 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8366 | String[] newArgs; |
| 8367 | String name; |
| 8368 | if (opti >= args.length) { |
| 8369 | name = null; |
| 8370 | newArgs = EMPTY_STRING_ARRAY; |
| 8371 | } else { |
| 8372 | name = args[opti]; |
| 8373 | opti++; |
| 8374 | newArgs = new String[args.length - opti]; |
| 8375 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8376 | args.length - opti); |
| 8377 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8378 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8379 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8380 | } |
| 8381 | return; |
| 8382 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8383 | String[] newArgs; |
| 8384 | String name; |
| 8385 | if (opti >= args.length) { |
| 8386 | name = null; |
| 8387 | newArgs = EMPTY_STRING_ARRAY; |
| 8388 | } else { |
| 8389 | name = args[opti]; |
| 8390 | opti++; |
| 8391 | newArgs = new String[args.length - opti]; |
| 8392 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8393 | args.length - opti); |
| 8394 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8395 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8396 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8397 | } |
| 8398 | return; |
| 8399 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8400 | String[] newArgs; |
| 8401 | String name; |
| 8402 | if (opti >= args.length) { |
| 8403 | name = null; |
| 8404 | newArgs = EMPTY_STRING_ARRAY; |
| 8405 | } else { |
| 8406 | name = args[opti]; |
| 8407 | opti++; |
| 8408 | newArgs = new String[args.length - opti]; |
| 8409 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8410 | args.length - opti); |
| 8411 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8412 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8413 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8414 | } |
| 8415 | return; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8416 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8417 | synchronized (this) { |
| 8418 | dumpOomLocked(fd, pw, args, opti, true); |
| 8419 | } |
| 8420 | return; |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8421 | } else if ("provider".equals(cmd)) { |
| 8422 | String[] newArgs; |
| 8423 | String name; |
| 8424 | if (opti >= args.length) { |
| 8425 | name = null; |
| 8426 | newArgs = EMPTY_STRING_ARRAY; |
| 8427 | } else { |
| 8428 | name = args[opti]; |
| 8429 | opti++; |
| 8430 | newArgs = new String[args.length - opti]; |
| 8431 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8432 | } |
| 8433 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8434 | pw.println("No providers match: " + name); |
| 8435 | pw.println("Use -h for help."); |
| 8436 | } |
| 8437 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8438 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8439 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8440 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8441 | } |
| 8442 | return; |
| 8443 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8444 | String[] newArgs; |
| 8445 | String name; |
| 8446 | if (opti >= args.length) { |
| 8447 | name = null; |
| 8448 | newArgs = EMPTY_STRING_ARRAY; |
| 8449 | } else { |
| 8450 | name = args[opti]; |
| 8451 | opti++; |
| 8452 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8453 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8454 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8455 | } |
| 8456 | if (!dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8457 | pw.println("No services match: " + name); |
| 8458 | pw.println("Use -h for help."); |
| 8459 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8460 | return; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8461 | } else if ("package".equals(cmd)) { |
| 8462 | String[] newArgs; |
| 8463 | if (opti >= args.length) { |
| 8464 | pw.println("package: no package name specified"); |
| 8465 | pw.println("Use -h for help."); |
| 8466 | return; |
| 8467 | } else { |
| 8468 | dumpPackage = args[opti]; |
| 8469 | opti++; |
| 8470 | newArgs = new String[args.length - opti]; |
| 8471 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8472 | args.length - opti); |
| 8473 | args = newArgs; |
| 8474 | opti = 0; |
| 8475 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8476 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 8477 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8478 | dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8479 | } |
| 8480 | return; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8481 | } else { |
| 8482 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8483 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 8484 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 8485 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8486 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8487 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8488 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8489 | } |
| 8490 | |
| 8491 | // No piece of data specified, dump everything. |
| 8492 | synchronized (this) { |
| 8493 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8494 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8495 | if (needSep) { |
| 8496 | pw.println(" "); |
| 8497 | } |
| 8498 | if (dumpAll) { |
| 8499 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8500 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8501 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8502 | if (needSep) { |
| 8503 | pw.println(" "); |
| 8504 | } |
| 8505 | if (dumpAll) { |
| 8506 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8507 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8508 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8509 | if (needSep) { |
| 8510 | pw.println(" "); |
| 8511 | } |
| 8512 | if (dumpAll) { |
| 8513 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8514 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8515 | needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8516 | if (needSep) { |
| 8517 | pw.println(" "); |
| 8518 | } |
| 8519 | if (dumpAll) { |
| 8520 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8521 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8522 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8523 | if (needSep) { |
| 8524 | pw.println(" "); |
| 8525 | } |
| 8526 | if (dumpAll) { |
| 8527 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8528 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8529 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8530 | } |
| 8531 | } |
| 8532 | |
| 8533 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8534 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8535 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 8536 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8537 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 8538 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8539 | pw.println(" "); |
| 8540 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8541 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 8542 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8543 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8544 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8545 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8546 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8547 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8548 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8549 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8550 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8551 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8552 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8553 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8554 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8555 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 8556 | pw.println(" "); |
| 8557 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8558 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8559 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8560 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8561 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8562 | pw.println(" "); |
| 8563 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8564 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8565 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8566 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8567 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8568 | pw.println(" "); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 8569 | if (mMainStack.mPausingActivity != null) { |
| 8570 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8571 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8572 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8573 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8574 | if (dumpAll) { |
| 8575 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 8576 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8577 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 8578 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8579 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8580 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8581 | if (mRecentTasks.size() > 0) { |
| 8582 | pw.println(); |
| 8583 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8584 | |
| 8585 | final int N = mRecentTasks.size(); |
| 8586 | for (int i=0; i<N; i++) { |
| 8587 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8588 | if (dumpPackage != null) { |
| 8589 | if (tr.realActivity == null || |
| 8590 | !dumpPackage.equals(tr.realActivity)) { |
| 8591 | continue; |
| 8592 | } |
| 8593 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8594 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 8595 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8596 | if (dumpAll) { |
| 8597 | mRecentTasks.get(i).dump(pw, " "); |
| 8598 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8599 | } |
| 8600 | } |
| 8601 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8602 | if (dumpAll) { |
| 8603 | pw.println(" "); |
| 8604 | pw.println(" mCurTask: " + mCurTask); |
| 8605 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8606 | |
| 8607 | return true; |
| 8608 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8609 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8610 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8611 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8612 | boolean needSep = false; |
| 8613 | int numPers = 0; |
| 8614 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8615 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 8616 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8617 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8618 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 8619 | final int NA = procs.size(); |
| 8620 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8621 | ProcessRecord r = procs.valueAt(ia); |
| 8622 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8623 | continue; |
| 8624 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8625 | if (!needSep) { |
| 8626 | pw.println(" All known processes:"); |
| 8627 | needSep = true; |
| 8628 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8629 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 8630 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 8631 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8632 | r.dump(pw, " "); |
| 8633 | if (r.persistent) { |
| 8634 | numPers++; |
| 8635 | } |
| 8636 | } |
| 8637 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8638 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8639 | |
| 8640 | if (mIsolatedProcesses.size() > 0) { |
| 8641 | if (needSep) pw.println(" "); |
| 8642 | needSep = true; |
| 8643 | pw.println(" Isolated process list (sorted by uid):"); |
| 8644 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 8645 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 8646 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8647 | continue; |
| 8648 | } |
| 8649 | pw.println(String.format("%sIsolated #%2d: %s", |
| 8650 | " ", i, r.toString())); |
| 8651 | } |
| 8652 | } |
| 8653 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8654 | if (mLruProcesses.size() > 0) { |
| 8655 | if (needSep) pw.println(" "); |
| 8656 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8657 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8658 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8659 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8660 | needSep = true; |
| 8661 | } |
| 8662 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8663 | if (dumpAll) { |
| 8664 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8665 | boolean printed = false; |
| 8666 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 8667 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 8668 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8669 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8670 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8671 | if (!printed) { |
| 8672 | if (needSep) pw.println(" "); |
| 8673 | needSep = true; |
| 8674 | pw.println(" PID mappings:"); |
| 8675 | printed = true; |
| 8676 | } |
| 8677 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 8678 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8679 | } |
| 8680 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8681 | } |
| 8682 | |
| 8683 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8684 | synchronized (mPidsSelfLocked) { |
| 8685 | boolean printed = false; |
| 8686 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 8687 | ProcessRecord r = mPidsSelfLocked.get( |
| 8688 | mForegroundProcesses.valueAt(i).pid); |
| 8689 | if (dumpPackage != null && (r == null |
| 8690 | || !dumpPackage.equals(r.info.packageName))) { |
| 8691 | continue; |
| 8692 | } |
| 8693 | if (!printed) { |
| 8694 | if (needSep) pw.println(" "); |
| 8695 | needSep = true; |
| 8696 | pw.println(" Foreground Processes:"); |
| 8697 | printed = true; |
| 8698 | } |
| 8699 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 8700 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 8701 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8702 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8703 | } |
| 8704 | |
| 8705 | if (mPersistentStartingProcesses.size() > 0) { |
| 8706 | if (needSep) pw.println(" "); |
| 8707 | needSep = true; |
| 8708 | pw.println(" Persisent processes that are starting:"); |
| 8709 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8710 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8711 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8712 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8713 | if (mRemovedProcesses.size() > 0) { |
| 8714 | if (needSep) pw.println(" "); |
| 8715 | needSep = true; |
| 8716 | pw.println(" Processes that are being removed:"); |
| 8717 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8718 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8719 | } |
| 8720 | |
| 8721 | if (mProcessesOnHold.size() > 0) { |
| 8722 | if (needSep) pw.println(" "); |
| 8723 | needSep = true; |
| 8724 | pw.println(" Processes that are on old until the system is ready:"); |
| 8725 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8726 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8727 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8728 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8729 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8730 | |
| 8731 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8732 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8733 | long now = SystemClock.uptimeMillis(); |
| 8734 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8735 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8736 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8737 | SparseArray<Long> uids = procs.getValue(); |
| 8738 | final int N = uids.size(); |
| 8739 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8740 | int puid = uids.keyAt(i); |
| 8741 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8742 | if (dumpPackage != null && (r == null |
| 8743 | || !dumpPackage.equals(r.info.packageName))) { |
| 8744 | continue; |
| 8745 | } |
| 8746 | if (!printed) { |
| 8747 | if (needSep) pw.println(" "); |
| 8748 | needSep = true; |
| 8749 | pw.println(" Time since processes crashed:"); |
| 8750 | printed = true; |
| 8751 | } |
| 8752 | pw.print(" Process "); pw.print(pname); |
| 8753 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8754 | pw.print(": last crashed "); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8755 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 8756 | pw.println(" ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8757 | } |
| 8758 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8759 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8760 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8761 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8762 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8763 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8764 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8765 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8766 | SparseArray<Long> uids = procs.getValue(); |
| 8767 | final int N = uids.size(); |
| 8768 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8769 | int puid = uids.keyAt(i); |
| 8770 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8771 | if (dumpPackage != null && (r == null |
| 8772 | || !dumpPackage.equals(r.info.packageName))) { |
| 8773 | continue; |
| 8774 | } |
| 8775 | if (!printed) { |
| 8776 | if (needSep) pw.println(" "); |
| 8777 | needSep = true; |
| 8778 | pw.println(" Bad processes:"); |
| 8779 | } |
| 8780 | pw.print(" Bad process "); pw.print(pname); |
| 8781 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8782 | pw.print(": crashed at time "); |
| 8783 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8784 | } |
| 8785 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8786 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8787 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8788 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8789 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8790 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 8791 | if (dumpAll) { |
| 8792 | StringBuilder sb = new StringBuilder(128); |
| 8793 | sb.append(" mPreviousProcessVisibleTime: "); |
| 8794 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 8795 | pw.println(sb); |
| 8796 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8797 | if (mHeavyWeightProcess != null) { |
| 8798 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8799 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8800 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8801 | if (dumpAll) { |
| 8802 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 8803 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8804 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8805 | for (Map.Entry<String, Integer> entry |
| 8806 | : mCompatModePackages.getPackages().entrySet()) { |
| 8807 | String pkg = entry.getKey(); |
| 8808 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8809 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 8810 | continue; |
| 8811 | } |
| 8812 | if (!printed) { |
| 8813 | pw.println(" mScreenCompatPackages:"); |
| 8814 | printed = true; |
| 8815 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8816 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 8817 | pw.print(mode); pw.println(); |
| 8818 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 8819 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8820 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8821 | pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown); |
| 8822 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 8823 | || mOrigWaitForDebugger) { |
| 8824 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 8825 | + " mDebugTransient=" + mDebugTransient |
| 8826 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 8827 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 8828 | if (mOpenGlTraceApp != null) { |
| 8829 | pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp); |
| 8830 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 8831 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 8832 | || mProfileFd != null) { |
| 8833 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 8834 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 8835 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 8836 | + mAutoStopProfiler); |
| 8837 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8838 | if (mAlwaysFinishActivities || mController != null) { |
| 8839 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 8840 | + " mController=" + mController); |
| 8841 | } |
| 8842 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8843 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8844 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 8845 | + " mProcessesReady=" + mProcessesReady |
| 8846 | + " mSystemReady=" + mSystemReady); |
| 8847 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8848 | + " mBooted=" + mBooted |
| 8849 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8850 | pw.print(" mLastPowerCheckRealtime="); |
| 8851 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 8852 | pw.println(""); |
| 8853 | pw.print(" mLastPowerCheckUptime="); |
| 8854 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 8855 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8856 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 8857 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 8858 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8859 | pw.println(" mNumServiceProcs=" + mNumServiceProcs |
| 8860 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8861 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8862 | |
| 8863 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8864 | } |
| 8865 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8866 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8867 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8868 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8869 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8870 | long now = SystemClock.uptimeMillis(); |
| 8871 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 8872 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8873 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 8874 | continue; |
| 8875 | } |
| 8876 | if (!printed) { |
| 8877 | if (needSep) pw.println(" "); |
| 8878 | needSep = true; |
| 8879 | pw.println(" Processes that are waiting to GC:"); |
| 8880 | printed = true; |
| 8881 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8882 | pw.print(" Process "); pw.println(proc); |
| 8883 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 8884 | pw.print(", last gced="); |
| 8885 | pw.print(now-proc.lastRequestedGc); |
| 8886 | pw.print(" ms ago, last lowMem="); |
| 8887 | pw.print(now-proc.lastLowMemory); |
| 8888 | pw.println(" ms ago"); |
| 8889 | |
| 8890 | } |
| 8891 | } |
| 8892 | return needSep; |
| 8893 | } |
| 8894 | |
| 8895 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8896 | int opti, boolean dumpAll) { |
| 8897 | boolean needSep = false; |
| 8898 | |
| 8899 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8900 | if (needSep) pw.println(" "); |
| 8901 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 8902 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8903 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8904 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8905 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 8906 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 8907 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 8908 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 8909 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8910 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8911 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8912 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8913 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8914 | 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] | 8915 | 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] | 8916 | |
| 8917 | if (needSep) pw.println(" "); |
| 8918 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8919 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8920 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8921 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8922 | needSep = true; |
| 8923 | } |
| 8924 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8925 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8926 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8927 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8928 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8929 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8930 | if (mHeavyWeightProcess != null) { |
| 8931 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8932 | } |
| 8933 | |
| 8934 | return true; |
| 8935 | } |
| 8936 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8937 | /** |
| 8938 | * There are three ways to call this: |
| 8939 | * - no service specified: dump all the services |
| 8940 | * - a flattened component name that matched an existing service was specified as the |
| 8941 | * first arg: dump that one service |
| 8942 | * - the first arg isn't the flattened component name of an existing service: |
| 8943 | * dump all services whose component contains the first arg as a substring |
| 8944 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8945 | protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8946 | int opti, boolean dumpAll) { |
| 8947 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8948 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8949 | if ("all".equals(name)) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8950 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 8951 | try { |
| 8952 | List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); |
| 8953 | for (UserInfo user : users) { |
| 8954 | for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { |
| 8955 | services.add(r1); |
| 8956 | } |
| 8957 | } |
| 8958 | } catch (RemoteException re) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8959 | } |
| 8960 | } |
| 8961 | } else { |
| 8962 | ComponentName componentName = name != null |
| 8963 | ? ComponentName.unflattenFromString(name) : null; |
| 8964 | int objectId = 0; |
| 8965 | if (componentName == null) { |
| 8966 | // Not a '/' separated full component name; maybe an object ID? |
| 8967 | try { |
| 8968 | objectId = Integer.parseInt(name, 16); |
| 8969 | name = null; |
| 8970 | componentName = null; |
| 8971 | } catch (RuntimeException e) { |
| 8972 | } |
| 8973 | } |
| 8974 | |
| 8975 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 8976 | try { |
| 8977 | List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); |
| 8978 | for (UserInfo user : users) { |
| 8979 | for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { |
| 8980 | if (componentName != null) { |
| 8981 | if (r1.name.equals(componentName)) { |
| 8982 | services.add(r1); |
| 8983 | } |
| 8984 | } else if (name != null) { |
| 8985 | if (r1.name.flattenToString().contains(name)) { |
| 8986 | services.add(r1); |
| 8987 | } |
| 8988 | } else if (System.identityHashCode(r1) == objectId) { |
| 8989 | services.add(r1); |
| 8990 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8991 | } |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8992 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 8993 | } catch (RemoteException re) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8994 | } |
| 8995 | } |
| 8996 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8997 | |
| 8998 | if (services.size() <= 0) { |
| 8999 | return false; |
| 9000 | } |
| 9001 | |
| 9002 | boolean needSep = false; |
| 9003 | for (int i=0; i<services.size(); i++) { |
| 9004 | if (needSep) { |
| 9005 | pw.println(); |
| 9006 | } |
| 9007 | needSep = true; |
| 9008 | dumpService("", fd, pw, services.get(i), args, dumpAll); |
| 9009 | } |
| 9010 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9011 | } |
| 9012 | |
| 9013 | /** |
| 9014 | * Invokes IApplicationThread.dumpService() on the thread of the specified service if |
| 9015 | * there is a thread associated with the service. |
| 9016 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9017 | private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, |
| 9018 | final ServiceRecord r, String[] args, boolean dumpAll) { |
| 9019 | String innerPrefix = prefix + " "; |
| 9020 | synchronized (this) { |
| 9021 | pw.print(prefix); pw.print("SERVICE "); |
| 9022 | pw.print(r.shortName); pw.print(" "); |
| 9023 | pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9024 | pw.print(" pid="); |
| 9025 | if (r.app != null) pw.println(r.app.pid); |
| 9026 | else pw.println("(not running)"); |
| 9027 | if (dumpAll) { |
| 9028 | r.dump(pw, innerPrefix); |
| 9029 | } |
| 9030 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9031 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9032 | pw.print(prefix); pw.println(" Client:"); |
| 9033 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9034 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9035 | TransferPipe tp = new TransferPipe(); |
| 9036 | try { |
| 9037 | r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); |
| 9038 | tp.setBufferPrefix(prefix + " "); |
| 9039 | tp.go(fd); |
| 9040 | } finally { |
| 9041 | tp.kill(); |
| 9042 | } |
| 9043 | } catch (IOException e) { |
| 9044 | pw.println(prefix + " Failure while dumping the service: " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9045 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9046 | pw.println(prefix + " Got a RemoteException while dumping the service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9047 | } |
| 9048 | } |
| 9049 | } |
| 9050 | |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9051 | /** |
| 9052 | * There are three ways to call this: |
| 9053 | * - no provider specified: dump all the providers |
| 9054 | * - a flattened component name that matched an existing provider was specified as the |
| 9055 | * first arg: dump that one provider |
| 9056 | * - the first arg isn't the flattened component name of an existing provider: |
| 9057 | * dump all providers whose component contains the first arg as a substring |
| 9058 | */ |
| 9059 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9060 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 9061 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9062 | } |
| 9063 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9064 | static class ItemMatcher { |
| 9065 | ArrayList<ComponentName> components; |
| 9066 | ArrayList<String> strings; |
| 9067 | ArrayList<Integer> objects; |
| 9068 | boolean all; |
| 9069 | |
| 9070 | ItemMatcher() { |
| 9071 | all = true; |
| 9072 | } |
| 9073 | |
| 9074 | void build(String name) { |
| 9075 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 9076 | if (componentName != null) { |
| 9077 | if (components == null) { |
| 9078 | components = new ArrayList<ComponentName>(); |
| 9079 | } |
| 9080 | components.add(componentName); |
| 9081 | all = false; |
| 9082 | } else { |
| 9083 | int objectId = 0; |
| 9084 | // Not a '/' separated full component name; maybe an object ID? |
| 9085 | try { |
| 9086 | objectId = Integer.parseInt(name, 16); |
| 9087 | if (objects == null) { |
| 9088 | objects = new ArrayList<Integer>(); |
| 9089 | } |
| 9090 | objects.add(objectId); |
| 9091 | all = false; |
| 9092 | } catch (RuntimeException e) { |
| 9093 | // Not an integer; just do string match. |
| 9094 | if (strings == null) { |
| 9095 | strings = new ArrayList<String>(); |
| 9096 | } |
| 9097 | strings.add(name); |
| 9098 | all = false; |
| 9099 | } |
| 9100 | } |
| 9101 | } |
| 9102 | |
| 9103 | int build(String[] args, int opti) { |
| 9104 | for (; opti<args.length; opti++) { |
| 9105 | String name = args[opti]; |
| 9106 | if ("--".equals(name)) { |
| 9107 | return opti+1; |
| 9108 | } |
| 9109 | build(name); |
| 9110 | } |
| 9111 | return opti; |
| 9112 | } |
| 9113 | |
| 9114 | boolean match(Object object, ComponentName comp) { |
| 9115 | if (all) { |
| 9116 | return true; |
| 9117 | } |
| 9118 | if (components != null) { |
| 9119 | for (int i=0; i<components.size(); i++) { |
| 9120 | if (components.get(i).equals(comp)) { |
| 9121 | return true; |
| 9122 | } |
| 9123 | } |
| 9124 | } |
| 9125 | if (objects != null) { |
| 9126 | for (int i=0; i<objects.size(); i++) { |
| 9127 | if (System.identityHashCode(object) == objects.get(i)) { |
| 9128 | return true; |
| 9129 | } |
| 9130 | } |
| 9131 | } |
| 9132 | if (strings != null) { |
| 9133 | String flat = comp.flattenToString(); |
| 9134 | for (int i=0; i<strings.size(); i++) { |
| 9135 | if (flat.contains(strings.get(i))) { |
| 9136 | return true; |
| 9137 | } |
| 9138 | } |
| 9139 | } |
| 9140 | return false; |
| 9141 | } |
| 9142 | } |
| 9143 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9144 | /** |
| 9145 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9146 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9147 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 9148 | * dump all activity whose component contains the cmd as a substring |
| 9149 | * - A hex number of the ActivityRecord object instance. |
| 9150 | */ |
| 9151 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9152 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9153 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9154 | |
| 9155 | if ("all".equals(name)) { |
| 9156 | synchronized (this) { |
| 9157 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9158 | activities.add(r1); |
| 9159 | } |
| 9160 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 9161 | } else if ("top".equals(name)) { |
| 9162 | synchronized (this) { |
| 9163 | final int N = mMainStack.mHistory.size(); |
| 9164 | if (N > 0) { |
| 9165 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 9166 | } |
| 9167 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9168 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9169 | ItemMatcher matcher = new ItemMatcher(); |
| 9170 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9171 | |
| 9172 | synchronized (this) { |
| 9173 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9174 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9175 | activities.add(r1); |
| 9176 | } |
| 9177 | } |
| 9178 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9179 | } |
| 9180 | |
| 9181 | if (activities.size() <= 0) { |
| 9182 | return false; |
| 9183 | } |
| 9184 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9185 | String[] newArgs = new String[args.length - opti]; |
| 9186 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9187 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9188 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9189 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9190 | for (int i=activities.size()-1; i>=0; i--) { |
| 9191 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9192 | if (needSep) { |
| 9193 | pw.println(); |
| 9194 | } |
| 9195 | needSep = true; |
| 9196 | synchronized (this) { |
| 9197 | if (lastTask != r.task) { |
| 9198 | lastTask = r.task; |
| 9199 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 9200 | pw.print(" id="); pw.println(lastTask.taskId); |
| 9201 | if (dumpAll) { |
| 9202 | lastTask.dump(pw, " "); |
| 9203 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9204 | } |
| 9205 | } |
| 9206 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9207 | } |
| 9208 | return true; |
| 9209 | } |
| 9210 | |
| 9211 | /** |
| 9212 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 9213 | * there is a thread associated with the activity. |
| 9214 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9215 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9216 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 9217 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9218 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9219 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 9220 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9221 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9222 | if (r.app != null) pw.println(r.app.pid); |
| 9223 | else pw.println("(not running)"); |
| 9224 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9225 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9226 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9227 | } |
| 9228 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9229 | // flush anything that is already in the PrintWriter since the thread is going |
| 9230 | // to write to the file descriptor directly |
| 9231 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9232 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9233 | TransferPipe tp = new TransferPipe(); |
| 9234 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9235 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9236 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9237 | tp.go(fd); |
| 9238 | } finally { |
| 9239 | tp.kill(); |
| 9240 | } |
| 9241 | } catch (IOException e) { |
| 9242 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9243 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9244 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9245 | } |
| 9246 | } |
| 9247 | } |
| 9248 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9249 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9250 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9251 | boolean needSep = false; |
| 9252 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9253 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9254 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9255 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9256 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9257 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 9258 | while (it.hasNext()) { |
| 9259 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9260 | if (dumpPackage != null && (r.app == null || |
| 9261 | !dumpPackage.equals(r.app.info.packageName))) { |
| 9262 | continue; |
| 9263 | } |
| 9264 | if (!printed) { |
| 9265 | pw.println(" Registered Receivers:"); |
| 9266 | needSep = true; |
| 9267 | printed = true; |
| 9268 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9269 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9270 | r.dump(pw, " "); |
| 9271 | } |
| 9272 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9273 | |
| 9274 | if (mReceiverResolver.dump(pw, needSep ? |
| 9275 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9276 | " ", dumpPackage, false)) { |
| 9277 | needSep = true; |
| 9278 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9279 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9280 | |
| 9281 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9282 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9283 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9284 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9285 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9286 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9287 | if (mStickyBroadcasts != null && dumpPackage == null) { |
| 9288 | if (needSep) { |
| 9289 | pw.println(); |
| 9290 | } |
| 9291 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9292 | pw.println(" Sticky broadcasts:"); |
| 9293 | StringBuilder sb = new StringBuilder(128); |
| 9294 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9295 | : mStickyBroadcasts.entrySet()) { |
| 9296 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9297 | if (dumpAll) { |
| 9298 | pw.println(":"); |
| 9299 | ArrayList<Intent> intents = ent.getValue(); |
| 9300 | final int N = intents.size(); |
| 9301 | for (int i=0; i<N; i++) { |
| 9302 | sb.setLength(0); |
| 9303 | sb.append(" Intent: "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9304 | intents.get(i).toShortString(sb, false, true, false, false); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9305 | pw.println(sb.toString()); |
| 9306 | Bundle bundle = intents.get(i).getExtras(); |
| 9307 | if (bundle != null) { |
| 9308 | pw.print(" "); |
| 9309 | pw.println(bundle.toString()); |
| 9310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9311 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9312 | } else { |
| 9313 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9314 | } |
| 9315 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9316 | needSep = true; |
| 9317 | } |
| 9318 | |
| 9319 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9320 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9321 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9322 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9323 | + queue.mBroadcastsScheduled); |
| 9324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9325 | pw.println(" mHandler:"); |
| 9326 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9327 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9328 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9329 | |
| 9330 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9331 | } |
| 9332 | |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9333 | /** |
| 9334 | * Prints a list of ServiceRecords (dumpsys activity services) |
| 9335 | */ |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9336 | boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9337 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9338 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9339 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9340 | ItemMatcher matcher = new ItemMatcher(); |
| 9341 | matcher.build(args, opti); |
| 9342 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9343 | pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9344 | try { |
| 9345 | List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); |
| 9346 | for (UserInfo user : users) { |
| 9347 | if (mServiceMap.getAllServices(user.id).size() > 0) { |
| 9348 | boolean printed = false; |
| 9349 | long nowReal = SystemClock.elapsedRealtime(); |
| 9350 | Iterator<ServiceRecord> it = mServiceMap.getAllServices( |
| 9351 | user.id).iterator(); |
| 9352 | needSep = false; |
| 9353 | while (it.hasNext()) { |
| 9354 | ServiceRecord r = it.next(); |
| 9355 | if (!matcher.match(r, r.name)) { |
| 9356 | continue; |
| 9357 | } |
| 9358 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9359 | continue; |
| 9360 | } |
| 9361 | if (!printed) { |
| 9362 | pw.println(" Active services:"); |
| 9363 | printed = true; |
| 9364 | } |
| 9365 | if (needSep) { |
| 9366 | pw.println(); |
| 9367 | } |
| 9368 | pw.print(" * "); |
| 9369 | pw.println(r); |
| 9370 | if (dumpAll) { |
| 9371 | r.dump(pw, " "); |
| 9372 | needSep = true; |
| 9373 | } else { |
| 9374 | pw.print(" app="); |
| 9375 | pw.println(r.app); |
| 9376 | pw.print(" created="); |
| 9377 | TimeUtils.formatDuration(r.createTime, nowReal, pw); |
| 9378 | pw.print(" started="); |
| 9379 | pw.print(r.startRequested); |
| 9380 | pw.print(" connections="); |
| 9381 | pw.println(r.connections.size()); |
| 9382 | if (r.connections.size() > 0) { |
| 9383 | pw.println(" Connections:"); |
| 9384 | for (ArrayList<ConnectionRecord> clist : r.connections.values()) { |
| 9385 | for (int i = 0; i < clist.size(); i++) { |
| 9386 | ConnectionRecord conn = clist.get(i); |
| 9387 | pw.print(" "); |
| 9388 | pw.print(conn.binding.intent.intent.getIntent() |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9389 | .toShortString(false, false, false, false)); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9390 | pw.print(" -> "); |
| 9391 | ProcessRecord proc = conn.binding.client; |
| 9392 | pw.println(proc != null ? proc.toShortString() : "null"); |
| 9393 | } |
| 9394 | } |
Dianne Hackborn | 8ec8d41 | 2011-11-14 18:27:24 -0800 | [diff] [blame] | 9395 | } |
| 9396 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9397 | if (dumpClient && r.app != null && r.app.thread != null) { |
| 9398 | pw.println(" Client:"); |
| 9399 | pw.flush(); |
| 9400 | try { |
| 9401 | TransferPipe tp = new TransferPipe(); |
| 9402 | try { |
| 9403 | r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), |
| 9404 | r, args); |
| 9405 | tp.setBufferPrefix(" "); |
| 9406 | // Short timeout, since blocking here can |
| 9407 | // deadlock with the application. |
| 9408 | tp.go(fd, 2000); |
| 9409 | } finally { |
| 9410 | tp.kill(); |
| 9411 | } |
| 9412 | } catch (IOException e) { |
| 9413 | pw.println(" Failure while dumping the service: " + e); |
| 9414 | } catch (RemoteException e) { |
| 9415 | pw.println(" Got a RemoteException while dumping the service"); |
| 9416 | } |
| 9417 | needSep = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9418 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9419 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9420 | needSep = printed; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9421 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9422 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9423 | } catch (RemoteException re) { |
| 9424 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9425 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9426 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9427 | if (mPendingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9428 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9429 | for (int i=0; i<mPendingServices.size(); i++) { |
| 9430 | ServiceRecord r = mPendingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9431 | if (!matcher.match(r, r.name)) { |
| 9432 | continue; |
| 9433 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9434 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9435 | continue; |
| 9436 | } |
| 9437 | if (!printed) { |
| 9438 | if (needSep) pw.println(" "); |
| 9439 | needSep = true; |
| 9440 | pw.println(" Pending services:"); |
| 9441 | printed = true; |
| 9442 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9443 | pw.print(" * Pending "); pw.println(r); |
| 9444 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9445 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9446 | needSep = true; |
| 9447 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9448 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9449 | if (mRestartingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9450 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9451 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 9452 | ServiceRecord r = mRestartingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9453 | if (!matcher.match(r, r.name)) { |
| 9454 | continue; |
| 9455 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9456 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9457 | continue; |
| 9458 | } |
| 9459 | if (!printed) { |
| 9460 | if (needSep) pw.println(" "); |
| 9461 | needSep = true; |
| 9462 | pw.println(" Restarting services:"); |
| 9463 | printed = true; |
| 9464 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9465 | pw.print(" * Restarting "); pw.println(r); |
| 9466 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9467 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9468 | needSep = true; |
| 9469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9470 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9471 | if (mStoppingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9472 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9473 | for (int i=0; i<mStoppingServices.size(); i++) { |
| 9474 | ServiceRecord r = mStoppingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9475 | if (!matcher.match(r, r.name)) { |
| 9476 | continue; |
| 9477 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9478 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9479 | continue; |
| 9480 | } |
| 9481 | if (!printed) { |
| 9482 | if (needSep) pw.println(" "); |
| 9483 | needSep = true; |
| 9484 | pw.println(" Stopping services:"); |
| 9485 | printed = true; |
| 9486 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9487 | pw.print(" * Stopping "); pw.println(r); |
| 9488 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9489 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9490 | needSep = true; |
| 9491 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9492 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9493 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9494 | if (mServiceConnections.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9495 | boolean printed = false; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9496 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9497 | = mServiceConnections.values().iterator(); |
| 9498 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9499 | ArrayList<ConnectionRecord> r = it.next(); |
| 9500 | for (int i=0; i<r.size(); i++) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9501 | ConnectionRecord cr = r.get(i); |
| 9502 | if (!matcher.match(cr.binding.service, cr.binding.service.name)) { |
| 9503 | continue; |
| 9504 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9505 | if (dumpPackage != null && (cr.binding.client == null |
| 9506 | || !dumpPackage.equals(cr.binding.client.info.packageName))) { |
| 9507 | continue; |
| 9508 | } |
| 9509 | if (!printed) { |
| 9510 | if (needSep) pw.println(" "); |
| 9511 | needSep = true; |
| 9512 | pw.println(" Connection bindings to services:"); |
| 9513 | printed = true; |
| 9514 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9515 | pw.print(" * "); pw.println(cr); |
| 9516 | cr.dump(pw, " "); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9518 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9519 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9520 | } |
| 9521 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9522 | |
| 9523 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9524 | } |
| 9525 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9526 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9527 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 9528 | boolean needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9529 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9530 | ItemMatcher matcher = new ItemMatcher(); |
| 9531 | matcher.build(args, opti); |
| 9532 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9533 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9534 | |
| 9535 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9536 | |
| 9537 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9538 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9539 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9540 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9541 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9542 | continue; |
| 9543 | } |
| 9544 | if (!printed) { |
| 9545 | if (needSep) pw.println(" "); |
| 9546 | needSep = true; |
| 9547 | pw.println(" Launching content providers:"); |
| 9548 | printed = true; |
| 9549 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9550 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9551 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9552 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9553 | } |
| 9554 | |
| 9555 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9556 | if (needSep) pw.println(); |
| 9557 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9558 | pw.println("Granted Uri Permissions:"); |
| 9559 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9560 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9561 | HashMap<Uri, UriPermission> perms |
| 9562 | = mGrantedUriPermissions.valueAt(i); |
| 9563 | pw.print(" * UID "); pw.print(uid); |
| 9564 | pw.println(" holds:"); |
| 9565 | for (UriPermission perm : perms.values()) { |
| 9566 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9567 | if (dumpAll) { |
| 9568 | perm.dump(pw, " "); |
| 9569 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9570 | } |
| 9571 | } |
| 9572 | needSep = true; |
| 9573 | } |
| 9574 | |
| 9575 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9576 | } |
| 9577 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9578 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9579 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9580 | boolean needSep = false; |
| 9581 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9582 | if (mIntentSenderRecords.size() > 0) { |
| 9583 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9584 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9585 | = mIntentSenderRecords.values().iterator(); |
| 9586 | while (it.hasNext()) { |
| 9587 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9588 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9589 | if (dumpPackage != null && (rec == null |
| 9590 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9591 | continue; |
| 9592 | } |
| 9593 | if (!printed) { |
| 9594 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9595 | printed = true; |
| 9596 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9597 | needSep = true; |
| 9598 | if (rec != null) { |
| 9599 | pw.print(" * "); pw.println(rec); |
| 9600 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9601 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9602 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9603 | } else { |
| 9604 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9605 | } |
| 9606 | } |
| 9607 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9608 | |
| 9609 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9610 | } |
| 9611 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9612 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9613 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9614 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9615 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9616 | boolean needNL = false; |
| 9617 | final String innerPrefix = prefix + " "; |
| 9618 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9619 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9620 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9621 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9622 | continue; |
| 9623 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9624 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9625 | if (needNL) { |
| 9626 | pw.println(" "); |
| 9627 | needNL = false; |
| 9628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9629 | if (lastTask != r.task) { |
| 9630 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9631 | pw.print(prefix); |
| 9632 | pw.print(full ? "* " : " "); |
| 9633 | pw.println(lastTask); |
| 9634 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9635 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9636 | } else if (complete) { |
| 9637 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9638 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9639 | pw.print(prefix); pw.print(" "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9640 | pw.println(lastTask.intent.toInsecureStringWithClip()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9641 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9642 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9643 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9644 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9645 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9646 | pw.println(r); |
| 9647 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9648 | r.dump(pw, innerPrefix); |
| 9649 | } else if (complete) { |
| 9650 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9651 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9652 | if (r.app != null) { |
| 9653 | pw.print(innerPrefix); pw.println(r.app); |
| 9654 | } |
| 9655 | } |
| 9656 | if (client && r.app != null && r.app.thread != null) { |
| 9657 | // flush anything that is already in the PrintWriter since the thread is going |
| 9658 | // to write to the file descriptor directly |
| 9659 | pw.flush(); |
| 9660 | try { |
| 9661 | TransferPipe tp = new TransferPipe(); |
| 9662 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9663 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9664 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9665 | // Short timeout, since blocking here can |
| 9666 | // deadlock with the application. |
| 9667 | tp.go(fd, 2000); |
| 9668 | } finally { |
| 9669 | tp.kill(); |
| 9670 | } |
| 9671 | } catch (IOException e) { |
| 9672 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9673 | } catch (RemoteException e) { |
| 9674 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9675 | } |
| 9676 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9677 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9678 | } |
| 9679 | } |
| 9680 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9681 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9682 | if (val == base) { |
| 9683 | if (space == null) return prefix; |
| 9684 | return prefix + " "; |
| 9685 | } |
| 9686 | return prefix + "+" + Integer.toString(val-base); |
| 9687 | } |
| 9688 | |
| 9689 | private static final int dumpProcessList(PrintWriter pw, |
| 9690 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9691 | String prefix, String normalLabel, String persistentLabel, |
| 9692 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9693 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9694 | final int N = list.size()-1; |
| 9695 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9696 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9697 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9698 | continue; |
| 9699 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9700 | pw.println(String.format("%s%s #%2d: %s", |
| 9701 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9702 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9703 | if (r.persistent) { |
| 9704 | numPers++; |
| 9705 | } |
| 9706 | } |
| 9707 | return numPers; |
| 9708 | } |
| 9709 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9710 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9711 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9712 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9713 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9714 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9715 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9716 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9717 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9718 | ProcessRecord r = origList.get(i); |
| 9719 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9720 | continue; |
| 9721 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9722 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9723 | } |
| 9724 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9725 | if (list.size() <= 0) { |
| 9726 | return false; |
| 9727 | } |
| 9728 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9729 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 9730 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 9731 | @Override |
| 9732 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 9733 | Pair<ProcessRecord, Integer> object2) { |
| 9734 | if (object1.first.setAdj != object2.first.setAdj) { |
| 9735 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 9736 | } |
| 9737 | if (object1.second.intValue() != object2.second.intValue()) { |
| 9738 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 9739 | } |
| 9740 | return 0; |
| 9741 | } |
| 9742 | }; |
| 9743 | |
| 9744 | Collections.sort(list, comparator); |
| 9745 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9746 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 9747 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 9748 | final long curUptime = SystemClock.uptimeMillis(); |
| 9749 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 9750 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9751 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9752 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9753 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9754 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9755 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9756 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 9757 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9758 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 9759 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9760 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 9761 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9762 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 9763 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9764 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9765 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9766 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 9767 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9768 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 9769 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9770 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 9771 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 9772 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9773 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9774 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 9775 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9776 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 9777 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9778 | } else { |
| 9779 | oomAdj = Integer.toString(r.setAdj); |
| 9780 | } |
| 9781 | String schedGroup; |
| 9782 | switch (r.setSchedGroup) { |
| 9783 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 9784 | schedGroup = "B"; |
| 9785 | break; |
| 9786 | case Process.THREAD_GROUP_DEFAULT: |
| 9787 | schedGroup = "F"; |
| 9788 | break; |
| 9789 | default: |
| 9790 | schedGroup = Integer.toString(r.setSchedGroup); |
| 9791 | break; |
| 9792 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9793 | String foreground; |
| 9794 | if (r.foregroundActivities) { |
| 9795 | foreground = "A"; |
| 9796 | } else if (r.foregroundServices) { |
| 9797 | foreground = "S"; |
| 9798 | } else { |
| 9799 | foreground = " "; |
| 9800 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9801 | 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] | 9802 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9803 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 9804 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9805 | if (r.adjSource != null || r.adjTarget != null) { |
| 9806 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9807 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9808 | if (r.adjTarget instanceof ComponentName) { |
| 9809 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 9810 | } else if (r.adjTarget != null) { |
| 9811 | pw.print(r.adjTarget.toString()); |
| 9812 | } else { |
| 9813 | pw.print("{null}"); |
| 9814 | } |
| 9815 | pw.print("<="); |
| 9816 | if (r.adjSource instanceof ProcessRecord) { |
| 9817 | pw.print("Proc{"); |
| 9818 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 9819 | pw.println("}"); |
| 9820 | } else if (r.adjSource != null) { |
| 9821 | pw.println(r.adjSource.toString()); |
| 9822 | } else { |
| 9823 | pw.println("{null}"); |
| 9824 | } |
| 9825 | } |
| 9826 | if (inclDetails) { |
| 9827 | pw.print(prefix); |
| 9828 | pw.print(" "); |
| 9829 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 9830 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
| 9831 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 9832 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 9833 | pw.print(" cur="); pw.print(r.curAdj); |
| 9834 | pw.print(" set="); pw.println(r.setAdj); |
| 9835 | pw.print(prefix); |
| 9836 | pw.print(" "); |
| 9837 | pw.print("keeping="); pw.print(r.keeping); |
| 9838 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9839 | pw.print(" empty="); pw.print(r.empty); |
| 9840 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9841 | |
| 9842 | if (!r.keeping) { |
| 9843 | if (r.lastWakeTime != 0) { |
| 9844 | long wtime; |
| 9845 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 9846 | synchronized (stats) { |
| 9847 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 9848 | r.pid, curRealtime); |
| 9849 | } |
| 9850 | long timeUsed = wtime - r.lastWakeTime; |
| 9851 | pw.print(prefix); |
| 9852 | pw.print(" "); |
| 9853 | pw.print("keep awake over "); |
| 9854 | TimeUtils.formatDuration(realtimeSince, pw); |
| 9855 | pw.print(" used "); |
| 9856 | TimeUtils.formatDuration(timeUsed, pw); |
| 9857 | pw.print(" ("); |
| 9858 | pw.print((timeUsed*100)/realtimeSince); |
| 9859 | pw.println("%)"); |
| 9860 | } |
| 9861 | if (r.lastCpuTime != 0) { |
| 9862 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 9863 | pw.print(prefix); |
| 9864 | pw.print(" "); |
| 9865 | pw.print("run cpu over "); |
| 9866 | TimeUtils.formatDuration(uptimeSince, pw); |
| 9867 | pw.print(" used "); |
| 9868 | TimeUtils.formatDuration(timeUsed, pw); |
| 9869 | pw.print(" ("); |
| 9870 | pw.print((timeUsed*100)/uptimeSince); |
| 9871 | pw.println("%)"); |
| 9872 | } |
| 9873 | } |
| 9874 | } |
| 9875 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9876 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9877 | } |
| 9878 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9879 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9880 | ArrayList<ProcessRecord> procs; |
| 9881 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9882 | if (args != null && args.length > start |
| 9883 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9884 | procs = new ArrayList<ProcessRecord>(); |
| 9885 | int pid = -1; |
| 9886 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9887 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9888 | } catch (NumberFormatException e) { |
| 9889 | |
| 9890 | } |
| 9891 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 9892 | ProcessRecord proc = mLruProcesses.get(i); |
| 9893 | if (proc.pid == pid) { |
| 9894 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9895 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9896 | procs.add(proc); |
| 9897 | } |
| 9898 | } |
| 9899 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9900 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9901 | return null; |
| 9902 | } |
| 9903 | } else { |
| 9904 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 9905 | } |
| 9906 | } |
| 9907 | return procs; |
| 9908 | } |
| 9909 | |
| 9910 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 9911 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9912 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9913 | if (procs == null) { |
| 9914 | return; |
| 9915 | } |
| 9916 | |
| 9917 | long uptime = SystemClock.uptimeMillis(); |
| 9918 | long realtime = SystemClock.elapsedRealtime(); |
| 9919 | pw.println("Applications Graphics Acceleration Info:"); |
| 9920 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9921 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9922 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9923 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9924 | if (r.thread != null) { |
| 9925 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9926 | pw.flush(); |
| 9927 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9928 | TransferPipe tp = new TransferPipe(); |
| 9929 | try { |
| 9930 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9931 | tp.go(fd); |
| 9932 | } finally { |
| 9933 | tp.kill(); |
| 9934 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9935 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9936 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9937 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9938 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9939 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9940 | pw.flush(); |
| 9941 | } |
| 9942 | } |
| 9943 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9944 | } |
| 9945 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 9946 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 9947 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 9948 | if (procs == null) { |
| 9949 | return; |
| 9950 | } |
| 9951 | |
| 9952 | pw.println("Applications Database Info:"); |
| 9953 | |
| 9954 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9955 | ProcessRecord r = procs.get(i); |
| 9956 | if (r.thread != null) { |
| 9957 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9958 | pw.flush(); |
| 9959 | try { |
| 9960 | TransferPipe tp = new TransferPipe(); |
| 9961 | try { |
| 9962 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9963 | tp.go(fd); |
| 9964 | } finally { |
| 9965 | tp.kill(); |
| 9966 | } |
| 9967 | } catch (IOException e) { |
| 9968 | pw.println("Failure while dumping the app: " + r); |
| 9969 | pw.flush(); |
| 9970 | } catch (RemoteException e) { |
| 9971 | pw.println("Got a RemoteException while dumping the app " + r); |
| 9972 | pw.flush(); |
| 9973 | } |
| 9974 | } |
| 9975 | } |
| 9976 | } |
| 9977 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9978 | final static class MemItem { |
| 9979 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9980 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9981 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9982 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9983 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9984 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9985 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9986 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9987 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9988 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9989 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9990 | } |
| 9991 | } |
| 9992 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9993 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9994 | boolean sort) { |
| 9995 | if (sort) { |
| 9996 | Collections.sort(items, new Comparator<MemItem>() { |
| 9997 | @Override |
| 9998 | public int compare(MemItem lhs, MemItem rhs) { |
| 9999 | if (lhs.pss < rhs.pss) { |
| 10000 | return 1; |
| 10001 | } else if (lhs.pss > rhs.pss) { |
| 10002 | return -1; |
| 10003 | } |
| 10004 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10005 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10006 | }); |
| 10007 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10008 | |
| 10009 | for (int i=0; i<items.size(); i++) { |
| 10010 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10011 | 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] | 10012 | if (mi.subitems != null) { |
| 10013 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 10014 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10015 | } |
| 10016 | } |
| 10017 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10018 | // These are in KB. |
| 10019 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 10020 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 10021 | 120*1024, 160*1024, 200*1024, |
| 10022 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 10023 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 10024 | }; |
| 10025 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10026 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 10027 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10028 | int start = label.lastIndexOf('.'); |
| 10029 | if (start >= 0) start++; |
| 10030 | else start = 0; |
| 10031 | int end = label.length(); |
| 10032 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 10033 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 10034 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 10035 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10036 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10037 | out.append(label, start, end); |
| 10038 | return; |
| 10039 | } |
| 10040 | } |
| 10041 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10042 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10043 | out.append(label, start, end); |
| 10044 | } |
| 10045 | |
| 10046 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 10047 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 10048 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 10049 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 10050 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 10051 | }; |
| 10052 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 10053 | "System", "Persistent", "Foreground", |
| 10054 | "Visible", "Perceptible", "Heavy Weight", |
| 10055 | "Backup", "A Services", "Home", "Previous", |
| 10056 | "B Services", "Background" |
| 10057 | }; |
| 10058 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10059 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10060 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10061 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10062 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10063 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10064 | |
| 10065 | int opti = 0; |
| 10066 | while (opti < args.length) { |
| 10067 | String opt = args[opti]; |
| 10068 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 10069 | break; |
| 10070 | } |
| 10071 | opti++; |
| 10072 | if ("-a".equals(opt)) { |
| 10073 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10074 | } else if ("--oom".equals(opt)) { |
| 10075 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10076 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10077 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10078 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10079 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10080 | pw.println("If [process] is specified it can be the name or "); |
| 10081 | pw.println("pid of a specific process to dump."); |
| 10082 | return; |
| 10083 | } else { |
| 10084 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 10085 | } |
| 10086 | } |
| 10087 | |
| 10088 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10089 | if (procs == null) { |
| 10090 | return; |
| 10091 | } |
| 10092 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 10093 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10094 | long uptime = SystemClock.uptimeMillis(); |
| 10095 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10096 | |
| 10097 | if (procs.size() == 1 || isCheckinRequest) { |
| 10098 | dumpAll = true; |
| 10099 | } |
| 10100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10101 | if (isCheckinRequest) { |
| 10102 | // short checkin version |
| 10103 | pw.println(uptime + "," + realtime); |
| 10104 | pw.flush(); |
| 10105 | } else { |
| 10106 | pw.println("Applications Memory Usage (kB):"); |
| 10107 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10108 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10109 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10110 | String[] innerArgs = new String[args.length-opti]; |
| 10111 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 10112 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10113 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 10114 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 10115 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 10116 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10117 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 10118 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 10119 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10120 | |
| 10121 | long totalPss = 0; |
| 10122 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10123 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10124 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10125 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10126 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10127 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 10128 | pw.flush(); |
| 10129 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10130 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10131 | if (dumpAll) { |
| 10132 | try { |
| 10133 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 10134 | } catch (RemoteException e) { |
| 10135 | if (!isCheckinRequest) { |
| 10136 | pw.println("Got RemoteException!"); |
| 10137 | pw.flush(); |
| 10138 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10139 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10140 | } else { |
| 10141 | mi = new Debug.MemoryInfo(); |
| 10142 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10143 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10144 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10145 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10146 | long myTotalPss = mi.getTotalPss(); |
| 10147 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10148 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10149 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10150 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10151 | |
| 10152 | nativePss += mi.nativePss; |
| 10153 | dalvikPss += mi.dalvikPss; |
| 10154 | otherPss += mi.otherPss; |
| 10155 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 10156 | long mem = mi.getOtherPss(j); |
| 10157 | miscPss[j] += mem; |
| 10158 | otherPss -= mem; |
| 10159 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10160 | |
| 10161 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10162 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 10163 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10164 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10165 | if (oomProcs[oomIndex] == null) { |
| 10166 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 10167 | } |
| 10168 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10169 | break; |
| 10170 | } |
| 10171 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10172 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10173 | } |
| 10174 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10175 | |
| 10176 | if (!isCheckinRequest && procs.size() > 1) { |
| 10177 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 10178 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10179 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10180 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10181 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10182 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10183 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10184 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10185 | } |
| 10186 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10187 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10188 | for (int j=0; j<oomPss.length; j++) { |
| 10189 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10190 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10191 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10192 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10193 | item.subitems = oomProcs[j]; |
| 10194 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10195 | } |
| 10196 | } |
| 10197 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10198 | if (outTag != null || outStack != null) { |
| 10199 | if (outTag != null) { |
| 10200 | appendMemBucket(outTag, totalPss, "total", false); |
| 10201 | } |
| 10202 | if (outStack != null) { |
| 10203 | appendMemBucket(outStack, totalPss, "total", true); |
| 10204 | } |
| 10205 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10206 | for (int i=0; i<oomMems.size(); i++) { |
| 10207 | MemItem miCat = oomMems.get(i); |
| 10208 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10209 | continue; |
| 10210 | } |
| 10211 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10212 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10213 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10214 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10215 | outTag.append(" / "); |
| 10216 | } |
| 10217 | if (outStack != null) { |
| 10218 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10219 | if (firstLine) { |
| 10220 | outStack.append(":"); |
| 10221 | firstLine = false; |
| 10222 | } |
| 10223 | outStack.append("\n\t at "); |
| 10224 | } else { |
| 10225 | outStack.append("$"); |
| 10226 | } |
| 10227 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10228 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10229 | MemItem mi = miCat.subitems.get(j); |
| 10230 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10231 | if (outTag != null) { |
| 10232 | outTag.append(" "); |
| 10233 | } |
| 10234 | if (outStack != null) { |
| 10235 | outStack.append("$"); |
| 10236 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10237 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10238 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10239 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10240 | } |
| 10241 | if (outStack != null) { |
| 10242 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10243 | } |
| 10244 | } |
| 10245 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10246 | outStack.append("("); |
| 10247 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10248 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10249 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10250 | outStack.append(":"); |
| 10251 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10252 | } |
| 10253 | } |
| 10254 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10255 | } |
| 10256 | } |
| 10257 | } |
| 10258 | } |
| 10259 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10260 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10261 | pw.println(); |
| 10262 | pw.println("Total PSS by process:"); |
| 10263 | dumpMemItems(pw, " ", procMems, true); |
| 10264 | pw.println(); |
| 10265 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10266 | pw.println("Total PSS by OOM adjustment:"); |
| 10267 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10268 | if (!oomOnly) { |
| 10269 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10270 | out.println(); |
| 10271 | out.println("Total PSS by category:"); |
| 10272 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10273 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10274 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10275 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10277 | } |
| 10278 | |
| 10279 | /** |
| 10280 | * Searches array of arguments for the specified string |
| 10281 | * @param args array of argument strings |
| 10282 | * @param value value to search for |
| 10283 | * @return true if the value is contained in the array |
| 10284 | */ |
| 10285 | private static boolean scanArgs(String[] args, String value) { |
| 10286 | if (args != null) { |
| 10287 | for (String arg : args) { |
| 10288 | if (value.equals(arg)) { |
| 10289 | return true; |
| 10290 | } |
| 10291 | } |
| 10292 | } |
| 10293 | return false; |
| 10294 | } |
| 10295 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10296 | private final void killServicesLocked(ProcessRecord app, |
| 10297 | boolean allowRestart) { |
| 10298 | // Report disconnected services. |
| 10299 | if (false) { |
| 10300 | // XXX we are letting the client link to the service for |
| 10301 | // death notifications. |
| 10302 | if (app.services.size() > 0) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10303 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10304 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10305 | ServiceRecord r = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10306 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10307 | Iterator<ArrayList<ConnectionRecord>> jt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10308 | = r.connections.values().iterator(); |
| 10309 | while (jt.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10310 | ArrayList<ConnectionRecord> cl = jt.next(); |
| 10311 | for (int i=0; i<cl.size(); i++) { |
| 10312 | ConnectionRecord c = cl.get(i); |
| 10313 | if (c.binding.client != app) { |
| 10314 | try { |
| 10315 | //c.conn.connected(r.className, null); |
| 10316 | } catch (Exception e) { |
| 10317 | // todo: this should be asynchronous! |
| 10318 | Slog.w(TAG, "Exception thrown disconnected servce " |
| 10319 | + r.shortName |
| 10320 | + " from app " + app.processName, e); |
| 10321 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10322 | } |
| 10323 | } |
| 10324 | } |
| 10325 | } |
| 10326 | } |
| 10327 | } |
| 10328 | } |
| 10329 | |
| 10330 | // Clean up any connections this application has to other services. |
| 10331 | if (app.connections.size() > 0) { |
| 10332 | Iterator<ConnectionRecord> it = app.connections.iterator(); |
| 10333 | while (it.hasNext()) { |
| 10334 | ConnectionRecord r = it.next(); |
| 10335 | removeConnectionLocked(r, app, null); |
| 10336 | } |
| 10337 | } |
| 10338 | app.connections.clear(); |
| 10339 | |
| 10340 | if (app.services.size() != 0) { |
| 10341 | // Any services running in the application need to be placed |
| 10342 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10343 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10344 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10345 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10346 | synchronized (sr.stats.getBatteryStats()) { |
| 10347 | sr.stats.stopLaunchedLocked(); |
| 10348 | } |
| 10349 | sr.app = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10350 | sr.isolatedProc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10351 | sr.executeNesting = 0; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10352 | if (mStoppingServices.remove(sr)) { |
| 10353 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
| 10354 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10355 | |
| 10356 | boolean hasClients = sr.bindings.size() > 0; |
| 10357 | if (hasClients) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10358 | Iterator<IntentBindRecord> bindings |
| 10359 | = sr.bindings.values().iterator(); |
| 10360 | while (bindings.hasNext()) { |
| 10361 | IntentBindRecord b = bindings.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10362 | if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10363 | + ": shouldUnbind=" + b.hasBound); |
| 10364 | b.binder = null; |
| 10365 | b.requested = b.received = b.hasBound = false; |
| 10366 | } |
| 10367 | } |
| 10368 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10369 | if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags |
| 10370 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10371 | Slog.w(TAG, "Service crashed " + sr.crashCount |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10372 | + " times, stopping: " + sr); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10373 | EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10374 | sr.crashCount, sr.shortName, app.pid); |
| 10375 | bringDownServiceLocked(sr, true); |
| 10376 | } else if (!allowRestart) { |
| 10377 | bringDownServiceLocked(sr, true); |
| 10378 | } else { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10379 | boolean canceled = scheduleServiceRestartLocked(sr, true); |
| 10380 | |
| 10381 | // Should the service remain running? Note that in the |
| 10382 | // extreme case of so many attempts to deliver a command |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10383 | // that it failed we also will stop it here. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10384 | if (sr.startRequested && (sr.stopIfKilled || canceled)) { |
| 10385 | if (sr.pendingStarts.size() == 0) { |
| 10386 | sr.startRequested = false; |
| 10387 | if (!hasClients) { |
| 10388 | // Whoops, no reason to restart! |
| 10389 | bringDownServiceLocked(sr, true); |
| 10390 | } |
| 10391 | } |
| 10392 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10393 | } |
| 10394 | } |
| 10395 | |
| 10396 | if (!allowRestart) { |
| 10397 | app.services.clear(); |
| 10398 | } |
| 10399 | } |
| 10400 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10401 | // Make sure we have no more records on the stopping list. |
| 10402 | int i = mStoppingServices.size(); |
| 10403 | while (i > 0) { |
| 10404 | i--; |
| 10405 | ServiceRecord sr = mStoppingServices.get(i); |
| 10406 | if (sr.app == app) { |
| 10407 | mStoppingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10408 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10409 | } |
| 10410 | } |
| 10411 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10412 | app.executingServices.clear(); |
| 10413 | } |
| 10414 | |
| 10415 | private final void removeDyingProviderLocked(ProcessRecord proc, |
| 10416 | ContentProviderRecord cpr) { |
| 10417 | synchronized (cpr) { |
| 10418 | cpr.launchingApp = null; |
| 10419 | cpr.notifyAll(); |
| 10420 | } |
| 10421 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10422 | mProviderMap.removeProviderByClass(cpr.name, UserId.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10423 | String names[] = cpr.info.authority.split(";"); |
| 10424 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10425 | mProviderMap.removeProviderByName(names[j], UserId.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10426 | } |
| 10427 | |
| 10428 | Iterator<ProcessRecord> cit = cpr.clients.iterator(); |
| 10429 | while (cit.hasNext()) { |
| 10430 | ProcessRecord capp = cit.next(); |
| 10431 | if (!capp.persistent && capp.thread != null |
| 10432 | && capp.pid != 0 |
| 10433 | && capp.pid != MY_PID) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10434 | Slog.i(TAG, "Kill " + capp.processName |
| 10435 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10436 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10437 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10438 | capp.processName, capp.setAdj, "dying provider " |
| 10439 | + cpr.name.toShortString()); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10440 | Process.killProcessQuiet(capp.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10441 | } |
| 10442 | } |
| 10443 | |
| 10444 | mLaunchingProviders.remove(cpr); |
| 10445 | } |
| 10446 | |
| 10447 | /** |
| 10448 | * Main code for cleaning up a process when it has gone away. This is |
| 10449 | * called both as a result of the process dying, or directly when stopping |
| 10450 | * a process when running in single process mode. |
| 10451 | */ |
| 10452 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10453 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10454 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10455 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10456 | } |
| 10457 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10458 | mProcessesToGc.remove(app); |
| 10459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10460 | // Dismiss any open dialogs. |
| 10461 | if (app.crashDialog != null) { |
| 10462 | app.crashDialog.dismiss(); |
| 10463 | app.crashDialog = null; |
| 10464 | } |
| 10465 | if (app.anrDialog != null) { |
| 10466 | app.anrDialog.dismiss(); |
| 10467 | app.anrDialog = null; |
| 10468 | } |
| 10469 | if (app.waitDialog != null) { |
| 10470 | app.waitDialog.dismiss(); |
| 10471 | app.waitDialog = null; |
| 10472 | } |
| 10473 | |
| 10474 | app.crashing = false; |
| 10475 | app.notResponding = false; |
| 10476 | |
| 10477 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10478 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10479 | app.thread = null; |
| 10480 | app.forcingToForeground = null; |
| 10481 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10482 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10483 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10484 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10485 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10486 | killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10487 | |
| 10488 | boolean restart = false; |
| 10489 | |
| 10490 | int NL = mLaunchingProviders.size(); |
| 10491 | |
| 10492 | // Remove published content providers. |
| 10493 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10494 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10495 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10496 | ContentProviderRecord cpr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10497 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10498 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10499 | |
| 10500 | // See if someone is waiting for this provider... in which |
| 10501 | // case we don't remove it, but just let it restart. |
| 10502 | int i = 0; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10503 | if (!app.bad && allowRestart) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10504 | for (; i<NL; i++) { |
| 10505 | if (mLaunchingProviders.get(i) == cpr) { |
| 10506 | restart = true; |
| 10507 | break; |
| 10508 | } |
| 10509 | } |
| 10510 | } else { |
| 10511 | i = NL; |
| 10512 | } |
| 10513 | |
| 10514 | if (i >= NL) { |
| 10515 | removeDyingProviderLocked(app, cpr); |
| 10516 | NL = mLaunchingProviders.size(); |
| 10517 | } |
| 10518 | } |
| 10519 | app.pubProviders.clear(); |
| 10520 | } |
| 10521 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10522 | // Take care of any launching providers waiting for this process. |
| 10523 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10524 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10525 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10526 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10527 | // Unregister from connected content providers. |
| 10528 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 10529 | Iterator it = app.conProviders.keySet().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10530 | while (it.hasNext()) { |
| 10531 | ContentProviderRecord cpr = (ContentProviderRecord)it.next(); |
| 10532 | cpr.clients.remove(app); |
| 10533 | } |
| 10534 | app.conProviders.clear(); |
| 10535 | } |
| 10536 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10537 | // At this point there may be remaining entries in mLaunchingProviders |
| 10538 | // where we were the only one waiting, so they are no longer of use. |
| 10539 | // Look for these and clean up if found. |
| 10540 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10541 | // the actual situation to identify what is actually going on. |
| 10542 | if (false) { |
| 10543 | for (int i=0; i<NL; i++) { |
| 10544 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10545 | mLaunchingProviders.get(i); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 10546 | if (cpr.clients.size() <= 0 && !cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10547 | synchronized (cpr) { |
| 10548 | cpr.launchingApp = null; |
| 10549 | cpr.notifyAll(); |
| 10550 | } |
| 10551 | } |
| 10552 | } |
| 10553 | } |
| 10554 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10555 | skipCurrentReceiverLocked(app); |
| 10556 | |
| 10557 | // Unregister any receivers. |
| 10558 | if (app.receivers.size() > 0) { |
| 10559 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10560 | while (it.hasNext()) { |
| 10561 | removeReceiverLocked(it.next()); |
| 10562 | } |
| 10563 | app.receivers.clear(); |
| 10564 | } |
| 10565 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10566 | // If the app is undergoing backup, tell the backup manager about it |
| 10567 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10568 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10569 | try { |
| 10570 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10571 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10572 | bm.agentDisconnected(app.info.packageName); |
| 10573 | } catch (RemoteException e) { |
| 10574 | // can't happen; backup manager is local |
| 10575 | } |
| 10576 | } |
| 10577 | |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10578 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10579 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10580 | // If the caller is restarting this app, then leave it in its |
| 10581 | // current lists and let the caller take care of it. |
| 10582 | if (restarting) { |
| 10583 | return; |
| 10584 | } |
| 10585 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10586 | if (!app.persistent || app.isolated) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10587 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10588 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10589 | mProcessNames.remove(app.processName, app.uid); |
| 10590 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10591 | if (mHeavyWeightProcess == app) { |
| 10592 | mHeavyWeightProcess = null; |
| 10593 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 10594 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10595 | } else if (!app.removed) { |
| 10596 | // This app is persistent, so we need to keep its record around. |
| 10597 | // If it is not already on the pending app list, add it there |
| 10598 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10599 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10600 | mPersistentStartingProcesses.add(app); |
| 10601 | restart = true; |
| 10602 | } |
| 10603 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10604 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 10605 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10606 | mProcessesOnHold.remove(app); |
| 10607 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10608 | if (app == mHomeProcess) { |
| 10609 | mHomeProcess = null; |
| 10610 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10611 | if (app == mPreviousProcess) { |
| 10612 | mPreviousProcess = null; |
| 10613 | } |
| 10614 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10615 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10616 | // We have components that still need to be running in the |
| 10617 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10618 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10619 | startProcessLocked(app, "restart", app.processName); |
| 10620 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10621 | // Goodbye! |
| 10622 | synchronized (mPidsSelfLocked) { |
| 10623 | mPidsSelfLocked.remove(app.pid); |
| 10624 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10625 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10626 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10627 | } |
| 10628 | } |
| 10629 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10630 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10631 | // Look through the content providers we are waiting to have launched, |
| 10632 | // and if any run in this process then either schedule a restart of |
| 10633 | // the process or kill the client waiting for it if this process has |
| 10634 | // gone bad. |
| 10635 | int NL = mLaunchingProviders.size(); |
| 10636 | boolean restart = false; |
| 10637 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10638 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10639 | if (cpr.launchingApp == app) { |
| 10640 | if (!alwaysBad && !app.bad) { |
| 10641 | restart = true; |
| 10642 | } else { |
| 10643 | removeDyingProviderLocked(app, cpr); |
| 10644 | NL = mLaunchingProviders.size(); |
| 10645 | } |
| 10646 | } |
| 10647 | } |
| 10648 | return restart; |
| 10649 | } |
| 10650 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10651 | // ========================================================= |
| 10652 | // SERVICES |
| 10653 | // ========================================================= |
| 10654 | |
| 10655 | ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { |
| 10656 | ActivityManager.RunningServiceInfo info = |
| 10657 | new ActivityManager.RunningServiceInfo(); |
| 10658 | info.service = r.name; |
| 10659 | if (r.app != null) { |
| 10660 | info.pid = r.app.pid; |
| 10661 | } |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10662 | info.uid = r.appInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10663 | info.process = r.processName; |
| 10664 | info.foreground = r.isForeground; |
| 10665 | info.activeSince = r.createTime; |
| 10666 | info.started = r.startRequested; |
| 10667 | info.clientCount = r.connections.size(); |
| 10668 | info.crashCount = r.crashCount; |
| 10669 | info.lastActivityTime = r.lastActivity; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10670 | if (r.isForeground) { |
| 10671 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; |
| 10672 | } |
| 10673 | if (r.startRequested) { |
| 10674 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; |
| 10675 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 10676 | if (r.app != null && r.app.pid == MY_PID) { |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10677 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; |
| 10678 | } |
| 10679 | if (r.app != null && r.app.persistent) { |
| 10680 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; |
| 10681 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10682 | |
| 10683 | for (ArrayList<ConnectionRecord> connl : r.connections.values()) { |
| 10684 | for (int i=0; i<connl.size(); i++) { |
| 10685 | ConnectionRecord conn = connl.get(i); |
| 10686 | if (conn.clientLabel != 0) { |
| 10687 | info.clientPackage = conn.binding.client.info.packageName; |
| 10688 | info.clientLabel = conn.clientLabel; |
| 10689 | return info; |
| 10690 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10691 | } |
| 10692 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10693 | return info; |
| 10694 | } |
| 10695 | |
| 10696 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10697 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10698 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10699 | synchronized (this) { |
| 10700 | ArrayList<ActivityManager.RunningServiceInfo> res |
| 10701 | = new ArrayList<ActivityManager.RunningServiceInfo>(); |
| 10702 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10703 | int userId = UserId.getUserId(Binder.getCallingUid()); |
| 10704 | if (mServiceMap.getAllServices(userId).size() > 0) { |
| 10705 | Iterator<ServiceRecord> it |
| 10706 | = mServiceMap.getAllServices(userId).iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10707 | while (it.hasNext() && res.size() < maxNum) { |
| 10708 | res.add(makeRunningServiceInfoLocked(it.next())); |
| 10709 | } |
| 10710 | } |
| 10711 | |
| 10712 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 10713 | ServiceRecord r = mRestartingServices.get(i); |
| 10714 | ActivityManager.RunningServiceInfo info = |
| 10715 | makeRunningServiceInfoLocked(r); |
| 10716 | info.restarting = r.nextRestartTime; |
| 10717 | res.add(info); |
| 10718 | } |
| 10719 | |
| 10720 | return res; |
| 10721 | } |
| 10722 | } |
| 10723 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10724 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10725 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10726 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10727 | int userId = UserId.getUserId(Binder.getCallingUid()); |
| 10728 | ServiceRecord r = mServiceMap.getServiceByName(name, userId); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10729 | if (r != null) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10730 | for (ArrayList<ConnectionRecord> conn : r.connections.values()) { |
| 10731 | for (int i=0; i<conn.size(); i++) { |
| 10732 | if (conn.get(i).clientIntent != null) { |
| 10733 | return conn.get(i).clientIntent; |
| 10734 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10735 | } |
| 10736 | } |
| 10737 | } |
| 10738 | } |
| 10739 | return null; |
| 10740 | } |
| 10741 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10742 | private final ServiceRecord findServiceLocked(ComponentName name, |
| 10743 | IBinder token) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10744 | ServiceRecord r = mServiceMap.getServiceByName(name, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10745 | return r == token ? r : null; |
| 10746 | } |
| 10747 | |
| 10748 | private final class ServiceLookupResult { |
| 10749 | final ServiceRecord record; |
| 10750 | final String permission; |
| 10751 | |
| 10752 | ServiceLookupResult(ServiceRecord _record, String _permission) { |
| 10753 | record = _record; |
| 10754 | permission = _permission; |
| 10755 | } |
| 10756 | }; |
| 10757 | |
| 10758 | private ServiceLookupResult findServiceLocked(Intent service, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 10759 | String resolvedType, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10760 | ServiceRecord r = null; |
| 10761 | if (service.getComponent() != null) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 10762 | r = mServiceMap.getServiceByName(service.getComponent(), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10763 | } |
| 10764 | if (r == null) { |
| 10765 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 10766 | r = mServiceMap.getServiceByIntent(filter, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10767 | } |
| 10768 | |
| 10769 | if (r == null) { |
| 10770 | try { |
| 10771 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10772 | AppGlobals.getPackageManager().resolveService( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 10773 | service, resolvedType, 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10774 | ServiceInfo sInfo = |
| 10775 | rInfo != null ? rInfo.serviceInfo : null; |
| 10776 | if (sInfo == null) { |
| 10777 | return null; |
| 10778 | } |
| 10779 | |
| 10780 | ComponentName name = new ComponentName( |
| 10781 | sInfo.applicationInfo.packageName, sInfo.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10782 | r = mServiceMap.getServiceByName(name, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10783 | } catch (RemoteException ex) { |
| 10784 | // pm is in same process, this will never happen. |
| 10785 | } |
| 10786 | } |
| 10787 | if (r != null) { |
| 10788 | int callingPid = Binder.getCallingPid(); |
| 10789 | int callingUid = Binder.getCallingUid(); |
| 10790 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10791 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10792 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10793 | if (!r.exported) { |
| 10794 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 10795 | + " from pid=" + callingPid |
| 10796 | + ", uid=" + callingUid |
| 10797 | + " that is not exported from uid " + r.appInfo.uid); |
| 10798 | return new ServiceLookupResult(null, "not exported from uid " |
| 10799 | + r.appInfo.uid); |
| 10800 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10801 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10802 | + " from pid=" + callingPid |
| 10803 | + ", uid=" + callingUid |
| 10804 | + " requires " + r.permission); |
| 10805 | return new ServiceLookupResult(null, r.permission); |
| 10806 | } |
| 10807 | return new ServiceLookupResult(r, null); |
| 10808 | } |
| 10809 | return null; |
| 10810 | } |
| 10811 | |
| 10812 | private class ServiceRestarter implements Runnable { |
| 10813 | private ServiceRecord mService; |
| 10814 | |
| 10815 | void setService(ServiceRecord service) { |
| 10816 | mService = service; |
| 10817 | } |
| 10818 | |
| 10819 | public void run() { |
| 10820 | synchronized(ActivityManagerService.this) { |
| 10821 | performServiceRestartLocked(mService); |
| 10822 | } |
| 10823 | } |
| 10824 | } |
| 10825 | |
| 10826 | private ServiceLookupResult retrieveServiceLocked(Intent service, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10827 | String resolvedType, int callingPid, int callingUid, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10828 | ServiceRecord r = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10829 | if (DEBUG_SERVICE) |
| 10830 | Slog.v(TAG, "retrieveServiceLocked: " + service + " type=" + resolvedType |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10831 | + " callingUid=" + callingUid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10832 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10833 | if (service.getComponent() != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10834 | r = mServiceMap.getServiceByName(service.getComponent(), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10835 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10836 | if (r == null) { |
| 10837 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10838 | r = mServiceMap.getServiceByIntent(filter, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10839 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10840 | if (r == null) { |
| 10841 | try { |
| 10842 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10843 | AppGlobals.getPackageManager().resolveService( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 10844 | service, resolvedType, STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10845 | ServiceInfo sInfo = |
| 10846 | rInfo != null ? rInfo.serviceInfo : null; |
| 10847 | if (sInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10848 | Slog.w(TAG, "Unable to start service " + service + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10849 | ": not found"); |
| 10850 | return null; |
| 10851 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10852 | if (userId > 0) { |
| 10853 | sInfo.applicationInfo = getAppInfoForUser(sInfo.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10854 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10855 | ComponentName name = new ComponentName( |
| 10856 | sInfo.applicationInfo.packageName, sInfo.name); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10857 | r = mServiceMap.getServiceByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10858 | if (r == null) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10859 | Intent.FilterComparison filter = new Intent.FilterComparison( |
| 10860 | service.cloneFilter()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10861 | ServiceRestarter res = new ServiceRestarter(); |
| 10862 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 10863 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 10864 | synchronized (stats) { |
| 10865 | ss = stats.getServiceStatsLocked( |
| 10866 | sInfo.applicationInfo.uid, sInfo.packageName, |
| 10867 | sInfo.name); |
| 10868 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 10869 | r = new ServiceRecord(this, ss, name, filter, sInfo, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10870 | res.setService(r); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10871 | mServiceMap.putServiceByName(name, UserId.getUserId(r.appInfo.uid), r); |
| 10872 | mServiceMap.putServiceByIntent(filter, UserId.getUserId(r.appInfo.uid), r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10873 | |
| 10874 | // Make sure this component isn't in the pending list. |
| 10875 | int N = mPendingServices.size(); |
| 10876 | for (int i=0; i<N; i++) { |
| 10877 | ServiceRecord pr = mPendingServices.get(i); |
| 10878 | if (pr.name.equals(name)) { |
| 10879 | mPendingServices.remove(i); |
| 10880 | i--; |
| 10881 | N--; |
| 10882 | } |
| 10883 | } |
| 10884 | } |
| 10885 | } catch (RemoteException ex) { |
| 10886 | // pm is in same process, this will never happen. |
| 10887 | } |
| 10888 | } |
| 10889 | if (r != null) { |
| 10890 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10891 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10892 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10893 | if (!r.exported) { |
| 10894 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 10895 | + " from pid=" + callingPid |
| 10896 | + ", uid=" + callingUid |
| 10897 | + " that is not exported from uid " + r.appInfo.uid); |
| 10898 | return new ServiceLookupResult(null, "not exported from uid " |
| 10899 | + r.appInfo.uid); |
| 10900 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10901 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10902 | + " from pid=" + callingPid |
| 10903 | + ", uid=" + callingUid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10904 | + " requires " + r.permission); |
| 10905 | return new ServiceLookupResult(null, r.permission); |
| 10906 | } |
| 10907 | return new ServiceLookupResult(r, null); |
| 10908 | } |
| 10909 | return null; |
| 10910 | } |
| 10911 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10912 | private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { |
| 10913 | if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " |
| 10914 | + why + " of " + r + " in app " + r.app); |
| 10915 | else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " |
| 10916 | + why + " of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10917 | long now = SystemClock.uptimeMillis(); |
| 10918 | if (r.executeNesting == 0 && r.app != null) { |
| 10919 | if (r.app.executingServices.size() == 0) { |
| 10920 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 10921 | msg.obj = r.app; |
| 10922 | mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); |
| 10923 | } |
| 10924 | r.app.executingServices.add(r); |
| 10925 | } |
| 10926 | r.executeNesting++; |
| 10927 | r.executingStart = now; |
| 10928 | } |
| 10929 | |
| 10930 | private final void sendServiceArgsLocked(ServiceRecord r, |
| 10931 | boolean oomAdjusted) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10932 | final int N = r.pendingStarts.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10933 | if (N == 0) { |
| 10934 | return; |
| 10935 | } |
| 10936 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10937 | while (r.pendingStarts.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10938 | try { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10939 | ServiceRecord.StartItem si = r.pendingStarts.remove(0); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10940 | if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " |
| 10941 | + r + " " + r.intent + " args=" + si.intent); |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10942 | if (si.intent == null && N > 1) { |
| 10943 | // If somehow we got a dummy null intent in the middle, |
| 10944 | // then skip it. DO NOT skip a null intent when it is |
| 10945 | // the only one in the list -- this is to support the |
| 10946 | // onStartCommand(null) case. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10947 | continue; |
| 10948 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10949 | si.deliveredTime = SystemClock.uptimeMillis(); |
| 10950 | r.deliveredStarts.add(si); |
| 10951 | si.deliveryCount++; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 10952 | if (si.neededGrants != null) { |
| 10953 | grantUriPermissionUncheckedFromIntentLocked(si.neededGrants, |
| 10954 | si.getUriPermissionsLocked()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10955 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10956 | bumpServiceExecutingLocked(r, "start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10957 | if (!oomAdjusted) { |
| 10958 | oomAdjusted = true; |
| 10959 | updateOomAdjLocked(r.app); |
| 10960 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10961 | int flags = 0; |
| 10962 | if (si.deliveryCount > 0) { |
| 10963 | flags |= Service.START_FLAG_RETRY; |
| 10964 | } |
| 10965 | if (si.doneExecutingCount > 0) { |
| 10966 | flags |= Service.START_FLAG_REDELIVERY; |
| 10967 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10968 | r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10969 | } catch (RemoteException e) { |
| 10970 | // Remote process gone... we'll let the normal cleanup take |
| 10971 | // care of this. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10972 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10973 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10974 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10975 | Slog.w(TAG, "Unexpected exception", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10976 | break; |
| 10977 | } |
| 10978 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10979 | } |
| 10980 | |
| 10981 | private final boolean requestServiceBindingLocked(ServiceRecord r, |
| 10982 | IntentBindRecord i, boolean rebind) { |
| 10983 | if (r.app == null || r.app.thread == null) { |
| 10984 | // If service is not currently running, can't yet bind. |
| 10985 | return false; |
| 10986 | } |
| 10987 | if ((!i.requested || rebind) && i.apps.size() > 0) { |
| 10988 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10989 | bumpServiceExecutingLocked(r, "bind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10990 | r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); |
| 10991 | if (!rebind) { |
| 10992 | i.requested = true; |
| 10993 | } |
| 10994 | i.hasBound = true; |
| 10995 | i.doRebind = false; |
| 10996 | } catch (RemoteException e) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10997 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while binding " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10998 | return false; |
| 10999 | } |
| 11000 | } |
| 11001 | return true; |
| 11002 | } |
| 11003 | |
| 11004 | private final void requestServiceBindingsLocked(ServiceRecord r) { |
| 11005 | Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); |
| 11006 | while (bindings.hasNext()) { |
| 11007 | IntentBindRecord i = bindings.next(); |
| 11008 | if (!requestServiceBindingLocked(r, i, false)) { |
| 11009 | break; |
| 11010 | } |
| 11011 | } |
| 11012 | } |
| 11013 | |
| 11014 | private final void realStartServiceLocked(ServiceRecord r, |
| 11015 | ProcessRecord app) throws RemoteException { |
| 11016 | if (app.thread == null) { |
| 11017 | throw new RemoteException(); |
| 11018 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11019 | if (DEBUG_MU) |
| 11020 | Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11021 | + ", ProcessRecord.uid = " + app.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11022 | r.app = app; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11023 | r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11024 | |
| 11025 | app.services.add(r); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11026 | bumpServiceExecutingLocked(r, "create"); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11027 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11028 | |
| 11029 | boolean created = false; |
| 11030 | try { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 11031 | mStringBuilder.setLength(0); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 11032 | r.intent.getIntent().toShortString(mStringBuilder, true, false, true, false); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11033 | EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11034 | System.identityHashCode(r), r.shortName, |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 11035 | mStringBuilder.toString(), r.app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11036 | synchronized (r.stats.getBatteryStats()) { |
| 11037 | r.stats.startLaunchedLocked(); |
| 11038 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 11039 | ensurePackageDexOpt(r.serviceInfo.packageName); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11040 | app.thread.scheduleCreateService(r, r.serviceInfo, |
| 11041 | compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11042 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11043 | created = true; |
| 11044 | } finally { |
| 11045 | if (!created) { |
| 11046 | app.services.remove(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11047 | scheduleServiceRestartLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11048 | } |
| 11049 | } |
| 11050 | |
| 11051 | requestServiceBindingsLocked(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11052 | |
| 11053 | // If the service is in the started state, and there are no |
| 11054 | // pending arguments, then fake up one so its onStartCommand() will |
| 11055 | // be called. |
| 11056 | if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11057 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 11058 | null, null)); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11059 | } |
| 11060 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11061 | sendServiceArgsLocked(r, true); |
| 11062 | } |
| 11063 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11064 | private final boolean scheduleServiceRestartLocked(ServiceRecord r, |
| 11065 | boolean allowCancel) { |
| 11066 | boolean canceled = false; |
| 11067 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11068 | final long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11069 | long minDuration = SERVICE_RESTART_DURATION; |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 11070 | long resetTime = SERVICE_RESET_RUN_DURATION; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11071 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 11072 | if ((r.serviceInfo.applicationInfo.flags |
| 11073 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 11074 | minDuration /= 4; |
| 11075 | } |
| 11076 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11077 | // Any delivered but not yet finished starts should be put back |
| 11078 | // on the pending list. |
| 11079 | final int N = r.deliveredStarts.size(); |
| 11080 | if (N > 0) { |
| 11081 | for (int i=N-1; i>=0; i--) { |
| 11082 | ServiceRecord.StartItem si = r.deliveredStarts.get(i); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11083 | si.removeUriPermissionsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11084 | if (si.intent == null) { |
| 11085 | // We'll generate this again if needed. |
| 11086 | } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT |
| 11087 | && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { |
| 11088 | r.pendingStarts.add(0, si); |
| 11089 | long dur = SystemClock.uptimeMillis() - si.deliveredTime; |
| 11090 | dur *= 2; |
| 11091 | if (minDuration < dur) minDuration = dur; |
| 11092 | if (resetTime < dur) resetTime = dur; |
| 11093 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11094 | Slog.w(TAG, "Canceling start item " + si.intent + " in service " |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11095 | + r.name); |
| 11096 | canceled = true; |
| 11097 | } |
| 11098 | } |
| 11099 | r.deliveredStarts.clear(); |
| 11100 | } |
| 11101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11102 | r.totalRestartCount++; |
| 11103 | if (r.restartDelay == 0) { |
| 11104 | r.restartCount++; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11105 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11106 | } else { |
| 11107 | // If it has been a "reasonably long time" since the service |
| 11108 | // was started, then reset our restart duration back to |
| 11109 | // the beginning, so we don't infinitely increase the duration |
| 11110 | // on a service that just occasionally gets killed (which is |
| 11111 | // a normal case, due to process being killed to reclaim memory). |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11112 | if (now > (r.restartTime+resetTime)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11113 | r.restartCount = 1; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11114 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11115 | } else { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 11116 | if ((r.serviceInfo.applicationInfo.flags |
| 11117 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 11118 | // Services in peristent processes will restart much more |
| 11119 | // quickly, since they are pretty important. (Think SystemUI). |
| 11120 | r.restartDelay += minDuration/2; |
| 11121 | } else { |
| 11122 | r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; |
| 11123 | if (r.restartDelay < minDuration) { |
| 11124 | r.restartDelay = minDuration; |
| 11125 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11126 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11127 | } |
| 11128 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11129 | |
| 11130 | r.nextRestartTime = now + r.restartDelay; |
| 11131 | |
| 11132 | // Make sure that we don't end up restarting a bunch of services |
| 11133 | // all at the same time. |
| 11134 | boolean repeat; |
| 11135 | do { |
| 11136 | repeat = false; |
| 11137 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 11138 | ServiceRecord r2 = mRestartingServices.get(i); |
| 11139 | if (r2 != r && r.nextRestartTime |
| 11140 | >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) |
| 11141 | && r.nextRestartTime |
| 11142 | < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { |
| 11143 | r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; |
| 11144 | r.restartDelay = r.nextRestartTime - now; |
| 11145 | repeat = true; |
| 11146 | break; |
| 11147 | } |
| 11148 | } |
| 11149 | } while (repeat); |
| 11150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11151 | if (!mRestartingServices.contains(r)) { |
| 11152 | mRestartingServices.add(r); |
| 11153 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11154 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11155 | r.cancelNotification(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11157 | mHandler.removeCallbacks(r.restarter); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11158 | mHandler.postAtTime(r.restarter, r.nextRestartTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11159 | r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11160 | Slog.w(TAG, "Scheduling restart of crashed service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11161 | + r.shortName + " in " + r.restartDelay + "ms"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11162 | EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11163 | r.shortName, r.restartDelay); |
| 11164 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11165 | return canceled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11166 | } |
| 11167 | |
| 11168 | final void performServiceRestartLocked(ServiceRecord r) { |
| 11169 | if (!mRestartingServices.contains(r)) { |
| 11170 | return; |
| 11171 | } |
| 11172 | bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); |
| 11173 | } |
| 11174 | |
| 11175 | private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { |
| 11176 | if (r.restartDelay == 0) { |
| 11177 | return false; |
| 11178 | } |
| 11179 | r.resetRestartCounter(); |
| 11180 | mRestartingServices.remove(r); |
| 11181 | mHandler.removeCallbacks(r.restarter); |
| 11182 | return true; |
| 11183 | } |
| 11184 | |
| 11185 | private final boolean bringUpServiceLocked(ServiceRecord r, |
| 11186 | int intentFlags, boolean whileRestarting) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11187 | //Slog.i(TAG, "Bring up service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11188 | //r.dump(" "); |
| 11189 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 11190 | if (r.app != null && r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11191 | sendServiceArgsLocked(r, false); |
| 11192 | return true; |
| 11193 | } |
| 11194 | |
| 11195 | if (!whileRestarting && r.restartDelay > 0) { |
| 11196 | // If waiting for a restart, then do nothing. |
| 11197 | return true; |
| 11198 | } |
| 11199 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11200 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up " + r + " " + r.intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11201 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11202 | // We are now bringing the service up, so no longer in the |
| 11203 | // restarting state. |
| 11204 | mRestartingServices.remove(r); |
| 11205 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11206 | // Service is now being launched, its package can't be stopped. |
| 11207 | try { |
| 11208 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11209 | r.packageName, false, r.userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11210 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11211 | } catch (IllegalArgumentException e) { |
| 11212 | Slog.w(TAG, "Failed trying to unstop package " |
| 11213 | + r.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11214 | } |
| 11215 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11216 | final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11217 | final String appName = r.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11218 | ProcessRecord app; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11219 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11220 | if (!isolated) { |
| 11221 | app = getProcessRecordLocked(appName, r.appInfo.uid); |
| 11222 | if (DEBUG_MU) |
| 11223 | Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid + " app=" + app); |
| 11224 | if (app != null && app.thread != null) { |
| 11225 | try { |
| 11226 | app.addPackage(r.appInfo.packageName); |
| 11227 | realStartServiceLocked(r, app); |
| 11228 | return true; |
| 11229 | } catch (RemoteException e) { |
| 11230 | Slog.w(TAG, "Exception when starting service " + r.shortName, e); |
| 11231 | } |
| 11232 | |
| 11233 | // If a dead object exception was thrown -- fall through to |
| 11234 | // restart the application. |
| 11235 | } |
| 11236 | } else { |
| 11237 | // If this service runs in an isolated process, then each time |
| 11238 | // we call startProcessLocked() we will get a new isolated |
| 11239 | // process, starting another process if we are currently waiting |
| 11240 | // for a previous process to come up. To deal with this, we store |
| 11241 | // in the service any current isolated process it is running in or |
| 11242 | // waiting to have come up. |
| 11243 | app = r.isolatedProc; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11244 | } |
| 11245 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 11246 | // Not running -- get it started, and enqueue this service record |
| 11247 | // to be executed when the app comes up. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11248 | if (app == null) { |
| 11249 | if ((app=startProcessLocked(appName, r.appInfo, true, intentFlags, |
| 11250 | "service", r.name, false, isolated)) == null) { |
| 11251 | Slog.w(TAG, "Unable to launch app " |
| 11252 | + r.appInfo.packageName + "/" |
| 11253 | + r.appInfo.uid + " for service " |
| 11254 | + r.intent.getIntent() + ": process is bad"); |
| 11255 | bringDownServiceLocked(r, true); |
| 11256 | return false; |
| 11257 | } |
| 11258 | if (isolated) { |
| 11259 | r.isolatedProc = app; |
| 11260 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 11261 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11263 | if (!mPendingServices.contains(r)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11264 | mPendingServices.add(r); |
| 11265 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 11266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11267 | return true; |
| 11268 | } |
| 11269 | |
| 11270 | private final void bringDownServiceLocked(ServiceRecord r, boolean force) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11271 | //Slog.i(TAG, "Bring down service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11272 | //r.dump(" "); |
| 11273 | |
| 11274 | // Does it still need to run? |
| 11275 | if (!force && r.startRequested) { |
| 11276 | return; |
| 11277 | } |
| 11278 | if (r.connections.size() > 0) { |
| 11279 | if (!force) { |
| 11280 | // XXX should probably keep a count of the number of auto-create |
| 11281 | // connections directly in the service. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11282 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11283 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11284 | ArrayList<ConnectionRecord> cr = it.next(); |
| 11285 | for (int i=0; i<cr.size(); i++) { |
| 11286 | if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11287 | return; |
| 11288 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11289 | } |
| 11290 | } |
| 11291 | } |
| 11292 | |
| 11293 | // Report to all of the connections that the service is no longer |
| 11294 | // available. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11295 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11296 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11297 | ArrayList<ConnectionRecord> c = it.next(); |
| 11298 | for (int i=0; i<c.size(); i++) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11299 | ConnectionRecord cr = c.get(i); |
| 11300 | // There is still a connection to the service that is |
| 11301 | // being brought down. Mark it as dead. |
| 11302 | cr.serviceDead = true; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11303 | try { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11304 | cr.conn.connected(r.name, null); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11305 | } catch (Exception e) { |
| 11306 | Slog.w(TAG, "Failure disconnecting service " + r.name + |
| 11307 | " to connection " + c.get(i).conn.asBinder() + |
| 11308 | " (in " + c.get(i).binding.client.processName + ")", e); |
| 11309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11310 | } |
| 11311 | } |
| 11312 | } |
| 11313 | |
| 11314 | // Tell the service that it has been unbound. |
| 11315 | if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { |
| 11316 | Iterator<IntentBindRecord> it = r.bindings.values().iterator(); |
| 11317 | while (it.hasNext()) { |
| 11318 | IntentBindRecord ibr = it.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11319 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11320 | + ": hasBound=" + ibr.hasBound); |
| 11321 | if (r.app != null && r.app.thread != null && ibr.hasBound) { |
| 11322 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11323 | bumpServiceExecutingLocked(r, "bring down unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11324 | updateOomAdjLocked(r.app); |
| 11325 | ibr.hasBound = false; |
| 11326 | r.app.thread.scheduleUnbindService(r, |
| 11327 | ibr.intent.getIntent()); |
| 11328 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11329 | Slog.w(TAG, "Exception when unbinding service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11330 | + r.shortName, e); |
| 11331 | serviceDoneExecutingLocked(r, true); |
| 11332 | } |
| 11333 | } |
| 11334 | } |
| 11335 | } |
| 11336 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11337 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11338 | EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11339 | System.identityHashCode(r), r.shortName, |
| 11340 | (r.app != null) ? r.app.pid : -1); |
| 11341 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11342 | mServiceMap.removeServiceByName(r.name, r.userId); |
| 11343 | mServiceMap.removeServiceByIntent(r.intent, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11344 | r.totalRestartCount = 0; |
| 11345 | unscheduleServiceRestartLocked(r); |
| 11346 | |
| 11347 | // Also make sure it is not on the pending list. |
| 11348 | int N = mPendingServices.size(); |
| 11349 | for (int i=0; i<N; i++) { |
| 11350 | if (mPendingServices.get(i) == r) { |
| 11351 | mPendingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11352 | if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11353 | i--; |
| 11354 | N--; |
| 11355 | } |
| 11356 | } |
| 11357 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11358 | r.cancelNotification(); |
| 11359 | r.isForeground = false; |
| 11360 | r.foregroundId = 0; |
| 11361 | r.foregroundNoti = null; |
| 11362 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11363 | // Clear start entries. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11364 | r.clearDeliveredStartsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11365 | r.pendingStarts.clear(); |
| 11366 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11367 | if (r.app != null) { |
| 11368 | synchronized (r.stats.getBatteryStats()) { |
| 11369 | r.stats.stopLaunchedLocked(); |
| 11370 | } |
| 11371 | r.app.services.remove(r); |
| 11372 | if (r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11373 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11374 | bumpServiceExecutingLocked(r, "stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11375 | mStoppingServices.add(r); |
| 11376 | updateOomAdjLocked(r.app); |
| 11377 | r.app.thread.scheduleStopService(r); |
| 11378 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11379 | Slog.w(TAG, "Exception when stopping service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11380 | + r.shortName, e); |
| 11381 | serviceDoneExecutingLocked(r, true); |
| 11382 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11383 | updateServiceForegroundLocked(r.app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11384 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11385 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11386 | TAG, "Removed service that has no process: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11387 | } |
| 11388 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11389 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11390 | TAG, "Removed service that is not running: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11391 | } |
Vairavan Srinivasan | a207ce2 | 2010-12-23 13:51:48 -0800 | [diff] [blame] | 11392 | |
| 11393 | if (r.bindings.size() > 0) { |
| 11394 | r.bindings.clear(); |
| 11395 | } |
| 11396 | |
| 11397 | if (r.restarter instanceof ServiceRestarter) { |
| 11398 | ((ServiceRestarter)r.restarter).setService(null); |
| 11399 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11400 | } |
| 11401 | |
| 11402 | ComponentName startServiceLocked(IApplicationThread caller, |
| 11403 | Intent service, String resolvedType, |
| 11404 | int callingPid, int callingUid) { |
| 11405 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11406 | if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11407 | + " type=" + resolvedType + " args=" + service.getExtras()); |
| 11408 | |
| 11409 | if (caller != null) { |
| 11410 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11411 | if (callerApp == null) { |
| 11412 | throw new SecurityException( |
| 11413 | "Unable to find app for caller " + caller |
| 11414 | + " (pid=" + Binder.getCallingPid() |
| 11415 | + ") when starting service " + service); |
| 11416 | } |
| 11417 | } |
| 11418 | |
| 11419 | ServiceLookupResult res = |
| 11420 | retrieveServiceLocked(service, resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 11421 | callingPid, callingUid, UserId.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11422 | if (res == null) { |
| 11423 | return null; |
| 11424 | } |
| 11425 | if (res.record == null) { |
| 11426 | return new ComponentName("!", res.permission != null |
| 11427 | ? res.permission : "private to package"); |
| 11428 | } |
| 11429 | ServiceRecord r = res.record; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 11430 | NeededUriGrants neededGrants = checkGrantUriPermissionFromIntentLocked( |
| 11431 | callingUid, r.packageName, service, service.getFlags(), null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11432 | if (unscheduleServiceRestartLocked(r)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11433 | if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11434 | } |
| 11435 | r.startRequested = true; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11436 | r.callStart = false; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11437 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 11438 | service, neededGrants)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11439 | r.lastActivity = SystemClock.uptimeMillis(); |
| 11440 | synchronized (r.stats.getBatteryStats()) { |
| 11441 | r.stats.startRunningLocked(); |
| 11442 | } |
| 11443 | if (!bringUpServiceLocked(r, service.getFlags(), false)) { |
| 11444 | return new ComponentName("!", "Service process is bad"); |
| 11445 | } |
| 11446 | return r.name; |
| 11447 | } |
| 11448 | } |
| 11449 | |
| 11450 | public ComponentName startService(IApplicationThread caller, Intent service, |
| 11451 | String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11452 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11453 | // Refuse possible leaked file descriptors |
| 11454 | if (service != null && service.hasFileDescriptors() == true) { |
| 11455 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11456 | } |
| 11457 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11458 | if (DEBUG_SERVICE) |
| 11459 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11460 | synchronized(this) { |
| 11461 | final int callingPid = Binder.getCallingPid(); |
| 11462 | final int callingUid = Binder.getCallingUid(); |
| 11463 | final long origId = Binder.clearCallingIdentity(); |
| 11464 | ComponentName res = startServiceLocked(caller, service, |
| 11465 | resolvedType, callingPid, callingUid); |
| 11466 | Binder.restoreCallingIdentity(origId); |
| 11467 | return res; |
| 11468 | } |
| 11469 | } |
| 11470 | |
| 11471 | ComponentName startServiceInPackage(int uid, |
| 11472 | Intent service, String resolvedType) { |
| 11473 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11474 | if (DEBUG_SERVICE) |
| 11475 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11476 | final long origId = Binder.clearCallingIdentity(); |
| 11477 | ComponentName res = startServiceLocked(null, service, |
| 11478 | resolvedType, -1, uid); |
| 11479 | Binder.restoreCallingIdentity(origId); |
| 11480 | return res; |
| 11481 | } |
| 11482 | } |
| 11483 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11484 | private void stopServiceLocked(ServiceRecord service) { |
| 11485 | synchronized (service.stats.getBatteryStats()) { |
| 11486 | service.stats.stopRunningLocked(); |
| 11487 | } |
| 11488 | service.startRequested = false; |
| 11489 | service.callStart = false; |
| 11490 | bringDownServiceLocked(service, false); |
| 11491 | } |
| 11492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11493 | public int stopService(IApplicationThread caller, Intent service, |
| 11494 | String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11495 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11496 | // Refuse possible leaked file descriptors |
| 11497 | if (service != null && service.hasFileDescriptors() == true) { |
| 11498 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11499 | } |
| 11500 | |
| 11501 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11502 | if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11503 | + " type=" + resolvedType); |
| 11504 | |
| 11505 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11506 | if (caller != null && callerApp == null) { |
| 11507 | throw new SecurityException( |
| 11508 | "Unable to find app for caller " + caller |
| 11509 | + " (pid=" + Binder.getCallingPid() |
| 11510 | + ") when stopping service " + service); |
| 11511 | } |
| 11512 | |
| 11513 | // If this service is active, make sure it is stopped. |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11514 | ServiceLookupResult r = findServiceLocked(service, resolvedType, |
| 11515 | callerApp == null ? UserId.getCallingUserId() : callerApp.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11516 | if (r != null) { |
| 11517 | if (r.record != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11518 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11519 | try { |
| 11520 | stopServiceLocked(r.record); |
| 11521 | } finally { |
| 11522 | Binder.restoreCallingIdentity(origId); |
| 11523 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11524 | return 1; |
| 11525 | } |
| 11526 | return -1; |
| 11527 | } |
| 11528 | } |
| 11529 | |
| 11530 | return 0; |
| 11531 | } |
| 11532 | |
| 11533 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11534 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11535 | // Refuse possible leaked file descriptors |
| 11536 | if (service != null && service.hasFileDescriptors() == true) { |
| 11537 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11538 | } |
| 11539 | |
| 11540 | IBinder ret = null; |
| 11541 | |
| 11542 | synchronized(this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11543 | ServiceLookupResult r = findServiceLocked(service, resolvedType, |
| 11544 | UserId.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11545 | |
| 11546 | if (r != null) { |
| 11547 | // r.record is null if findServiceLocked() failed the caller permission check |
| 11548 | if (r.record == null) { |
| 11549 | throw new SecurityException( |
| 11550 | "Permission Denial: Accessing service " + r.record.name |
| 11551 | + " from pid=" + Binder.getCallingPid() |
| 11552 | + ", uid=" + Binder.getCallingUid() |
| 11553 | + " requires " + r.permission); |
| 11554 | } |
| 11555 | IntentBindRecord ib = r.record.bindings.get(r.record.intent); |
| 11556 | if (ib != null) { |
| 11557 | ret = ib.binder; |
| 11558 | } |
| 11559 | } |
| 11560 | } |
| 11561 | |
| 11562 | return ret; |
| 11563 | } |
| 11564 | |
| 11565 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 11566 | int startId) { |
| 11567 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11568 | if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11569 | + " " + token + " startId=" + startId); |
| 11570 | ServiceRecord r = findServiceLocked(className, token); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11571 | if (r != null) { |
| 11572 | if (startId >= 0) { |
| 11573 | // Asked to only stop if done with all work. Note that |
| 11574 | // to avoid leaks, we will take this as dropping all |
| 11575 | // start items up to and including this one. |
| 11576 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 11577 | if (si != null) { |
| 11578 | while (r.deliveredStarts.size() > 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11579 | ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); |
| 11580 | cur.removeUriPermissionsLocked(); |
| 11581 | if (cur == si) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11582 | break; |
| 11583 | } |
| 11584 | } |
| 11585 | } |
| 11586 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11587 | if (r.getLastStartId() != startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11588 | return false; |
| 11589 | } |
| 11590 | |
| 11591 | if (r.deliveredStarts.size() > 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11592 | Slog.w(TAG, "stopServiceToken startId " + startId |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11593 | + " is last, but have " + r.deliveredStarts.size() |
| 11594 | + " remaining args"); |
| 11595 | } |
| 11596 | } |
| 11597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11598 | synchronized (r.stats.getBatteryStats()) { |
| 11599 | r.stats.stopRunningLocked(); |
| 11600 | r.startRequested = false; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11601 | r.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11602 | } |
| 11603 | final long origId = Binder.clearCallingIdentity(); |
| 11604 | bringDownServiceLocked(r, false); |
| 11605 | Binder.restoreCallingIdentity(origId); |
| 11606 | return true; |
| 11607 | } |
| 11608 | } |
| 11609 | return false; |
| 11610 | } |
| 11611 | |
| 11612 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11613 | int id, Notification notification, boolean removeNotification) { |
| 11614 | final long origId = Binder.clearCallingIdentity(); |
| 11615 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11616 | synchronized(this) { |
| 11617 | ServiceRecord r = findServiceLocked(className, token); |
| 11618 | if (r != null) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11619 | if (id != 0) { |
| 11620 | if (notification == null) { |
| 11621 | throw new IllegalArgumentException("null notification"); |
| 11622 | } |
| 11623 | if (r.foregroundId != id) { |
| 11624 | r.cancelNotification(); |
| 11625 | r.foregroundId = id; |
| 11626 | } |
| 11627 | notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 11628 | r.foregroundNoti = notification; |
| 11629 | r.isForeground = true; |
| 11630 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11631 | if (r.app != null) { |
| 11632 | updateServiceForegroundLocked(r.app, true); |
| 11633 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11634 | } else { |
| 11635 | if (r.isForeground) { |
| 11636 | r.isForeground = false; |
| 11637 | if (r.app != null) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 11638 | updateLruProcessLocked(r.app, false, true); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11639 | updateServiceForegroundLocked(r.app, true); |
| 11640 | } |
| 11641 | } |
| 11642 | if (removeNotification) { |
| 11643 | r.cancelNotification(); |
| 11644 | r.foregroundId = 0; |
| 11645 | r.foregroundNoti = null; |
| 11646 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11647 | } |
| 11648 | } |
| 11649 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11650 | } finally { |
| 11651 | Binder.restoreCallingIdentity(origId); |
| 11652 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11653 | } |
| 11654 | |
| 11655 | public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { |
| 11656 | boolean anyForeground = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 11657 | for (ServiceRecord sr : proc.services) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11658 | if (sr.isForeground) { |
| 11659 | anyForeground = true; |
| 11660 | break; |
| 11661 | } |
| 11662 | } |
| 11663 | if (anyForeground != proc.foregroundServices) { |
| 11664 | proc.foregroundServices = anyForeground; |
| 11665 | if (oomAdj) { |
| 11666 | updateOomAdjLocked(); |
| 11667 | } |
| 11668 | } |
| 11669 | } |
| 11670 | |
| 11671 | public int bindService(IApplicationThread caller, IBinder token, |
| 11672 | Intent service, String resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 11673 | IServiceConnection connection, int flags, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11674 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11675 | // Refuse possible leaked file descriptors |
| 11676 | if (service != null && service.hasFileDescriptors() == true) { |
| 11677 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11678 | } |
| 11679 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 11680 | checkValidCaller(Binder.getCallingUid(), userId); |
| 11681 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11682 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11683 | if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11684 | + " type=" + resolvedType + " conn=" + connection.asBinder() |
| 11685 | + " flags=0x" + Integer.toHexString(flags)); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11686 | if (DEBUG_MU) |
| 11687 | Slog.i(TAG_MU, "bindService uid=" + Binder.getCallingUid() + " origUid=" |
| 11688 | + Binder.getOrigCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11689 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11690 | if (callerApp == null) { |
| 11691 | throw new SecurityException( |
| 11692 | "Unable to find app for caller " + caller |
| 11693 | + " (pid=" + Binder.getCallingPid() |
| 11694 | + ") when binding service " + service); |
| 11695 | } |
| 11696 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11697 | ActivityRecord activity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11698 | if (token != null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 11699 | activity = mMainStack.isInStackLocked(token); |
| 11700 | if (activity == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11701 | Slog.w(TAG, "Binding with unknown activity: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11702 | return 0; |
| 11703 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11704 | } |
| 11705 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11706 | int clientLabel = 0; |
| 11707 | PendingIntent clientIntent = null; |
| 11708 | |
| 11709 | if (callerApp.info.uid == Process.SYSTEM_UID) { |
| 11710 | // Hacky kind of thing -- allow system stuff to tell us |
| 11711 | // what they are, so we can report this elsewhere for |
| 11712 | // others to know why certain services are running. |
| 11713 | try { |
| 11714 | clientIntent = (PendingIntent)service.getParcelableExtra( |
| 11715 | Intent.EXTRA_CLIENT_INTENT); |
| 11716 | } catch (RuntimeException e) { |
| 11717 | } |
| 11718 | if (clientIntent != null) { |
| 11719 | clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); |
| 11720 | if (clientLabel != 0) { |
| 11721 | // There are no useful extras in the intent, trash them. |
| 11722 | // System code calling with this stuff just needs to know |
| 11723 | // this will happen. |
| 11724 | service = service.cloneFilter(); |
| 11725 | } |
| 11726 | } |
| 11727 | } |
| 11728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11729 | ServiceLookupResult res = |
| 11730 | retrieveServiceLocked(service, resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 11731 | Binder.getCallingPid(), Binder.getCallingUid(), userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11732 | if (res == null) { |
| 11733 | return 0; |
| 11734 | } |
| 11735 | if (res.record == null) { |
| 11736 | return -1; |
| 11737 | } |
| 11738 | ServiceRecord s = res.record; |
| 11739 | |
| 11740 | final long origId = Binder.clearCallingIdentity(); |
| 11741 | |
| 11742 | if (unscheduleServiceRestartLocked(s)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11743 | if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11744 | + s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11745 | } |
| 11746 | |
| 11747 | AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); |
| 11748 | ConnectionRecord c = new ConnectionRecord(b, activity, |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11749 | connection, flags, clientLabel, clientIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11750 | |
| 11751 | IBinder binder = connection.asBinder(); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11752 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 11753 | if (clist == null) { |
| 11754 | clist = new ArrayList<ConnectionRecord>(); |
| 11755 | s.connections.put(binder, clist); |
| 11756 | } |
| 11757 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11758 | b.connections.add(c); |
| 11759 | if (activity != null) { |
| 11760 | if (activity.connections == null) { |
| 11761 | activity.connections = new HashSet<ConnectionRecord>(); |
| 11762 | } |
| 11763 | activity.connections.add(c); |
| 11764 | } |
| 11765 | b.client.connections.add(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11766 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 11767 | b.client.hasAboveClient = true; |
| 11768 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11769 | clist = mServiceConnections.get(binder); |
| 11770 | if (clist == null) { |
| 11771 | clist = new ArrayList<ConnectionRecord>(); |
| 11772 | mServiceConnections.put(binder, clist); |
| 11773 | } |
| 11774 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11775 | |
| 11776 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11777 | s.lastActivity = SystemClock.uptimeMillis(); |
| 11778 | if (!bringUpServiceLocked(s, service.getFlags(), false)) { |
| 11779 | return 0; |
| 11780 | } |
| 11781 | } |
| 11782 | |
| 11783 | if (s.app != null) { |
| 11784 | // This could have made the service more important. |
| 11785 | updateOomAdjLocked(s.app); |
| 11786 | } |
| 11787 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11788 | if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11789 | + ": received=" + b.intent.received |
| 11790 | + " apps=" + b.intent.apps.size() |
| 11791 | + " doRebind=" + b.intent.doRebind); |
| 11792 | |
| 11793 | if (s.app != null && b.intent.received) { |
| 11794 | // Service is already running, so we can immediately |
| 11795 | // publish the connection. |
| 11796 | try { |
| 11797 | c.conn.connected(s.name, b.intent.binder); |
| 11798 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11799 | Slog.w(TAG, "Failure sending service " + s.shortName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11800 | + " to connection " + c.conn.asBinder() |
| 11801 | + " (in " + c.binding.client.processName + ")", e); |
| 11802 | } |
| 11803 | |
| 11804 | // If this is the first app connected back to this binding, |
| 11805 | // and the service had previously asked to be told when |
| 11806 | // rebound, then do so. |
| 11807 | if (b.intent.apps.size() == 1 && b.intent.doRebind) { |
| 11808 | requestServiceBindingLocked(s, b.intent, true); |
| 11809 | } |
| 11810 | } else if (!b.intent.requested) { |
| 11811 | requestServiceBindingLocked(s, b.intent, false); |
| 11812 | } |
| 11813 | |
| 11814 | Binder.restoreCallingIdentity(origId); |
| 11815 | } |
| 11816 | |
| 11817 | return 1; |
| 11818 | } |
| 11819 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 11820 | void removeConnectionLocked( |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11821 | ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11822 | IBinder binder = c.conn.asBinder(); |
| 11823 | AppBindRecord b = c.binding; |
| 11824 | ServiceRecord s = b.service; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11825 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 11826 | if (clist != null) { |
| 11827 | clist.remove(c); |
| 11828 | if (clist.size() == 0) { |
| 11829 | s.connections.remove(binder); |
| 11830 | } |
| 11831 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11832 | b.connections.remove(c); |
| 11833 | if (c.activity != null && c.activity != skipAct) { |
| 11834 | if (c.activity.connections != null) { |
| 11835 | c.activity.connections.remove(c); |
| 11836 | } |
| 11837 | } |
| 11838 | if (b.client != skipApp) { |
| 11839 | b.client.connections.remove(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11840 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 11841 | b.client.updateHasAboveClientLocked(); |
| 11842 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11843 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11844 | clist = mServiceConnections.get(binder); |
| 11845 | if (clist != null) { |
| 11846 | clist.remove(c); |
| 11847 | if (clist.size() == 0) { |
| 11848 | mServiceConnections.remove(binder); |
| 11849 | } |
| 11850 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11851 | |
| 11852 | if (b.connections.size() == 0) { |
| 11853 | b.intent.apps.remove(b.client); |
| 11854 | } |
| 11855 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11856 | if (!c.serviceDead) { |
| 11857 | if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent |
| 11858 | + ": shouldUnbind=" + b.intent.hasBound); |
| 11859 | if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 |
| 11860 | && b.intent.hasBound) { |
| 11861 | try { |
| 11862 | bumpServiceExecutingLocked(s, "unbind"); |
| 11863 | updateOomAdjLocked(s.app); |
| 11864 | b.intent.hasBound = false; |
| 11865 | // Assume the client doesn't want to know about a rebind; |
| 11866 | // we will deal with that later if it asks for one. |
| 11867 | b.intent.doRebind = false; |
| 11868 | s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); |
| 11869 | } catch (Exception e) { |
| 11870 | Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); |
| 11871 | serviceDoneExecutingLocked(s, true); |
| 11872 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11873 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11874 | |
| 11875 | if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11876 | bringDownServiceLocked(s, false); |
| 11877 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11878 | } |
| 11879 | } |
| 11880 | |
| 11881 | public boolean unbindService(IServiceConnection connection) { |
| 11882 | synchronized (this) { |
| 11883 | IBinder binder = connection.asBinder(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11884 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11885 | ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); |
| 11886 | if (clist == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11887 | Slog.w(TAG, "Unbind failed: could not find connection for " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11888 | + connection.asBinder()); |
| 11889 | return false; |
| 11890 | } |
| 11891 | |
| 11892 | final long origId = Binder.clearCallingIdentity(); |
| 11893 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11894 | while (clist.size() > 0) { |
| 11895 | ConnectionRecord r = clist.get(0); |
| 11896 | removeConnectionLocked(r, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11897 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11898 | if (r.binding.service.app != null) { |
| 11899 | // This could have made the service less important. |
| 11900 | updateOomAdjLocked(r.binding.service.app); |
| 11901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11902 | } |
| 11903 | |
| 11904 | Binder.restoreCallingIdentity(origId); |
| 11905 | } |
| 11906 | |
| 11907 | return true; |
| 11908 | } |
| 11909 | |
| 11910 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 11911 | // Refuse possible leaked file descriptors |
| 11912 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11913 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11914 | } |
| 11915 | |
| 11916 | synchronized(this) { |
| 11917 | if (!(token instanceof ServiceRecord)) { |
| 11918 | throw new IllegalArgumentException("Invalid service token"); |
| 11919 | } |
| 11920 | ServiceRecord r = (ServiceRecord)token; |
| 11921 | |
| 11922 | final long origId = Binder.clearCallingIdentity(); |
| 11923 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11924 | if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11925 | + " " + intent + ": " + service); |
| 11926 | if (r != null) { |
| 11927 | Intent.FilterComparison filter |
| 11928 | = new Intent.FilterComparison(intent); |
| 11929 | IntentBindRecord b = r.bindings.get(filter); |
| 11930 | if (b != null && !b.received) { |
| 11931 | b.binder = service; |
| 11932 | b.requested = true; |
| 11933 | b.received = true; |
| 11934 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11935 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11936 | = r.connections.values().iterator(); |
| 11937 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11938 | ArrayList<ConnectionRecord> clist = it.next(); |
| 11939 | for (int i=0; i<clist.size(); i++) { |
| 11940 | ConnectionRecord c = clist.get(i); |
| 11941 | if (!filter.equals(c.binding.intent.intent)) { |
| 11942 | if (DEBUG_SERVICE) Slog.v( |
| 11943 | TAG, "Not publishing to: " + c); |
| 11944 | if (DEBUG_SERVICE) Slog.v( |
| 11945 | TAG, "Bound intent: " + c.binding.intent.intent); |
| 11946 | if (DEBUG_SERVICE) Slog.v( |
| 11947 | TAG, "Published intent: " + intent); |
| 11948 | continue; |
| 11949 | } |
| 11950 | if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); |
| 11951 | try { |
| 11952 | c.conn.connected(r.name, service); |
| 11953 | } catch (Exception e) { |
| 11954 | Slog.w(TAG, "Failure sending service " + r.name + |
| 11955 | " to connection " + c.conn.asBinder() + |
| 11956 | " (in " + c.binding.client.processName + ")", e); |
| 11957 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11958 | } |
| 11959 | } |
| 11960 | } |
| 11961 | } |
| 11962 | |
| 11963 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 11964 | |
| 11965 | Binder.restoreCallingIdentity(origId); |
| 11966 | } |
| 11967 | } |
| 11968 | } |
| 11969 | |
| 11970 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 11971 | // Refuse possible leaked file descriptors |
| 11972 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11973 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11974 | } |
| 11975 | |
| 11976 | synchronized(this) { |
| 11977 | if (!(token instanceof ServiceRecord)) { |
| 11978 | throw new IllegalArgumentException("Invalid service token"); |
| 11979 | } |
| 11980 | ServiceRecord r = (ServiceRecord)token; |
| 11981 | |
| 11982 | final long origId = Binder.clearCallingIdentity(); |
| 11983 | |
| 11984 | if (r != null) { |
| 11985 | Intent.FilterComparison filter |
| 11986 | = new Intent.FilterComparison(intent); |
| 11987 | IntentBindRecord b = r.bindings.get(filter); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11988 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11989 | + " at " + b + ": apps=" |
| 11990 | + (b != null ? b.apps.size() : 0)); |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11991 | |
| 11992 | boolean inStopping = mStoppingServices.contains(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11993 | if (b != null) { |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11994 | if (b.apps.size() > 0 && !inStopping) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11995 | // Applications have already bound since the last |
| 11996 | // unbind, so just rebind right here. |
| 11997 | requestServiceBindingLocked(r, b, true); |
| 11998 | } else { |
| 11999 | // Note to tell the service the next time there is |
| 12000 | // a new client. |
| 12001 | b.doRebind = true; |
| 12002 | } |
| 12003 | } |
| 12004 | |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 12005 | serviceDoneExecutingLocked(r, inStopping); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12006 | |
| 12007 | Binder.restoreCallingIdentity(origId); |
| 12008 | } |
| 12009 | } |
| 12010 | } |
| 12011 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 12012 | 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] | 12013 | synchronized(this) { |
| 12014 | if (!(token instanceof ServiceRecord)) { |
| 12015 | throw new IllegalArgumentException("Invalid service token"); |
| 12016 | } |
| 12017 | ServiceRecord r = (ServiceRecord)token; |
| 12018 | boolean inStopping = mStoppingServices.contains(token); |
| 12019 | if (r != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12020 | if (r != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12021 | Slog.w(TAG, "Done executing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12022 | + " with incorrect token: given " + token |
| 12023 | + ", expected " + r); |
| 12024 | return; |
| 12025 | } |
| 12026 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 12027 | if (type == 1) { |
| 12028 | // This is a call from a service start... take care of |
| 12029 | // book-keeping. |
| 12030 | r.callStart = true; |
| 12031 | switch (res) { |
| 12032 | case Service.START_STICKY_COMPATIBILITY: |
| 12033 | case Service.START_STICKY: { |
| 12034 | // We are done with the associated start arguments. |
| 12035 | r.findDeliveredStart(startId, true); |
| 12036 | // Don't stop if killed. |
| 12037 | r.stopIfKilled = false; |
| 12038 | break; |
| 12039 | } |
| 12040 | case Service.START_NOT_STICKY: { |
| 12041 | // We are done with the associated start arguments. |
| 12042 | r.findDeliveredStart(startId, true); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 12043 | if (r.getLastStartId() == startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 12044 | // There is no more work, and this service |
| 12045 | // doesn't want to hang around if killed. |
| 12046 | r.stopIfKilled = true; |
| 12047 | } |
| 12048 | break; |
| 12049 | } |
| 12050 | case Service.START_REDELIVER_INTENT: { |
| 12051 | // We'll keep this item until they explicitly |
| 12052 | // call stop for it, but keep track of the fact |
| 12053 | // that it was delivered. |
| 12054 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 12055 | if (si != null) { |
| 12056 | si.deliveryCount = 0; |
| 12057 | si.doneExecutingCount++; |
| 12058 | // Don't stop if killed. |
| 12059 | r.stopIfKilled = true; |
| 12060 | } |
| 12061 | break; |
| 12062 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 12063 | case Service.START_TASK_REMOVED_COMPLETE: { |
| 12064 | // Special processing for onTaskRemoved(). Don't |
| 12065 | // impact normal onStartCommand() processing. |
| 12066 | r.findDeliveredStart(startId, true); |
| 12067 | break; |
| 12068 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 12069 | default: |
| 12070 | throw new IllegalArgumentException( |
| 12071 | "Unknown service start result: " + res); |
| 12072 | } |
| 12073 | if (res == Service.START_STICKY_COMPATIBILITY) { |
| 12074 | r.callStart = false; |
| 12075 | } |
| 12076 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12077 | if (DEBUG_MU) |
| 12078 | Slog.v(TAG_MU, "before serviceDontExecutingLocked, uid=" |
| 12079 | + Binder.getOrigCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12080 | final long origId = Binder.clearCallingIdentity(); |
| 12081 | serviceDoneExecutingLocked(r, inStopping); |
| 12082 | Binder.restoreCallingIdentity(origId); |
| 12083 | } else { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12084 | Slog.w(TAG, "Done executing unknown service from pid " |
| 12085 | + Binder.getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12086 | } |
| 12087 | } |
| 12088 | } |
| 12089 | |
| 12090 | public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12091 | if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r |
| 12092 | + ": nesting=" + r.executeNesting |
| 12093 | + ", inStopping=" + inStopping + ", app=" + r.app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12094 | else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, "<<< DONE EXECUTING " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12095 | r.executeNesting--; |
| 12096 | if (r.executeNesting <= 0 && r.app != null) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12097 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 12098 | "Nesting at 0 of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12099 | r.app.executingServices.remove(r); |
| 12100 | if (r.app.executingServices.size() == 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12101 | if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, |
| 12102 | "No more executingServices of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12103 | mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app); |
| 12104 | } |
| 12105 | if (inStopping) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12106 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 12107 | "doneExecuting remove stopping " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12108 | mStoppingServices.remove(r); |
Mattias Petersson | 3996b41 | 2010-10-27 09:32:51 +0200 | [diff] [blame] | 12109 | r.bindings.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12110 | } |
| 12111 | updateOomAdjLocked(r.app); |
| 12112 | } |
| 12113 | } |
| 12114 | |
| 12115 | void serviceTimeout(ProcessRecord proc) { |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12116 | String anrMessage = null; |
| 12117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12118 | synchronized(this) { |
| 12119 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 12120 | return; |
| 12121 | } |
| 12122 | long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; |
| 12123 | Iterator<ServiceRecord> it = proc.executingServices.iterator(); |
| 12124 | ServiceRecord timeout = null; |
| 12125 | long nextTime = 0; |
| 12126 | while (it.hasNext()) { |
| 12127 | ServiceRecord sr = it.next(); |
| 12128 | if (sr.executingStart < maxTime) { |
| 12129 | timeout = sr; |
| 12130 | break; |
| 12131 | } |
| 12132 | if (sr.executingStart > nextTime) { |
| 12133 | nextTime = sr.executingStart; |
| 12134 | } |
| 12135 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12136 | if (timeout != null && mLruProcesses.contains(proc)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12137 | Slog.w(TAG, "Timeout executing service: " + timeout); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12138 | anrMessage = "Executing service " + timeout.shortName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12139 | } else { |
| 12140 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 12141 | msg.obj = proc; |
| 12142 | mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); |
| 12143 | } |
| 12144 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12145 | |
| 12146 | if (anrMessage != null) { |
| 12147 | appNotResponding(proc, null, null, anrMessage); |
| 12148 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12149 | } |
| 12150 | |
| 12151 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12152 | // BACKUP AND RESTORE |
| 12153 | // ========================================================= |
| 12154 | |
| 12155 | // Cause the target app to be launched if necessary and its backup agent |
| 12156 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 12157 | // activity manager to announce its creation. |
| 12158 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12159 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12160 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 12161 | |
| 12162 | synchronized(this) { |
| 12163 | // !!! TODO: currently no check here that we're already bound |
| 12164 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 12165 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 12166 | synchronized (stats) { |
| 12167 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 12168 | } |
| 12169 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12170 | // Backup agent is now in use, its package can't be stopped. |
| 12171 | try { |
| 12172 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12173 | app.packageName, false, UserId.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12174 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 12175 | } catch (IllegalArgumentException e) { |
| 12176 | Slog.w(TAG, "Failed trying to unstop package " |
| 12177 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12178 | } |
| 12179 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12180 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 12181 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 12182 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 12183 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12184 | // startProcessLocked() returns existing proc's record if it's already running |
| 12185 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 12186 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12187 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12188 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12189 | return false; |
| 12190 | } |
| 12191 | |
| 12192 | r.app = proc; |
| 12193 | mBackupTarget = r; |
| 12194 | mBackupAppName = app.packageName; |
| 12195 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12196 | // Try not to kill the process during backup |
| 12197 | updateOomAdjLocked(proc); |
| 12198 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12199 | // If the process is already attached, schedule the creation of the backup agent now. |
| 12200 | // If it is not yet live, this will be done when it attaches to the framework. |
| 12201 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12202 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12203 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 12204 | proc.thread.scheduleCreateBackupAgent(app, |
| 12205 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12206 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 12207 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12208 | } |
| 12209 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12210 | 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] | 12211 | } |
| 12212 | // Invariants: at this point, the target app process exists and the application |
| 12213 | // is either already running or in the process of coming up. mBackupTarget and |
| 12214 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 12215 | // know that it's scheduled for a backup-agent operation. |
| 12216 | } |
| 12217 | |
| 12218 | return true; |
| 12219 | } |
| 12220 | |
| 12221 | // A backup agent has just come up |
| 12222 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12223 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12224 | + " = " + agent); |
| 12225 | |
| 12226 | synchronized(this) { |
| 12227 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12228 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12229 | return; |
| 12230 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 12231 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12232 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 12233 | long oldIdent = Binder.clearCallingIdentity(); |
| 12234 | try { |
| 12235 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 12236 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 12237 | bm.agentConnected(agentPackageName, agent); |
| 12238 | } catch (RemoteException e) { |
| 12239 | // can't happen; the backup manager service is local |
| 12240 | } catch (Exception e) { |
| 12241 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 12242 | e.printStackTrace(); |
| 12243 | } finally { |
| 12244 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12245 | } |
| 12246 | } |
| 12247 | |
| 12248 | // done with this agent |
| 12249 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12250 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 12251 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12252 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 12253 | return; |
| 12254 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12255 | |
| 12256 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 12257 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12258 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 12259 | return; |
| 12260 | } |
| 12261 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12262 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12263 | 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] | 12264 | return; |
| 12265 | } |
| 12266 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12267 | ProcessRecord proc = mBackupTarget.app; |
| 12268 | mBackupTarget = null; |
| 12269 | mBackupAppName = null; |
| 12270 | |
| 12271 | // Not backing this app up any more; reset its OOM adjustment |
| 12272 | updateOomAdjLocked(proc); |
| 12273 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 12274 | // If the app crashed during backup, 'thread' will be null here |
| 12275 | if (proc.thread != null) { |
| 12276 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 12277 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 12278 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 12279 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12280 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 12281 | e.printStackTrace(); |
| 12282 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12283 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12284 | } |
| 12285 | } |
| 12286 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12287 | // BROADCASTS |
| 12288 | // ========================================================= |
| 12289 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12290 | private final List getStickiesLocked(String action, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12291 | List cur) { |
| 12292 | final ContentResolver resolver = mContext.getContentResolver(); |
| 12293 | final ArrayList<Intent> list = mStickyBroadcasts.get(action); |
| 12294 | if (list == null) { |
| 12295 | return cur; |
| 12296 | } |
| 12297 | int N = list.size(); |
| 12298 | for (int i=0; i<N; i++) { |
| 12299 | Intent intent = list.get(i); |
| 12300 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 12301 | if (cur == null) { |
| 12302 | cur = new ArrayList<Intent>(); |
| 12303 | } |
| 12304 | cur.add(intent); |
| 12305 | } |
| 12306 | } |
| 12307 | return cur; |
| 12308 | } |
| 12309 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12310 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 12311 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 12312 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 12313 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12314 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12315 | void skipPendingBroadcastLocked(int pid) { |
| 12316 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 12317 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12318 | queue.skipPendingBroadcastLocked(pid); |
| 12319 | } |
| 12320 | } |
| 12321 | |
| 12322 | // The app just attached; send any pending broadcasts that it should receive |
| 12323 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 12324 | boolean didSomething = false; |
| 12325 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12326 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12327 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12328 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12329 | } |
| 12330 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12331 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12332 | IIntentReceiver receiver, IntentFilter filter, String permission) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12333 | enforceNotIsolatedCaller("registerReceiver"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12334 | synchronized(this) { |
| 12335 | ProcessRecord callerApp = null; |
| 12336 | if (caller != null) { |
| 12337 | callerApp = getRecordForAppLocked(caller); |
| 12338 | if (callerApp == null) { |
| 12339 | throw new SecurityException( |
| 12340 | "Unable to find app for caller " + caller |
| 12341 | + " (pid=" + Binder.getCallingPid() |
| 12342 | + ") when registering receiver " + receiver); |
| 12343 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12344 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 12345 | !callerApp.pkgList.contains(callerPackage)) { |
| 12346 | throw new SecurityException("Given caller package " + callerPackage |
| 12347 | + " is not running in process " + callerApp); |
| 12348 | } |
| 12349 | } else { |
| 12350 | callerPackage = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12351 | } |
| 12352 | |
| 12353 | List allSticky = null; |
| 12354 | |
| 12355 | // Look for any matching sticky broadcasts... |
| 12356 | Iterator actions = filter.actionsIterator(); |
| 12357 | if (actions != null) { |
| 12358 | while (actions.hasNext()) { |
| 12359 | String action = (String)actions.next(); |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12360 | allSticky = getStickiesLocked(action, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12361 | } |
| 12362 | } else { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12363 | allSticky = getStickiesLocked(null, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12364 | } |
| 12365 | |
| 12366 | // The first sticky in the list is returned directly back to |
| 12367 | // the client. |
| 12368 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 12369 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12370 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12371 | + ": " + sticky); |
| 12372 | |
| 12373 | if (receiver == null) { |
| 12374 | return sticky; |
| 12375 | } |
| 12376 | |
| 12377 | ReceiverList rl |
| 12378 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 12379 | if (rl == null) { |
| 12380 | rl = new ReceiverList(this, callerApp, |
| 12381 | Binder.getCallingPid(), |
| 12382 | Binder.getCallingUid(), receiver); |
| 12383 | if (rl.app != null) { |
| 12384 | rl.app.receivers.add(rl); |
| 12385 | } else { |
| 12386 | try { |
| 12387 | receiver.asBinder().linkToDeath(rl, 0); |
| 12388 | } catch (RemoteException e) { |
| 12389 | return sticky; |
| 12390 | } |
| 12391 | rl.linkedToDeath = true; |
| 12392 | } |
| 12393 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
| 12394 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12395 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, permission); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12396 | rl.add(bf); |
| 12397 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12398 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12399 | } |
| 12400 | mReceiverResolver.addFilter(bf); |
| 12401 | |
| 12402 | // Enqueue broadcasts for all existing stickies that match |
| 12403 | // this filter. |
| 12404 | if (allSticky != null) { |
| 12405 | ArrayList receivers = new ArrayList(); |
| 12406 | receivers.add(bf); |
| 12407 | |
| 12408 | int N = allSticky.size(); |
| 12409 | for (int i=0; i<N; i++) { |
| 12410 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12411 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 12412 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12413 | null, -1, -1, null, receivers, null, 0, null, null, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12414 | false, true, true); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12415 | queue.enqueueParallelBroadcastLocked(r); |
| 12416 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12417 | } |
| 12418 | } |
| 12419 | |
| 12420 | return sticky; |
| 12421 | } |
| 12422 | } |
| 12423 | |
| 12424 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12425 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12426 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12427 | final long origId = Binder.clearCallingIdentity(); |
| 12428 | try { |
| 12429 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12430 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12431 | synchronized(this) { |
| 12432 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12433 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12434 | if (rl != null) { |
| 12435 | if (rl.curBroadcast != null) { |
| 12436 | BroadcastRecord r = rl.curBroadcast; |
| 12437 | final boolean doNext = finishReceiverLocked( |
| 12438 | receiver.asBinder(), r.resultCode, r.resultData, |
| 12439 | r.resultExtras, r.resultAbort, true); |
| 12440 | if (doNext) { |
| 12441 | doTrim = true; |
| 12442 | r.queue.processNextBroadcast(false); |
| 12443 | } |
| 12444 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12445 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12446 | if (rl.app != null) { |
| 12447 | rl.app.receivers.remove(rl); |
| 12448 | } |
| 12449 | removeReceiverLocked(rl); |
| 12450 | if (rl.linkedToDeath) { |
| 12451 | rl.linkedToDeath = false; |
| 12452 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 12453 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12454 | } |
| 12455 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12456 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12457 | // If we actually concluded any broadcasts, we might now be able |
| 12458 | // to trim the recipients' apps from our working set |
| 12459 | if (doTrim) { |
| 12460 | trimApplications(); |
| 12461 | return; |
| 12462 | } |
| 12463 | |
| 12464 | } finally { |
| 12465 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12466 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12467 | } |
| 12468 | |
| 12469 | void removeReceiverLocked(ReceiverList rl) { |
| 12470 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 12471 | int N = rl.size(); |
| 12472 | for (int i=0; i<N; i++) { |
| 12473 | mReceiverResolver.removeFilter(rl.get(i)); |
| 12474 | } |
| 12475 | } |
| 12476 | |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12477 | private final void sendPackageBroadcastLocked(int cmd, String[] packages) { |
| 12478 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 12479 | ProcessRecord r = mLruProcesses.get(i); |
| 12480 | if (r.thread != null) { |
| 12481 | try { |
| 12482 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 12483 | } catch (RemoteException ex) { |
| 12484 | } |
| 12485 | } |
| 12486 | } |
| 12487 | } |
| 12488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12489 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 12490 | String callerPackage, Intent intent, String resolvedType, |
| 12491 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 12492 | Bundle map, String requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12493 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 12494 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12495 | intent = new Intent(intent); |
| 12496 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12497 | // By default broadcasts do not go to stopped apps. |
| 12498 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 12499 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12500 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12501 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 12502 | + " ordered=" + ordered + " userid=" + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12503 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12504 | 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] | 12505 | } |
| 12506 | |
| 12507 | // Handle special intents: if this broadcast is from the package |
| 12508 | // manager about a package being removed, we need to remove all of |
| 12509 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12510 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12511 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12512 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 12513 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 12514 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12515 | || uidRemoved) { |
| 12516 | if (checkComponentPermission( |
| 12517 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12518 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12519 | == PackageManager.PERMISSION_GRANTED) { |
| 12520 | if (uidRemoved) { |
| 12521 | final Bundle intentExtras = intent.getExtras(); |
| 12522 | final int uid = intentExtras != null |
| 12523 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 12524 | if (uid >= 0) { |
| 12525 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 12526 | synchronized (bs) { |
| 12527 | bs.removeUidStatsLocked(uid); |
| 12528 | } |
| 12529 | } |
| 12530 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12531 | // If resources are unvailble just force stop all |
| 12532 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 12533 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12534 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 12535 | if (list != null && (list.length > 0)) { |
| 12536 | for (String pkg : list) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12537 | forceStopPackageLocked(pkg, -1, false, true, true, false, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12538 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12539 | sendPackageBroadcastLocked( |
| 12540 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12541 | } |
| 12542 | } else { |
| 12543 | Uri data = intent.getData(); |
| 12544 | String ssp; |
| 12545 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 12546 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 12547 | forceStopPackageLocked(ssp, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12548 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, |
| 12549 | false, userId); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 12550 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12551 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12552 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
| 12553 | new String[] {ssp}); |
| 12554 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12555 | } |
| 12556 | } |
| 12557 | } |
| 12558 | } else { |
| 12559 | String msg = "Permission Denial: " + intent.getAction() |
| 12560 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 12561 | + ", uid=" + callingUid + ")" |
| 12562 | + " requires " |
| 12563 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12564 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12565 | throw new SecurityException(msg); |
| 12566 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12567 | |
| 12568 | // Special case for adding a package: by default turn on compatibility |
| 12569 | // mode. |
| 12570 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 12571 | Uri data = intent.getData(); |
| 12572 | String ssp; |
| 12573 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 12574 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 12575 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12576 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12577 | } |
| 12578 | |
| 12579 | /* |
| 12580 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 12581 | * of all currently running processes. This message will get queued up before the broadcast |
| 12582 | * happens. |
| 12583 | */ |
| 12584 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 12585 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 12586 | } |
| 12587 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 12588 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 12589 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 12590 | } |
| 12591 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 12592 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 12593 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 12594 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 12595 | } |
| 12596 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12597 | /* |
| 12598 | * Prevent non-system code (defined here to be non-persistent |
| 12599 | * processes) from sending protected broadcasts. |
| 12600 | */ |
| 12601 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 12602 | || callingUid == Process.SHELL_UID || callingUid == 0) { |
| 12603 | // Always okay. |
| 12604 | } else if (callerApp == null || !callerApp.persistent) { |
| 12605 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12606 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12607 | intent.getAction())) { |
| 12608 | String msg = "Permission Denial: not allowed to send broadcast " |
| 12609 | + intent.getAction() + " from pid=" |
| 12610 | + callingPid + ", uid=" + callingUid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12611 | Slog.w(TAG, msg); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12612 | throw new SecurityException(msg); |
| 12613 | } |
| 12614 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12615 | Slog.w(TAG, "Remote exception", e); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 12616 | return ActivityManager.BROADCAST_SUCCESS; |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12617 | } |
| 12618 | } |
| 12619 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12620 | // Add to the sticky list if requested. |
| 12621 | if (sticky) { |
| 12622 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 12623 | callingPid, callingUid) |
| 12624 | != PackageManager.PERMISSION_GRANTED) { |
| 12625 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 12626 | + callingPid + ", uid=" + callingUid |
| 12627 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12628 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12629 | throw new SecurityException(msg); |
| 12630 | } |
| 12631 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12632 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12633 | + " and enforce permission " + requiredPermission); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 12634 | return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12635 | } |
| 12636 | if (intent.getComponent() != null) { |
| 12637 | throw new SecurityException( |
| 12638 | "Sticky broadcasts can't target a specific component"); |
| 12639 | } |
| 12640 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 12641 | if (list == null) { |
| 12642 | list = new ArrayList<Intent>(); |
| 12643 | mStickyBroadcasts.put(intent.getAction(), list); |
| 12644 | } |
| 12645 | int N = list.size(); |
| 12646 | int i; |
| 12647 | for (i=0; i<N; i++) { |
| 12648 | if (intent.filterEquals(list.get(i))) { |
| 12649 | // This sticky already exists, replace it. |
| 12650 | list.set(i, new Intent(intent)); |
| 12651 | break; |
| 12652 | } |
| 12653 | } |
| 12654 | if (i >= N) { |
| 12655 | list.add(new Intent(intent)); |
| 12656 | } |
| 12657 | } |
| 12658 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12659 | // Figure out who all will receive this broadcast. |
| 12660 | List receivers = null; |
| 12661 | List<BroadcastFilter> registeredReceivers = null; |
| 12662 | try { |
| 12663 | if (intent.getComponent() != null) { |
| 12664 | // Broadcast is going to one specific receiver class... |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12665 | ActivityInfo ai = AppGlobals.getPackageManager(). |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12666 | getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12667 | if (ai != null) { |
| 12668 | receivers = new ArrayList(); |
| 12669 | ResolveInfo ri = new ResolveInfo(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12670 | ri.activityInfo = getActivityInfoForUser(ai, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12671 | receivers.add(ri); |
| 12672 | } |
| 12673 | } else { |
| 12674 | // Need to resolve the intent to interested receivers... |
| 12675 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 12676 | == 0) { |
| 12677 | receivers = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12678 | AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12679 | intent, resolvedType, STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12680 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12681 | registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false, |
| 12682 | userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12683 | } |
| 12684 | } catch (RemoteException ex) { |
| 12685 | // pm is in same process, this will never happen. |
| 12686 | } |
| 12687 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12688 | final boolean replacePending = |
| 12689 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 12690 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12691 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12692 | + " replacePending=" + replacePending); |
| 12693 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12694 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 12695 | if (!ordered && NR > 0) { |
| 12696 | // If we are not serializing this broadcast, then send the |
| 12697 | // registered receivers separately so they don't wait for the |
| 12698 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12699 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 12700 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12701 | callerPackage, callingPid, callingUid, requiredPermission, |
| 12702 | registeredReceivers, resultTo, resultCode, resultData, map, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12703 | ordered, sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12704 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12705 | TAG, "Enqueueing parallel broadcast " + r); |
| 12706 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12707 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12708 | queue.enqueueParallelBroadcastLocked(r); |
| 12709 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12710 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12711 | registeredReceivers = null; |
| 12712 | NR = 0; |
| 12713 | } |
| 12714 | |
| 12715 | // Merge into one list. |
| 12716 | int ir = 0; |
| 12717 | if (receivers != null) { |
| 12718 | // A special case for PACKAGE_ADDED: do not allow the package |
| 12719 | // being added to see this broadcast. This prevents them from |
| 12720 | // using this as a back door to get run as soon as they are |
| 12721 | // installed. Maybe in the future we want to have a special install |
| 12722 | // broadcast or such for apps, but we'd like to deliberately make |
| 12723 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12724 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12725 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 12726 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 12727 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12728 | Uri data = intent.getData(); |
| 12729 | if (data != null) { |
| 12730 | String pkgName = data.getSchemeSpecificPart(); |
| 12731 | if (pkgName != null) { |
| 12732 | skipPackages = new String[] { pkgName }; |
| 12733 | } |
| 12734 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12735 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12736 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12737 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12738 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 12739 | for (String skipPackage : skipPackages) { |
| 12740 | if (skipPackage != null) { |
| 12741 | int NT = receivers.size(); |
| 12742 | for (int it=0; it<NT; it++) { |
| 12743 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 12744 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 12745 | receivers.remove(it); |
| 12746 | it--; |
| 12747 | NT--; |
| 12748 | } |
| 12749 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12750 | } |
| 12751 | } |
| 12752 | } |
| 12753 | |
| 12754 | int NT = receivers != null ? receivers.size() : 0; |
| 12755 | int it = 0; |
| 12756 | ResolveInfo curt = null; |
| 12757 | BroadcastFilter curr = null; |
| 12758 | while (it < NT && ir < NR) { |
| 12759 | if (curt == null) { |
| 12760 | curt = (ResolveInfo)receivers.get(it); |
| 12761 | } |
| 12762 | if (curr == null) { |
| 12763 | curr = registeredReceivers.get(ir); |
| 12764 | } |
| 12765 | if (curr.getPriority() >= curt.priority) { |
| 12766 | // Insert this broadcast record into the final list. |
| 12767 | receivers.add(it, curr); |
| 12768 | ir++; |
| 12769 | curr = null; |
| 12770 | it++; |
| 12771 | NT++; |
| 12772 | } else { |
| 12773 | // Skip to the next ResolveInfo in the final list. |
| 12774 | it++; |
| 12775 | curt = null; |
| 12776 | } |
| 12777 | } |
| 12778 | } |
| 12779 | while (ir < NR) { |
| 12780 | if (receivers == null) { |
| 12781 | receivers = new ArrayList(); |
| 12782 | } |
| 12783 | receivers.add(registeredReceivers.get(ir)); |
| 12784 | ir++; |
| 12785 | } |
| 12786 | |
| 12787 | if ((receivers != null && receivers.size() > 0) |
| 12788 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12789 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 12790 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12791 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12792 | receivers, resultTo, resultCode, resultData, map, ordered, |
| 12793 | sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12794 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12795 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12796 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12797 | if (DEBUG_BROADCAST) { |
| 12798 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12799 | 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] | 12800 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12801 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12802 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12803 | queue.enqueueOrderedBroadcastLocked(r); |
| 12804 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12806 | } |
| 12807 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 12808 | return ActivityManager.BROADCAST_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12809 | } |
| 12810 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12811 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12812 | // Refuse possible leaked file descriptors |
| 12813 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12814 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12815 | } |
| 12816 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12817 | int flags = intent.getFlags(); |
| 12818 | |
| 12819 | if (!mProcessesReady) { |
| 12820 | // if the caller really truly claims to know what they're doing, go |
| 12821 | // ahead and allow the broadcast without launching any receivers |
| 12822 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 12823 | intent = new Intent(intent); |
| 12824 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 12825 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 12826 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 12827 | + " before boot completion"); |
| 12828 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 12829 | } |
| 12830 | } |
| 12831 | |
| 12832 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 12833 | throw new IllegalArgumentException( |
| 12834 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 12835 | } |
| 12836 | |
| 12837 | return intent; |
| 12838 | } |
| 12839 | |
| 12840 | public final int broadcastIntent(IApplicationThread caller, |
| 12841 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12842 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12843 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12844 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12845 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12846 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12847 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12848 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 12849 | final int callingPid = Binder.getCallingPid(); |
| 12850 | final int callingUid = Binder.getCallingUid(); |
| 12851 | final long origId = Binder.clearCallingIdentity(); |
| 12852 | int res = broadcastIntentLocked(callerApp, |
| 12853 | callerApp != null ? callerApp.info.packageName : null, |
| 12854 | intent, resolvedType, resultTo, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12855 | resultCode, resultData, map, requiredPermission, serialized, sticky, |
| 12856 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12857 | Binder.restoreCallingIdentity(origId); |
| 12858 | return res; |
| 12859 | } |
| 12860 | } |
| 12861 | |
| 12862 | int broadcastIntentInPackage(String packageName, int uid, |
| 12863 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12864 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12865 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12866 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12867 | intent = verifyBroadcastLocked(intent); |
| 12868 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12869 | final long origId = Binder.clearCallingIdentity(); |
| 12870 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 12871 | resultTo, resultCode, resultData, map, requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12872 | serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12873 | Binder.restoreCallingIdentity(origId); |
| 12874 | return res; |
| 12875 | } |
| 12876 | } |
| 12877 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12878 | // TODO: Use the userId; maybe mStickyBroadcasts need to be tied to the user. |
| 12879 | 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] | 12880 | // Refuse possible leaked file descriptors |
| 12881 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12882 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12883 | } |
| 12884 | |
| 12885 | synchronized(this) { |
| 12886 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 12887 | != PackageManager.PERMISSION_GRANTED) { |
| 12888 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 12889 | + Binder.getCallingPid() |
| 12890 | + ", uid=" + Binder.getCallingUid() |
| 12891 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12892 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12893 | throw new SecurityException(msg); |
| 12894 | } |
| 12895 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 12896 | if (list != null) { |
| 12897 | int N = list.size(); |
| 12898 | int i; |
| 12899 | for (i=0; i<N; i++) { |
| 12900 | if (intent.filterEquals(list.get(i))) { |
| 12901 | list.remove(i); |
| 12902 | break; |
| 12903 | } |
| 12904 | } |
| 12905 | } |
| 12906 | } |
| 12907 | } |
| 12908 | |
| 12909 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 12910 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 12911 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12912 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 12913 | if (r == null) { |
| 12914 | 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] | 12915 | return false; |
| 12916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12917 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12918 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 12919 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12920 | } |
| 12921 | |
| 12922 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 12923 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12924 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12925 | |
| 12926 | // Refuse possible leaked file descriptors |
| 12927 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 12928 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12929 | } |
| 12930 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12931 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12932 | try { |
| 12933 | boolean doNext = false; |
| 12934 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12935 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12936 | synchronized(this) { |
| 12937 | r = broadcastRecordForReceiverLocked(who); |
| 12938 | if (r != null) { |
| 12939 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 12940 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12941 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12942 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12943 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12944 | if (doNext) { |
| 12945 | r.queue.processNextBroadcast(false); |
| 12946 | } |
| 12947 | trimApplications(); |
| 12948 | } finally { |
| 12949 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12950 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12951 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12952 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12953 | // ========================================================= |
| 12954 | // INSTRUMENTATION |
| 12955 | // ========================================================= |
| 12956 | |
| 12957 | public boolean startInstrumentation(ComponentName className, |
| 12958 | String profileFile, int flags, Bundle arguments, |
| 12959 | IInstrumentationWatcher watcher) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12960 | enforceNotIsolatedCaller("startInstrumentation"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12961 | // Refuse possible leaked file descriptors |
| 12962 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 12963 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12964 | } |
| 12965 | |
| 12966 | synchronized(this) { |
| 12967 | InstrumentationInfo ii = null; |
| 12968 | ApplicationInfo ai = null; |
| 12969 | try { |
| 12970 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12971 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12972 | ai = mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12973 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12974 | } catch (PackageManager.NameNotFoundException e) { |
| 12975 | } |
| 12976 | if (ii == null) { |
| 12977 | reportStartInstrumentationFailure(watcher, className, |
| 12978 | "Unable to find instrumentation info for: " + className); |
| 12979 | return false; |
| 12980 | } |
| 12981 | if (ai == null) { |
| 12982 | reportStartInstrumentationFailure(watcher, className, |
| 12983 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12984 | return false; |
| 12985 | } |
| 12986 | |
| 12987 | int match = mContext.getPackageManager().checkSignatures( |
| 12988 | ii.targetPackage, ii.packageName); |
| 12989 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12990 | String msg = "Permission Denial: starting instrumentation " |
| 12991 | + className + " from pid=" |
| 12992 | + Binder.getCallingPid() |
| 12993 | + ", uid=" + Binder.getCallingPid() |
| 12994 | + " not allowed because package " + ii.packageName |
| 12995 | + " does not have a signature matching the target " |
| 12996 | + ii.targetPackage; |
| 12997 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12998 | throw new SecurityException(msg); |
| 12999 | } |
| 13000 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 13001 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13002 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 13003 | // Instrumentation can kill and relaunch even persistent processes |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 13004 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13005 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13006 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13007 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13008 | app.instrumentationProfileFile = profileFile; |
| 13009 | app.instrumentationArguments = arguments; |
| 13010 | app.instrumentationWatcher = watcher; |
| 13011 | app.instrumentationResultClass = className; |
| 13012 | Binder.restoreCallingIdentity(origId); |
| 13013 | } |
| 13014 | |
| 13015 | return true; |
| 13016 | } |
| 13017 | |
| 13018 | /** |
| 13019 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 13020 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 13021 | * the "am" command to report errors with more information. |
| 13022 | * |
| 13023 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 13024 | * @param cn The component name of the instrumentation. |
| 13025 | * @param report The error report. |
| 13026 | */ |
| 13027 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 13028 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13029 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13030 | try { |
| 13031 | if (watcher != null) { |
| 13032 | Bundle results = new Bundle(); |
| 13033 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 13034 | results.putString("Error", report); |
| 13035 | watcher.instrumentationStatus(cn, -1, results); |
| 13036 | } |
| 13037 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13038 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13039 | } |
| 13040 | } |
| 13041 | |
| 13042 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 13043 | if (app.instrumentationWatcher != null) { |
| 13044 | try { |
| 13045 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 13046 | app.instrumentationWatcher.instrumentationFinished( |
| 13047 | app.instrumentationClass, |
| 13048 | resultCode, |
| 13049 | results); |
| 13050 | } catch (RemoteException e) { |
| 13051 | } |
| 13052 | } |
| 13053 | app.instrumentationWatcher = null; |
| 13054 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13055 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13056 | app.instrumentationProfileFile = null; |
| 13057 | app.instrumentationArguments = null; |
| 13058 | |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 13059 | 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] | 13060 | } |
| 13061 | |
| 13062 | public void finishInstrumentation(IApplicationThread target, |
| 13063 | int resultCode, Bundle results) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 13064 | int userId = UserId.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13065 | // Refuse possible leaked file descriptors |
| 13066 | if (results != null && results.hasFileDescriptors()) { |
| 13067 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 13068 | } |
| 13069 | |
| 13070 | synchronized(this) { |
| 13071 | ProcessRecord app = getRecordForAppLocked(target); |
| 13072 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13073 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13074 | return; |
| 13075 | } |
| 13076 | final long origId = Binder.clearCallingIdentity(); |
| 13077 | finishInstrumentationLocked(app, resultCode, results); |
| 13078 | Binder.restoreCallingIdentity(origId); |
| 13079 | } |
| 13080 | } |
| 13081 | |
| 13082 | // ========================================================= |
| 13083 | // CONFIGURATION |
| 13084 | // ========================================================= |
| 13085 | |
| 13086 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 13087 | ConfigurationInfo config = new ConfigurationInfo(); |
| 13088 | synchronized (this) { |
| 13089 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 13090 | config.reqKeyboardType = mConfiguration.keyboard; |
| 13091 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 13092 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 13093 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13094 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 13095 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 13096 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 13097 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13098 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 13099 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 13100 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13101 | } |
| 13102 | return config; |
| 13103 | } |
| 13104 | |
| 13105 | public Configuration getConfiguration() { |
| 13106 | Configuration ci; |
| 13107 | synchronized(this) { |
| 13108 | ci = new Configuration(mConfiguration); |
| 13109 | } |
| 13110 | return ci; |
| 13111 | } |
| 13112 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13113 | public void updatePersistentConfiguration(Configuration values) { |
| 13114 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 13115 | "updateConfiguration()"); |
| 13116 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 13117 | "updateConfiguration()"); |
| 13118 | if (values == null) { |
| 13119 | throw new NullPointerException("Configuration must not be null"); |
| 13120 | } |
| 13121 | |
| 13122 | synchronized(this) { |
| 13123 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13124 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13125 | Binder.restoreCallingIdentity(origId); |
| 13126 | } |
| 13127 | } |
| 13128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13129 | public void updateConfiguration(Configuration values) { |
| 13130 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 13131 | "updateConfiguration()"); |
| 13132 | |
| 13133 | synchronized(this) { |
| 13134 | if (values == null && mWindowManager != null) { |
| 13135 | // sentinel: fetch the current configuration from the window manager |
| 13136 | values = mWindowManager.computeNewConfiguration(); |
| 13137 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13138 | |
| 13139 | if (mWindowManager != null) { |
| 13140 | mProcessList.applyDisplaySize(mWindowManager); |
| 13141 | } |
| 13142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13143 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13144 | if (values != null) { |
| 13145 | Settings.System.clearConfiguration(values); |
| 13146 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13147 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13148 | Binder.restoreCallingIdentity(origId); |
| 13149 | } |
| 13150 | } |
| 13151 | |
| 13152 | /** |
| 13153 | * Do either or both things: (1) change the current configuration, and (2) |
| 13154 | * make sure the given activity is running with the (now) current |
| 13155 | * configuration. Returns true if the activity has been left running, or |
| 13156 | * false if <var>starting</var> is being destroyed to match the new |
| 13157 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13158 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13159 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 13160 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13161 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 13162 | // do nothing if we are headless |
| 13163 | if (mHeadless) return true; |
| 13164 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13165 | int changes = 0; |
| 13166 | |
| 13167 | boolean kept = true; |
| 13168 | |
| 13169 | if (values != null) { |
| 13170 | Configuration newConfig = new Configuration(mConfiguration); |
| 13171 | changes = newConfig.updateFrom(values); |
| 13172 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 13173 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13174 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13175 | } |
| 13176 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 13177 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13178 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13179 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13180 | saveLocaleLocked(values.locale, |
| 13181 | !values.locale.equals(mConfiguration.locale), |
| 13182 | values.userSetLocale); |
| 13183 | } |
| 13184 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 13185 | mConfigurationSeq++; |
| 13186 | if (mConfigurationSeq <= 0) { |
| 13187 | mConfigurationSeq = 1; |
| 13188 | } |
| 13189 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13190 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13191 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13192 | |
| 13193 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 13194 | |
| 13195 | // TODO: If our config changes, should we auto dismiss any currently |
| 13196 | // showing dialogs? |
| 13197 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13198 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 13199 | AttributeCache ac = AttributeCache.instance(); |
| 13200 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13201 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 13202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13203 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 13204 | // Make sure all resources in our process are updated |
| 13205 | // right now, so that anyone who is going to retrieve |
| 13206 | // resource values after we return will be sure to get |
| 13207 | // the new ones. This is especially important during |
| 13208 | // boot, where the first config change needs to guarantee |
| 13209 | // all resources have that config before following boot |
| 13210 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13211 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 13212 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13213 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 13214 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13215 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 13216 | mHandler.sendMessage(msg); |
| 13217 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13218 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13219 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 13220 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13221 | try { |
| 13222 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13223 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 13224 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13225 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13226 | } |
| 13227 | } catch (Exception e) { |
| 13228 | } |
| 13229 | } |
| 13230 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 13231 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 13232 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13233 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13234 | null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 13235 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 13236 | broadcastIntentLocked(null, null, |
| 13237 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 13238 | null, null, 0, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13239 | null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 13240 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13241 | } |
| 13242 | } |
| 13243 | |
| 13244 | if (changes != 0 && starting == null) { |
| 13245 | // If the configuration changed, and the caller is not already |
| 13246 | // in the process of starting an activity, then find the top |
| 13247 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13248 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13249 | } |
| 13250 | |
| 13251 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13252 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 13253 | // And we need to make sure at this point that all other activities |
| 13254 | // are made visible with the correct configuration. |
| 13255 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13256 | } |
| 13257 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 13258 | if (values != null && mWindowManager != null) { |
| 13259 | mWindowManager.setNewConfiguration(mConfiguration); |
| 13260 | } |
| 13261 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13262 | return kept; |
| 13263 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 13264 | |
| 13265 | /** |
| 13266 | * Decide based on the configuration whether we should shouw the ANR, |
| 13267 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 13268 | * press the on-screen buttons, we shouldn't show the dialog. |
| 13269 | * |
| 13270 | * A thought: SystemUI might also want to get told about this, the Power |
| 13271 | * dialog / global actions also might want different behaviors. |
| 13272 | */ |
| 13273 | private static final boolean shouldShowDialogs(Configuration config) { |
| 13274 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 13275 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 13276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13277 | |
| 13278 | /** |
| 13279 | * Save the locale. You must be inside a synchronized (this) block. |
| 13280 | */ |
| 13281 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 13282 | if(isDiff) { |
| 13283 | SystemProperties.set("user.language", l.getLanguage()); |
| 13284 | SystemProperties.set("user.region", l.getCountry()); |
| 13285 | } |
| 13286 | |
| 13287 | if(isPersist) { |
| 13288 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 13289 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 13290 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 13291 | } |
| 13292 | } |
| 13293 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 13294 | @Override |
| 13295 | public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) { |
| 13296 | ActivityRecord srec = ActivityRecord.forToken(token); |
| 13297 | return srec.task.affinity != null && srec.task.affinity.equals(destAffinity); |
| 13298 | } |
| 13299 | |
| 13300 | public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode, |
| 13301 | Intent resultData) { |
| 13302 | ComponentName dest = destIntent.getComponent(); |
| 13303 | |
| 13304 | synchronized (this) { |
| 13305 | ActivityRecord srec = ActivityRecord.forToken(token); |
| 13306 | ArrayList<ActivityRecord> history = srec.stack.mHistory; |
| 13307 | final int start = history.indexOf(srec); |
| 13308 | if (start < 0) { |
| 13309 | // Current activity is not in history stack; do nothing. |
| 13310 | return false; |
| 13311 | } |
| 13312 | int finishTo = start - 1; |
| 13313 | ActivityRecord parent = null; |
| 13314 | boolean foundParentInTask = false; |
| 13315 | if (dest != null) { |
| 13316 | TaskRecord tr = srec.task; |
| 13317 | for (int i = start - 1; i >= 0; i--) { |
| 13318 | ActivityRecord r = history.get(i); |
| 13319 | if (tr != r.task) { |
| 13320 | // Couldn't find parent in the same task; stop at the one above this. |
| 13321 | // (Root of current task; in-app "home" behavior) |
| 13322 | // Always at least finish the current activity. |
| 13323 | finishTo = Math.min(start - 1, i + 1); |
| 13324 | parent = history.get(finishTo); |
| 13325 | break; |
| 13326 | } else if (r.info.packageName.equals(dest.getPackageName()) && |
| 13327 | r.info.name.equals(dest.getClassName())) { |
| 13328 | finishTo = i; |
| 13329 | parent = r; |
| 13330 | foundParentInTask = true; |
| 13331 | break; |
| 13332 | } |
| 13333 | } |
| 13334 | } |
| 13335 | |
| 13336 | if (mController != null) { |
| 13337 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
| 13338 | if (next != null) { |
| 13339 | // ask watcher if this is allowed |
| 13340 | boolean resumeOK = true; |
| 13341 | try { |
| 13342 | resumeOK = mController.activityResuming(next.packageName); |
| 13343 | } catch (RemoteException e) { |
| 13344 | mController = null; |
| 13345 | } |
| 13346 | |
| 13347 | if (!resumeOK) { |
| 13348 | return false; |
| 13349 | } |
| 13350 | } |
| 13351 | } |
| 13352 | final long origId = Binder.clearCallingIdentity(); |
| 13353 | for (int i = start; i > finishTo; i--) { |
| 13354 | ActivityRecord r = history.get(i); |
| 13355 | mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData, |
| 13356 | "navigate-up"); |
| 13357 | // Only return the supplied result for the first activity finished |
| 13358 | resultCode = Activity.RESULT_CANCELED; |
| 13359 | resultData = null; |
| 13360 | } |
| 13361 | |
| 13362 | if (parent != null && foundParentInTask) { |
| 13363 | final int parentLaunchMode = parent.info.launchMode; |
| 13364 | final int destIntentFlags = destIntent.getFlags(); |
| 13365 | if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || |
| 13366 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK || |
| 13367 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP || |
| 13368 | (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
| 13369 | parent.deliverNewIntentLocked(srec.app.uid, destIntent); |
| 13370 | } else { |
| 13371 | try { |
| 13372 | ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo( |
| 13373 | destIntent.getComponent(), 0, UserId.getCallingUserId()); |
| 13374 | int res = mMainStack.startActivityLocked(srec.app.thread, destIntent, |
| 13375 | null, aInfo, parent.appToken, null, |
| 13376 | 0, -1, parent.launchedFromUid, 0, null, true, null); |
| 13377 | foundParentInTask = res == ActivityManager.START_SUCCESS; |
| 13378 | } catch (RemoteException e) { |
| 13379 | foundParentInTask = false; |
| 13380 | } |
| 13381 | mMainStack.requestFinishActivityLocked(parent.appToken, resultCode, |
| 13382 | resultData, "navigate-up"); |
| 13383 | } |
| 13384 | } |
| 13385 | Binder.restoreCallingIdentity(origId); |
| 13386 | return foundParentInTask; |
| 13387 | } |
| 13388 | } |
| 13389 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13390 | // ========================================================= |
| 13391 | // LIFETIME MANAGEMENT |
| 13392 | // ========================================================= |
| 13393 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13394 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 13395 | // on, or 'null' if the app is not an active broadcast recipient. |
| 13396 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 13397 | BroadcastRecord r = app.curReceiver; |
| 13398 | if (r != null) { |
| 13399 | return r.queue; |
| 13400 | } |
| 13401 | |
| 13402 | // It's not the current receiver, but it might be starting up to become one |
| 13403 | synchronized (this) { |
| 13404 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 13405 | r = queue.mPendingBroadcast; |
| 13406 | if (r != null && r.curApp == app) { |
| 13407 | // found it; report which queue it's in |
| 13408 | return queue; |
| 13409 | } |
| 13410 | } |
| 13411 | } |
| 13412 | |
| 13413 | return null; |
| 13414 | } |
| 13415 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13416 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13417 | ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13418 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13419 | // This adjustment has already been computed. If we are calling |
| 13420 | // from the top, we may have already computed our adjustment with |
| 13421 | // an earlier hidden adjustment that isn't really for us... if |
| 13422 | // so, use the new hidden adjustment. |
| 13423 | if (!recursed && app.hidden) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13424 | app.curAdj = app.curRawAdj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13425 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13426 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13427 | } |
| 13428 | |
| 13429 | if (app.thread == null) { |
| 13430 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13431 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13432 | return (app.curAdj=ProcessList.HIDDEN_APP_MAX_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13433 | } |
| 13434 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13435 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 13436 | app.adjSource = null; |
| 13437 | app.adjTarget = null; |
| 13438 | app.empty = false; |
| 13439 | app.hidden = false; |
| 13440 | |
| 13441 | final int activitiesSize = app.activities.size(); |
| 13442 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13443 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13444 | // The max adjustment doesn't allow this app to be anything |
| 13445 | // below foreground, so it is not worth doing work for it. |
| 13446 | app.adjType = "fixed"; |
| 13447 | app.adjSeq = mAdjSeq; |
| 13448 | app.curRawAdj = app.maxAdj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13449 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13450 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13451 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13452 | // System process can do UI, and when they do we want to have |
| 13453 | // them trim their memory after the user leaves the UI. To |
| 13454 | // facilitate this, here we need to determine whether or not it |
| 13455 | // is currently showing UI. |
| 13456 | app.systemNoUi = true; |
| 13457 | if (app == TOP_APP) { |
| 13458 | app.systemNoUi = false; |
| 13459 | } else if (activitiesSize > 0) { |
| 13460 | for (int j = 0; j < activitiesSize; j++) { |
| 13461 | final ActivityRecord r = app.activities.get(j); |
| 13462 | if (r.visible) { |
| 13463 | app.systemNoUi = false; |
| 13464 | break; |
| 13465 | } |
| 13466 | } |
| 13467 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13468 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13469 | } |
| 13470 | |
| 13471 | final boolean hadForegroundActivities = app.foregroundActivities; |
| 13472 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13473 | app.foregroundActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13474 | app.keeping = false; |
| 13475 | app.systemNoUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13476 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13477 | // Determine the importance of the process, starting with most |
| 13478 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13479 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13480 | int schedGroup; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13481 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13482 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13483 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13484 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13485 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13486 | app.adjType = "top-activity"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13487 | app.foregroundActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13488 | } else if (app.instrumentationClass != null) { |
| 13489 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13490 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13491 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13492 | app.adjType = "instrumentation"; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13493 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13494 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13495 | // counts as being in the foreground for OOM killer purposes. |
| 13496 | // It's placed in a sched group based on the nature of the |
| 13497 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13498 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13499 | schedGroup = (queue == mFgBroadcastQueue) |
| 13500 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13501 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13502 | } else if (app.executingServices.size() > 0) { |
| 13503 | // An app that is currently executing a service callback also |
| 13504 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13505 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13506 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13507 | app.adjType = "exec-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13508 | } else if (activitiesSize > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13509 | // This app is in the background with paused activities. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13510 | // We inspect activities to potentially upgrade adjustment further below. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13511 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13512 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13513 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13514 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13515 | } else { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13516 | // A very not-needed process. If this is lower in the lru list, |
| 13517 | // we will push it in to the empty bucket. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13518 | adj = hiddenAdj; |
| 13519 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13520 | app.hidden = true; |
| 13521 | app.empty = true; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13522 | app.adjType = "bg-empty"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13523 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13524 | |
| 13525 | // Examine all activities if not already foreground. |
| 13526 | if (!app.foregroundActivities && activitiesSize > 0) { |
| 13527 | for (int j = 0; j < activitiesSize; j++) { |
| 13528 | final ActivityRecord r = app.activities.get(j); |
| 13529 | if (r.visible) { |
| 13530 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13531 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13532 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13533 | app.adjType = "visible"; |
| 13534 | } |
| 13535 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13536 | app.hidden = false; |
| 13537 | app.foregroundActivities = true; |
| 13538 | break; |
| 13539 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED |
| 13540 | || r.state == ActivityState.STOPPING) { |
| 13541 | // Only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13542 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13543 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13544 | app.adjType = "stopping"; |
| 13545 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 13546 | app.hidden = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13547 | app.foregroundActivities = true; |
| 13548 | } |
| 13549 | } |
| 13550 | } |
| 13551 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13552 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13553 | if (app.foregroundServices) { |
| 13554 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13555 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13556 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13557 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13558 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13559 | } else if (app.forcingToForeground != null) { |
| 13560 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13561 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13562 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13563 | app.adjType = "force-foreground"; |
| 13564 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13565 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13566 | } |
| 13567 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13568 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13569 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13570 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13571 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13572 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13573 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13574 | app.adjType = "heavy"; |
| 13575 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13576 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13577 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13578 | // This process is hosting what we currently consider to be the |
| 13579 | // 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] | 13580 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13581 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13582 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13583 | app.adjType = "home"; |
| 13584 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13585 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 13586 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 13587 | && app.activities.size() > 0) { |
| 13588 | // This was the previous process that showed UI to the user. |
| 13589 | // We want to try to keep it around more aggressively, to give |
| 13590 | // a good experience around switching between two apps. |
| 13591 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 13592 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 13593 | app.hidden = false; |
| 13594 | app.adjType = "previous"; |
| 13595 | } |
| 13596 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13597 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 13598 | + " reason=" + app.adjType); |
| 13599 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13600 | // 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] | 13601 | // there are applications dependent on our services or providers, but |
| 13602 | // this gives us a baseline and makes sure we don't get into an |
| 13603 | // infinite recursion. |
| 13604 | app.adjSeq = mAdjSeq; |
| 13605 | app.curRawAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13606 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13607 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 13608 | // 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] | 13609 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13610 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13611 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13612 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13613 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13614 | } |
| 13615 | } |
| 13616 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13617 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13618 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13619 | final long now = SystemClock.uptimeMillis(); |
| 13620 | // This process is more important if the top activity is |
| 13621 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13622 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13623 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13624 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13625 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13626 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13627 | // If this process has shown some UI, let it immediately |
| 13628 | // go to the LRU list because it may be pretty heavy with |
| 13629 | // UI stuff. We'll tag it with a label just to help |
| 13630 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13631 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13632 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13633 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13634 | } else { |
| 13635 | if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13636 | // This service has seen some activity within |
| 13637 | // recent memory, so we will keep its process ahead |
| 13638 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13639 | if (adj > ProcessList.SERVICE_ADJ) { |
| 13640 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13641 | app.adjType = "started-services"; |
| 13642 | app.hidden = false; |
| 13643 | } |
| 13644 | } |
| 13645 | // If we have let the service slide into the background |
| 13646 | // state, still have some text describing what it is doing |
| 13647 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13648 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13649 | app.adjType = "started-bg-services"; |
| 13650 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13651 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13652 | // Don't kill this process because it is doing work; it |
| 13653 | // has said it is doing work. |
| 13654 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13655 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13656 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13657 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13658 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13659 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13660 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13661 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13662 | 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] | 13663 | // XXX should compute this based on the max of |
| 13664 | // all connected clients. |
| 13665 | ConnectionRecord cr = clist.get(i); |
| 13666 | if (cr.binding.client == app) { |
| 13667 | // Binding to ourself is not interesting. |
| 13668 | continue; |
| 13669 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13670 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13671 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13672 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13673 | int myHiddenAdj = hiddenAdj; |
| 13674 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13675 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13676 | myHiddenAdj = client.hiddenAdj; |
| 13677 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13678 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13679 | } |
| 13680 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13681 | clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13682 | client, myHiddenAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13683 | String adjType = null; |
| 13684 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 13685 | // Not doing bind OOM management, so treat |
| 13686 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13687 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13688 | // If this process has shown some UI, let it immediately |
| 13689 | // go to the LRU list because it may be pretty heavy with |
| 13690 | // UI stuff. We'll tag it with a label just to help |
| 13691 | // debug and understand what is going on. |
| 13692 | if (adj > clientAdj) { |
| 13693 | adjType = "bound-bg-ui-services"; |
| 13694 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13695 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13696 | clientAdj = adj; |
| 13697 | } else { |
| 13698 | if (now >= (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13699 | // This service has not seen activity within |
| 13700 | // recent memory, so allow it to drop to the |
| 13701 | // LRU list if there is no other reason to keep |
| 13702 | // it around. We'll also tag it with a label just |
| 13703 | // to help debug and undertand what is going on. |
| 13704 | if (adj > clientAdj) { |
| 13705 | adjType = "bound-bg-services"; |
| 13706 | } |
| 13707 | clientAdj = adj; |
| 13708 | } |
| 13709 | } |
| 13710 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13711 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13712 | // If this process has recently shown UI, and |
| 13713 | // the process that is binding to it is less |
| 13714 | // important than being visible, then we don't |
| 13715 | // care about the binding as much as we care |
| 13716 | // about letting this process get into the LRU |
| 13717 | // list to be killed and restarted if needed for |
| 13718 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13719 | if (app.hasShownUi && app != mHomeProcess |
| 13720 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13721 | adjType = "bound-bg-ui-services"; |
| 13722 | } else { |
| 13723 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 13724 | |Context.BIND_IMPORTANT)) != 0) { |
| 13725 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13726 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 13727 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 13728 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13729 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13730 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13731 | adj = clientAdj; |
| 13732 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13733 | app.pendingUiClean = true; |
| 13734 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13735 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13736 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13737 | } |
| 13738 | if (!client.hidden) { |
| 13739 | app.hidden = false; |
| 13740 | } |
| 13741 | if (client.keeping) { |
| 13742 | app.keeping = true; |
| 13743 | } |
| 13744 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13745 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13746 | } |
| 13747 | if (adjType != null) { |
| 13748 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13749 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13750 | .REASON_SERVICE_IN_USE; |
| 13751 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13752 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13753 | app.adjTarget = s.name; |
| 13754 | } |
| 13755 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13756 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13757 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13758 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13759 | } |
| 13760 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13761 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
| 13762 | ActivityRecord a = cr.activity; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13763 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13764 | (a.visible || a.state == ActivityState.RESUMED |
| 13765 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13766 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13767 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13768 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13769 | } |
| 13770 | app.hidden = false; |
| 13771 | app.adjType = "service"; |
| 13772 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13773 | .REASON_SERVICE_IN_USE; |
| 13774 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13775 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13776 | app.adjTarget = s.name; |
| 13777 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13778 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13779 | } |
| 13780 | } |
| 13781 | } |
| 13782 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13783 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13784 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13785 | // would like to avoid killing it unless it would prevent the current |
| 13786 | // application from running. By default we put the process in |
| 13787 | // with the rest of the background processes; as we scan through |
| 13788 | // its services we may bump it up from there. |
| 13789 | if (adj > hiddenAdj) { |
| 13790 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13791 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13792 | app.adjType = "bg-services"; |
| 13793 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13794 | } |
| 13795 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13796 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13797 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13798 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13799 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13800 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13801 | ContentProviderRecord cpr = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13802 | if (cpr.clients.size() != 0) { |
| 13803 | Iterator<ProcessRecord> kt = cpr.clients.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13804 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13805 | ProcessRecord client = kt.next(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 13806 | if (client == app) { |
| 13807 | // Being our own client is not interesting. |
| 13808 | continue; |
| 13809 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13810 | int myHiddenAdj = hiddenAdj; |
| 13811 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13812 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13813 | myHiddenAdj = client.hiddenAdj; |
| 13814 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13815 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13816 | } |
| 13817 | } |
| 13818 | int clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13819 | client, myHiddenAdj, TOP_APP, true, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13820 | if (adj > clientAdj) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13821 | if (app.hasShownUi && app != mHomeProcess |
| 13822 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13823 | app.adjType = "bg-ui-provider"; |
| 13824 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13825 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13826 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13827 | app.adjType = "provider"; |
| 13828 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13829 | if (!client.hidden) { |
| 13830 | app.hidden = false; |
| 13831 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13832 | if (client.keeping) { |
| 13833 | app.keeping = true; |
| 13834 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 13835 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13836 | .REASON_PROVIDER_IN_USE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13837 | app.adjSource = client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13838 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13839 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13840 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13841 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13842 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13843 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13844 | } |
| 13845 | } |
| 13846 | // If the provider has external (non-framework) process |
| 13847 | // dependencies, ensure that its adjustment is at least |
| 13848 | // FOREGROUND_APP_ADJ. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 13849 | if (cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13850 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13851 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13852 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13853 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13854 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13855 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13856 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13857 | } |
| 13858 | } |
| 13859 | } |
| 13860 | } |
| 13861 | |
| 13862 | app.curRawAdj = adj; |
| 13863 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13864 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13865 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 13866 | if (adj > app.maxAdj) { |
| 13867 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13868 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13869 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13870 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13871 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13872 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13873 | app.keeping = true; |
| 13874 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13875 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13876 | if (app.hasAboveClient) { |
| 13877 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 13878 | // then we need to drop its adjustment to be lower than the service's |
| 13879 | // in order to honor the request. We want to drop it by one adjustment |
| 13880 | // level... but there is special meaning applied to various levels so |
| 13881 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13882 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13883 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13884 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 13885 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13886 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13887 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13888 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13889 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13890 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13891 | adj++; |
| 13892 | } |
| 13893 | } |
| 13894 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13895 | if (adj == ProcessList.SERVICE_ADJ) { |
| 13896 | if (doingAll) { |
| 13897 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 13898 | mNewNumServiceProcs++; |
| 13899 | } |
| 13900 | if (app.serviceb) { |
| 13901 | adj = ProcessList.SERVICE_B_ADJ; |
| 13902 | } |
| 13903 | } else { |
| 13904 | app.serviceb = false; |
| 13905 | } |
| 13906 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13907 | app.curAdj = adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13908 | app.curSchedGroup = schedGroup; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13909 | |
| 13910 | if (hadForegroundActivities != app.foregroundActivities) { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 13911 | mHandler.obtainMessage(DISPATCH_FOREGROUND_ACTIVITIES_CHANGED, app.pid, app.info.uid, |
| 13912 | app.foregroundActivities).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13913 | } |
| 13914 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13915 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13916 | } |
| 13917 | |
| 13918 | /** |
| 13919 | * Ask a given process to GC right now. |
| 13920 | */ |
| 13921 | final void performAppGcLocked(ProcessRecord app) { |
| 13922 | try { |
| 13923 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13924 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13925 | if (app.reportLowMemory) { |
| 13926 | app.reportLowMemory = false; |
| 13927 | app.thread.scheduleLowMemory(); |
| 13928 | } else { |
| 13929 | app.thread.processInBackground(); |
| 13930 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13931 | } |
| 13932 | } catch (Exception e) { |
| 13933 | // whatever. |
| 13934 | } |
| 13935 | } |
| 13936 | |
| 13937 | /** |
| 13938 | * Returns true if things are idle enough to perform GCs. |
| 13939 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13940 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13941 | boolean processingBroadcasts = false; |
| 13942 | for (BroadcastQueue q : mBroadcastQueues) { |
| 13943 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 13944 | processingBroadcasts = true; |
| 13945 | } |
| 13946 | } |
| 13947 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13948 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13949 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13950 | } |
| 13951 | |
| 13952 | /** |
| 13953 | * Perform GCs on all processes that are waiting for it, but only |
| 13954 | * if things are idle. |
| 13955 | */ |
| 13956 | final void performAppGcsLocked() { |
| 13957 | final int N = mProcessesToGc.size(); |
| 13958 | if (N <= 0) { |
| 13959 | return; |
| 13960 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13961 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13962 | while (mProcessesToGc.size() > 0) { |
| 13963 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13964 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13965 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 13966 | <= SystemClock.uptimeMillis()) { |
| 13967 | // To avoid spamming the system, we will GC processes one |
| 13968 | // at a time, waiting a few seconds between each. |
| 13969 | performAppGcLocked(proc); |
| 13970 | scheduleAppGcsLocked(); |
| 13971 | return; |
| 13972 | } else { |
| 13973 | // It hasn't been long enough since we last GCed this |
| 13974 | // process... put it in the list to wait for its time. |
| 13975 | addProcessToGcListLocked(proc); |
| 13976 | break; |
| 13977 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13978 | } |
| 13979 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13980 | |
| 13981 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13982 | } |
| 13983 | } |
| 13984 | |
| 13985 | /** |
| 13986 | * If all looks good, perform GCs on all processes waiting for them. |
| 13987 | */ |
| 13988 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13989 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13990 | performAppGcsLocked(); |
| 13991 | return; |
| 13992 | } |
| 13993 | // Still not idle, wait some more. |
| 13994 | scheduleAppGcsLocked(); |
| 13995 | } |
| 13996 | |
| 13997 | /** |
| 13998 | * Schedule the execution of all pending app GCs. |
| 13999 | */ |
| 14000 | final void scheduleAppGcsLocked() { |
| 14001 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14002 | |
| 14003 | if (mProcessesToGc.size() > 0) { |
| 14004 | // Schedule a GC for the time to the next process. |
| 14005 | ProcessRecord proc = mProcessesToGc.get(0); |
| 14006 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 14007 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14008 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14009 | long now = SystemClock.uptimeMillis(); |
| 14010 | if (when < (now+GC_TIMEOUT)) { |
| 14011 | when = now + GC_TIMEOUT; |
| 14012 | } |
| 14013 | mHandler.sendMessageAtTime(msg, when); |
| 14014 | } |
| 14015 | } |
| 14016 | |
| 14017 | /** |
| 14018 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 14019 | * list in sorted order by the last GC time. The process can't already be |
| 14020 | * on the list. |
| 14021 | */ |
| 14022 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 14023 | boolean added = false; |
| 14024 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 14025 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 14026 | proc.lastRequestedGc) { |
| 14027 | added = true; |
| 14028 | mProcessesToGc.add(i+1, proc); |
| 14029 | break; |
| 14030 | } |
| 14031 | } |
| 14032 | if (!added) { |
| 14033 | mProcessesToGc.add(0, proc); |
| 14034 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14035 | } |
| 14036 | |
| 14037 | /** |
| 14038 | * Set up to ask a process to GC itself. This will either do it |
| 14039 | * immediately, or put it on the list of processes to gc the next |
| 14040 | * time things are idle. |
| 14041 | */ |
| 14042 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 14043 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14044 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14045 | return; |
| 14046 | } |
| 14047 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14048 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14049 | scheduleAppGcsLocked(); |
| 14050 | } |
| 14051 | } |
| 14052 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14053 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 14054 | updateCpuStatsNow(); |
| 14055 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14056 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14057 | boolean doWakeKills = doKills; |
| 14058 | boolean doCpuKills = doKills; |
| 14059 | if (mLastPowerCheckRealtime == 0) { |
| 14060 | doWakeKills = false; |
| 14061 | } |
| 14062 | if (mLastPowerCheckUptime == 0) { |
| 14063 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14064 | } |
| 14065 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14066 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14067 | } |
| 14068 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14069 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 14070 | final long curUptime = SystemClock.uptimeMillis(); |
| 14071 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 14072 | mLastPowerCheckRealtime = curRealtime; |
| 14073 | mLastPowerCheckUptime = curUptime; |
| 14074 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 14075 | doWakeKills = false; |
| 14076 | } |
| 14077 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 14078 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14079 | } |
| 14080 | int i = mLruProcesses.size(); |
| 14081 | while (i > 0) { |
| 14082 | i--; |
| 14083 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14084 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14085 | long wtime; |
| 14086 | synchronized (stats) { |
| 14087 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 14088 | app.pid, curRealtime); |
| 14089 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14090 | long wtimeUsed = wtime - app.lastWakeTime; |
| 14091 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 14092 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14093 | StringBuilder sb = new StringBuilder(128); |
| 14094 | sb.append("Wake for "); |
| 14095 | app.toShortString(sb); |
| 14096 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14097 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14098 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14099 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14100 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14101 | sb.append((wtimeUsed*100)/realtimeSince); |
| 14102 | sb.append("%)"); |
| 14103 | Slog.i(TAG, sb.toString()); |
| 14104 | sb.setLength(0); |
| 14105 | sb.append("CPU for "); |
| 14106 | app.toShortString(sb); |
| 14107 | sb.append(": over "); |
| 14108 | TimeUtils.formatDuration(uptimeSince, sb); |
| 14109 | sb.append(" used "); |
| 14110 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 14111 | sb.append(" ("); |
| 14112 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14113 | sb.append("%)"); |
| 14114 | Slog.i(TAG, sb.toString()); |
| 14115 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14116 | // If a process has held a wake lock for more |
| 14117 | // than 50% of the time during this period, |
| 14118 | // that sounds pad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14119 | if (doWakeKills && realtimeSince > 0 |
| 14120 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 14121 | synchronized (stats) { |
| 14122 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 14123 | realtimeSince, wtimeUsed); |
| 14124 | } |
| 14125 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 14126 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 14127 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14128 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14129 | app.processName, app.setAdj, "excessive wake lock"); |
| 14130 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14131 | } else if (doCpuKills && uptimeSince > 0 |
| 14132 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 14133 | synchronized (stats) { |
| 14134 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 14135 | uptimeSince, cputimeUsed); |
| 14136 | } |
| 14137 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 14138 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 14139 | + " during " + uptimeSince); |
| 14140 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14141 | app.processName, app.setAdj, "excessive cpu"); |
| 14142 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14143 | } else { |
| 14144 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14145 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14146 | } |
| 14147 | } |
| 14148 | } |
| 14149 | } |
| 14150 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14151 | private final boolean updateOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14152 | ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14153 | app.hiddenAdj = hiddenAdj; |
| 14154 | |
| 14155 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14156 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14157 | } |
| 14158 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14159 | final boolean wasKeeping = app.keeping; |
| 14160 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14161 | boolean success = true; |
| 14162 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14163 | computeOomAdjLocked(app, hiddenAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14164 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14165 | if (app.curRawAdj != app.setRawAdj) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 14166 | if (false) { |
| 14167 | // Removing for now. Forcing GCs is not so useful anymore |
| 14168 | // with Dalvik, and the new memory level hint facility is |
| 14169 | // better for what we need to do these days. |
| 14170 | if (app.curRawAdj > ProcessList.FOREGROUND_APP_ADJ |
| 14171 | && app.setRawAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
| 14172 | // If this app is transitioning from foreground to |
| 14173 | // non-foreground, have it do a gc. |
| 14174 | scheduleAppGcLocked(app); |
| 14175 | } else if (app.curRawAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14176 | && app.setRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 14177 | // Likewise do a gc when an app is moving in to the |
| 14178 | // background (such as a service stopping). |
| 14179 | scheduleAppGcLocked(app); |
| 14180 | } |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14181 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14182 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14183 | if (wasKeeping && !app.keeping) { |
| 14184 | // This app is no longer something we want to keep. Note |
| 14185 | // its current wake lock time to later know to kill it if |
| 14186 | // it is not behaving well. |
| 14187 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 14188 | synchronized (stats) { |
| 14189 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 14190 | app.pid, SystemClock.elapsedRealtime()); |
| 14191 | } |
| 14192 | app.lastCpuTime = app.curCpuTime; |
| 14193 | } |
| 14194 | |
| 14195 | app.setRawAdj = app.curRawAdj; |
| 14196 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14197 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14198 | if (app.curAdj != app.setAdj) { |
| 14199 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 14200 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14201 | TAG, "Set " + app.pid + " " + app.processName + |
| 14202 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14203 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14204 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14205 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14206 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14207 | } |
| 14208 | } |
| 14209 | if (app.setSchedGroup != app.curSchedGroup) { |
| 14210 | app.setSchedGroup = app.curSchedGroup; |
| 14211 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 14212 | "Setting process group of " + app.processName |
| 14213 | + " to " + app.curSchedGroup); |
| 14214 | if (app.waitingToKill != null && |
| 14215 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 14216 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
| 14217 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14218 | app.processName, app.setAdj, app.waitingToKill); |
| 14219 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14220 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14221 | } else { |
| 14222 | if (true) { |
| 14223 | long oldId = Binder.clearCallingIdentity(); |
| 14224 | try { |
| 14225 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 14226 | } catch (Exception e) { |
| 14227 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 14228 | + " to " + app.curSchedGroup); |
| 14229 | e.printStackTrace(); |
| 14230 | } finally { |
| 14231 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14232 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14233 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14234 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 14235 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14236 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 14237 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 14238 | } |
| 14239 | } |
| 14240 | } |
| 14241 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14242 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14243 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14244 | } |
| 14245 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14246 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14247 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14248 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 14249 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14250 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14251 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14252 | } |
| 14253 | } |
| 14254 | return resumedActivity; |
| 14255 | } |
| 14256 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14257 | private final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14258 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14259 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 14260 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14261 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14262 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14263 | |
| 14264 | mAdjSeq++; |
| 14265 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14266 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14267 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14268 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14269 | if (nowHidden != wasHidden) { |
| 14270 | // Changed to/from hidden state, so apps after it in the LRU |
| 14271 | // list may also be changed. |
| 14272 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14273 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14274 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14275 | } |
| 14276 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14277 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14278 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14279 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 14280 | |
| 14281 | if (false) { |
| 14282 | RuntimeException e = new RuntimeException(); |
| 14283 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14284 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14285 | } |
| 14286 | |
| 14287 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14288 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14289 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 14290 | // Let's determine how many processes we have running vs. |
| 14291 | // how many slots we have for background processes; we may want |
| 14292 | // to put multiple processes in a slot of there are enough of |
| 14293 | // them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14294 | 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] | 14295 | int factor = (mLruProcesses.size()-4)/numSlots; |
| 14296 | if (factor < 1) factor = 1; |
| 14297 | int step = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 14298 | int numHidden = 0; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 14299 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14300 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14301 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14302 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14303 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14304 | while (i > 0) { |
| 14305 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14306 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14307 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14308 | updateOomAdjLocked(app, curHiddenAdj, TOP_APP, true); |
| 14309 | if (curHiddenAdj < ProcessList.HIDDEN_APP_MAX_ADJ |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14310 | && app.curAdj == curHiddenAdj) { |
| 14311 | step++; |
| 14312 | if (step >= factor) { |
| 14313 | step = 0; |
| 14314 | curHiddenAdj++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14315 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14316 | } |
| 14317 | if (!app.killedBackground) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14318 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14319 | numHidden++; |
| 14320 | if (numHidden > mProcessLimit) { |
| 14321 | Slog.i(TAG, "No longer want " + app.processName |
| 14322 | + " (pid " + app.pid + "): hidden #" + numHidden); |
| 14323 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14324 | app.processName, app.setAdj, "too many background"); |
| 14325 | app.killedBackground = true; |
| 14326 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 14327 | } |
| 14328 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 14329 | if (!app.killedBackground && app.isolated && app.services.size() <= 0) { |
| 14330 | // If this is an isolated process, and there are no |
| 14331 | // services running in it, then the process is no longer |
| 14332 | // needed. We agressively kill these because we can by |
| 14333 | // definition not re-use the same process again, and it is |
| 14334 | // good to avoid having whatever code was running in them |
| 14335 | // left sitting around after no longer needed. |
| 14336 | Slog.i(TAG, "Isolated process " + app.processName |
| 14337 | + " (pid " + app.pid + ") no longer needed"); |
| 14338 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14339 | app.processName, app.setAdj, "isolated not needed"); |
| 14340 | app.killedBackground = true; |
| 14341 | Process.killProcessQuiet(app.pid); |
| 14342 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14343 | } |
| 14344 | } |
| 14345 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14346 | mNumServiceProcs = mNewNumServiceProcs; |
| 14347 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14348 | // Now determine the memory trimming level of background processes. |
| 14349 | // Unfortunately we need to start at the back of the list to do this |
| 14350 | // properly. We only do this if the number of background apps we |
| 14351 | // are managing to keep around is less than half the maximum we desire; |
| 14352 | // if we are keeping a good number around, we'll let them use whatever |
| 14353 | // memory they want. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14354 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/2)) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14355 | final int N = mLruProcesses.size(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 14356 | factor = numHidden/3; |
| 14357 | int minFactor = 2; |
| 14358 | if (mHomeProcess != null) minFactor++; |
| 14359 | if (mPreviousProcess != null) minFactor++; |
| 14360 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14361 | step = 0; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 14362 | int fgTrimLevel; |
| 14363 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/5)) { |
| 14364 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
| 14365 | } else if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/3)) { |
| 14366 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
| 14367 | } else { |
| 14368 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
| 14369 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14370 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14371 | for (i=0; i<N; i++) { |
| 14372 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 14373 | if (app.curAdj >= ProcessList.HOME_APP_ADJ |
| 14374 | && app.curAdj != ProcessList.SERVICE_B_ADJ |
| 14375 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14376 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 14377 | try { |
| 14378 | app.thread.scheduleTrimMemory(curLevel); |
| 14379 | } catch (RemoteException e) { |
| 14380 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 14381 | if (false) { |
| 14382 | // For now we won't do this; our memory trimming seems |
| 14383 | // to be good enough at this point that destroying |
| 14384 | // activities causes more harm than good. |
| 14385 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 14386 | && app != mHomeProcess && app != mPreviousProcess) { |
| 14387 | // For these apps we will also finish their activities |
| 14388 | // to help them free memory. |
| 14389 | mMainStack.destroyActivitiesLocked(app, false, "trim"); |
| 14390 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14391 | } |
| 14392 | } |
| 14393 | app.trimMemoryLevel = curLevel; |
| 14394 | step++; |
| 14395 | if (step >= factor) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 14396 | step = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14397 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14398 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 14399 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14400 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14401 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 14402 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14403 | break; |
| 14404 | } |
| 14405 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14406 | } else if (app.curAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14407 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14408 | && app.thread != null) { |
| 14409 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14410 | app.thread.scheduleTrimMemory( |
| 14411 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14412 | } catch (RemoteException e) { |
| 14413 | } |
| 14414 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14415 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 14416 | } else { |
| 14417 | if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
| 14418 | && app.pendingUiClean) { |
| 14419 | // If this application is now in the background and it |
| 14420 | // had done UI, then give it the special trim level to |
| 14421 | // have it free UI resources. |
| 14422 | final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 14423 | if (app.trimMemoryLevel < level && app.thread != null) { |
| 14424 | try { |
| 14425 | app.thread.scheduleTrimMemory(level); |
| 14426 | } catch (RemoteException e) { |
| 14427 | } |
| 14428 | } |
| 14429 | app.pendingUiClean = false; |
| 14430 | } |
| 14431 | if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14432 | try { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 14433 | app.thread.scheduleTrimMemory(fgTrimLevel); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14434 | } catch (RemoteException e) { |
| 14435 | } |
| 14436 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 14437 | app.trimMemoryLevel = fgTrimLevel; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14438 | } |
| 14439 | } |
| 14440 | } else { |
| 14441 | final int N = mLruProcesses.size(); |
| 14442 | for (i=0; i<N; i++) { |
| 14443 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14444 | if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14445 | && app.pendingUiClean) { |
| 14446 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 14447 | && app.thread != null) { |
| 14448 | try { |
| 14449 | app.thread.scheduleTrimMemory( |
| 14450 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 14451 | } catch (RemoteException e) { |
| 14452 | } |
| 14453 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14454 | app.pendingUiClean = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14455 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 14456 | app.trimMemoryLevel = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14457 | } |
| 14458 | } |
| 14459 | |
| 14460 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 14461 | mMainStack.destroyActivitiesLocked(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14462 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14463 | } |
| 14464 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14465 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14466 | synchronized (this) { |
| 14467 | int i; |
| 14468 | |
| 14469 | // First remove any unused application processes whose package |
| 14470 | // has been removed. |
| 14471 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 14472 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 14473 | if (app.activities.size() == 0 |
| 14474 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14475 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14476 | TAG, "Exiting empty application process " |
| 14477 | + app.processName + " (" |
| 14478 | + (app.thread != null ? app.thread.asBinder() : null) |
| 14479 | + ")\n"); |
| 14480 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 14481 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14482 | app.processName, app.setAdj, "empty"); |
| 14483 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14484 | } else { |
| 14485 | try { |
| 14486 | app.thread.scheduleExit(); |
| 14487 | } catch (Exception e) { |
| 14488 | // Ignore exceptions. |
| 14489 | } |
| 14490 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 14491 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14492 | mRemovedProcesses.remove(i); |
| 14493 | |
| 14494 | if (app.persistent) { |
| 14495 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 14496 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14497 | } |
| 14498 | } |
| 14499 | } |
| 14500 | } |
| 14501 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14502 | // Now update the oom adj for all processes. |
| 14503 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14504 | } |
| 14505 | } |
| 14506 | |
| 14507 | /** This method sends the specified signal to each of the persistent apps */ |
| 14508 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 14509 | if (sig != Process.SIGNAL_USR1) { |
| 14510 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 14511 | } |
| 14512 | |
| 14513 | synchronized (this) { |
| 14514 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 14515 | != PackageManager.PERMISSION_GRANTED) { |
| 14516 | throw new SecurityException("Requires permission " |
| 14517 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 14518 | } |
| 14519 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14520 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 14521 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14522 | if (r.thread != null && r.persistent) { |
| 14523 | Process.sendSignal(r.pid, sig); |
| 14524 | } |
| 14525 | } |
| 14526 | } |
| 14527 | } |
| 14528 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14529 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 14530 | if (proc == null || proc == mProfileProc) { |
| 14531 | proc = mProfileProc; |
| 14532 | path = mProfileFile; |
| 14533 | profileType = mProfileType; |
| 14534 | clearProfilerLocked(); |
| 14535 | } |
| 14536 | if (proc == null) { |
| 14537 | return; |
| 14538 | } |
| 14539 | try { |
| 14540 | proc.thread.profilerControl(false, path, null, profileType); |
| 14541 | } catch (RemoteException e) { |
| 14542 | throw new IllegalStateException("Process disappeared"); |
| 14543 | } |
| 14544 | } |
| 14545 | |
| 14546 | private void clearProfilerLocked() { |
| 14547 | if (mProfileFd != null) { |
| 14548 | try { |
| 14549 | mProfileFd.close(); |
| 14550 | } catch (IOException e) { |
| 14551 | } |
| 14552 | } |
| 14553 | mProfileApp = null; |
| 14554 | mProfileProc = null; |
| 14555 | mProfileFile = null; |
| 14556 | mProfileType = 0; |
| 14557 | mAutoStopProfiler = false; |
| 14558 | } |
| 14559 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14560 | public boolean profileControl(String process, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 14561 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14562 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14563 | try { |
| 14564 | synchronized (this) { |
| 14565 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14566 | // its own permission. |
| 14567 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14568 | != PackageManager.PERMISSION_GRANTED) { |
| 14569 | throw new SecurityException("Requires permission " |
| 14570 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14571 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14572 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14573 | if (start && fd == null) { |
| 14574 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14575 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14576 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14577 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14578 | if (process != null) { |
| 14579 | try { |
| 14580 | int pid = Integer.parseInt(process); |
| 14581 | synchronized (mPidsSelfLocked) { |
| 14582 | proc = mPidsSelfLocked.get(pid); |
| 14583 | } |
| 14584 | } catch (NumberFormatException e) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14585 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14586 | |
| 14587 | if (proc == null) { |
| 14588 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14589 | = mProcessNames.getMap(); |
| 14590 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14591 | if (procs != null && procs.size() > 0) { |
| 14592 | proc = procs.valueAt(0); |
| 14593 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14594 | } |
| 14595 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14596 | |
| 14597 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14598 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14599 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14600 | |
| 14601 | if (start) { |
| 14602 | stopProfilerLocked(null, null, 0); |
| 14603 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 14604 | mProfileProc = proc; |
| 14605 | mProfileType = profileType; |
| 14606 | try { |
| 14607 | fd = fd.dup(); |
| 14608 | } catch (IOException e) { |
| 14609 | fd = null; |
| 14610 | } |
| 14611 | proc.thread.profilerControl(start, path, fd, profileType); |
| 14612 | fd = null; |
| 14613 | mProfileFd = null; |
| 14614 | } else { |
| 14615 | stopProfilerLocked(proc, path, profileType); |
| 14616 | if (fd != null) { |
| 14617 | try { |
| 14618 | fd.close(); |
| 14619 | } catch (IOException e) { |
| 14620 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14621 | } |
| 14622 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14623 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14624 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14625 | } |
| 14626 | } catch (RemoteException e) { |
| 14627 | throw new IllegalStateException("Process disappeared"); |
| 14628 | } finally { |
| 14629 | if (fd != null) { |
| 14630 | try { |
| 14631 | fd.close(); |
| 14632 | } catch (IOException e) { |
| 14633 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14634 | } |
| 14635 | } |
| 14636 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14637 | |
| 14638 | public boolean dumpHeap(String process, boolean managed, |
| 14639 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 14640 | |
| 14641 | try { |
| 14642 | synchronized (this) { |
| 14643 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14644 | // its own permission (same as profileControl). |
| 14645 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14646 | != PackageManager.PERMISSION_GRANTED) { |
| 14647 | throw new SecurityException("Requires permission " |
| 14648 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 14649 | } |
| 14650 | |
| 14651 | if (fd == null) { |
| 14652 | throw new IllegalArgumentException("null fd"); |
| 14653 | } |
| 14654 | |
| 14655 | ProcessRecord proc = null; |
| 14656 | try { |
| 14657 | int pid = Integer.parseInt(process); |
| 14658 | synchronized (mPidsSelfLocked) { |
| 14659 | proc = mPidsSelfLocked.get(pid); |
| 14660 | } |
| 14661 | } catch (NumberFormatException e) { |
| 14662 | } |
| 14663 | |
| 14664 | if (proc == null) { |
| 14665 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14666 | = mProcessNames.getMap(); |
| 14667 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14668 | if (procs != null && procs.size() > 0) { |
| 14669 | proc = procs.valueAt(0); |
| 14670 | } |
| 14671 | } |
| 14672 | |
| 14673 | if (proc == null || proc.thread == null) { |
| 14674 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14675 | } |
| 14676 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 14677 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 14678 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14679 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 14680 | throw new SecurityException("Process not debuggable: " + proc); |
| 14681 | } |
| 14682 | } |
| 14683 | |
| 14684 | proc.thread.dumpHeap(managed, path, fd); |
| 14685 | fd = null; |
| 14686 | return true; |
| 14687 | } |
| 14688 | } catch (RemoteException e) { |
| 14689 | throw new IllegalStateException("Process disappeared"); |
| 14690 | } finally { |
| 14691 | if (fd != null) { |
| 14692 | try { |
| 14693 | fd.close(); |
| 14694 | } catch (IOException e) { |
| 14695 | } |
| 14696 | } |
| 14697 | } |
| 14698 | } |
| 14699 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14700 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 14701 | public void monitor() { |
| 14702 | synchronized (this) { } |
| 14703 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14704 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 14705 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14706 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 14707 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 14708 | try { |
| 14709 | if (processRecord.thread != null) { |
| 14710 | processRecord.thread.setCoreSettings(settings); |
| 14711 | } |
| 14712 | } catch (RemoteException re) { |
| 14713 | /* ignore */ |
| 14714 | } |
| 14715 | } |
| 14716 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14717 | |
| 14718 | // Multi-user methods |
| 14719 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14720 | private int mCurrentUserId; |
| 14721 | private SparseIntArray mLoggedInUsers = new SparseIntArray(5); |
| 14722 | |
| 14723 | public boolean switchUser(int userId) { |
| 14724 | final int callingUid = Binder.getCallingUid(); |
| 14725 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 14726 | Slog.e(TAG, "Trying to switch user from unauthorized app"); |
| 14727 | return false; |
| 14728 | } |
| 14729 | if (mCurrentUserId == userId) |
| 14730 | return true; |
| 14731 | |
| 14732 | synchronized (this) { |
| 14733 | // Check if user is already logged in, otherwise check if user exists first before |
| 14734 | // adding to the list of logged in users. |
| 14735 | if (mLoggedInUsers.indexOfKey(userId) < 0) { |
| 14736 | if (!userExists(userId)) { |
| 14737 | return false; |
| 14738 | } |
| 14739 | mLoggedInUsers.append(userId, userId); |
| 14740 | } |
| 14741 | |
| 14742 | mCurrentUserId = userId; |
| 14743 | boolean haveActivities = mMainStack.switchUser(userId); |
| 14744 | if (!haveActivities) { |
| 14745 | startHomeActivityLocked(userId); |
| 14746 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14747 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14748 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14749 | |
| 14750 | // Inform of user switch |
| 14751 | Intent addedIntent = new Intent(Intent.ACTION_USER_SWITCHED); |
| 14752 | addedIntent.putExtra(Intent.EXTRA_USERID, userId); |
| 14753 | mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_ACCOUNTS); |
| 14754 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14755 | return true; |
| 14756 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14757 | |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14758 | @Override |
| 14759 | public UserInfo getCurrentUser() throws RemoteException { |
| 14760 | final int callingUid = Binder.getCallingUid(); |
| 14761 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 14762 | Slog.e(TAG, "Trying to get user from unauthorized app"); |
| 14763 | return null; |
| 14764 | } |
| 14765 | return AppGlobals.getPackageManager().getUser(mCurrentUserId); |
| 14766 | } |
| 14767 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14768 | private void onUserRemoved(Intent intent) { |
| 14769 | int extraUserId = intent.getIntExtra(Intent.EXTRA_USERID, -1); |
| 14770 | if (extraUserId < 1) return; |
| 14771 | |
| 14772 | // Kill all the processes for the user |
| 14773 | ArrayList<Pair<String, Integer>> pkgAndUids = new ArrayList<Pair<String,Integer>>(); |
| 14774 | synchronized (this) { |
| 14775 | HashMap<String,SparseArray<ProcessRecord>> map = mProcessNames.getMap(); |
| 14776 | for (Entry<String, SparseArray<ProcessRecord>> uidMap : map.entrySet()) { |
| 14777 | SparseArray<ProcessRecord> uids = uidMap.getValue(); |
| 14778 | for (int i = 0; i < uids.size(); i++) { |
| 14779 | if (UserId.getUserId(uids.keyAt(i)) == extraUserId) { |
| 14780 | pkgAndUids.add(new Pair<String,Integer>(uidMap.getKey(), uids.keyAt(i))); |
| 14781 | } |
| 14782 | } |
| 14783 | } |
| 14784 | |
| 14785 | for (Pair<String,Integer> pkgAndUid : pkgAndUids) { |
| 14786 | forceStopPackageLocked(pkgAndUid.first, pkgAndUid.second, |
| 14787 | false, false, true, true, extraUserId); |
| 14788 | } |
| 14789 | } |
| 14790 | } |
| 14791 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14792 | private boolean userExists(int userId) { |
| 14793 | try { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14794 | UserInfo user = AppGlobals.getPackageManager().getUser(userId); |
| 14795 | return user != null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14796 | } catch (RemoteException re) { |
| 14797 | // Won't happen, in same process |
| 14798 | } |
| 14799 | |
| 14800 | return false; |
| 14801 | } |
| 14802 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14803 | private void checkValidCaller(int uid, int userId) { |
| 14804 | if (UserId.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return; |
| 14805 | |
| 14806 | throw new SecurityException("Caller uid=" + uid |
| 14807 | + " is not privileged to communicate with user=" + userId); |
| 14808 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14809 | |
| 14810 | private int applyUserId(int uid, int userId) { |
| 14811 | return UserId.getUid(userId, uid); |
| 14812 | } |
| 14813 | |
| 14814 | private ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14815 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14816 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 14817 | newInfo.uid = applyUserId(info.uid, userId); |
| 14818 | if (newInfo.uid >= Process.FIRST_APPLICATION_UID) { |
| 14819 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 14820 | + info.packageName; |
| 14821 | } |
| 14822 | return newInfo; |
| 14823 | } |
| 14824 | |
| 14825 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14826 | if (aInfo == null || aInfo.applicationInfo.uid < Process.FIRST_APPLICATION_UID |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14827 | || userId < 1) { |
| 14828 | return aInfo; |
| 14829 | } |
| 14830 | |
| 14831 | ActivityInfo info = new ActivityInfo(aInfo); |
| 14832 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 14833 | return info; |
| 14834 | } |
| 14835 | |
| 14836 | static class ServiceMap { |
| 14837 | |
| 14838 | private final SparseArray<HashMap<ComponentName, ServiceRecord>> mServicesByNamePerUser |
| 14839 | = new SparseArray<HashMap<ComponentName, ServiceRecord>>(); |
| 14840 | private final SparseArray<HashMap<Intent.FilterComparison, ServiceRecord>> |
| 14841 | mServicesByIntentPerUser = new SparseArray< |
| 14842 | HashMap<Intent.FilterComparison, ServiceRecord>>(); |
| 14843 | |
| 14844 | ServiceRecord getServiceByName(ComponentName name, int callingUser) { |
| 14845 | // TODO: Deal with global services |
| 14846 | if (DEBUG_MU) |
| 14847 | Slog.v(TAG_MU, "getServiceByName(" + name + "), callingUser = " + callingUser); |
| 14848 | return getServices(callingUser).get(name); |
| 14849 | } |
| 14850 | |
| 14851 | ServiceRecord getServiceByName(ComponentName name) { |
| 14852 | return getServiceByName(name, -1); |
| 14853 | } |
| 14854 | |
| 14855 | ServiceRecord getServiceByIntent(Intent.FilterComparison filter, int callingUser) { |
| 14856 | // TODO: Deal with global services |
| 14857 | if (DEBUG_MU) |
| 14858 | Slog.v(TAG_MU, "getServiceByIntent(" + filter + "), callingUser = " + callingUser); |
| 14859 | return getServicesByIntent(callingUser).get(filter); |
| 14860 | } |
| 14861 | |
| 14862 | ServiceRecord getServiceByIntent(Intent.FilterComparison filter) { |
| 14863 | return getServiceByIntent(filter, -1); |
| 14864 | } |
| 14865 | |
| 14866 | void putServiceByName(ComponentName name, int callingUser, ServiceRecord value) { |
| 14867 | // TODO: Deal with global services |
| 14868 | getServices(callingUser).put(name, value); |
| 14869 | } |
| 14870 | |
| 14871 | void putServiceByIntent(Intent.FilterComparison filter, int callingUser, |
| 14872 | ServiceRecord value) { |
| 14873 | // TODO: Deal with global services |
| 14874 | getServicesByIntent(callingUser).put(filter, value); |
| 14875 | } |
| 14876 | |
| 14877 | void removeServiceByName(ComponentName name, int callingUser) { |
| 14878 | // TODO: Deal with global services |
| 14879 | ServiceRecord removed = getServices(callingUser).remove(name); |
| 14880 | if (DEBUG_MU) |
| 14881 | Slog.v(TAG, "removeServiceByName user=" + callingUser + " name=" + name |
| 14882 | + " removed=" + removed); |
| 14883 | } |
| 14884 | |
| 14885 | void removeServiceByIntent(Intent.FilterComparison filter, int callingUser) { |
| 14886 | // TODO: Deal with global services |
| 14887 | ServiceRecord removed = getServicesByIntent(callingUser).remove(filter); |
| 14888 | if (DEBUG_MU) |
| 14889 | Slog.v(TAG_MU, "removeServiceByIntent user=" + callingUser + " intent=" + filter |
| 14890 | + " removed=" + removed); |
| 14891 | } |
| 14892 | |
| 14893 | Collection<ServiceRecord> getAllServices(int callingUser) { |
| 14894 | // TODO: Deal with global services |
| 14895 | return getServices(callingUser).values(); |
| 14896 | } |
| 14897 | |
| 14898 | private HashMap<ComponentName, ServiceRecord> getServices(int callingUser) { |
| 14899 | HashMap map = mServicesByNamePerUser.get(callingUser); |
| 14900 | if (map == null) { |
| 14901 | map = new HashMap<ComponentName, ServiceRecord>(); |
| 14902 | mServicesByNamePerUser.put(callingUser, map); |
| 14903 | } |
| 14904 | return map; |
| 14905 | } |
| 14906 | |
| 14907 | private HashMap<Intent.FilterComparison, ServiceRecord> getServicesByIntent( |
| 14908 | int callingUser) { |
| 14909 | HashMap map = mServicesByIntentPerUser.get(callingUser); |
| 14910 | if (map == null) { |
| 14911 | map = new HashMap<Intent.FilterComparison, ServiceRecord>(); |
| 14912 | mServicesByIntentPerUser.put(callingUser, map); |
| 14913 | } |
| 14914 | return map; |
| 14915 | } |
| 14916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14917 | } |