blob: c49959d0bb73a9fc7023702942a2bc880264ed13 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server.am;
18
Jeff Sharkey110a6b62012-03-12 11:12:41 -070019import static android.content.pm.PackageManager.PERMISSION_GRANTED;
20
Dianne Hackborn860755f2010-06-03 18:47:52 -070021import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.os.BatteryStatsImpl;
Dianne Hackborn45ce8642011-07-14 16:10:16 -070023import com.android.internal.os.ProcessStats;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070024import com.android.server.AttributeCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.server.IntentResolver;
26import com.android.server.ProcessMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.server.SystemServer;
28import com.android.server.Watchdog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070029import com.android.server.am.ActivityStack.ActivityState;
Dianne Hackborn1676c852012-09-10 14:52:30 -070030import com.android.server.pm.UserManagerService;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080031import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Dianne Hackborndd71fc82009-12-16 19:24:32 -080033import dalvik.system.Zygote;
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.app.Activity;
36import android.app.ActivityManager;
37import android.app.ActivityManagerNative;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -070038import android.app.ActivityOptions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.app.ActivityThread;
40import android.app.AlertDialog;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070041import android.app.AppGlobals;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020042import android.app.ApplicationErrorReport;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.app.Dialog;
Dianne Hackbornb06ea702009-07-13 13:07:51 -070044import android.app.IActivityController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.app.IApplicationThread;
46import android.app.IInstrumentationWatcher;
Dianne Hackborn860755f2010-06-03 18:47:52 -070047import android.app.INotificationManager;
Jeff Sharkeya4620792011-05-20 15:29:23 -070048import android.app.IProcessObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.app.IServiceConnection;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070050import android.app.IStopUserCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.app.IThumbnailReceiver;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070052import android.app.IUserSwitchObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.app.Instrumentation;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070054import android.app.Notification;
Dianne Hackborn860755f2010-06-03 18:47:52 -070055import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.app.PendingIntent;
Christopher Tate45281862010-03-05 15:46:30 -080057import android.app.backup.IBackupManager;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020058import android.content.ActivityNotFoundException;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080059import android.content.BroadcastReceiver;
Dianne Hackborn21c241e2012-03-08 13:57:23 -080060import android.content.ClipData;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070061import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.ComponentName;
Jeff Sharkey110a6b62012-03-12 11:12:41 -070063import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.ContentResolver;
65import android.content.Context;
Christian Mehlmauer7664e202010-07-20 08:46:17 +020066import android.content.DialogInterface;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070067import android.content.IContentProvider;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070068import android.content.IIntentReceiver;
69import android.content.IIntentSender;
Adam Powelldd8fab22012-03-22 17:47:27 -070070import android.content.Intent;
71import android.content.IntentFilter;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070072import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.content.pm.ActivityInfo;
74import android.content.pm.ApplicationInfo;
75import android.content.pm.ConfigurationInfo;
76import android.content.pm.IPackageDataObserver;
77import android.content.pm.IPackageManager;
78import android.content.pm.InstrumentationInfo;
Dan Egnor66c40e72010-01-26 16:23:11 -080079import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.content.pm.PackageManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070081import android.content.pm.UserInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070082import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070083import android.content.pm.PathPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.content.pm.ProviderInfo;
85import android.content.pm.ResolveInfo;
86import android.content.pm.ServiceInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -040087import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.content.res.Configuration;
89import android.graphics.Bitmap;
Robert Greenwalt434203a2010-10-11 16:00:27 -070090import android.net.Proxy;
91import android.net.ProxyProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.net.Uri;
93import android.os.Binder;
Dan Egnor60d87622009-12-16 16:32:58 -080094import android.os.Build;
Dan Egnor42471dd2010-01-07 17:25:22 -080095import android.os.Bundle;
Dianne Hackborn3025ef32009-08-31 21:31:47 -070096import android.os.Debug;
Dan Egnor60d87622009-12-16 16:32:58 -080097import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.Environment;
Dan Egnor42471dd2010-01-07 17:25:22 -080099import android.os.FileObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.os.FileUtils;
101import android.os.Handler;
102import android.os.IBinder;
103import android.os.IPermissionController;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700104import android.os.IRemoteCallback;
Dianne Hackborn1676c852012-09-10 14:52:30 -0700105import android.os.IUserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.os.Looper;
107import android.os.Message;
108import android.os.Parcel;
109import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.os.Process;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700111import android.os.RemoteCallbackList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.os.RemoteException;
rpcraigec7ed14c2012-07-25 13:10:37 -0400113import android.os.SELinux;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.os.ServiceManager;
Brad Fitzpatrick46d42382010-06-11 13:57:58 -0700115import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.os.SystemClock;
117import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700118import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import android.provider.Settings;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700120import android.text.format.Time;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import android.util.EventLog;
Joe Onorato5d3bea62010-03-01 13:44:29 -0800122import android.util.Log;
Adam Powelldd8fab22012-03-22 17:47:27 -0700123import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import android.util.PrintWriterPrinter;
Adam Powelldd8fab22012-03-22 17:47:27 -0700125import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700127import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import android.view.Gravity;
129import android.view.LayoutInflater;
130import android.view.View;
131import android.view.WindowManager;
132import android.view.WindowManagerPolicy;
133
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700134import java.io.BufferedInputStream;
135import java.io.BufferedOutputStream;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700136import java.io.BufferedReader;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700137import java.io.DataInputStream;
138import java.io.DataOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139import java.io.File;
140import java.io.FileDescriptor;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700141import java.io.FileInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.io.FileNotFoundException;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700143import java.io.FileOutputStream;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200144import java.io.IOException;
Dan Egnora455d192010-03-12 08:52:28 -0800145import java.io.InputStreamReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146import java.io.PrintWriter;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700147import java.io.StringWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148import java.lang.ref.WeakReference;
149import java.util.ArrayList;
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700150import java.util.Arrays;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700151import java.util.Collections;
152import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153import java.util.HashMap;
154import java.util.HashSet;
155import java.util.Iterator;
156import java.util.List;
157import java.util.Locale;
158import java.util.Map;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -0700159import java.util.Set;
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700160import java.util.concurrent.atomic.AtomicBoolean;
161import java.util.concurrent.atomic.AtomicLong;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700163public final class ActivityManagerService extends ActivityManagerNative
164 implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
Amith Yamasani742a6712011-05-04 14:49:28 -0700165 private static final String USER_DATA_DIR = "/data/user/";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 static final String TAG = "ActivityManager";
Amith Yamasani742a6712011-05-04 14:49:28 -0700167 static final String TAG_MU = "ActivityManagerServiceMU";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 static final boolean DEBUG = false;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400169 static final boolean localLOGV = DEBUG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 static final boolean DEBUG_SWITCH = localLOGV || false;
171 static final boolean DEBUG_TASKS = localLOGV || false;
Dianne Hackborn15491c62012-09-19 10:59:14 -0700172 static final boolean DEBUG_THUMBNAILS = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 static final boolean DEBUG_PAUSE = localLOGV || false;
174 static final boolean DEBUG_OOM_ADJ = localLOGV || false;
175 static final boolean DEBUG_TRANSITION = localLOGV || false;
176 static final boolean DEBUG_BROADCAST = localLOGV || false;
Christopher Tatef46723b2012-01-26 14:19:24 -0800177 static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false;
Dianne Hackborn82f3f002009-06-16 18:49:05 -0700178 static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 static final boolean DEBUG_SERVICE = localLOGV || false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700180 static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 static final boolean DEBUG_VISBILITY = localLOGV || false;
182 static final boolean DEBUG_PROCESSES = localLOGV || false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700183 static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false;
Dianne Hackborna1e989b2009-09-01 19:54:29 -0700184 static final boolean DEBUG_PROVIDER = localLOGV || false;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800185 static final boolean DEBUG_URI_PERMISSION = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 static final boolean DEBUG_USER_LEAVING = localLOGV || false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700187 static final boolean DEBUG_RESULTS = localLOGV || false;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700188 static final boolean DEBUG_BACKUP = localLOGV || false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700189 static final boolean DEBUG_CONFIGURATION = localLOGV || false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700190 static final boolean DEBUG_POWER = localLOGV || false;
191 static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false;
Amith Yamasani742a6712011-05-04 14:49:28 -0700192 static final boolean DEBUG_MU = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 static final boolean VALIDATE_TOKENS = false;
194 static final boolean SHOW_ACTIVITY_START_TIME = true;
195
196 // Control over CPU and battery monitoring.
197 static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes.
198 static final boolean MONITOR_CPU_USAGE = true;
199 static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds.
200 static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample.
201 static final boolean MONITOR_THREAD_CPU_USAGE = false;
202
Dianne Hackborn1655be42009-05-08 14:29:01 -0700203 // The flags that are set for all calls we make to the package manager.
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700204 static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
Dianne Hackborn1655be42009-05-08 14:29:01 -0700205
Dianne Hackbornf02e57b2011-03-01 22:21:04 -0800206 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700208 static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 // Maximum number of recent tasks that we can remember.
211 static final int MAX_RECENT_TASKS = 20;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700212
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700213 // Amount of time after a call to stopAppSwitches() during which we will
214 // prevent further untrusted switches from happening.
215 static final long APP_SWITCH_DELAY_TIME = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
217 // How long we wait for a launched process to attach to the activity manager
218 // before we decide it's never going to come up for real.
219 static final int PROC_START_TIMEOUT = 10*1000;
220
Jeff Brown3f9dd282011-07-08 20:02:19 -0700221 // How long we wait for a launched process to attach to the activity manager
222 // before we decide it's never going to come up for real, when the process was
223 // started with a wrapper for instrumentation (such as Valgrind) because it
224 // could take much longer than usual.
225 static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000;
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 // How long to wait after going idle before forcing apps to GC.
228 static final int GC_TIMEOUT = 5*1000;
229
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700230 // The minimum amount of time between successive GC requests for a process.
231 static final int GC_MIN_INTERVAL = 60*1000;
232
Dianne Hackborn287952c2010-09-22 22:34:31 -0700233 // The rate at which we check for apps using excessive power -- 15 mins.
234 static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000;
235
236 // The minimum sample duration we will allow before deciding we have
237 // enough data on wake locks to start killing things.
238 static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
239
240 // The minimum sample duration we will allow before deciding we have
241 // enough data on CPU usage to start killing things.
242 static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 // How long we allow a receiver to run before giving up on it.
Christopher Tatef46723b2012-01-26 14:19:24 -0800245 static final int BROADCAST_FG_TIMEOUT = 10*1000;
246 static final int BROADCAST_BG_TIMEOUT = 60*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 // How long we wait until we timeout on key dispatching.
249 static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 // How long we wait until we timeout on key dispatching during instrumentation.
252 static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;
253
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700254 // Amount of time we wait for observers to handle a user switch before
255 // giving up on them and unfreezing the screen.
256 static final int USER_SWITCH_TIMEOUT = 2*1000;
257
Dianne Hackbornbb1aeff2012-09-18 15:48:21 -0700258 // Maximum number of users we allow to be running at a time.
259 static final int MAX_RUNNING_USERS = 3;
260
Dan Egnor42471dd2010-01-07 17:25:22 -0800261 static final int MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 static final String[] EMPTY_STRING_ARRAY = new String[0];
264
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700265 public ActivityStack mMainStack;
Mike Lockwood3a74bd32011-08-12 13:55:22 -0700266
267 private final boolean mHeadless;
268
Joe Onorato54a4a412011-11-02 20:50:08 -0700269 // Whether we should show our dialogs (ANR, crash, etc) or just perform their
270 // default actuion automatically. Important for devices without direct input
271 // devices.
272 private boolean mShowDialogs = true;
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700275 * Description of a request to start a new activity, which has been held
276 * due to app switches being disabled.
277 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 static class PendingActivityLaunch {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700279 ActivityRecord r;
280 ActivityRecord sourceRecord;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700281 int startFlags;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700282 }
283
284 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
285 = new ArrayList<PendingActivityLaunch>();
286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800288 BroadcastQueue mFgBroadcastQueue;
289 BroadcastQueue mBgBroadcastQueue;
Christopher Tatef46723b2012-01-26 14:19:24 -0800290 // Convenient for easy iteration over the queues. Foreground is first
291 // so that dispatch of foreground broadcasts gets precedence.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800292 final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2];
Christopher Tatef46723b2012-01-26 14:19:24 -0800293
294 BroadcastQueue broadcastQueueForIntent(Intent intent) {
295 final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0;
296 if (DEBUG_BACKGROUND_BROADCAST) {
297 Slog.i(TAG, "Broadcast intent " + intent + " on "
298 + (isFg ? "foreground" : "background")
299 + " queue");
300 }
301 return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue;
302 }
303
304 BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) {
305 for (BroadcastQueue queue : mBroadcastQueues) {
306 BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver);
307 if (r != null) {
308 return r;
309 }
310 }
311 return null;
312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
314 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 * Activity we have told the window manager to have key focus.
316 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700317 ActivityRecord mFocusedActivity = null;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * List of intents that were used to start the most recent tasks.
320 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700321 final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322
323 /**
Dianne Hackborn7d608422011-08-07 16:24:18 -0700324 * Process management.
325 */
326 final ProcessList mProcessList = new ProcessList();
327
328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 * All of the applications we currently have running organized by name.
330 * The keys are strings of the application package name (as
331 * returned by the package manager), and the keys are ApplicationRecord
332 * objects.
333 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700334 final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
336 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800337 * The currently running isolated processes.
338 */
339 final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>();
340
341 /**
342 * Counter for assigning isolated process uids, to avoid frequently reusing the
343 * same ones.
344 */
345 int mNextIsolatedProcessUid = 0;
346
347 /**
Dianne Hackborn860755f2010-06-03 18:47:52 -0700348 * The currently running heavy-weight process, if any.
349 */
350 ProcessRecord mHeavyWeightProcess = null;
351
352 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 * The last time that various processes have crashed.
354 */
355 final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>();
356
357 /**
358 * Set of applications that we consider to be bad, and will reject
359 * incoming broadcasts from (which the user has no control over).
360 * Processes are added to this set when they have crashed twice within
361 * a minimum amount of time; they are removed from it when they are
362 * later restarted (hopefully due to some user action). The value is the
363 * time it was added to the list.
364 */
365 final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>();
366
367 /**
368 * All of the processes we currently have running organized by pid.
369 * The keys are the pid running the application.
370 *
371 * <p>NOTE: This object is protected by its own lock, NOT the global
372 * activity manager lock!
373 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700374 final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375
376 /**
377 * All of the processes that have been forced to be foreground. The key
378 * is the pid of the caller who requested it (we hold a death
379 * link on it).
380 */
381 abstract class ForegroundToken implements IBinder.DeathRecipient {
382 int pid;
383 IBinder token;
384 }
385 final SparseArray<ForegroundToken> mForegroundProcesses
386 = new SparseArray<ForegroundToken>();
387
388 /**
389 * List of records for processes that someone had tried to start before the
390 * system was ready. We don't start them at that point, but ensure they
391 * are started by the time booting is complete.
392 */
393 final ArrayList<ProcessRecord> mProcessesOnHold
394 = new ArrayList<ProcessRecord>();
395
396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 * List of persistent applications that are in the process
398 * of being started.
399 */
400 final ArrayList<ProcessRecord> mPersistentStartingProcesses
401 = new ArrayList<ProcessRecord>();
402
403 /**
404 * Processes that are being forcibly torn down.
405 */
406 final ArrayList<ProcessRecord> mRemovedProcesses
407 = new ArrayList<ProcessRecord>();
408
409 /**
410 * List of running applications, sorted by recent usage.
411 * The first entry in the list is the least recently used.
412 * It contains ApplicationRecord objects. This list does NOT include
413 * any persistent application records (since we never want to exit them).
414 */
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800415 final ArrayList<ProcessRecord> mLruProcesses
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 = new ArrayList<ProcessRecord>();
417
418 /**
419 * List of processes that should gc as soon as things are idle.
420 */
421 final ArrayList<ProcessRecord> mProcessesToGc
422 = new ArrayList<ProcessRecord>();
423
424 /**
The Android Open Source Project4df24232009-03-05 14:34:35 -0800425 * This is the process holding what we currently consider to be
426 * the "home" activity.
427 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 ProcessRecord mHomeProcess;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800429
430 /**
Dianne Hackbornf35fe232011-11-01 19:25:20 -0700431 * This is the process holding the activity the user last visited that
432 * is in a different process from the one they are currently in.
433 */
434 ProcessRecord mPreviousProcess;
Dianne Hackborn50685602011-12-01 12:23:37 -0800435
436 /**
437 * The time at which the previous process was last visible.
438 */
439 long mPreviousProcessVisibleTime;
440
Dianne Hackbornf35fe232011-11-01 19:25:20 -0700441 /**
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700442 * Which uses have been started, so are allowed to run code.
443 */
444 final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>();
445
446 /**
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -0700447 * LRU list of history of current users. Most recently current is at the end.
448 */
449 final ArrayList<Integer> mUserLru = new ArrayList<Integer>();
450
451 /**
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700452 * Constant array of the users that are currently started.
453 */
454 int[] mStartedUserArray = new int[] { 0 };
455
456 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700457 * Registered observers of the user switching mechanics.
458 */
459 final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers
460 = new RemoteCallbackList<IUserSwitchObserver>();
461
462 /**
463 * Currently active user switch.
464 */
465 Object mCurUserSwitchCallback;
466
467 /**
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400468 * Packages that the user has asked to have run in screen size
469 * compatibility mode instead of filling the screen.
470 */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700471 final CompatModePackages mCompatModePackages;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400472
473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 * Set of PendingResultRecord objects that are currently active.
475 */
476 final HashSet mPendingResultRecords = new HashSet();
477
478 /**
479 * Set of IntentSenderRecord objects that are currently active.
480 */
481 final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords
482 = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>();
483
484 /**
Brad Fitzpatrickf3d86be2010-11-23 10:31:52 -0800485 * Fingerprints (hashCode()) of stack traces that we've
Brad Fitzpatrick143666f2010-06-14 12:40:21 -0700486 * already logged DropBox entries for. Guarded by itself. If
487 * something (rogue user app) forces this over
488 * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared.
489 */
490 private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>();
491 private static final int MAX_DUP_SUPPRESSED_STACKS = 5000;
492
493 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -0700494 * Strict Mode background batched logging state.
495 *
496 * The string buffer is guarded by itself, and its lock is also
497 * used to determine if another batched write is already
498 * in-flight.
499 */
500 private final StringBuilder mStrictModeBuffer = new StringBuilder();
501
502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 * Keeps track of all IIntentReceivers that have been registered for
504 * broadcasts. Hash keys are the receiver IBinder, hash value is
505 * a ReceiverList.
506 */
507 final HashMap mRegisteredReceivers = new HashMap();
508
509 /**
510 * Resolver for broadcast intents to registered receivers.
511 * Holds BroadcastFilter (subclass of IntentFilter).
512 */
513 final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver
514 = new IntentResolver<BroadcastFilter, BroadcastFilter>() {
515 @Override
516 protected boolean allowFilterResult(
517 BroadcastFilter filter, List<BroadcastFilter> dest) {
518 IBinder target = filter.receiverList.receiver.asBinder();
519 for (int i=dest.size()-1; i>=0; i--) {
520 if (dest.get(i).receiverList.receiver.asBinder() == target) {
521 return false;
522 }
523 }
524 return true;
525 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700526
527 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700528 protected BroadcastFilter newResult(BroadcastFilter filter, int match, int userId) {
Dianne Hackborn20e80982012-08-31 19:00:44 -0700529 if (userId == UserHandle.USER_ALL || filter.owningUserId == UserHandle.USER_ALL
530 || userId == filter.owningUserId) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700531 return super.newResult(filter, match, userId);
532 }
533 return null;
534 }
535
536 @Override
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700537 protected BroadcastFilter[] newArray(int size) {
538 return new BroadcastFilter[size];
539 }
540
541 @Override
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700542 protected String packageForFilter(BroadcastFilter filter) {
543 return filter.packageName;
544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 };
546
547 /**
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700548 * State of all active sticky broadcasts per user. Keys are the action of the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 * sticky Intent, values are an ArrayList of all broadcasted intents with
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700550 * that action (which should usually be one). The SparseArray is keyed
551 * by the user ID the sticky is for, and can include UserHandle.USER_ALL
552 * for stickies that are sent to all users.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 */
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700554 final SparseArray<HashMap<String, ArrayList<Intent>>> mStickyBroadcasts =
555 new SparseArray<HashMap<String, ArrayList<Intent>>>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
Dianne Hackborn599db5c2012-08-03 19:28:48 -0700557 final ActiveServices mServices;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558
559 /**
Christopher Tate181fafa2009-05-14 11:12:14 -0700560 * Backup/restore process management
561 */
562 String mBackupAppName = null;
563 BackupRecord mBackupTarget = null;
564
565 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 * List of PendingThumbnailsRecord objects of clients who are still
567 * waiting to receive all of the thumbnails for a task.
568 */
569 final ArrayList mPendingThumbnails = new ArrayList();
570
571 /**
572 * List of HistoryRecord objects that have been finished and must
573 * still report back to a pending thumbnail receiver.
574 */
575 final ArrayList mCancelledThumbnails = new ArrayList();
576
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700577 final ProviderMap mProviderMap;
Amith Yamasani742a6712011-05-04 14:49:28 -0700578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 /**
580 * List of content providers who have clients waiting for them. The
581 * application is currently being launched and the provider will be
582 * removed from this list once it is published.
583 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700584 final ArrayList<ContentProviderRecord> mLaunchingProviders
585 = new ArrayList<ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586
587 /**
588 * Global set of specific Uri permissions that have been granted.
589 */
590 final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions
591 = new SparseArray<HashMap<Uri, UriPermission>>();
592
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800593 CoreSettingsObserver mCoreSettingsObserver;
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 /**
596 * Thread-local storage used to carry caller permissions over through
597 * indirect content-provider access.
598 * @see #ActivityManagerService.openContentUri()
599 */
600 private class Identity {
601 public int pid;
602 public int uid;
603
604 Identity(int _pid, int _uid) {
605 pid = _pid;
606 uid = _uid;
607 }
608 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>();
611
612 /**
613 * All information we have collected about the runtime performance of
614 * any user id that can impact battery performance.
615 */
616 final BatteryStatsService mBatteryStatsService;
617
618 /**
619 * information about component usage
620 */
621 final UsageStatsService mUsageStatsService;
622
623 /**
624 * Current configuration information. HistoryRecord objects are given
625 * a reference to this object to indicate which configuration they are
626 * currently running in, so this object must be kept immutable.
627 */
628 Configuration mConfiguration = new Configuration();
629
630 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800631 * Current sequencing integer of the configuration, for skipping old
632 * configurations.
633 */
634 int mConfigurationSeq = 0;
635
636 /**
Jack Palevichb90d28c2009-07-22 15:35:24 -0700637 * Hardware-reported OpenGLES version.
638 */
639 final int GL_ES_VERSION;
640
641 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 * List of initialization arguments to pass to all processes when binding applications to them.
643 * For example, references to the commonly used services.
644 */
645 HashMap<String, IBinder> mAppBindArgs;
646
647 /**
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700648 * Temporary to avoid allocations. Protected by main lock.
649 */
650 final StringBuilder mStringBuilder = new StringBuilder(256);
651
652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 * Used to control how we initialize the service.
654 */
655 boolean mStartRunning = false;
656 ComponentName mTopComponent;
657 String mTopAction;
658 String mTopData;
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700659 boolean mProcessesReady = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 boolean mSystemReady = false;
661 boolean mBooting = false;
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700662 boolean mWaitingUpdate = false;
663 boolean mDidUpdate = false;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700664 boolean mOnBattery = false;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700665 boolean mLaunchWarningShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666
667 Context mContext;
668
669 int mFactoryTest;
670
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -0700671 boolean mCheckedForSetup;
672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700674 * The time at which we will allow normal application switches again,
675 * after a call to {@link #stopAppSwitches()}.
676 */
677 long mAppSwitchesAllowedTime;
678
679 /**
680 * This is set to true after the first switch after mAppSwitchesAllowedTime
681 * is set; any switches after that will clear the time.
682 */
683 boolean mDidAppSwitch;
684
685 /**
Dianne Hackborn287952c2010-09-22 22:34:31 -0700686 * Last time (in realtime) at which we checked for power usage.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700687 */
Dianne Hackborn287952c2010-09-22 22:34:31 -0700688 long mLastPowerCheckRealtime;
689
690 /**
691 * Last time (in uptime) at which we checked for power usage.
692 */
693 long mLastPowerCheckUptime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700694
695 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 * Set while we are wanting to sleep, to prevent any
697 * activities from being started/resumed.
698 */
699 boolean mSleeping = false;
700
701 /**
Dianne Hackbornff5b1582012-04-12 17:24:07 -0700702 * State of external calls telling us if the device is asleep.
703 */
704 boolean mWentToSleep = false;
705
706 /**
707 * State of external call telling us if the lock screen is shown.
708 */
709 boolean mLockScreenShown = false;
710
711 /**
Dianne Hackborn55280a92009-05-07 15:53:46 -0700712 * Set if we are shutting down the system, similar to sleeping.
713 */
714 boolean mShuttingDown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715
716 /**
717 * Task identifier that activities are currently being started
718 * in. Incremented each time a new task is created.
719 * todo: Replace this with a TokenSpace class that generates non-repeating
720 * integers that won't wrap.
721 */
722 int mCurTask = 1;
723
724 /**
725 * Current sequence id for oom_adj computation traversal.
726 */
727 int mAdjSeq = 0;
728
729 /**
Dianne Hackborn906497c2010-05-10 15:57:38 -0700730 * Current sequence id for process LRU updating.
731 */
732 int mLruSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
734 /**
Dianne Hackbornee7621c2012-08-13 16:42:18 -0700735 * Keep track of the non-hidden/empty process we last found, to help
736 * determine how to distribute hidden/empty processes next time.
737 */
738 int mNumNonHiddenProcs = 0;
739
740 /**
741 * Keep track of the number of hidden procs, to balance oom adj
742 * distribution between those and empty procs.
743 */
744 int mNumHiddenProcs = 0;
745
746 /**
Dianne Hackborne02c88a2011-10-28 13:58:15 -0700747 * Keep track of the number of service processes we last found, to
748 * determine on the next iteration which should be B services.
749 */
750 int mNumServiceProcs = 0;
751 int mNewNumServiceProcs = 0;
752
753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 * System monitoring: number of processes that died since the last
755 * N procs were started.
756 */
757 int[] mProcDeaths = new int[20];
758
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700759 /**
760 * This is set if we had to do a delayed dexopt of an app before launching
761 * it, to increasing the ANR timeouts in that case.
762 */
763 boolean mDidDexOpt;
764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 String mDebugApp = null;
766 boolean mWaitForDebugger = false;
767 boolean mDebugTransient = false;
768 String mOrigDebugApp = null;
769 boolean mOrigWaitForDebugger = false;
770 boolean mAlwaysFinishActivities = false;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700771 IActivityController mController = null;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700772 String mProfileApp = null;
773 ProcessRecord mProfileProc = null;
774 String mProfileFile;
775 ParcelFileDescriptor mProfileFd;
776 int mProfileType = 0;
777 boolean mAutoStopProfiler = false;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800778 String mOpenGlTraceApp = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700780 static class ProcessChangeItem {
781 static final int CHANGE_ACTIVITIES = 1<<0;
782 static final int CHANGE_IMPORTANCE= 1<<1;
783 int changes;
784 int uid;
785 int pid;
786 int importance;
787 boolean foregroundActivities;
788 }
789
Jeff Sharkeya4620792011-05-20 15:29:23 -0700790 final RemoteCallbackList<IProcessObserver> mProcessObservers
791 = new RemoteCallbackList<IProcessObserver>();
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700792 ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5];
793
794 final ArrayList<ProcessChangeItem> mPendingProcessChanges
795 = new ArrayList<ProcessChangeItem>();
796 final ArrayList<ProcessChangeItem> mAvailProcessChanges
797 = new ArrayList<ProcessChangeItem>();
Jeff Sharkeya4620792011-05-20 15:29:23 -0700798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 /**
800 * Callback of last caller to {@link #requestPss}.
801 */
802 Runnable mRequestPssCallback;
803
804 /**
805 * Remaining processes for which we are waiting results from the last
806 * call to {@link #requestPss}.
807 */
808 final ArrayList<ProcessRecord> mRequestPssList
809 = new ArrayList<ProcessRecord>();
810
811 /**
812 * Runtime statistics collection thread. This object's lock is used to
813 * protect all related state.
814 */
815 final Thread mProcessStatsThread;
816
817 /**
818 * Used to collect process stats when showing not responding dialog.
819 * Protected by mProcessStatsThread.
820 */
821 final ProcessStats mProcessStats = new ProcessStats(
822 MONITOR_THREAD_CPU_USAGE);
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700823 final AtomicLong mLastCpuTime = new AtomicLong(0);
824 final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true);
825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 long mLastWriteTime = 0;
827
828 /**
829 * Set to true after the system has finished booting.
830 */
831 boolean mBooted = false;
832
Dianne Hackborn7d608422011-08-07 16:24:18 -0700833 int mProcessLimit = ProcessList.MAX_HIDDEN_APPS;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700834 int mProcessLimitOverride = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835
836 WindowManagerService mWindowManager;
837
838 static ActivityManagerService mSelf;
839 static ActivityThread mSystemThread;
840
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700841 private int mCurrentUserId = 0;
842 private int[] mCurrentUserArray = new int[] { 0 };
Dianne Hackborn1676c852012-09-10 14:52:30 -0700843 private UserManagerService mUserManager;
Amith Yamasani258848d2012-08-10 17:06:33 -0700844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 private final class AppDeathRecipient implements IBinder.DeathRecipient {
846 final ProcessRecord mApp;
847 final int mPid;
848 final IApplicationThread mAppThread;
849
850 AppDeathRecipient(ProcessRecord app, int pid,
851 IApplicationThread thread) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800852 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 TAG, "New death recipient " + this
854 + " for thread " + thread.asBinder());
855 mApp = app;
856 mPid = pid;
857 mAppThread = thread;
858 }
859
860 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800861 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 TAG, "Death received in " + this
863 + " for thread " + mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 synchronized(ActivityManagerService.this) {
865 appDiedLocked(mApp, mPid, mAppThread);
866 }
867 }
868 }
869
870 static final int SHOW_ERROR_MSG = 1;
871 static final int SHOW_NOT_RESPONDING_MSG = 2;
872 static final int SHOW_FACTORY_ERROR_MSG = 3;
873 static final int UPDATE_CONFIGURATION_MSG = 4;
874 static final int GC_BACKGROUND_PROCESSES_MSG = 5;
875 static final int WAIT_FOR_DEBUGGER_MSG = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 static final int SERVICE_TIMEOUT_MSG = 12;
877 static final int UPDATE_TIME_ZONE = 13;
878 static final int SHOW_UID_ERROR_MSG = 14;
879 static final int IM_FEELING_LUCKY_MSG = 15;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 static final int PROC_START_TIMEOUT_MSG = 20;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700881 static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -0700882 static final int KILL_APPLICATION_MSG = 22;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800883 static final int FINALIZE_PENDING_INTENT_MSG = 23;
Dianne Hackborn860755f2010-06-03 18:47:52 -0700884 static final int POST_HEAVY_NOTIFICATION_MSG = 24;
885 static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700886 static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700887 static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700888 static final int CLEAR_DNS_CACHE = 28;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700889 static final int UPDATE_HTTP_PROXY = 29;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700890 static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30;
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700891 static final int DISPATCH_PROCESSES_CHANGED = 31;
Dianne Hackborn36f80f32011-05-31 18:26:45 -0700892 static final int DISPATCH_PROCESS_DIED = 32;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700893 static final int REPORT_MEM_USAGE = 33;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700894 static final int REPORT_USER_SWITCH_MSG = 34;
895 static final int CONTINUE_USER_SWITCH_MSG = 35;
896 static final int USER_SWITCH_TIMEOUT_MSG = 36;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800898 static final int FIRST_ACTIVITY_STACK_MSG = 100;
899 static final int FIRST_BROADCAST_QUEUE_MSG = 200;
900 static final int FIRST_COMPAT_MODE_MSG = 300;
901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 AlertDialog mUidAlert;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700903 CompatModeDialog mCompatModeDialog;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700904 long mLastMemUsageReportTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905
906 final Handler mHandler = new Handler() {
907 //public Handler() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800908 // if (localLOGV) Slog.v(TAG, "Handler started!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 //}
910
911 public void handleMessage(Message msg) {
912 switch (msg.what) {
913 case SHOW_ERROR_MSG: {
914 HashMap data = (HashMap) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 synchronized (ActivityManagerService.this) {
916 ProcessRecord proc = (ProcessRecord)data.get("app");
917 if (proc != null && proc.crashDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800918 Slog.e(TAG, "App already has crash dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 return;
920 }
921 AppErrorResult res = (AppErrorResult) data.get("result");
Joe Onorato54a4a412011-11-02 20:50:08 -0700922 if (mShowDialogs && !mSleeping && !mShuttingDown) {
Dan Egnorb7f03672009-12-09 16:22:32 -0800923 Dialog d = new AppErrorDialog(mContext, res, proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 d.show();
925 proc.crashDialog = d;
926 } else {
927 // The device is asleep, so just pretend that the user
928 // saw a crash dialog and hit "force quit".
929 res.set(0);
930 }
931 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700932
933 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 } break;
935 case SHOW_NOT_RESPONDING_MSG: {
936 synchronized (ActivityManagerService.this) {
937 HashMap data = (HashMap) msg.obj;
938 ProcessRecord proc = (ProcessRecord)data.get("app");
939 if (proc != null && proc.anrDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800940 Slog.e(TAG, "App already has anr dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 return;
942 }
The Android Open Source Project4df24232009-03-05 14:34:35 -0800943
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700944 Intent intent = new Intent("android.intent.action.ANR");
945 if (!mProcessesReady) {
Christopher Tatef46723b2012-01-26 14:19:24 -0800946 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
947 | Intent.FLAG_RECEIVER_FOREGROUND);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700948 }
949 broadcastIntentLocked(null, null, intent,
The Android Open Source Project4df24232009-03-05 14:34:35 -0800950 null, null, 0, null, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700951 false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800952
Justin Kohbc52ca22012-03-29 15:11:44 -0700953 if (mShowDialogs) {
954 Dialog d = new AppNotRespondingDialog(ActivityManagerService.this,
Mike Lockwood69ccdbd2012-04-03 11:53:47 -0700955 mContext, proc, (ActivityRecord)data.get("activity"));
Justin Kohbc52ca22012-03-29 15:11:44 -0700956 d.show();
957 proc.anrDialog = d;
958 } else {
959 // Just kill the app if there is no dialog to be shown.
960 killAppAtUsersRequest(proc, null);
961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700963
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700964 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 } break;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700966 case SHOW_STRICT_MODE_VIOLATION_MSG: {
967 HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
968 synchronized (ActivityManagerService.this) {
969 ProcessRecord proc = (ProcessRecord) data.get("app");
970 if (proc == null) {
971 Slog.e(TAG, "App not found when showing strict mode dialog.");
972 break;
973 }
974 if (proc.crashDialog != null) {
975 Slog.e(TAG, "App already has strict mode dialog: " + proc);
976 return;
977 }
978 AppErrorResult res = (AppErrorResult) data.get("result");
Joe Onorato54a4a412011-11-02 20:50:08 -0700979 if (mShowDialogs && !mSleeping && !mShuttingDown) {
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700980 Dialog d = new StrictModeViolationDialog(mContext, res, proc);
981 d.show();
982 proc.crashDialog = d;
983 } else {
984 // The device is asleep, so just pretend that the user
985 // saw a crash dialog and hit "force quit".
986 res.set(0);
987 }
988 }
989 ensureBootCompleted();
990 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 case SHOW_FACTORY_ERROR_MSG: {
992 Dialog d = new FactoryErrorDialog(
993 mContext, msg.getData().getCharSequence("msg"));
994 d.show();
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700995 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 } break;
997 case UPDATE_CONFIGURATION_MSG: {
998 final ContentResolver resolver = mContext.getContentResolver();
999 Settings.System.putConfiguration(resolver, (Configuration)msg.obj);
1000 } break;
1001 case GC_BACKGROUND_PROCESSES_MSG: {
1002 synchronized (ActivityManagerService.this) {
1003 performAppGcsIfAppropriateLocked();
1004 }
1005 } break;
1006 case WAIT_FOR_DEBUGGER_MSG: {
1007 synchronized (ActivityManagerService.this) {
1008 ProcessRecord app = (ProcessRecord)msg.obj;
1009 if (msg.arg1 != 0) {
1010 if (!app.waitedForDebugger) {
1011 Dialog d = new AppWaitingForDebuggerDialog(
1012 ActivityManagerService.this,
1013 mContext, app);
1014 app.waitDialog = d;
1015 app.waitedForDebugger = true;
1016 d.show();
1017 }
1018 } else {
1019 if (app.waitDialog != null) {
1020 app.waitDialog.dismiss();
1021 app.waitDialog = null;
1022 }
1023 }
1024 }
1025 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 case SERVICE_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001027 if (mDidDexOpt) {
1028 mDidDexOpt = false;
1029 Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
1030 nmsg.obj = msg.obj;
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001031 mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001032 return;
1033 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001034 mServices.serviceTimeout((ProcessRecord)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 } break;
1036 case UPDATE_TIME_ZONE: {
1037 synchronized (ActivityManagerService.this) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001038 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1039 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 if (r.thread != null) {
1041 try {
1042 r.thread.updateTimeZone();
1043 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001044 Slog.w(TAG, "Failed to update time zone for: " + r.info.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046 }
1047 }
1048 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001049 } break;
Robert Greenwalt03595d02010-11-02 14:08:23 -07001050 case CLEAR_DNS_CACHE: {
1051 synchronized (ActivityManagerService.this) {
1052 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1053 ProcessRecord r = mLruProcesses.get(i);
1054 if (r.thread != null) {
1055 try {
1056 r.thread.clearDnsCache();
1057 } catch (RemoteException ex) {
1058 Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName);
1059 }
1060 }
1061 }
1062 }
1063 } break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001064 case UPDATE_HTTP_PROXY: {
1065 ProxyProperties proxy = (ProxyProperties)msg.obj;
1066 String host = "";
1067 String port = "";
1068 String exclList = "";
1069 if (proxy != null) {
1070 host = proxy.getHost();
1071 port = Integer.toString(proxy.getPort());
1072 exclList = proxy.getExclusionList();
1073 }
1074 synchronized (ActivityManagerService.this) {
1075 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1076 ProcessRecord r = mLruProcesses.get(i);
1077 if (r.thread != null) {
1078 try {
1079 r.thread.setHttpProxy(host, port, exclList);
1080 } catch (RemoteException ex) {
1081 Slog.w(TAG, "Failed to update http proxy for: " +
1082 r.info.processName);
1083 }
1084 }
1085 }
1086 }
1087 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 case SHOW_UID_ERROR_MSG: {
Joe Onorato54a4a412011-11-02 20:50:08 -07001089 String title = "System UIDs Inconsistent";
1090 String text = "UIDs on the system are inconsistent, you need to wipe your"
1091 + " data partition or your device will be unstable.";
1092 Log.e(TAG, title + ": " + text);
1093 if (mShowDialogs) {
1094 // XXX This is a temporary dialog, no need to localize.
1095 AlertDialog d = new BaseErrorDialog(mContext);
1096 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
1097 d.setCancelable(false);
1098 d.setTitle(title);
1099 d.setMessage(text);
1100 d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky",
1101 mHandler.obtainMessage(IM_FEELING_LUCKY_MSG));
1102 mUidAlert = d;
1103 d.show();
1104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 } break;
1106 case IM_FEELING_LUCKY_MSG: {
1107 if (mUidAlert != null) {
1108 mUidAlert.dismiss();
1109 mUidAlert = null;
1110 }
1111 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 case PROC_START_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001113 if (mDidDexOpt) {
1114 mDidDexOpt = false;
1115 Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1116 nmsg.obj = msg.obj;
1117 mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT);
1118 return;
1119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 ProcessRecord app = (ProcessRecord)msg.obj;
1121 synchronized (ActivityManagerService.this) {
1122 processStartTimedOutLocked(app);
1123 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001124 } break;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001125 case DO_PENDING_ACTIVITY_LAUNCHES_MSG: {
1126 synchronized (ActivityManagerService.this) {
1127 doPendingActivityLaunchesLocked(true);
1128 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001129 } break;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001130 case KILL_APPLICATION_MSG: {
1131 synchronized (ActivityManagerService.this) {
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07001132 int appid = msg.arg1;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001133 boolean restart = (msg.arg2 == 1);
1134 String pkg = (String) msg.obj;
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07001135 forceStopPackageLocked(pkg, appid, restart, false, true, false,
1136 UserHandle.USER_ALL);
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001137 }
1138 } break;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001139 case FINALIZE_PENDING_INTENT_MSG: {
1140 ((PendingIntentRecord)msg.obj).completeFinalize();
1141 } break;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001142 case POST_HEAVY_NOTIFICATION_MSG: {
1143 INotificationManager inm = NotificationManager.getService();
1144 if (inm == null) {
1145 return;
1146 }
1147
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001148 ActivityRecord root = (ActivityRecord)msg.obj;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001149 ProcessRecord process = root.app;
1150 if (process == null) {
1151 return;
1152 }
1153
1154 try {
1155 Context context = mContext.createPackageContext(process.info.packageName, 0);
1156 String text = mContext.getString(R.string.heavy_weight_notification,
1157 context.getApplicationInfo().loadLabel(context.getPackageManager()));
1158 Notification notification = new Notification();
1159 notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon;
1160 notification.when = 0;
1161 notification.flags = Notification.FLAG_ONGOING_EVENT;
1162 notification.tickerText = text;
1163 notification.defaults = 0; // please be quiet
1164 notification.sound = null;
1165 notification.vibrate = null;
1166 notification.setLatestEventInfo(context, text,
1167 mContext.getText(R.string.heavy_weight_notification_detail),
Dianne Hackborn41203752012-08-31 14:05:51 -07001168 PendingIntent.getActivityAsUser(mContext, 0, root.intent,
1169 PendingIntent.FLAG_CANCEL_CURRENT, null,
1170 new UserHandle(root.userId)));
Dianne Hackborn860755f2010-06-03 18:47:52 -07001171
1172 try {
1173 int[] outId = new int[1];
Dianne Hackborn41203752012-08-31 14:05:51 -07001174 inm.enqueueNotificationWithTag("android", null,
1175 R.string.heavy_weight_notification,
1176 notification, outId, root.userId);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001177 } catch (RuntimeException e) {
1178 Slog.w(ActivityManagerService.TAG,
1179 "Error showing notification for heavy-weight app", e);
1180 } catch (RemoteException e) {
1181 }
1182 } catch (NameNotFoundException e) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07001183 Slog.w(TAG, "Unable to create context for heavy notification", e);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001184 }
1185 } break;
1186 case CANCEL_HEAVY_NOTIFICATION_MSG: {
1187 INotificationManager inm = NotificationManager.getService();
1188 if (inm == null) {
1189 return;
1190 }
1191 try {
Dianne Hackborn41203752012-08-31 14:05:51 -07001192 inm.cancelNotificationWithTag("android", null,
1193 R.string.heavy_weight_notification, msg.arg1);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001194 } catch (RuntimeException e) {
1195 Slog.w(ActivityManagerService.TAG,
1196 "Error canceling notification for service", e);
1197 } catch (RemoteException e) {
1198 }
1199 } break;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001200 case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: {
1201 synchronized (ActivityManagerService.this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001202 checkExcessivePowerUsageLocked(true);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07001203 removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07001204 Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
1205 sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001206 }
1207 } break;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001208 case SHOW_COMPAT_MODE_DIALOG_MSG: {
1209 synchronized (ActivityManagerService.this) {
1210 ActivityRecord ar = (ActivityRecord)msg.obj;
1211 if (mCompatModeDialog != null) {
1212 if (mCompatModeDialog.mAppInfo.packageName.equals(
1213 ar.info.applicationInfo.packageName)) {
1214 return;
1215 }
1216 mCompatModeDialog.dismiss();
1217 mCompatModeDialog = null;
1218 }
Dianne Hackborn29478262011-06-07 15:44:22 -07001219 if (ar != null && false) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001220 if (mCompatModePackages.getPackageAskCompatModeLocked(
1221 ar.packageName)) {
1222 int mode = mCompatModePackages.computeCompatModeLocked(
1223 ar.info.applicationInfo);
1224 if (mode == ActivityManager.COMPAT_MODE_DISABLED
1225 || mode == ActivityManager.COMPAT_MODE_ENABLED) {
1226 mCompatModeDialog = new CompatModeDialog(
1227 ActivityManagerService.this, mContext,
1228 ar.info.applicationInfo);
1229 mCompatModeDialog.show();
1230 }
1231 }
1232 }
1233 }
Dianne Hackborn36f80f32011-05-31 18:26:45 -07001234 break;
1235 }
Dianne Hackborna93c2c12012-05-31 15:29:36 -07001236 case DISPATCH_PROCESSES_CHANGED: {
1237 dispatchProcessesChanged();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001238 break;
1239 }
1240 case DISPATCH_PROCESS_DIED: {
Jeff Sharkey287bd832011-05-28 19:36:26 -07001241 final int pid = msg.arg1;
1242 final int uid = msg.arg2;
1243 dispatchProcessDied(pid, uid);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001244 break;
1245 }
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001246 case REPORT_MEM_USAGE: {
1247 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
1248 if (!isDebuggable) {
1249 return;
1250 }
1251 synchronized (ActivityManagerService.this) {
1252 long now = SystemClock.uptimeMillis();
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001253 if (now < (mLastMemUsageReportTime+5*60*1000)) {
1254 // Don't report more than every 5 minutes to somewhat
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001255 // avoid spamming.
1256 return;
1257 }
1258 mLastMemUsageReportTime = now;
1259 }
1260 Thread thread = new Thread() {
1261 @Override public void run() {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001262 StringBuilder dropBuilder = new StringBuilder(1024);
1263 StringBuilder logBuilder = new StringBuilder(1024);
Dianne Hackborn672342c2011-11-29 11:29:02 -08001264 StringWriter oomSw = new StringWriter();
1265 PrintWriter oomPw = new PrintWriter(oomSw);
1266 StringWriter catSw = new StringWriter();
1267 PrintWriter catPw = new PrintWriter(catSw);
1268 String[] emptyArgs = new String[] { };
1269 StringBuilder tag = new StringBuilder(128);
1270 StringBuilder stack = new StringBuilder(128);
1271 tag.append("Low on memory -- ");
1272 dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw,
1273 tag, stack);
1274 dropBuilder.append(stack);
1275 dropBuilder.append('\n');
1276 dropBuilder.append('\n');
1277 String oomString = oomSw.toString();
1278 dropBuilder.append(oomString);
1279 dropBuilder.append('\n');
1280 logBuilder.append(oomString);
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001281 try {
1282 java.lang.Process proc = Runtime.getRuntime().exec(new String[] {
1283 "procrank", });
1284 final InputStreamReader converter = new InputStreamReader(
1285 proc.getInputStream());
1286 BufferedReader in = new BufferedReader(converter);
1287 String line;
1288 while (true) {
1289 line = in.readLine();
1290 if (line == null) {
1291 break;
1292 }
1293 if (line.length() > 0) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001294 logBuilder.append(line);
1295 logBuilder.append('\n');
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001296 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001297 dropBuilder.append(line);
1298 dropBuilder.append('\n');
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001299 }
1300 converter.close();
1301 } catch (IOException e) {
1302 }
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001303 synchronized (ActivityManagerService.this) {
Dianne Hackborn672342c2011-11-29 11:29:02 -08001304 catPw.println();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001305 dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001306 catPw.println();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001307 mServices.dumpServicesLocked(null, catPw, emptyArgs, 0,
1308 false, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001309 catPw.println();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001310 dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001311 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001312 dropBuilder.append(catSw.toString());
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08001313 addErrorToDropBox("lowmem", null, "system_server", null,
1314 null, tag.toString(), dropBuilder.toString(), null, null);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001315 Slog.i(TAG, logBuilder.toString());
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001316 synchronized (ActivityManagerService.this) {
1317 long now = SystemClock.uptimeMillis();
1318 if (mLastMemUsageReportTime < now) {
1319 mLastMemUsageReportTime = now;
1320 }
1321 }
1322 }
1323 };
1324 thread.start();
1325 break;
1326 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001327 case REPORT_USER_SWITCH_MSG: {
1328 dispatchUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2);
1329 break;
1330 }
1331 case CONTINUE_USER_SWITCH_MSG: {
1332 continueUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2);
1333 break;
1334 }
1335 case USER_SWITCH_TIMEOUT_MSG: {
1336 timeoutUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2);
1337 break;
1338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
1340 }
1341 };
1342
1343 public static void setSystemProcess() {
1344 try {
1345 ActivityManagerService m = mSelf;
1346
Dianne Hackborna573f6a2012-02-09 16:12:18 -08001347 ServiceManager.addService("activity", m, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 ServiceManager.addService("meminfo", new MemBinder(m));
Chet Haase9c1e23b2011-03-24 10:51:31 -07001349 ServiceManager.addService("gfxinfo", new GraphicsBinder(m));
Jeff Brown6754ba22011-12-14 20:20:01 -08001350 ServiceManager.addService("dbinfo", new DbBinder(m));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 if (MONITOR_CPU_USAGE) {
1352 ServiceManager.addService("cpuinfo", new CpuBinder(m));
1353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 ServiceManager.addService("permission", new PermissionController(m));
1355
1356 ApplicationInfo info =
1357 mSelf.mContext.getPackageManager().getApplicationInfo(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001358 "android", STOCK_PM_FLAGS);
Mike Cleron432b7132009-09-24 15:28:29 -07001359 mSystemThread.installSystemApplicationInfo(info);
1360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 synchronized (mSelf) {
1362 ProcessRecord app = mSelf.newProcessRecordLocked(
1363 mSystemThread.getApplicationThread(), info,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001364 info.processName, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 app.persistent = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08001366 app.pid = MY_PID;
Dianne Hackborn7d608422011-08-07 16:24:18 -07001367 app.maxAdj = ProcessList.SYSTEM_ADJ;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001368 mSelf.mProcessNames.put(app.processName, app.uid, app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 synchronized (mSelf.mPidsSelfLocked) {
1370 mSelf.mPidsSelfLocked.put(app.pid, app);
1371 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001372 mSelf.updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
1374 } catch (PackageManager.NameNotFoundException e) {
1375 throw new RuntimeException(
1376 "Unable to find android system package", e);
1377 }
1378 }
1379
1380 public void setWindowManager(WindowManagerService wm) {
1381 mWindowManager = wm;
1382 }
1383
1384 public static final Context main(int factoryTest) {
1385 AThread thr = new AThread();
1386 thr.start();
1387
1388 synchronized (thr) {
1389 while (thr.mService == null) {
1390 try {
1391 thr.wait();
1392 } catch (InterruptedException e) {
1393 }
1394 }
1395 }
1396
1397 ActivityManagerService m = thr.mService;
1398 mSelf = m;
1399 ActivityThread at = ActivityThread.systemMain();
1400 mSystemThread = at;
1401 Context context = at.getSystemContext();
Dianne Hackborn247fe742011-01-08 17:25:57 -08001402 context.setTheme(android.R.style.Theme_Holo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 m.mContext = context;
1404 m.mFactoryTest = factoryTest;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001405 m.mMainStack = new ActivityStack(m, context, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406
1407 m.mBatteryStatsService.publish(context);
1408 m.mUsageStatsService.publish(context);
1409
1410 synchronized (thr) {
1411 thr.mReady = true;
1412 thr.notifyAll();
1413 }
1414
1415 m.startRunning(null, null, null, null);
1416
1417 return context;
1418 }
1419
1420 public static ActivityManagerService self() {
1421 return mSelf;
1422 }
1423
1424 static class AThread extends Thread {
1425 ActivityManagerService mService;
1426 boolean mReady = false;
1427
1428 public AThread() {
1429 super("ActivityManager");
1430 }
1431
1432 public void run() {
1433 Looper.prepare();
1434
1435 android.os.Process.setThreadPriority(
1436 android.os.Process.THREAD_PRIORITY_FOREGROUND);
Christopher Tate160edb32010-06-30 17:46:30 -07001437 android.os.Process.setCanSelfBackground(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438
1439 ActivityManagerService m = new ActivityManagerService();
1440
1441 synchronized (this) {
1442 mService = m;
1443 notifyAll();
1444 }
1445
1446 synchronized (this) {
1447 while (!mReady) {
1448 try {
1449 wait();
1450 } catch (InterruptedException e) {
1451 }
1452 }
1453 }
1454
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001455 // For debug builds, log event loop stalls to dropbox for analysis.
1456 if (StrictMode.conditionallyEnableDebugLogging()) {
1457 Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper");
1458 }
1459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 Looper.loop();
1461 }
1462 }
1463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 static class MemBinder extends Binder {
1465 ActivityManagerService mActivityManagerService;
1466 MemBinder(ActivityManagerService activityManagerService) {
1467 mActivityManagerService = activityManagerService;
1468 }
1469
1470 @Override
1471 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001472 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1473 != PackageManager.PERMISSION_GRANTED) {
1474 pw.println("Permission Denial: can't dump meminfo from from pid="
1475 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1476 + " without permission " + android.Manifest.permission.DUMP);
1477 return;
1478 }
1479
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08001480 mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args,
Dianne Hackborn672342c2011-11-29 11:29:02 -08001481 false, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 }
1483 }
1484
Chet Haase9c1e23b2011-03-24 10:51:31 -07001485 static class GraphicsBinder extends Binder {
1486 ActivityManagerService mActivityManagerService;
1487 GraphicsBinder(ActivityManagerService activityManagerService) {
1488 mActivityManagerService = activityManagerService;
1489 }
1490
1491 @Override
1492 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001493 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1494 != PackageManager.PERMISSION_GRANTED) {
1495 pw.println("Permission Denial: can't dump gfxinfo from from pid="
1496 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1497 + " without permission " + android.Manifest.permission.DUMP);
1498 return;
1499 }
1500
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001501 mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args);
Chet Haase9c1e23b2011-03-24 10:51:31 -07001502 }
1503 }
1504
Jeff Brown6754ba22011-12-14 20:20:01 -08001505 static class DbBinder extends Binder {
1506 ActivityManagerService mActivityManagerService;
1507 DbBinder(ActivityManagerService activityManagerService) {
1508 mActivityManagerService = activityManagerService;
1509 }
1510
1511 @Override
1512 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1513 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1514 != PackageManager.PERMISSION_GRANTED) {
1515 pw.println("Permission Denial: can't dump dbinfo from from pid="
1516 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1517 + " without permission " + android.Manifest.permission.DUMP);
1518 return;
1519 }
1520
1521 mActivityManagerService.dumpDbInfo(fd, pw, args);
1522 }
1523 }
1524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 static class CpuBinder extends Binder {
1526 ActivityManagerService mActivityManagerService;
1527 CpuBinder(ActivityManagerService activityManagerService) {
1528 mActivityManagerService = activityManagerService;
1529 }
1530
1531 @Override
1532 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001533 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1534 != PackageManager.PERMISSION_GRANTED) {
1535 pw.println("Permission Denial: can't dump cpuinfo from from pid="
1536 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1537 + " without permission " + android.Manifest.permission.DUMP);
1538 return;
1539 }
1540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 synchronized (mActivityManagerService.mProcessStatsThread) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07001542 pw.print(mActivityManagerService.mProcessStats.printCurrentLoad());
1543 pw.print(mActivityManagerService.mProcessStats.printCurrentState(
1544 SystemClock.uptimeMillis()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 }
1546 }
1547 }
1548
1549 private ActivityManagerService() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001550 Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -07001551
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001552 mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT);
1553 mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT);
1554 mBroadcastQueues[0] = mFgBroadcastQueue;
1555 mBroadcastQueues[1] = mBgBroadcastQueue;
1556
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001557 mServices = new ActiveServices(this);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001558 mProviderMap = new ProviderMap(this);
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 File dataDir = Environment.getDataDirectory();
1561 File systemDir = new File(dataDir, "system");
1562 systemDir.mkdirs();
1563 mBatteryStatsService = new BatteryStatsService(new File(
1564 systemDir, "batterystats.bin").toString());
1565 mBatteryStatsService.getActiveStatistics().readLocked();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001566 mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
Dianne Hackborn287952c2010-09-22 22:34:31 -07001567 mOnBattery = DEBUG_POWER ? true
1568 : mBatteryStatsService.getActiveStatistics().getIsOnBattery();
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001569 mBatteryStatsService.getActiveStatistics().setCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001571 mUsageStatsService = new UsageStatsService(new File(
Dianne Hackborn6447ca32009-04-07 19:50:08 -07001572 systemDir, "usagestats").toString());
Mike Lockwood3a74bd32011-08-12 13:55:22 -07001573 mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001575 // User 0 is the first and only user that runs at boot.
1576 mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true));
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07001577 mUserLru.add(Integer.valueOf(0));
Dianne Hackbornc72fc672012-09-20 13:12:03 -07001578 updateStartedUserArrayLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001579
Jack Palevichb90d28c2009-07-22 15:35:24 -07001580 GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
1581 ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
1582
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001583 mConfiguration.setToDefaults();
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07001584 mConfiguration.setLocale(Locale.getDefault());
1585
Dianne Hackborn813075a62011-11-14 17:45:19 -08001586 mConfigurationSeq = mConfiguration.seq = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 mProcessStats.init();
1588
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07001589 mCompatModePackages = new CompatModePackages(this, systemDir);
1590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 // Add ourself to the Watchdog monitors.
1592 Watchdog.getInstance().addMonitor(this);
1593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 mProcessStatsThread = new Thread("ProcessStats") {
1595 public void run() {
1596 while (true) {
1597 try {
1598 try {
1599 synchronized(this) {
1600 final long now = SystemClock.uptimeMillis();
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001601 long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 // + ", write delay=" + nextWriteDelay);
1605 if (nextWriteDelay < nextCpuDelay) {
1606 nextCpuDelay = nextWriteDelay;
1607 }
1608 if (nextCpuDelay > 0) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001609 mProcessStatsMutexFree.set(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 this.wait(nextCpuDelay);
1611 }
1612 }
1613 } catch (InterruptedException e) {
1614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 updateCpuStatsNow();
1616 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001617 Slog.e(TAG, "Unexpected exception collecting process stats", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
1619 }
1620 }
1621 };
1622 mProcessStatsThread.start();
1623 }
1624
1625 @Override
1626 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1627 throws RemoteException {
Dianne Hackborna53de062012-05-08 18:53:51 -07001628 if (code == SYSPROPS_TRANSACTION) {
1629 // We need to tell all apps about the system property change.
1630 ArrayList<IBinder> procs = new ArrayList<IBinder>();
1631 synchronized(this) {
1632 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
1633 final int NA = apps.size();
1634 for (int ia=0; ia<NA; ia++) {
1635 ProcessRecord app = apps.valueAt(ia);
1636 if (app.thread != null) {
1637 procs.add(app.thread.asBinder());
1638 }
1639 }
1640 }
1641 }
1642
1643 int N = procs.size();
1644 for (int i=0; i<N; i++) {
1645 Parcel data2 = Parcel.obtain();
1646 try {
1647 procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0);
1648 } catch (RemoteException e) {
1649 }
1650 data2.recycle();
1651 }
1652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 try {
1654 return super.onTransact(code, data, reply, flags);
1655 } catch (RuntimeException e) {
1656 // The activity manager only throws security exceptions, so let's
1657 // log all others.
1658 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001659 Slog.e(TAG, "Activity Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 }
1661 throw e;
1662 }
1663 }
1664
1665 void updateCpuStats() {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001666 final long now = SystemClock.uptimeMillis();
1667 if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) {
1668 return;
1669 }
1670 if (mProcessStatsMutexFree.compareAndSet(true, false)) {
1671 synchronized (mProcessStatsThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 mProcessStatsThread.notify();
1673 }
1674 }
1675 }
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 void updateCpuStatsNow() {
1678 synchronized (mProcessStatsThread) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001679 mProcessStatsMutexFree.set(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 final long now = SystemClock.uptimeMillis();
1681 boolean haveNewCpuStats = false;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 if (MONITOR_CPU_USAGE &&
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001684 mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) {
1685 mLastCpuTime.set(now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 haveNewCpuStats = true;
1687 mProcessStats.update();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001688 //Slog.i(TAG, mProcessStats.printCurrentState());
1689 //Slog.i(TAG, "Total CPU usage: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 // + mProcessStats.getTotalCpuPercent() + "%");
1691
Joe Onorato8a9b2202010-02-26 18:56:32 -08001692 // Slog the cpu usage if the property is set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if ("true".equals(SystemProperties.get("events.cpu"))) {
1694 int user = mProcessStats.getLastUserTime();
1695 int system = mProcessStats.getLastSystemTime();
1696 int iowait = mProcessStats.getLastIoWaitTime();
1697 int irq = mProcessStats.getLastIrqTime();
1698 int softIrq = mProcessStats.getLastSoftIrqTime();
1699 int idle = mProcessStats.getLastIdleTime();
1700
1701 int total = user + system + iowait + irq + softIrq + idle;
1702 if (total == 0) total = 1;
1703
Doug Zongker2bec3d42009-12-04 12:52:44 -08001704 EventLog.writeEvent(EventLogTags.CPU,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 ((user+system+iowait+irq+softIrq) * 100) / total,
1706 (user * 100) / total,
1707 (system * 100) / total,
1708 (iowait * 100) / total,
1709 (irq * 100) / total,
1710 (softIrq * 100) / total);
1711 }
1712 }
1713
Amith Yamasanie43530a2009-08-21 13:11:37 -07001714 long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes();
Amith Yamasani819f9282009-06-24 23:18:15 -07001715 final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001716 synchronized(bstats) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 synchronized(mPidsSelfLocked) {
1718 if (haveNewCpuStats) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001719 if (mOnBattery) {
1720 int perc = bstats.startAddingCpuLocked();
1721 int totalUTime = 0;
1722 int totalSTime = 0;
Dianne Hackborn287952c2010-09-22 22:34:31 -07001723 final int N = mProcessStats.countStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001725 ProcessStats.Stats st = mProcessStats.getStats(i);
1726 if (!st.working) {
1727 continue;
1728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 ProcessRecord pr = mPidsSelfLocked.get(st.pid);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001730 int otherUTime = (st.rel_utime*perc)/100;
1731 int otherSTime = (st.rel_stime*perc)/100;
1732 totalUTime += otherUTime;
1733 totalSTime += otherSTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 if (pr != null) {
1735 BatteryStatsImpl.Uid.Proc ps = pr.batteryStats;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001736 ps.addCpuTimeLocked(st.rel_utime-otherUTime,
1737 st.rel_stime-otherSTime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001738 ps.addSpeedStepTimes(cpuSpeedTimes);
Dianne Hackborn287952c2010-09-22 22:34:31 -07001739 pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10;
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001740 } else {
1741 BatteryStatsImpl.Uid.Proc ps =
Amith Yamasani819f9282009-06-24 23:18:15 -07001742 bstats.getProcessStatsLocked(st.name, st.pid);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001743 if (ps != null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001744 ps.addCpuTimeLocked(st.rel_utime-otherUTime,
1745 st.rel_stime-otherSTime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001746 ps.addSpeedStepTimes(cpuSpeedTimes);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 }
1749 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001750 bstats.finishAddingCpuLocked(perc, totalUTime,
1751 totalSTime, cpuSpeedTimes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
1753 }
1754 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 if (mLastWriteTime < (now-BATTERY_STATS_TIME)) {
1757 mLastWriteTime = now;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001758 mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760 }
1761 }
1762 }
1763
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001764 @Override
1765 public void batteryNeedsCpuUpdate() {
1766 updateCpuStatsNow();
1767 }
1768
1769 @Override
1770 public void batteryPowerChanged(boolean onBattery) {
1771 // When plugging in, update the CPU stats first before changing
1772 // the plug state.
1773 updateCpuStatsNow();
1774 synchronized (this) {
1775 synchronized(mPidsSelfLocked) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001776 mOnBattery = DEBUG_POWER ? true : onBattery;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001777 }
1778 }
1779 }
1780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 /**
1782 * Initialize the application bind args. These are passed to each
1783 * process when the bindApplication() IPC is sent to the process. They're
1784 * lazily setup to make sure the services are running when they're asked for.
1785 */
1786 private HashMap<String, IBinder> getCommonServicesLocked() {
1787 if (mAppBindArgs == null) {
1788 mAppBindArgs = new HashMap<String, IBinder>();
1789
1790 // Setup the application init args
1791 mAppBindArgs.put("package", ServiceManager.getService("package"));
1792 mAppBindArgs.put("window", ServiceManager.getService("window"));
1793 mAppBindArgs.put(Context.ALARM_SERVICE,
1794 ServiceManager.getService(Context.ALARM_SERVICE));
1795 }
1796 return mAppBindArgs;
1797 }
1798
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 final void setFocusedActivityLocked(ActivityRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 if (mFocusedActivity != r) {
1801 mFocusedActivity = r;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001802 if (r != null) {
1803 mWindowManager.setFocusedApp(r.appToken, true);
1804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 }
1806 }
1807
Dianne Hackborn906497c2010-05-10 15:57:38 -07001808 private final void updateLruProcessInternalLocked(ProcessRecord app,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001809 boolean updateActivityTime, int bestPos) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 // put it on the LRU to keep track of when it should be exited.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001811 int lrui = mLruProcesses.indexOf(app);
1812 if (lrui >= 0) mLruProcesses.remove(lrui);
1813
1814 int i = mLruProcesses.size()-1;
1815 int skipTop = 0;
1816
Dianne Hackborn906497c2010-05-10 15:57:38 -07001817 app.lruSeq = mLruSeq;
1818
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001819 // compute the new weight for this process.
1820 if (updateActivityTime) {
1821 app.lastActivityTime = SystemClock.uptimeMillis();
1822 }
1823 if (app.activities.size() > 0) {
1824 // If this process has activities, we more strongly want to keep
1825 // it around.
1826 app.lruWeight = app.lastActivityTime;
1827 } else if (app.pubProviders.size() > 0) {
1828 // If this process contains content providers, we want to keep
1829 // it a little more strongly.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001830 app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001831 // Also don't let it kick out the first few "real" hidden processes.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001832 skipTop = ProcessList.MIN_HIDDEN_APPS;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001833 } else {
1834 // If this process doesn't have activities, we less strongly
1835 // want to keep it around, and generally want to avoid getting
1836 // in front of any very recently used activities.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001837 app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001838 // Also don't let it kick out the first few "real" hidden processes.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001839 skipTop = ProcessList.MIN_HIDDEN_APPS;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001840 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001841
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001842 while (i >= 0) {
1843 ProcessRecord p = mLruProcesses.get(i);
1844 // If this app shouldn't be in front of the first N background
1845 // apps, then skip over that many that are currently hidden.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001846 if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001847 skipTop--;
1848 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001849 if (p.lruWeight <= app.lruWeight || i < bestPos) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001850 mLruProcesses.add(i+1, app);
1851 break;
1852 }
1853 i--;
1854 }
1855 if (i < 0) {
1856 mLruProcesses.add(0, app);
1857 }
1858
Dianne Hackborn906497c2010-05-10 15:57:38 -07001859 // If the app is currently using a content provider or service,
1860 // bump those processes as well.
1861 if (app.connections.size() > 0) {
1862 for (ConnectionRecord cr : app.connections) {
1863 if (cr.binding != null && cr.binding.service != null
1864 && cr.binding.service.app != null
1865 && cr.binding.service.app.lruSeq != mLruSeq) {
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001866 updateLruProcessInternalLocked(cr.binding.service.app,
Dianne Hackborn906497c2010-05-10 15:57:38 -07001867 updateActivityTime, i+1);
1868 }
1869 }
1870 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001871 for (int j=app.conProviders.size()-1; j>=0; j--) {
1872 ContentProviderRecord cpr = app.conProviders.get(j).provider;
1873 if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) {
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001874 updateLruProcessInternalLocked(cpr.proc,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001875 updateActivityTime, i+1);
Dianne Hackborn906497c2010-05-10 15:57:38 -07001876 }
1877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 }
1879
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 final void updateLruProcessLocked(ProcessRecord app,
Dianne Hackborn906497c2010-05-10 15:57:38 -07001881 boolean oomAdj, boolean updateActivityTime) {
1882 mLruSeq++;
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001883 updateLruProcessInternalLocked(app, updateActivityTime, 0);
1884
1885 //Slog.i(TAG, "Putting proc to front: " + app.processName);
1886 if (oomAdj) {
1887 updateOomAdjLocked();
1888 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001889 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001890
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001891 final ProcessRecord getProcessRecordLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 String processName, int uid) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07001893 if (uid == Process.SYSTEM_UID) {
Amith Yamasani0184ce92012-03-28 22:41:41 -07001894 // The system gets to run in any process. If there are multiple
1895 // processes with the same uid, just pick the first (this
1896 // should never happen).
1897 SparseArray<ProcessRecord> procs = mProcessNames.getMap().get(
1898 processName);
Amith Yamasania4a54e22012-04-16 15:44:19 -07001899 if (procs == null) return null;
1900 final int N = procs.size();
1901 for (int i = 0; i < N; i++) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001902 if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i);
Amith Yamasania4a54e22012-04-16 15:44:19 -07001903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
1905 ProcessRecord proc = mProcessNames.get(processName, uid);
1906 return proc;
1907 }
1908
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 void ensurePackageDexOpt(String packageName) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001910 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001911 try {
1912 if (pm.performDexOpt(packageName)) {
1913 mDidDexOpt = true;
1914 }
1915 } catch (RemoteException e) {
1916 }
1917 }
1918
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001919 boolean isNextTransitionForward() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 int transit = mWindowManager.getPendingAppTransition();
1921 return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
1922 || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
1923 || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT;
1924 }
1925
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001926 final ProcessRecord startProcessLocked(String processName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 ApplicationInfo info, boolean knownToBeDead, int intentFlags,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001928 String hostingType, ComponentName hostingName, boolean allowWhileBooting,
1929 boolean isolated) {
1930 ProcessRecord app;
1931 if (!isolated) {
1932 app = getProcessRecordLocked(processName, info.uid);
1933 } else {
1934 // If this is an isolated process, it can't re-use an existing process.
1935 app = null;
1936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 // We don't have to do anything more if:
1938 // (1) There is an existing application record; and
1939 // (2) The caller doesn't think it is dead, OR there is no thread
1940 // object attached to it so we know it couldn't have crashed; and
1941 // (3) There is a pid assigned to it, so it is either starting or
1942 // already running.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001943 if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 + " app=" + app + " knownToBeDead=" + knownToBeDead
1945 + " thread=" + (app != null ? app.thread : null)
1946 + " pid=" + (app != null ? app.pid : -1));
Magnus Edlund7bb25812010-02-24 15:45:06 +01001947 if (app != null && app.pid > 0) {
1948 if (!knownToBeDead || app.thread == null) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001949 // We already have the app running, or are waiting for it to
1950 // come up (we have a pid but not yet its thread), so keep it.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001951 if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001952 // If this is a new package in the process, add the package to the list
1953 app.addPackage(info.packageName);
Magnus Edlund7bb25812010-02-24 15:45:06 +01001954 return app;
1955 } else {
1956 // An application record is attached to a previous process,
1957 // clean it up now.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001958 if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app);
Dianne Hackborn130b0d22011-07-26 22:07:48 -07001959 handleAppDiedLocked(app, true, true);
Magnus Edlund7bb25812010-02-24 15:45:06 +01001960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01001962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 String hostingNameStr = hostingName != null
1964 ? hostingName.flattenToShortString() : null;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001965
1966 if (!isolated) {
1967 if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) {
1968 // If we are in the background, then check to see if this process
1969 // is bad. If so, we will just silently fail.
1970 if (mBadProcesses.get(info.processName, info.uid) != null) {
1971 if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid
1972 + "/" + info.processName);
1973 return null;
1974 }
1975 } else {
1976 // When the user is explicitly starting a process, then clear its
1977 // crash count so that we won't make it bad until they see at
1978 // least one crash dialog again, and make the process good again
1979 // if it had been bad.
1980 if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001981 + "/" + info.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001982 mProcessCrashTimes.remove(info.processName, info.uid);
1983 if (mBadProcesses.get(info.processName, info.uid) != null) {
1984 EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid,
1985 info.processName);
1986 mBadProcesses.remove(info.processName, info.uid);
1987 if (app != null) {
1988 app.bad = false;
1989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
1991 }
1992 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 if (app == null) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001995 app = newProcessRecordLocked(null, info, processName, isolated);
1996 if (app == null) {
1997 Slog.w(TAG, "Failed making new process record for "
1998 + processName + "/" + info.uid + " isolated=" + isolated);
1999 return null;
2000 }
2001 mProcessNames.put(processName, app.uid, app);
2002 if (isolated) {
2003 mIsolatedProcesses.put(app.uid, app);
2004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 } else {
2006 // If this is a new package in the process, add the package to the list
2007 app.addPackage(info.packageName);
2008 }
2009
2010 // If the system is not ready yet, then hold off on starting this
2011 // process until it is.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07002012 if (!mProcessesReady
Dianne Hackborn9acc0302009-08-25 00:27:12 -07002013 && !isAllowedWhileBooting(info)
2014 && !allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 if (!mProcessesOnHold.contains(app)) {
2016 mProcessesOnHold.add(app);
2017 }
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07002018 if (DEBUG_PROCESSES) Slog.v(TAG, "System not ready, putting on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 return app;
2020 }
2021
2022 startProcessLocked(app, hostingType, hostingNameStr);
2023 return (app.pid != 0) ? app : null;
2024 }
2025
Dianne Hackborn9acc0302009-08-25 00:27:12 -07002026 boolean isAllowedWhileBooting(ApplicationInfo ai) {
2027 return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0;
2028 }
2029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 private final void startProcessLocked(ProcessRecord app,
2031 String hostingType, String hostingNameStr) {
2032 if (app.pid > 0 && app.pid != MY_PID) {
2033 synchronized (mPidsSelfLocked) {
2034 mPidsSelfLocked.remove(app.pid);
2035 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
2036 }
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07002037 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 }
2039
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07002040 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
2041 "startProcessLocked removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 mProcessesOnHold.remove(app);
2043
2044 updateCpuStats();
2045
2046 System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1);
2047 mProcDeaths[0] = 0;
2048
2049 try {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002050 int uid = app.uid;
Amith Yamasani742a6712011-05-04 14:49:28 -07002051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 int[] gids = null;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07002053 int mountExternal = Zygote.MOUNT_EXTERNAL_NONE;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002054 if (!app.isolated) {
Kenny Roote091f222012-09-11 15:01:26 -07002055 int[] permGids = null;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002056 try {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07002057 final PackageManager pm = mContext.getPackageManager();
Kenny Roote091f222012-09-11 15:01:26 -07002058 permGids = pm.getPackageGids(app.info.packageName);
Jeff Sharkeye217ee42012-08-28 16:23:01 -07002059
2060 if (Environment.isExternalStorageEmulated()) {
2061 if (pm.checkPermission(
2062 android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE,
2063 app.info.packageName) == PERMISSION_GRANTED) {
2064 mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL;
2065 } else {
2066 mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER;
2067 }
2068 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002069 } catch (PackageManager.NameNotFoundException e) {
2070 Slog.w(TAG, "Unable to retrieve gids", e);
2071 }
Kenny Roote091f222012-09-11 15:01:26 -07002072
2073 /*
2074 * Add shared application GID so applications can share some
2075 * resources like shared libraries
2076 */
2077 if (permGids == null) {
2078 gids = new int[1];
2079 } else {
2080 gids = new int[permGids.length + 1];
2081 System.arraycopy(permGids, 0, gids, 1, permGids.length);
2082 }
2083 gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
2085 if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) {
2086 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
2087 && mTopComponent != null
2088 && app.processName.equals(mTopComponent.getPackageName())) {
2089 uid = 0;
2090 }
2091 if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL
2092 && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) {
2093 uid = 0;
2094 }
2095 }
2096 int debugFlags = 0;
2097 if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
2098 debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;
Elliott Hughesfa36aee2011-06-17 14:39:41 -07002099 // Also turn on CheckJNI for debuggable apps. It's quite
2100 // awkward to turn on otherwise.
2101 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
Ben Cheng6c0afff2010-02-14 16:18:56 -08002103 // Run the app in safe mode if its manifest requests so or the
2104 // system is booted in safe mode.
2105 if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||
2106 Zygote.systemInSafeMode == true) {
Ben Cheng23085b72010-02-08 16:06:32 -08002107 debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
2108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 if ("1".equals(SystemProperties.get("debug.checkjni"))) {
2110 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
2111 }
Elliott Hughesae07ecf2011-07-06 17:33:27 -07002112 if ("1".equals(SystemProperties.get("debug.jni.logging"))) {
2113 debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING;
2114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 if ("1".equals(SystemProperties.get("debug.assert"))) {
2116 debugFlags |= Zygote.DEBUG_ENABLE_ASSERT;
2117 }
Jeff Brown3f9dd282011-07-08 20:02:19 -07002118
2119 // Start the process. It will either succeed and return a result containing
2120 // the PID of the new process, or else throw a RuntimeException.
2121 Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread",
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07002122 app.processName, uid, uid, gids, debugFlags, mountExternal,
Stephen Smalley83d9eda2012-01-13 08:34:17 -05002123 app.info.targetSdkVersion, null, null);
Jeff Brown3f9dd282011-07-08 20:02:19 -07002124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 BatteryStatsImpl bs = app.batteryStats.getBatteryStats();
2126 synchronized (bs) {
2127 if (bs.isOnBattery()) {
2128 app.batteryStats.incStartsLocked();
2129 }
2130 }
2131
Jeff Brown3f9dd282011-07-08 20:02:19 -07002132 EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 app.processName, hostingType,
2134 hostingNameStr != null ? hostingNameStr : "");
2135
2136 if (app.persistent) {
Jeff Brown3f9dd282011-07-08 20:02:19 -07002137 Watchdog.getInstance().processStarted(app.processName, startResult.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
2139
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002140 StringBuilder buf = mStringBuilder;
2141 buf.setLength(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 buf.append("Start proc ");
2143 buf.append(app.processName);
2144 buf.append(" for ");
2145 buf.append(hostingType);
2146 if (hostingNameStr != null) {
2147 buf.append(" ");
2148 buf.append(hostingNameStr);
2149 }
2150 buf.append(": pid=");
Jeff Brown3f9dd282011-07-08 20:02:19 -07002151 buf.append(startResult.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 buf.append(" uid=");
2153 buf.append(uid);
2154 buf.append(" gids={");
2155 if (gids != null) {
2156 for (int gi=0; gi<gids.length; gi++) {
2157 if (gi != 0) buf.append(", ");
2158 buf.append(gids[gi]);
2159
2160 }
2161 }
2162 buf.append("}");
Joe Onorato8a9b2202010-02-26 18:56:32 -08002163 Slog.i(TAG, buf.toString());
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07002164 app.setPid(startResult.pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07002165 app.usingWrapper = startResult.usingWrapper;
2166 app.removed = false;
2167 synchronized (mPidsSelfLocked) {
2168 this.mPidsSelfLocked.put(startResult.pid, app);
2169 Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
2170 msg.obj = app;
2171 mHandler.sendMessageDelayed(msg, startResult.usingWrapper
2172 ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
2174 } catch (RuntimeException e) {
2175 // XXX do better error recovery.
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07002176 app.setPid(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002177 Slog.e(TAG, "Failure starting process " + app.processName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
2179 }
2180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002181 void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 if (resumed) {
2183 mUsageStatsService.noteResumeComponent(resumedComponent.realActivity);
2184 } else {
2185 mUsageStatsService.notePauseComponent(resumedComponent.realActivity);
2186 }
2187 }
2188
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002189 boolean startHomeActivityLocked(int userId) {
Mike Lockwooda8f767a2011-08-31 14:32:37 -04002190 if (mHeadless) {
2191 // Added because none of the other calls to ensureBootCompleted seem to fire
2192 // when running headless.
2193 ensureBootCompleted();
2194 return false;
2195 }
2196
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002197 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
2198 && mTopAction == null) {
2199 // We are running in factory test mode, but unable to find
2200 // the factory test app, so just sit around displaying the
2201 // error message and don't try to start anything.
2202 return false;
2203 }
2204 Intent intent = new Intent(
2205 mTopAction,
2206 mTopData != null ? Uri.parse(mTopData) : null);
2207 intent.setComponent(mTopComponent);
2208 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
2209 intent.addCategory(Intent.CATEGORY_HOME);
2210 }
2211 ActivityInfo aInfo =
Amith Yamasani259d5e52012-08-31 15:11:01 -07002212 resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002213 if (aInfo != null) {
2214 intent.setComponent(new ComponentName(
2215 aInfo.applicationInfo.packageName, aInfo.name));
2216 // Don't do this if the home app is currently being
2217 // instrumented.
Amith Yamasani742a6712011-05-04 14:49:28 -07002218 aInfo = new ActivityInfo(aInfo);
2219 aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002220 ProcessRecord app = getProcessRecordLocked(aInfo.processName,
2221 aInfo.applicationInfo.uid);
2222 if (app == null || app.instrumentationClass == null) {
2223 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002224 mMainStack.startActivityLocked(null, intent, null, aInfo,
2225 null, null, 0, 0, 0, 0, null, false, null);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002226 }
2227 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002228
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002229 return true;
2230 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002231
Amith Yamasani259d5e52012-08-31 15:11:01 -07002232 private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) {
2233 ActivityInfo ai = null;
2234 ComponentName comp = intent.getComponent();
2235 try {
2236 if (comp != null) {
2237 ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId);
2238 } else {
2239 ResolveInfo info = AppGlobals.getPackageManager().resolveIntent(
2240 intent,
2241 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
2242 flags, userId);
2243
2244 if (info != null) {
2245 ai = info.activityInfo;
2246 }
2247 }
2248 } catch (RemoteException e) {
2249 // ignore
2250 }
2251
2252 return ai;
2253 }
2254
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002255 /**
2256 * Starts the "new version setup screen" if appropriate.
2257 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002258 void startSetupActivityLocked() {
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002259 // Only do this once per boot.
2260 if (mCheckedForSetup) {
2261 return;
2262 }
2263
2264 // We will show this screen if the current one is a different
2265 // version than the last one shown, and we are not running in
2266 // low-level factory test mode.
2267 final ContentResolver resolver = mContext.getContentResolver();
2268 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL &&
2269 Settings.Secure.getInt(resolver,
2270 Settings.Secure.DEVICE_PROVISIONED, 0) != 0) {
2271 mCheckedForSetup = true;
2272
2273 // See if we should be showing the platform update setup UI.
2274 Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP);
2275 List<ResolveInfo> ris = mSelf.mContext.getPackageManager()
2276 .queryIntentActivities(intent, PackageManager.GET_META_DATA);
2277
2278 // We don't allow third party apps to replace this.
2279 ResolveInfo ri = null;
2280 for (int i=0; ris != null && i<ris.size(); i++) {
2281 if ((ris.get(i).activityInfo.applicationInfo.flags
2282 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2283 ri = ris.get(i);
2284 break;
2285 }
2286 }
2287
2288 if (ri != null) {
2289 String vers = ri.activityInfo.metaData != null
2290 ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION)
2291 : null;
2292 if (vers == null && ri.activityInfo.applicationInfo.metaData != null) {
2293 vers = ri.activityInfo.applicationInfo.metaData.getString(
2294 Intent.METADATA_SETUP_VERSION);
2295 }
2296 String lastVers = Settings.Secure.getString(
2297 resolver, Settings.Secure.LAST_SETUP_SHOWN);
2298 if (vers != null && !vers.equals(lastVers)) {
2299 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2300 intent.setComponent(new ComponentName(
2301 ri.activityInfo.packageName, ri.activityInfo.name));
Dianne Hackborna4972e92012-03-14 10:38:05 -07002302 mMainStack.startActivityLocked(null, intent, null, ri.activityInfo,
2303 null, null, 0, 0, 0, 0, null, false, null);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002304 }
2305 }
2306 }
2307 }
2308
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002309 CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002310 return mCompatModePackages.compatibilityInfoForPackageLocked(ai);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002311 }
2312
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002313 void enforceNotIsolatedCaller(String caller) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002314 if (UserHandle.isIsolated(Binder.getCallingUid())) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002315 throw new SecurityException("Isolated process not allowed to call " + caller);
2316 }
2317 }
2318
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002319 public int getFrontActivityScreenCompatMode() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002320 enforceNotIsolatedCaller("getFrontActivityScreenCompatMode");
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002321 synchronized (this) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002322 return mCompatModePackages.getFrontActivityScreenCompatModeLocked();
2323 }
2324 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002325
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002326 public void setFrontActivityScreenCompatMode(int mode) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002327 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2328 "setFrontActivityScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002329 synchronized (this) {
2330 mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode);
2331 }
2332 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002333
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002334 public int getPackageScreenCompatMode(String packageName) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002335 enforceNotIsolatedCaller("getPackageScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002336 synchronized (this) {
2337 return mCompatModePackages.getPackageScreenCompatModeLocked(packageName);
2338 }
2339 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002340
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002341 public void setPackageScreenCompatMode(String packageName, int mode) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002342 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2343 "setPackageScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002344 synchronized (this) {
2345 mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002346 }
2347 }
2348
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002349 public boolean getPackageAskScreenCompat(String packageName) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002350 enforceNotIsolatedCaller("getPackageAskScreenCompat");
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002351 synchronized (this) {
2352 return mCompatModePackages.getPackageAskCompatModeLocked(packageName);
2353 }
2354 }
2355
2356 public void setPackageAskScreenCompat(String packageName, boolean ask) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002357 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2358 "setPackageAskScreenCompat");
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002359 synchronized (this) {
2360 mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask);
2361 }
2362 }
2363
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 void reportResumedActivityLocked(ActivityRecord r) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002365 //Slog.i(TAG, "**** REPORT RESUME: " + r);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002366 updateUsageStats(r, true);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368
Dianne Hackborna93c2c12012-05-31 15:29:36 -07002369 private void dispatchProcessesChanged() {
2370 int N;
2371 synchronized (this) {
2372 N = mPendingProcessChanges.size();
2373 if (mActiveProcessChanges.length < N) {
2374 mActiveProcessChanges = new ProcessChangeItem[N];
2375 }
2376 mPendingProcessChanges.toArray(mActiveProcessChanges);
2377 mAvailProcessChanges.addAll(mPendingProcessChanges);
2378 mPendingProcessChanges.clear();
2379 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes");
2380 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07002381 int i = mProcessObservers.beginBroadcast();
2382 while (i > 0) {
2383 i--;
2384 final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
2385 if (observer != null) {
2386 try {
Dianne Hackborna93c2c12012-05-31 15:29:36 -07002387 for (int j=0; j<N; j++) {
2388 ProcessChangeItem item = mActiveProcessChanges[j];
2389 if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) {
2390 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid="
2391 + item.pid + " uid=" + item.uid + ": "
2392 + item.foregroundActivities);
2393 observer.onForegroundActivitiesChanged(item.pid, item.uid,
2394 item.foregroundActivities);
2395 }
2396 if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) {
2397 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid="
2398 + item.pid + " uid=" + item.uid + ": " + item.importance);
2399 observer.onImportanceChanged(item.pid, item.uid,
2400 item.importance);
2401 }
2402 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07002403 } catch (RemoteException e) {
2404 }
2405 }
2406 }
2407 mProcessObservers.finishBroadcast();
2408 }
2409
2410 private void dispatchProcessDied(int pid, int uid) {
2411 int i = mProcessObservers.beginBroadcast();
2412 while (i > 0) {
2413 i--;
2414 final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
2415 if (observer != null) {
2416 try {
2417 observer.onProcessDied(pid, uid);
2418 } catch (RemoteException e) {
2419 }
2420 }
2421 }
2422 mProcessObservers.finishBroadcast();
2423 }
2424
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 final void doPendingActivityLaunchesLocked(boolean doResume) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002426 final int N = mPendingActivityLaunches.size();
2427 if (N <= 0) {
2428 return;
2429 }
2430 for (int i=0; i<N; i++) {
2431 PendingActivityLaunch pal = mPendingActivityLaunches.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002433 pal.startFlags, doResume && i == (N-1), null);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002434 }
2435 mPendingActivityLaunches.clear();
2436 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002437
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002438 public final int startActivity(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002439 Intent intent, String resolvedType, IBinder resultTo,
2440 String resultWho, int requestCode, int startFlags,
2441 String profileFile, ParcelFileDescriptor profileFd, Bundle options) {
Amith Yamasani82644082012-08-03 13:09:11 -07002442 return startActivityAsUser(caller, intent, resolvedType, resultTo, resultWho, requestCode,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002443 startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId());
Amith Yamasani82644082012-08-03 13:09:11 -07002444 }
2445
2446 public final int startActivityAsUser(IApplicationThread caller,
2447 Intent intent, String resolvedType, IBinder resultTo,
2448 String resultWho, int requestCode, int startFlags,
2449 String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002450 enforceNotIsolatedCaller("startActivity");
Dianne Hackbornd8883992012-09-07 15:58:52 -07002451 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId,
2452 false, true, "startActivity", null);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002453 return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002454 resultTo, resultWho, requestCode, startFlags, profileFile, profileFd,
2455 null, null, options, userId);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002456 }
2457
2458 public final WaitResult startActivityAndWait(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002459 Intent intent, String resolvedType, IBinder resultTo,
2460 String resultWho, int requestCode, int startFlags, String profileFile,
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07002461 ParcelFileDescriptor profileFd, Bundle options, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002462 enforceNotIsolatedCaller("startActivityAndWait");
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07002463 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId,
2464 false, true, "startActivityAndWait", null);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002465 WaitResult res = new WaitResult();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002466 mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002467 resultTo, resultWho, requestCode, startFlags, profileFile, profileFd,
Dianne Hackborn41203752012-08-31 14:05:51 -07002468 res, null, options, UserHandle.getCallingUserId());
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002469 return res;
2470 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08002471
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002472 public final int startActivityWithConfig(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002473 Intent intent, String resolvedType, IBinder resultTo,
2474 String resultWho, int requestCode, int startFlags, Configuration config,
Dianne Hackborn41203752012-08-31 14:05:51 -07002475 Bundle options, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002476 enforceNotIsolatedCaller("startActivityWithConfig");
Dianne Hackborn41203752012-08-31 14:05:51 -07002477 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId,
2478 false, true, "startActivityWithConfig", null);
Amith Yamasani742a6712011-05-04 14:49:28 -07002479 int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002480 resultTo, resultWho, requestCode, startFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07002481 null, null, null, config, options, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07002482 return ret;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002483 }
2484
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002485 public int startActivityIntentSender(IApplicationThread caller,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002486 IntentSender intent, Intent fillInIntent, String resolvedType,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002487 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002488 int flagsMask, int flagsValues, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002489 enforceNotIsolatedCaller("startActivityIntentSender");
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002490 // Refuse possible leaked file descriptors
2491 if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
2492 throw new IllegalArgumentException("File descriptors passed in Intent");
2493 }
2494
2495 IIntentSender sender = intent.getTarget();
2496 if (!(sender instanceof PendingIntentRecord)) {
2497 throw new IllegalArgumentException("Bad PendingIntent object");
2498 }
2499
2500 PendingIntentRecord pir = (PendingIntentRecord)sender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002501
2502 synchronized (this) {
2503 // If this is coming from the currently resumed activity, it is
2504 // effectively saying that app switches are allowed at this point.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 if (mMainStack.mResumedActivity != null
2506 && mMainStack.mResumedActivity.info.applicationInfo.uid ==
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002507 Binder.getCallingUid()) {
2508 mAppSwitchesAllowedTime = 0;
2509 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002510 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002511 int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null,
2512 resultTo, resultWho, requestCode, flagsMask, flagsValues, options);
Amith Yamasani742a6712011-05-04 14:49:28 -07002513 return ret;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002514 }
2515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 public boolean startNextMatchingActivity(IBinder callingActivity,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002517 Intent intent, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 // Refuse possible leaked file descriptors
2519 if (intent != null && intent.hasFileDescriptors() == true) {
2520 throw new IllegalArgumentException("File descriptors passed in Intent");
2521 }
2522
2523 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002524 ActivityRecord r = mMainStack.isInStackLocked(callingActivity);
2525 if (r == null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002526 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 return false;
2528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 if (r.app == null || r.app.thread == null) {
2530 // The caller is not running... d'oh!
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002531 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 return false;
2533 }
2534 intent = new Intent(intent);
2535 // The caller is not allowed to change the data.
2536 intent.setDataAndType(r.intent.getData(), r.intent.getType());
2537 // And we are resetting to find the next component...
2538 intent.setComponent(null);
2539
2540 ActivityInfo aInfo = null;
2541 try {
2542 List<ResolveInfo> resolves =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002543 AppGlobals.getPackageManager().queryIntentActivities(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 intent, r.resolvedType,
Amith Yamasani483f3b02012-03-13 16:08:00 -07002545 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002546 UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547
2548 // Look for the original activity in the list...
2549 final int N = resolves != null ? resolves.size() : 0;
2550 for (int i=0; i<N; i++) {
2551 ResolveInfo rInfo = resolves.get(i);
2552 if (rInfo.activityInfo.packageName.equals(r.packageName)
2553 && rInfo.activityInfo.name.equals(r.info.name)) {
2554 // We found the current one... the next matching is
2555 // after it.
2556 i++;
2557 if (i<N) {
2558 aInfo = resolves.get(i).activityInfo;
2559 }
2560 break;
2561 }
2562 }
2563 } catch (RemoteException e) {
2564 }
2565
2566 if (aInfo == null) {
2567 // Nobody who is next!
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002568 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 return false;
2570 }
2571
2572 intent.setComponent(new ComponentName(
2573 aInfo.applicationInfo.packageName, aInfo.name));
2574 intent.setFlags(intent.getFlags()&~(
2575 Intent.FLAG_ACTIVITY_FORWARD_RESULT|
2576 Intent.FLAG_ACTIVITY_CLEAR_TOP|
2577 Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
2578 Intent.FLAG_ACTIVITY_NEW_TASK));
2579
2580 // Okay now we need to start the new activity, replacing the
2581 // currently running activity. This is a little tricky because
2582 // we want to start the new one as if the current one is finished,
2583 // but not finish the current one first so that there is no flicker.
2584 // And thus...
2585 final boolean wasFinishing = r.finishing;
2586 r.finishing = true;
2587
2588 // Propagate reply information over to the new activity.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002589 final ActivityRecord resultTo = r.resultTo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 final String resultWho = r.resultWho;
2591 final int requestCode = r.requestCode;
2592 r.resultTo = null;
2593 if (resultTo != null) {
2594 resultTo.removeResultsLocked(r, resultWho, requestCode);
2595 }
2596
2597 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002598 int res = mMainStack.startActivityLocked(r.app.thread, intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002599 r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null,
2600 resultWho, requestCode, -1, r.launchedFromUid, 0,
2601 options, false, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 Binder.restoreCallingIdentity(origId);
2603
2604 r.finishing = wasFinishing;
Dianne Hackborna4972e92012-03-14 10:38:05 -07002605 if (res != ActivityManager.START_SUCCESS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 return false;
2607 }
2608 return true;
2609 }
2610 }
2611
Dianne Hackborn41203752012-08-31 14:05:51 -07002612 final int startActivityInPackage(int uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 Intent intent, String resolvedType, IBinder resultTo,
Dianne Hackborn41203752012-08-31 14:05:51 -07002614 String resultWho, int requestCode, int startFlags, Bundle options, int userId) {
2615
2616 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId,
2617 false, true, "startActivityInPackage", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618
Amith Yamasani742a6712011-05-04 14:49:28 -07002619 int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002620 resultTo, resultWho, requestCode, startFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07002621 null, null, null, null, options, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07002622 return ret;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002623 }
2624
2625 public final int startActivities(IApplicationThread caller,
Amith Yamasaniea7e9152012-09-24 16:11:18 -07002626 Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options,
2627 int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002628 enforceNotIsolatedCaller("startActivities");
Amith Yamasaniea7e9152012-09-24 16:11:18 -07002629 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId,
2630 false, true, "startActivity", null);
Amith Yamasani742a6712011-05-04 14:49:28 -07002631 int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo,
Amith Yamasaniea7e9152012-09-24 16:11:18 -07002632 options, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07002633 return ret;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002634 }
2635
Dianne Hackborn41203752012-08-31 14:05:51 -07002636 final int startActivitiesInPackage(int uid,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002637 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Dianne Hackborn41203752012-08-31 14:05:51 -07002638 Bundle options, int userId) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002639
Dianne Hackborn41203752012-08-31 14:05:51 -07002640 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId,
2641 false, true, "startActivityInPackage", null);
Amith Yamasani742a6712011-05-04 14:49:28 -07002642 int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo,
Dianne Hackborn41203752012-08-31 14:05:51 -07002643 options, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07002644 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
2646
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647 final void addRecentTaskLocked(TaskRecord task) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 int N = mRecentTasks.size();
Dianne Hackborn7c0e75e2010-12-21 19:15:40 -08002649 // Quick case: check if the top-most recent task is the same.
2650 if (N > 0 && mRecentTasks.get(0) == task) {
2651 return;
2652 }
2653 // Remove any existing entries that are the same kind of task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 for (int i=0; i<N; i++) {
2655 TaskRecord tr = mRecentTasks.get(i);
Amith Yamasani742a6712011-05-04 14:49:28 -07002656 if (task.userId == tr.userId
2657 && ((task.affinity != null && task.affinity.equals(tr.affinity))
2658 || (task.intent != null && task.intent.filterEquals(tr.intent)))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 mRecentTasks.remove(i);
2660 i--;
2661 N--;
2662 if (task.intent == null) {
2663 // If the new recent task we are adding is not fully
2664 // specified, then replace it with the existing recent task.
2665 task = tr;
2666 }
2667 }
2668 }
2669 if (N >= MAX_RECENT_TASKS) {
2670 mRecentTasks.remove(N-1);
2671 }
2672 mRecentTasks.add(0, task);
2673 }
2674
2675 public void setRequestedOrientation(IBinder token,
2676 int requestedOrientation) {
2677 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002678 ActivityRecord r = mMainStack.isInStackLocked(token);
2679 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 return;
2681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbe707852011-11-11 14:32:10 -08002683 mWindowManager.setAppOrientation(r.appToken, requestedOrientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 Configuration config = mWindowManager.updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002685 mConfiguration,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002686 r.mayFreezeScreenLocked(r.app) ? r.appToken : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 if (config != null) {
2688 r.frozenBeforeDestroy = true;
Dianne Hackborn813075a62011-11-14 17:45:19 -08002689 if (!updateConfigurationLocked(config, r, false, false)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 }
2692 }
2693 Binder.restoreCallingIdentity(origId);
2694 }
2695 }
2696
2697 public int getRequestedOrientation(IBinder token) {
2698 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002699 ActivityRecord r = mMainStack.isInStackLocked(token);
2700 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2702 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002703 return mWindowManager.getAppOrientation(r.appToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 }
2705 }
2706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 /**
2708 * This is the internal entry point for handling Activity.finish().
2709 *
2710 * @param token The Binder token referencing the Activity we want to finish.
2711 * @param resultCode Result code, if any, from this Activity.
2712 * @param resultData Result data (Intent), if any, from this Activity.
2713 *
Alexey Tarasov83bad3d2009-08-12 15:05:43 +11002714 * @return Returns true if the activity successfully finished, or false if it is still running.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 */
2716 public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) {
2717 // Refuse possible leaked file descriptors
2718 if (resultData != null && resultData.hasFileDescriptors() == true) {
2719 throw new IllegalArgumentException("File descriptors passed in Intent");
2720 }
2721
2722 synchronized(this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002723 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 // Find the first activity that is not finishing.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002725 ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 if (next != null) {
2727 // ask watcher if this is allowed
2728 boolean resumeOK = true;
2729 try {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002730 resumeOK = mController.activityResuming(next.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002732 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 }
2734
2735 if (!resumeOK) {
2736 return false;
2737 }
2738 }
2739 }
2740 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002741 boolean res = mMainStack.requestFinishActivityLocked(token, resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002742 resultData, "app-request", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 Binder.restoreCallingIdentity(origId);
2744 return res;
2745 }
2746 }
2747
Dianne Hackborn860755f2010-06-03 18:47:52 -07002748 public final void finishHeavyWeightApp() {
2749 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2750 != PackageManager.PERMISSION_GRANTED) {
2751 String msg = "Permission Denial: finishHeavyWeightApp() from pid="
2752 + Binder.getCallingPid()
2753 + ", uid=" + Binder.getCallingUid()
2754 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2755 Slog.w(TAG, msg);
2756 throw new SecurityException(msg);
2757 }
2758
2759 synchronized(this) {
2760 if (mHeavyWeightProcess == null) {
2761 return;
2762 }
2763
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002764 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(
Dianne Hackborn860755f2010-06-03 18:47:52 -07002765 mHeavyWeightProcess.activities);
2766 for (int i=0; i<activities.size(); i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002767 ActivityRecord r = activities.get(i);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002768 if (!r.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002769 int index = mMainStack.indexOfTokenLocked(r.appToken);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002770 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002771 mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002772 null, "finish-heavy", true);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002773 }
2774 }
2775 }
2776
Dianne Hackborn41203752012-08-31 14:05:51 -07002777 mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
2778 mHeavyWeightProcess.userId, 0));
Dianne Hackborn860755f2010-06-03 18:47:52 -07002779 mHeavyWeightProcess = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07002780 }
2781 }
2782
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002783 public void crashApplication(int uid, int initialPid, String packageName,
2784 String message) {
2785 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2786 != PackageManager.PERMISSION_GRANTED) {
2787 String msg = "Permission Denial: crashApplication() from pid="
2788 + Binder.getCallingPid()
2789 + ", uid=" + Binder.getCallingUid()
2790 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2791 Slog.w(TAG, msg);
2792 throw new SecurityException(msg);
2793 }
2794
2795 synchronized(this) {
2796 ProcessRecord proc = null;
2797
2798 // Figure out which process to kill. We don't trust that initialPid
2799 // still has any relation to current pids, so must scan through the
2800 // list.
2801 synchronized (mPidsSelfLocked) {
2802 for (int i=0; i<mPidsSelfLocked.size(); i++) {
2803 ProcessRecord p = mPidsSelfLocked.valueAt(i);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002804 if (p.uid != uid) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002805 continue;
2806 }
2807 if (p.pid == initialPid) {
2808 proc = p;
2809 break;
2810 }
2811 for (String str : p.pkgList) {
2812 if (str.equals(packageName)) {
2813 proc = p;
2814 }
2815 }
2816 }
2817 }
2818
2819 if (proc == null) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07002820 Slog.w(TAG, "crashApplication: nothing for uid=" + uid
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002821 + " initialPid=" + initialPid
2822 + " packageName=" + packageName);
2823 return;
2824 }
2825
2826 if (proc.thread != null) {
Dianne Hackborn9f531192010-08-04 17:48:03 -07002827 if (proc.pid == Process.myPid()) {
2828 Log.w(TAG, "crashApplication: trying to crash self!");
2829 return;
2830 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002831 long ident = Binder.clearCallingIdentity();
2832 try {
2833 proc.thread.scheduleCrash(message);
2834 } catch (RemoteException e) {
2835 }
2836 Binder.restoreCallingIdentity(ident);
2837 }
2838 }
2839 }
2840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 public final void finishSubActivity(IBinder token, String resultWho,
2842 int requestCode) {
2843 synchronized(this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002845 mMainStack.finishSubActivityLocked(token, resultWho, requestCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 Binder.restoreCallingIdentity(origId);
2847 }
2848 }
2849
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002850 public boolean finishActivityAffinity(IBinder token) {
2851 synchronized(this) {
2852 final long origId = Binder.clearCallingIdentity();
2853 boolean res = mMainStack.finishActivityAffinityLocked(token);
2854 Binder.restoreCallingIdentity(origId);
2855 return res;
2856 }
2857 }
2858
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002859 public boolean willActivityBeVisible(IBinder token) {
2860 synchronized(this) {
2861 int i;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002862 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
2863 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002864 if (r.appToken == token) {
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002865 return true;
2866 }
2867 if (r.fullscreen && !r.finishing) {
2868 return false;
2869 }
2870 }
2871 return true;
2872 }
2873 }
2874
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002875 public void overridePendingTransition(IBinder token, String packageName,
2876 int enterAnim, int exitAnim) {
2877 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002878 ActivityRecord self = mMainStack.isInStackLocked(token);
2879 if (self == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002880 return;
2881 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002882
2883 final long origId = Binder.clearCallingIdentity();
2884
2885 if (self.state == ActivityState.RESUMED
2886 || self.state == ActivityState.PAUSING) {
2887 mWindowManager.overridePendingAppTransition(packageName,
Dianne Hackborn84375872012-06-01 19:03:50 -07002888 enterAnim, exitAnim, null);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002889 }
2890
2891 Binder.restoreCallingIdentity(origId);
2892 }
2893 }
2894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 * Main function for removing an existing process from the activity manager
2897 * as a result of that process going away. Clears out all connections
2898 * to the process.
2899 */
2900 private final void handleAppDiedLocked(ProcessRecord app,
Dianne Hackborn130b0d22011-07-26 22:07:48 -07002901 boolean restarting, boolean allowRestart) {
2902 cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 if (!restarting) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002904 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 }
2906
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002907 if (mProfileProc == app) {
2908 clearProfilerLocked();
2909 }
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 // Just in case...
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002912 if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) {
2913 if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity);
2914 mMainStack.mPausingActivity = null;
2915 }
2916 if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) {
2917 mMainStack.mLastPausedActivity = null;
2918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919
2920 // Remove this application's activities from active lists.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002921 mMainStack.removeHistoryRecordsForAppLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922
2923 boolean atTop = true;
2924 boolean hasVisibleActivities = false;
2925
2926 // Clean out the history list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 int i = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002928 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 TAG, "Removing app " + app + " from history with " + i + " entries");
2930 while (i > 0) {
2931 i--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002932 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002933 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2935 if (r.app == app) {
2936 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002937 if (ActivityStack.DEBUG_ADD_REMOVE) {
2938 RuntimeException here = new RuntimeException("here");
2939 here.fillInStackTrace();
2940 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2941 + ": haveState=" + r.haveState
2942 + " stateNotNeeded=" + r.stateNotNeeded
2943 + " finishing=" + r.finishing
2944 + " state=" + r.state, here);
2945 }
2946 if (!r.finishing) {
2947 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
Dianne Hackborn8bf0aa92011-11-29 13:54:43 -08002948 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2949 System.identityHashCode(r),
2950 r.task.taskId, r.shortComponentName,
2951 "proc died without state saved");
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002952 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002953 mMainStack.removeActivityFromHistoryLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954
2955 } else {
2956 // We have the current state for this activity, so
2957 // it can be restarted later when needed.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002958 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 TAG, "Keeping entry, setting app to null");
2960 if (r.visible) {
2961 hasVisibleActivities = true;
2962 }
2963 r.app = null;
2964 r.nowVisible = false;
2965 if (!r.haveState) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002966 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
2967 "App died, clearing saved state of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 r.icicle = null;
2969 }
2970 }
2971
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002972 r.stack.cleanUpActivityLocked(r, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 }
2974 atTop = false;
2975 }
2976
2977 app.activities.clear();
2978
2979 if (app.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002980 Slog.w(TAG, "Crash of app " + app.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 + " running instrumentation " + app.instrumentationClass);
2982 Bundle info = new Bundle();
2983 info.putString("shortMsg", "Process crashed.");
2984 finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info);
2985 }
2986
2987 if (!restarting) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 if (!mMainStack.resumeTopActivityLocked(null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 // If there was nothing to resume, and we are not already
2990 // restarting this process, but there is a visible activity that
2991 // is hosted by the process... then make sure all visible
2992 // activities are running, taking care of restarting this
2993 // process.
2994 if (hasVisibleActivities) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002995 mMainStack.ensureActivitiesVisibleLocked(null, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 }
2997 }
2998 }
2999 }
3000
3001 private final int getLRURecordIndexForAppLocked(IApplicationThread thread) {
3002 IBinder threadBinder = thread.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 // Find the application record.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003004 for (int i=mLruProcesses.size()-1; i>=0; i--) {
3005 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 if (rec.thread != null && rec.thread.asBinder() == threadBinder) {
3007 return i;
3008 }
3009 }
3010 return -1;
3011 }
3012
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013 final ProcessRecord getRecordForAppLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 IApplicationThread thread) {
3015 if (thread == null) {
3016 return null;
3017 }
3018
3019 int appIndex = getLRURecordIndexForAppLocked(thread);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003020 return appIndex >= 0 ? mLruProcesses.get(appIndex) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 }
3022
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 final void appDiedLocked(ProcessRecord app, int pid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 IApplicationThread thread) {
3025
3026 mProcDeaths[0]++;
3027
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003028 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
3029 synchronized (stats) {
3030 stats.noteProcessDiedLocked(app.info.uid, pid);
3031 }
3032
Magnus Edlund7bb25812010-02-24 15:45:06 +01003033 // Clean up already done if the process has been re-started.
3034 if (app.pid == pid && app.thread != null &&
3035 app.thread.asBinder() == thread.asBinder()) {
Dianne Hackborn906497c2010-05-10 15:57:38 -07003036 if (!app.killedBackground) {
3037 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
3038 + ") has died.");
3039 }
Doug Zongker2bec3d42009-12-04 12:52:44 -08003040 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003041 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 TAG, "Dying app: " + app + ", pid: " + pid
3043 + ", thread: " + thread.asBinder());
3044 boolean doLowMem = app.instrumentationClass == null;
Dianne Hackborn130b0d22011-07-26 22:07:48 -07003045 handleAppDiedLocked(app, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046
3047 if (doLowMem) {
3048 // If there are no longer any background processes running,
3049 // and the app that died was not running instrumentation,
3050 // then tell everyone we are now low on memory.
3051 boolean haveBg = false;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003052 for (int i=mLruProcesses.size()-1; i>=0; i--) {
3053 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn7d608422011-08-07 16:24:18 -07003054 if (rec.thread != null && rec.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 haveBg = true;
3056 break;
3057 }
3058 }
3059
3060 if (!haveBg) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003061 EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size());
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003062 long now = SystemClock.uptimeMillis();
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003063 for (int i=mLruProcesses.size()-1; i>=0; i--) {
3064 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn36124872009-10-08 16:22:03 -07003065 if (rec != app && rec.thread != null &&
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003066 (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) {
3067 // The low memory report is overriding any current
3068 // state for a GC request. Make sure to do
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003069 // heavy/important/visible/foreground processes first.
Dianne Hackborn7d608422011-08-07 16:24:18 -07003070 if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003071 rec.lastRequestedGc = 0;
3072 } else {
3073 rec.lastRequestedGc = rec.lastLowMemory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003075 rec.reportLowMemory = true;
3076 rec.lastLowMemory = now;
3077 mProcessesToGc.remove(rec);
3078 addProcessToGcListLocked(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
3080 }
Dianne Hackborn04d6db32011-11-04 20:07:24 -07003081 mHandler.sendEmptyMessage(REPORT_MEM_USAGE);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003082 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 }
3084 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01003085 } else if (app.pid != pid) {
3086 // A new process has already been started.
Joe Onorato8a9b2202010-02-26 18:56:32 -08003087 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
Magnus Edlund7bb25812010-02-24 15:45:06 +01003088 + ") has died and restarted (pid " + app.pid + ").");
Dianne Hackborn28a8c2b2010-03-01 11:30:02 -08003089 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003090 } else if (DEBUG_PROCESSES) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003091 Slog.d(TAG, "Received spurious death notification for thread "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 + thread.asBinder());
3093 }
3094 }
3095
Dan Egnor42471dd2010-01-07 17:25:22 -08003096 /**
3097 * If a stack trace dump file is configured, dump process stack traces.
Christopher Tate6ee412d2010-05-28 12:01:56 -07003098 * @param clearTraces causes the dump file to be erased prior to the new
3099 * traces being written, if true; when false, the new traces will be
3100 * appended to any existing file content.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003101 * @param firstPids of dalvik VM processes to dump stack traces for first
3102 * @param lastPids of dalvik VM processes to dump stack traces for last
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003103 * @param nativeProcs optional list of native process names to dump stack crawls
Dan Egnor42471dd2010-01-07 17:25:22 -08003104 * @return file containing stack traces, or null if no dump file is configured
3105 */
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003106 public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids,
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003107 ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003108 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
3109 if (tracesPath == null || tracesPath.length() == 0) {
3110 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003112
3113 File tracesFile = new File(tracesPath);
3114 try {
3115 File tracesDir = tracesFile.getParentFile();
rpcraigec7ed14c2012-07-25 13:10:37 -04003116 if (!tracesDir.exists()) {
3117 tracesFile.mkdirs();
3118 if (!SELinux.restorecon(tracesDir)) {
3119 return null;
3120 }
3121 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003122 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
3123
Christopher Tate6ee412d2010-05-28 12:01:56 -07003124 if (clearTraces && tracesFile.exists()) tracesFile.delete();
Dan Egnor42471dd2010-01-07 17:25:22 -08003125 tracesFile.createNewFile();
3126 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
3127 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003128 Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
Dan Egnor42471dd2010-01-07 17:25:22 -08003129 return null;
3130 }
3131
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003132 dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003133 return tracesFile;
3134 }
3135
3136 private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids,
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003137 ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003138 // Use a FileObserver to detect when traces finish writing.
3139 // The order of traces is considered important to maintain for legibility.
3140 FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {
3141 public synchronized void onEvent(int event, String path) { notify(); }
3142 };
3143
3144 try {
3145 observer.startWatching();
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003146
3147 // First collect all of the stacks of the most important pids.
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003148 if (firstPids != null) {
3149 try {
3150 int num = firstPids.size();
3151 for (int i = 0; i < num; i++) {
3152 synchronized (observer) {
3153 Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
3154 observer.wait(200); // Wait for write-close, give up after 200msec
3155 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003156 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003157 } catch (InterruptedException e) {
3158 Log.wtf(TAG, e);
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003159 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003160 }
3161
3162 // Next measure CPU usage.
3163 if (processStats != null) {
3164 processStats.init();
3165 System.gc();
3166 processStats.update();
3167 try {
3168 synchronized (processStats) {
3169 processStats.wait(500); // measure over 1/2 second.
3170 }
3171 } catch (InterruptedException e) {
3172 }
3173 processStats.update();
3174
3175 // We'll take the stack crawls of just the top apps using CPU.
3176 final int N = processStats.countWorkingStats();
3177 int numProcs = 0;
3178 for (int i=0; i<N && numProcs<5; i++) {
3179 ProcessStats.Stats stats = processStats.getWorkingStats(i);
3180 if (lastPids.indexOfKey(stats.pid) >= 0) {
3181 numProcs++;
3182 try {
3183 synchronized (observer) {
3184 Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
3185 observer.wait(200); // Wait for write-close, give up after 200msec
3186 }
3187 } catch (InterruptedException e) {
3188 Log.wtf(TAG, e);
3189 }
3190
3191 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003192 }
3193 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003194
Dan Egnor42471dd2010-01-07 17:25:22 -08003195 } finally {
3196 observer.stopWatching();
3197 }
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003198
3199 if (nativeProcs != null) {
3200 int[] pids = Process.getPidsForCommands(nativeProcs);
3201 if (pids != null) {
3202 for (int pid : pids) {
3203 Debug.dumpNativeBacktraceToFile(pid, tracesPath);
3204 }
3205 }
3206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 }
3208
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003209 final void logAppTooSlow(ProcessRecord app, long startTime, String msg) {
Dianne Hackborn69dc66e2012-03-26 10:50:54 -07003210 if (true || IS_USER_BUILD) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003211 return;
3212 }
3213 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
3214 if (tracesPath == null || tracesPath.length() == 0) {
3215 return;
3216 }
3217
3218 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
3219 StrictMode.allowThreadDiskWrites();
3220 try {
3221 final File tracesFile = new File(tracesPath);
3222 final File tracesDir = tracesFile.getParentFile();
3223 final File tracesTmp = new File(tracesDir, "__tmp__");
3224 try {
rpcraigec7ed14c2012-07-25 13:10:37 -04003225 if (!tracesDir.exists()) {
3226 tracesFile.mkdirs();
3227 if (!SELinux.restorecon(tracesDir.getPath())) {
3228 return;
3229 }
3230 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003231 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
3232
3233 if (tracesFile.exists()) {
3234 tracesTmp.delete();
3235 tracesFile.renameTo(tracesTmp);
3236 }
3237 StringBuilder sb = new StringBuilder();
3238 Time tobj = new Time();
3239 tobj.set(System.currentTimeMillis());
3240 sb.append(tobj.format("%Y-%m-%d %H:%M:%S"));
3241 sb.append(": ");
3242 TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb);
3243 sb.append(" since ");
3244 sb.append(msg);
3245 FileOutputStream fos = new FileOutputStream(tracesFile);
3246 fos.write(sb.toString().getBytes());
3247 if (app == null) {
3248 fos.write("\n*** No application process!".getBytes());
3249 }
3250 fos.close();
3251 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
3252 } catch (IOException e) {
3253 Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e);
3254 return;
3255 }
3256
3257 if (app != null) {
3258 ArrayList<Integer> firstPids = new ArrayList<Integer>();
3259 firstPids.add(app.pid);
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003260 dumpStackTraces(tracesPath, firstPids, null, null, null);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003261 }
3262
3263 File lastTracesFile = null;
3264 File curTracesFile = null;
3265 for (int i=9; i>=0; i--) {
3266 String name = String.format("slow%02d.txt", i);
3267 curTracesFile = new File(tracesDir, name);
3268 if (curTracesFile.exists()) {
3269 if (lastTracesFile != null) {
3270 curTracesFile.renameTo(lastTracesFile);
3271 } else {
3272 curTracesFile.delete();
3273 }
3274 }
3275 lastTracesFile = curTracesFile;
3276 }
3277 tracesFile.renameTo(curTracesFile);
3278 if (tracesTmp.exists()) {
3279 tracesTmp.renameTo(tracesFile);
3280 }
3281 } finally {
3282 StrictMode.setThreadPolicy(oldPolicy);
3283 }
3284 }
3285
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003286 final void appNotResponding(ProcessRecord app, ActivityRecord activity,
3287 ActivityRecord parent, final String annotation) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003288 ArrayList<Integer> firstPids = new ArrayList<Integer>(5);
3289 SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20);
3290
Dianne Hackborn287952c2010-09-22 22:34:31 -07003291 if (mController != null) {
3292 try {
3293 // 0 == continue, -1 = kill process immediately
3294 int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation);
3295 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
3296 } catch (RemoteException e) {
3297 mController = null;
3298 }
3299 }
3300
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003301 long anrTime = SystemClock.uptimeMillis();
3302 if (MONITOR_CPU_USAGE) {
3303 updateCpuStatsNow();
3304 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003305
3306 synchronized (this) {
3307 // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
3308 if (mShuttingDown) {
3309 Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
3310 return;
3311 } else if (app.notResponding) {
3312 Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
3313 return;
3314 } else if (app.crashing) {
3315 Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
3316 return;
3317 }
3318
3319 // In case we come through here for the same app before completing
3320 // this one, mark as anring now so we will bail out.
3321 app.notResponding = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08003322
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003323 // Log the ANR to the event log.
3324 EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags,
3325 annotation);
Dan Egnor42471dd2010-01-07 17:25:22 -08003326
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003327 // Dump thread traces as quickly as we can, starting with "interesting" processes.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003328 firstPids.add(app.pid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003329
3330 int parentPid = app.pid;
3331 if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid;
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003332 if (parentPid != app.pid) firstPids.add(parentPid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003333
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003334 if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID);
Dan Egnor42471dd2010-01-07 17:25:22 -08003335
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003336 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
3337 ProcessRecord r = mLruProcesses.get(i);
3338 if (r != null && r.thread != null) {
3339 int pid = r.pid;
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003340 if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) {
3341 if (r.persistent) {
3342 firstPids.add(pid);
3343 } else {
3344 lastPids.put(pid, Boolean.TRUE);
3345 }
3346 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 }
3349 }
3350
Dan Egnor42471dd2010-01-07 17:25:22 -08003351 // Log the ANR to the main log.
Jeff Browndeb6ed82012-04-10 14:26:26 -07003352 StringBuilder info = new StringBuilder();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003353 info.setLength(0);
Dan Egnor42471dd2010-01-07 17:25:22 -08003354 info.append("ANR in ").append(app.processName);
3355 if (activity != null && activity.shortComponentName != null) {
3356 info.append(" (").append(activity.shortComponentName).append(")");
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07003357 }
Eric Rowe6f4f6192010-02-17 18:29:04 -08003358 info.append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 if (annotation != null) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08003360 info.append("Reason: ").append(annotation).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003362 if (parent != null && parent != activity) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08003363 info.append("Parent: ").append(parent.shortComponentName).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365
Dianne Hackborn287952c2010-09-22 22:34:31 -07003366 final ProcessStats processStats = new ProcessStats(true);
3367
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003368 File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003369
Dan Egnor42471dd2010-01-07 17:25:22 -08003370 String cpuInfo = null;
3371 if (MONITOR_CPU_USAGE) {
3372 updateCpuStatsNow();
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003373 synchronized (mProcessStatsThread) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003374 cpuInfo = mProcessStats.printCurrentState(anrTime);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003375 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003376 info.append(processStats.printCurrentLoad());
Dan Egnor42471dd2010-01-07 17:25:22 -08003377 info.append(cpuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 }
3379
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003380 info.append(processStats.printCurrentState(anrTime));
3381
Joe Onorato8a9b2202010-02-26 18:56:32 -08003382 Slog.e(TAG, info.toString());
Dan Egnor42471dd2010-01-07 17:25:22 -08003383 if (tracesFile == null) {
3384 // There is no trace file, so dump (only) the alleged culprit's threads to the log
3385 Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
3386 }
3387
Jeff Sharkeya353d262011-10-28 11:12:06 -07003388 addErrorToDropBox("anr", app, app.processName, activity, parent, annotation,
3389 cpuInfo, tracesFile, null);
Dan Egnor42471dd2010-01-07 17:25:22 -08003390
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003391 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 try {
Dan Egnor42471dd2010-01-07 17:25:22 -08003393 // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
3394 int res = mController.appNotResponding(app.processName, app.pid, info.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 if (res != 0) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003396 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
3397 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 }
3399 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003400 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 }
3402 }
3403
Dan Egnor42471dd2010-01-07 17:25:22 -08003404 // Unless configured otherwise, swallow ANRs in background processes & kill the process.
3405 boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
3406 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003407
3408 synchronized (this) {
3409 if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003410 Slog.w(TAG, "Killing " + app + ": background ANR");
3411 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
3412 app.processName, app.setAdj, "background ANR");
3413 Process.killProcessQuiet(app.pid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003414 return;
3415 }
3416
3417 // Set the app's notResponding state, and look up the errorReportReceiver
3418 makeAppNotRespondingLocked(app,
3419 activity != null ? activity.shortComponentName : null,
3420 annotation != null ? "ANR " + annotation : "ANR",
3421 info.toString());
3422
3423 // Bring up the infamous App Not Responding dialog
3424 Message msg = Message.obtain();
3425 HashMap map = new HashMap();
3426 msg.what = SHOW_NOT_RESPONDING_MSG;
3427 msg.obj = map;
3428 map.put("app", app);
3429 if (activity != null) {
3430 map.put("activity", activity);
3431 }
3432
3433 mHandler.sendMessage(msg);
Dan Egnor42471dd2010-01-07 17:25:22 -08003434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 }
3436
Dianne Hackborn0dad3642010-09-09 21:25:35 -07003437 final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) {
3438 if (!mLaunchWarningShown) {
3439 mLaunchWarningShown = true;
3440 mHandler.post(new Runnable() {
3441 @Override
3442 public void run() {
3443 synchronized (ActivityManagerService.this) {
3444 final Dialog d = new LaunchWarningWindow(mContext, cur, next);
3445 d.show();
3446 mHandler.postDelayed(new Runnable() {
3447 @Override
3448 public void run() {
3449 synchronized (ActivityManagerService.this) {
3450 d.dismiss();
3451 mLaunchWarningShown = false;
3452 }
3453 }
3454 }, 4000);
3455 }
3456 }
3457 });
3458 }
3459 }
3460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 public boolean clearApplicationUserData(final String packageName,
Dianne Hackborn1676c852012-09-10 14:52:30 -07003462 final IPackageDataObserver observer, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003463 enforceNotIsolatedCaller("clearApplicationUserData");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 int uid = Binder.getCallingUid();
3465 int pid = Binder.getCallingPid();
Dianne Hackborn1676c852012-09-10 14:52:30 -07003466 userId = handleIncomingUserLocked(pid, uid,
3467 userId, false, true, "clearApplicationUserData", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 long callingId = Binder.clearCallingIdentity();
3469 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003470 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 int pkgUid = -1;
3472 synchronized(this) {
3473 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003474 pkgUid = pm.getPackageUid(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 } catch (RemoteException e) {
3476 }
3477 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003478 Slog.w(TAG, "Invalid packageName:" + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 return false;
3480 }
3481 if (uid == pkgUid || checkComponentPermission(
3482 android.Manifest.permission.CLEAR_APP_USER_DATA,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08003483 pid, uid, -1, true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003485 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 } else {
3487 throw new SecurityException(pid+" does not have permission:"+
3488 android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" +
3489 "for process:"+packageName);
3490 }
3491 }
3492
3493 try {
3494 //clear application user data
Amith Yamasani483f3b02012-03-13 16:08:00 -07003495 pm.clearApplicationUserData(packageName, observer, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
3497 Uri.fromParts("package", packageName, null));
3498 intent.putExtra(Intent.EXTRA_UID, pkgUid);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003499 broadcastIntentInPackage("android", Process.SYSTEM_UID, intent,
Amith Yamasani742a6712011-05-04 14:49:28 -07003500 null, null, 0, null, null, null, false, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 } catch (RemoteException e) {
3502 }
3503 } finally {
3504 Binder.restoreCallingIdentity(callingId);
3505 }
3506 return true;
3507 }
3508
Dianne Hackborn1676c852012-09-10 14:52:30 -07003509 public void killBackgroundProcesses(final String packageName, int userId) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003510 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
3511 != PackageManager.PERMISSION_GRANTED &&
3512 checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES)
3513 != PackageManager.PERMISSION_GRANTED) {
3514 String msg = "Permission Denial: killBackgroundProcesses() from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 + Binder.getCallingPid()
3516 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn03abb812010-01-04 18:43:19 -08003517 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003518 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 throw new SecurityException(msg);
3520 }
Dianne Hackborn1676c852012-09-10 14:52:30 -07003521
3522 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(),
3523 userId, true, true, "killBackgroundProcesses", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 long callingId = Binder.clearCallingIdentity();
3525 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003526 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 synchronized(this) {
Dianne Hackborn1676c852012-09-10 14:52:30 -07003528 int appId = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 try {
Dianne Hackborn1676c852012-09-10 14:52:30 -07003530 appId = UserHandle.getAppId(pm.getPackageUid(packageName, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 } catch (RemoteException e) {
3532 }
Dianne Hackborn1676c852012-09-10 14:52:30 -07003533 if (appId == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003534 Slog.w(TAG, "Invalid packageName: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 return;
3536 }
Dianne Hackborn1676c852012-09-10 14:52:30 -07003537 killPackageProcessesLocked(packageName, appId, userId,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003538 ProcessList.SERVICE_ADJ, false, true, true, false, "kill background");
3539 }
3540 } finally {
3541 Binder.restoreCallingIdentity(callingId);
3542 }
3543 }
3544
3545 public void killAllBackgroundProcesses() {
3546 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
3547 != PackageManager.PERMISSION_GRANTED) {
3548 String msg = "Permission Denial: killAllBackgroundProcesses() from pid="
3549 + Binder.getCallingPid()
3550 + ", uid=" + Binder.getCallingUid()
3551 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
3552 Slog.w(TAG, msg);
3553 throw new SecurityException(msg);
3554 }
3555
3556 long callingId = Binder.clearCallingIdentity();
3557 try {
3558 synchronized(this) {
3559 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
3560 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
3561 final int NA = apps.size();
3562 for (int ia=0; ia<NA; ia++) {
3563 ProcessRecord app = apps.valueAt(ia);
3564 if (app.persistent) {
3565 // we don't kill persistent processes
3566 continue;
3567 }
3568 if (app.removed) {
3569 procs.add(app);
3570 } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
3571 app.removed = true;
3572 procs.add(app);
3573 }
3574 }
3575 }
3576
3577 int N = procs.size();
3578 for (int i=0; i<N; i++) {
3579 removeProcessLocked(procs.get(i), false, true, "kill all background");
3580 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003581 }
3582 } finally {
3583 Binder.restoreCallingIdentity(callingId);
3584 }
3585 }
3586
Dianne Hackborn1676c852012-09-10 14:52:30 -07003587 public void forceStopPackage(final String packageName, int userId) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003588 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
3589 != PackageManager.PERMISSION_GRANTED) {
3590 String msg = "Permission Denial: forceStopPackage() from pid="
3591 + Binder.getCallingPid()
3592 + ", uid=" + Binder.getCallingUid()
3593 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003594 Slog.w(TAG, msg);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003595 throw new SecurityException(msg);
3596 }
Dianne Hackborn1676c852012-09-10 14:52:30 -07003597 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(),
3598 userId, true, true, "forceStopPackage", null);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003599 long callingId = Binder.clearCallingIdentity();
3600 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003601 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn03abb812010-01-04 18:43:19 -08003602 synchronized(this) {
Dianne Hackborn1676c852012-09-10 14:52:30 -07003603 int[] users = userId == UserHandle.USER_ALL
3604 ? getUsersLocked() : new int[] { userId };
3605 for (int user : users) {
3606 int pkgUid = -1;
3607 try {
3608 pkgUid = pm.getPackageUid(packageName, user);
3609 } catch (RemoteException e) {
3610 }
3611 if (pkgUid == -1) {
3612 Slog.w(TAG, "Invalid packageName: " + packageName);
3613 continue;
3614 }
3615 try {
3616 pm.setPackageStoppedState(packageName, true, user);
3617 } catch (RemoteException e) {
3618 } catch (IllegalArgumentException e) {
3619 Slog.w(TAG, "Failed trying to unstop package "
3620 + packageName + ": " + e);
3621 }
3622 if (isUserRunningLocked(user)) {
3623 forceStopPackageLocked(packageName, pkgUid);
3624 }
Dianne Hackborne7f97212011-02-24 14:40:20 -08003625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 }
3627 } finally {
3628 Binder.restoreCallingIdentity(callingId);
3629 }
3630 }
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003631
3632 /*
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003633 * The pkg name and app id have to be specified.
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003634 */
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003635 public void killApplicationWithAppId(String pkg, int appid) {
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003636 if (pkg == null) {
3637 return;
3638 }
3639 // Make sure the uid is valid.
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003640 if (appid < 0) {
3641 Slog.w(TAG, "Invalid appid specified for pkg : " + pkg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003642 return;
3643 }
3644 int callerUid = Binder.getCallingUid();
3645 // Only the system server can kill an application
3646 if (callerUid == Process.SYSTEM_UID) {
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07003647 // Post an aysnc message to kill the application
3648 Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003649 msg.arg1 = appid;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07003650 msg.arg2 = 0;
3651 msg.obj = pkg;
Suchi Amalapurapud50066f2009-08-18 16:57:41 -07003652 mHandler.sendMessage(msg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003653 } else {
3654 throw new SecurityException(callerUid + " cannot kill pkg: " +
3655 pkg);
3656 }
3657 }
3658
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003659 public void closeSystemDialogs(String reason) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003660 enforceNotIsolatedCaller("closeSystemDialogs");
Dianne Hackborne302a162012-05-15 14:58:32 -07003661
Dianne Hackbornb8839dd2012-09-04 10:55:44 -07003662 final int pid = Binder.getCallingPid();
Dianne Hackborne302a162012-05-15 14:58:32 -07003663 final int uid = Binder.getCallingUid();
3664 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornb8839dd2012-09-04 10:55:44 -07003665 try {
3666 synchronized (this) {
3667 // Only allow this from foreground processes, so that background
3668 // applications can't abuse it to prevent system UI from being shown.
3669 if (uid >= Process.FIRST_APPLICATION_UID) {
3670 ProcessRecord proc;
3671 synchronized (mPidsSelfLocked) {
3672 proc = mPidsSelfLocked.get(pid);
3673 }
3674 if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
3675 Slog.w(TAG, "Ignoring closeSystemDialogs " + reason
3676 + " from background process " + proc);
3677 return;
3678 }
3679 }
3680 closeSystemDialogsLocked(reason);
3681 }
3682 } finally {
3683 Binder.restoreCallingIdentity(origId);
Dianne Hackborne302a162012-05-15 14:58:32 -07003684 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003685 }
3686
Dianne Hackbornb8839dd2012-09-04 10:55:44 -07003687 void closeSystemDialogsLocked(String reason) {
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003688 Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003689 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003690 if (reason != null) {
3691 intent.putExtra("reason", reason);
3692 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003693 mWindowManager.closeSystemDialogs(reason);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07003694
Dianne Hackborne302a162012-05-15 14:58:32 -07003695 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
3696 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackborn1927ae82012-06-22 15:21:36 -07003697 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Dianne Hackborne302a162012-05-15 14:58:32 -07003698 r.stack.finishActivityLocked(r, i,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003699 Activity.RESULT_CANCELED, null, "close-sys", true);
Dianne Hackbornffa42482009-09-23 22:20:11 -07003700 }
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003701 }
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07003702
Dianne Hackbornb8839dd2012-09-04 10:55:44 -07003703 broadcastIntentLocked(null, null, intent, null,
3704 null, 0, null, null, null, false, false, -1,
3705 Process.SYSTEM_UID, UserHandle.USER_ALL);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003706 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003707
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003708 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids)
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003709 throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003710 enforceNotIsolatedCaller("getProcessMemoryInfo");
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003711 Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length];
3712 for (int i=pids.length-1; i>=0; i--) {
3713 infos[i] = new Debug.MemoryInfo();
3714 Debug.getMemoryInfo(pids[i], infos[i]);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003715 }
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003716 return infos;
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003717 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07003718
Dianne Hackbornb437e092011-08-05 17:50:29 -07003719 public long[] getProcessPss(int[] pids) throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003720 enforceNotIsolatedCaller("getProcessPss");
Dianne Hackbornb437e092011-08-05 17:50:29 -07003721 long[] pss = new long[pids.length];
3722 for (int i=pids.length-1; i>=0; i--) {
3723 pss[i] = Debug.getPss(pids[i]);
3724 }
3725 return pss;
3726 }
3727
Christopher Tate5e1ab332009-09-01 20:32:49 -07003728 public void killApplicationProcess(String processName, int uid) {
3729 if (processName == null) {
3730 return;
3731 }
3732
3733 int callerUid = Binder.getCallingUid();
3734 // Only the system server can kill an application
3735 if (callerUid == Process.SYSTEM_UID) {
3736 synchronized (this) {
3737 ProcessRecord app = getProcessRecordLocked(processName, uid);
Christopher Tate4a627c72011-04-01 14:43:32 -07003738 if (app != null && app.thread != null) {
Christopher Tate5e1ab332009-09-01 20:32:49 -07003739 try {
3740 app.thread.scheduleSuicide();
3741 } catch (RemoteException e) {
3742 // If the other end already died, then our work here is done.
3743 }
3744 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003745 Slog.w(TAG, "Process/uid not found attempting kill of "
Christopher Tate5e1ab332009-09-01 20:32:49 -07003746 + processName + " / " + uid);
3747 }
3748 }
3749 } else {
3750 throw new SecurityException(callerUid + " cannot kill app process: " +
3751 processName);
3752 }
3753 }
3754
Dianne Hackborn03abb812010-01-04 18:43:19 -08003755 private void forceStopPackageLocked(final String packageName, int uid) {
Dianne Hackborn1676c852012-09-10 14:52:30 -07003756 forceStopPackageLocked(packageName, UserHandle.getAppId(uid), false,
3757 false, true, false, UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
3759 Uri.fromParts("package", packageName, null));
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003760 if (!mProcessesReady) {
3761 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
3762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 intent.putExtra(Intent.EXTRA_UID, uid);
Dianne Hackbornc72fc672012-09-20 13:12:03 -07003764 intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 broadcastIntentLocked(null, null, intent,
3766 null, null, 0, null, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07003767 false, false,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003768 MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003770
3771 private void forceStopUserLocked(int userId) {
3772 forceStopPackageLocked(null, -1, false, false, true, false, userId);
3773 Intent intent = new Intent(Intent.ACTION_USER_STOPPED);
3774 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
3775 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
3776 broadcastIntentLocked(null, null, intent,
3777 null, null, 0, null, null, null,
3778 false, false,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07003779 MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003780 }
3781
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003782 private final boolean killPackageProcessesLocked(String packageName, int appId,
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003783 int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart,
3784 boolean doit, boolean evenPersistent, String reason) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003785 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786
Dianne Hackborn03abb812010-01-04 18:43:19 -08003787 // Remove all processes this package may have touched: all with the
3788 // same UID (except for the system or root user), and all whose name
3789 // matches the package name.
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003790 final String procNamePrefix = packageName != null ? (packageName + ":") : null;
Dianne Hackborn03abb812010-01-04 18:43:19 -08003791 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
3792 final int NA = apps.size();
3793 for (int ia=0; ia<NA; ia++) {
3794 ProcessRecord app = apps.valueAt(ia);
Christopher Tate3dacd842011-08-19 14:56:15 -07003795 if (app.persistent && !evenPersistent) {
Christopher Tate064d8422011-07-26 15:38:07 -07003796 // we don't kill persistent processes
3797 continue;
3798 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003799 if (app.removed) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003800 if (doit) {
3801 procs.add(app);
3802 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003803 continue;
3804 }
3805
3806 // Skip process if it doesn't meet our oom adj requirement.
3807 if (app.setAdj < minOomAdj) {
3808 continue;
3809 }
3810
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003811 // If no package is specified, we call all processes under the
3812 // give user id.
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003813 if (packageName == null) {
3814 if (app.userId != userId) {
3815 continue;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003816 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003817 // Package has been specified, we want to hit all processes
3818 // that match it. We need to qualify this by the processes
3819 // that are running under the specified app and user ID.
3820 } else {
3821 if (UserHandle.getAppId(app.uid) != appId) {
3822 continue;
3823 }
3824 if (userId != UserHandle.USER_ALL && app.userId != userId) {
3825 continue;
3826 }
3827 if (!app.pkgList.contains(packageName)) {
3828 continue;
Dianne Hackborn03abb812010-01-04 18:43:19 -08003829 }
3830 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003831
3832 // Process has passed all conditions, kill it!
3833 if (!doit) {
3834 return true;
3835 }
3836 app.removed = true;
3837 procs.add(app);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003838 }
3839 }
3840
3841 int N = procs.size();
3842 for (int i=0; i<N; i++) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003843 removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003844 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003845 return N > 0;
Dianne Hackborn03abb812010-01-04 18:43:19 -08003846 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003847
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003848 private final boolean forceStopPackageLocked(String name, int appId,
Christopher Tate3dacd842011-08-19 14:56:15 -07003849 boolean callerWillRestart, boolean purgeCache, boolean doit,
Amith Yamasani483f3b02012-03-13 16:08:00 -07003850 boolean evenPersistent, int userId) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07003851 int i;
3852 int N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003854 if (userId == UserHandle.USER_ALL && name == null) {
3855 Slog.w(TAG, "Can't force stop all processes of all users, that is insane!");
3856 }
3857
3858 if (appId < 0 && name != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 try {
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003860 appId = UserHandle.getAppId(
3861 AppGlobals.getPackageManager().getPackageUid(name, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 } catch (RemoteException e) {
3863 }
3864 }
3865
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003866 if (doit) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003867 if (name != null) {
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003868 Slog.i(TAG, "Force stopping package " + name + " appid=" + appId
3869 + " user=" + userId);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003870 } else {
3871 Slog.i(TAG, "Force stopping user " + userId);
3872 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003873
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003874 Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator();
3875 while (badApps.hasNext()) {
3876 SparseArray<Long> ba = badApps.next();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003877 for (i=ba.size()-1; i>=0; i--) {
3878 boolean remove = false;
3879 final int entUid = ba.keyAt(i);
3880 if (name != null) {
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003881 if (userId == UserHandle.USER_ALL) {
3882 if (UserHandle.getAppId(entUid) == appId) {
3883 remove = true;
3884 }
3885 } else {
3886 if (entUid == UserHandle.getUid(userId, appId)) {
3887 remove = true;
3888 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003889 }
3890 } else if (UserHandle.getUserId(entUid) == userId) {
3891 remove = true;
3892 }
3893 if (remove) {
3894 ba.removeAt(i);
3895 }
3896 }
3897 if (ba.size() == 0) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003898 badApps.remove();
3899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 }
3901 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003902
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003903 boolean didSomething = killPackageProcessesLocked(name, appId, userId,
3904 -100, callerWillRestart, false, doit, evenPersistent,
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003905 name == null ? ("force stop user " + userId) : ("force stop " + name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003907 TaskRecord lastTask = null;
3908 for (i=0; i<mMainStack.mHistory.size(); i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003909 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003910 final boolean samePackage = r.packageName.equals(name)
3911 || (name == null && r.userId == userId);
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003912 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Amith Yamasani13593602012-03-22 16:16:17 -07003913 && (samePackage || r.task == lastTask)
Christopher Tate3dacd842011-08-19 14:56:15 -07003914 && (r.app == null || evenPersistent || !r.app.persistent)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003915 if (!doit) {
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003916 if (r.finishing) {
3917 // If this activity is just finishing, then it is not
3918 // interesting as far as something to stop.
3919 continue;
3920 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003921 return true;
3922 }
3923 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003924 Slog.i(TAG, " Force finishing activity " + r);
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003925 if (samePackage) {
3926 if (r.app != null) {
3927 r.app.removed = true;
3928 }
3929 r.app = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 }
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003931 lastTask = r.task;
3932 if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED,
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003933 null, "force-stop", true)) {
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003934 i--;
3935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
3937 }
3938
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003939 if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) {
3940 if (!doit) {
3941 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003943 didSomething = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 }
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003945
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07003946 if (name == null) {
3947 // Remove all sticky broadcasts from this user.
3948 mStickyBroadcasts.remove(userId);
3949 }
3950
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003951 ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>();
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003952 if (mProviderMap.collectForceStopProviders(name, appId, doit, evenPersistent,
3953 userId, providers)) {
3954 if (!doit) {
3955 return true;
3956 }
3957 didSomething = true;
3958 }
3959 N = providers.size();
3960 for (i=0; i<N; i++) {
3961 removeDyingProviderLocked(null, providers.get(i), true);
3962 }
3963
3964 if (mIntentSenderRecords.size() > 0) {
3965 Iterator<WeakReference<PendingIntentRecord>> it
3966 = mIntentSenderRecords.values().iterator();
3967 while (it.hasNext()) {
3968 WeakReference<PendingIntentRecord> wpir = it.next();
3969 if (wpir == null) {
3970 it.remove();
3971 continue;
3972 }
3973 PendingIntentRecord pir = wpir.get();
3974 if (pir == null) {
3975 it.remove();
3976 continue;
3977 }
3978 if (name == null) {
3979 // Stopping user, remove all objects for the user.
3980 if (pir.key.userId != userId) {
3981 // Not the same user, skip it.
3982 continue;
3983 }
3984 } else {
3985 if (UserHandle.getAppId(pir.uid) != appId) {
3986 // Different app id, skip it.
3987 continue;
3988 }
3989 if (userId != UserHandle.USER_ALL && pir.key.userId != userId) {
3990 // Different user, skip it.
3991 continue;
3992 }
3993 if (!pir.key.packageName.equals(name)) {
3994 // Different package, skip it.
3995 continue;
3996 }
3997 }
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003998 if (!doit) {
3999 return true;
4000 }
4001 didSomething = true;
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07004002 it.remove();
4003 pir.canceled = true;
4004 if (pir.key.activity != null) {
4005 pir.key.activity.pendingResults.remove(pir.ref);
4006 }
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07004007 }
4008 }
4009
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004010 if (doit) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07004011 if (purgeCache && name != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004012 AttributeCache ac = AttributeCache.instance();
4013 if (ac != null) {
4014 ac.removePackage(name);
4015 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08004016 }
Dianne Hackborn38cc8962011-10-13 11:33:55 -07004017 if (mBooted) {
4018 mMainStack.resumeTopActivityLocked(null);
4019 mMainStack.scheduleIdleLocked();
4020 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08004021 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004022
4023 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 }
4025
Dianne Hackborn130b0d22011-07-26 22:07:48 -07004026 private final boolean removeProcessLocked(ProcessRecord app,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08004027 boolean callerWillRestart, boolean allowRestart, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 final String name = app.processName;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004029 final int uid = app.uid;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004030 if (DEBUG_PROCESSES) Slog.d(
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08004031 TAG, "Force removing proc " + app.toShortString() + " (" + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 + "/" + uid + ")");
4033
4034 mProcessNames.remove(name, uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004035 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07004036 if (mHeavyWeightProcess == app) {
Dianne Hackborn41203752012-08-31 14:05:51 -07004037 mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
4038 mHeavyWeightProcess.userId, 0));
Dianne Hackborn860755f2010-06-03 18:47:52 -07004039 mHeavyWeightProcess = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07004040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 boolean needRestart = false;
4042 if (app.pid > 0 && app.pid != MY_PID) {
4043 int pid = app.pid;
4044 synchronized (mPidsSelfLocked) {
4045 mPidsSelfLocked.remove(pid);
4046 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
4047 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08004048 Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason);
Dianne Hackborn130b0d22011-07-26 22:07:48 -07004049 handleAppDiedLocked(app, true, allowRestart);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004050 mLruProcesses.remove(app);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08004051 Process.killProcessQuiet(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004053 if (app.persistent && !app.isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 if (!callerWillRestart) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004055 addAppLocked(app.info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 } else {
4057 needRestart = true;
4058 }
4059 }
4060 } else {
4061 mRemovedProcesses.add(app);
4062 }
4063
4064 return needRestart;
4065 }
4066
4067 private final void processStartTimedOutLocked(ProcessRecord app) {
4068 final int pid = app.pid;
4069 boolean gone = false;
4070 synchronized (mPidsSelfLocked) {
4071 ProcessRecord knownApp = mPidsSelfLocked.get(pid);
4072 if (knownApp != null && knownApp.thread == null) {
4073 mPidsSelfLocked.remove(pid);
4074 gone = true;
4075 }
4076 }
4077
4078 if (gone) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004079 Slog.w(TAG, "Process " + app + " failed to attach");
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004080 EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.uid,
Dianne Hackbornf670ef72009-11-16 13:59:16 -08004081 app.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004082 mProcessNames.remove(app.processName, app.uid);
4083 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07004084 if (mHeavyWeightProcess == app) {
Dianne Hackborn41203752012-08-31 14:05:51 -07004085 mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
4086 mHeavyWeightProcess.userId, 0));
Dianne Hackborn860755f2010-06-03 18:47:52 -07004087 mHeavyWeightProcess = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07004088 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08004089 // Take care of any launching providers waiting for this process.
4090 checkAppInLaunchingProvidersLocked(app, true);
4091 // Take care of any services that are waiting for the process.
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004092 mServices.processStartTimedOutLocked(app);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004093 EventLog.writeEvent(EventLogTags.AM_KILL, pid,
4094 app.processName, app.setAdj, "start timeout");
4095 Process.killProcessQuiet(pid);
Christopher Tate181fafa2009-05-14 11:12:14 -07004096 if (mBackupTarget != null && mBackupTarget.app.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004097 Slog.w(TAG, "Unattached app died before backup, skipping");
Christopher Tate181fafa2009-05-14 11:12:14 -07004098 try {
4099 IBackupManager bm = IBackupManager.Stub.asInterface(
4100 ServiceManager.getService(Context.BACKUP_SERVICE));
4101 bm.agentDisconnected(app.info.packageName);
4102 } catch (RemoteException e) {
4103 // Can't happen; the backup manager is local
4104 }
4105 }
Christopher Tatef46723b2012-01-26 14:19:24 -08004106 if (isPendingBroadcastProcessLocked(pid)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004107 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
Christopher Tatef46723b2012-01-26 14:19:24 -08004108 skipPendingBroadcastLocked(pid);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08004109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004111 Slog.w(TAG, "Spurious process start timeout - pid not known for " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
4113 }
4114
4115 private final boolean attachApplicationLocked(IApplicationThread thread,
4116 int pid) {
4117
4118 // Find the application record that is being attached... either via
4119 // the pid if we are running in multiple processes, or just pull the
4120 // next app record if we are emulating process with anonymous threads.
4121 ProcessRecord app;
4122 if (pid != MY_PID && pid >= 0) {
4123 synchronized (mPidsSelfLocked) {
4124 app = mPidsSelfLocked.get(pid);
4125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 } else {
4127 app = null;
4128 }
4129
4130 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004131 Slog.w(TAG, "No pending application record for pid " + pid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 + " (IApplicationThread " + thread + "); dropping process");
Doug Zongker2bec3d42009-12-04 12:52:44 -08004133 EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 if (pid > 0 && pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004135 Process.killProcessQuiet(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 } else {
4137 try {
4138 thread.scheduleExit();
4139 } catch (Exception e) {
4140 // Ignore exceptions.
4141 }
4142 }
4143 return false;
4144 }
4145
4146 // If this application record is still attached to a previous
4147 // process, clean it up now.
4148 if (app.thread != null) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -07004149 handleAppDiedLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 }
4151
4152 // Tell the process all about itself.
4153
Joe Onorato8a9b2202010-02-26 18:56:32 -08004154 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 TAG, "Binding process pid " + pid + " to record " + app);
4156
4157 String processName = app.processName;
4158 try {
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -07004159 AppDeathRecipient adr = new AppDeathRecipient(
4160 app, pid, thread);
4161 thread.asBinder().linkToDeath(adr, 0);
4162 app.deathRecipient = adr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 } catch (RemoteException e) {
4164 app.resetPackageList();
4165 startProcessLocked(app, "link fail", processName);
4166 return false;
4167 }
4168
Doug Zongker2bec3d42009-12-04 12:52:44 -08004169 EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170
4171 app.thread = thread;
4172 app.curAdj = app.setAdj = -100;
Dianne Hackborn09c916b2009-12-08 14:50:51 -08004173 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
4174 app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 app.forcingToForeground = null;
4176 app.foregroundServices = false;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07004177 app.hasShownUi = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 app.debugging = false;
4179
4180 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
4181
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07004182 boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004183 List providers = normalMode ? generateApplicationProvidersLocked(app) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004185 if (!normalMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004186 Slog.i(TAG, "Launching preboot mode app: " + app);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004187 }
4188
Joe Onorato8a9b2202010-02-26 18:56:32 -08004189 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 TAG, "New app record " + app
4191 + " thread=" + thread.asBinder() + " pid=" + pid);
4192 try {
4193 int testMode = IApplicationThread.DEBUG_OFF;
4194 if (mDebugApp != null && mDebugApp.equals(processName)) {
4195 testMode = mWaitForDebugger
4196 ? IApplicationThread.DEBUG_WAIT
4197 : IApplicationThread.DEBUG_ON;
4198 app.debugging = true;
4199 if (mDebugTransient) {
4200 mDebugApp = mOrigDebugApp;
4201 mWaitForDebugger = mOrigWaitForDebugger;
4202 }
4203 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004204 String profileFile = app.instrumentationProfileFile;
4205 ParcelFileDescriptor profileFd = null;
4206 boolean profileAutoStop = false;
4207 if (mProfileApp != null && mProfileApp.equals(processName)) {
4208 mProfileProc = app;
4209 profileFile = mProfileFile;
4210 profileFd = mProfileFd;
4211 profileAutoStop = mAutoStopProfiler;
4212 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004213 boolean enableOpenGlTrace = false;
4214 if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) {
4215 enableOpenGlTrace = true;
4216 mOpenGlTraceApp = null;
4217 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004218
Christopher Tate181fafa2009-05-14 11:12:14 -07004219 // If the app is being launched for restore or full backup, set it up specially
4220 boolean isRestrictedBackupMode = false;
4221 if (mBackupTarget != null && mBackupAppName.equals(processName)) {
4222 isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE)
Christopher Tate75a99702011-05-18 16:28:19 -07004223 || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL)
Christopher Tate181fafa2009-05-14 11:12:14 -07004224 || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL);
4225 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004226
Dianne Hackbornd7f6daa2009-06-22 17:06:35 -07004227 ensurePackageDexOpt(app.instrumentationInfo != null
4228 ? app.instrumentationInfo.packageName
4229 : app.info.packageName);
4230 if (app.instrumentationClass != null) {
4231 ensurePackageDexOpt(app.instrumentationClass.getPackageName());
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07004232 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004233 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004234 + processName + " with config " + mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004235 ApplicationInfo appInfo = app.instrumentationInfo != null
4236 ? app.instrumentationInfo : app.info;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -07004237 app.compat = compatibilityInfoForPackageLocked(appInfo);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004238 if (profileFd != null) {
4239 profileFd = profileFd.dup();
4240 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004241 thread.bindApplication(processName, appInfo, providers,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004242 app.instrumentationClass, profileFile, profileFd, profileAutoStop,
Siva Velusamy92a8b222012-03-09 16:24:04 -08004243 app.instrumentationArguments, app.instrumentationWatcher, testMode,
4244 enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent,
Dianne Hackborn813075a62011-11-14 17:45:19 -08004245 new Configuration(mConfiguration), app.compat, getCommonServicesLocked(),
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004246 mCoreSettingsObserver.getCoreSettingsLocked());
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004247 updateLruProcessLocked(app, false, true);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07004248 app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 } catch (Exception e) {
4250 // todo: Yikes! What should we do? For now we will try to
4251 // start another process, but that could easily get us in
4252 // an infinite loop of restarting processes...
Joe Onorato8a9b2202010-02-26 18:56:32 -08004253 Slog.w(TAG, "Exception thrown during bind!", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254
4255 app.resetPackageList();
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -07004256 app.unlinkDeathRecipient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 startProcessLocked(app, "bind fail", processName);
4258 return false;
4259 }
4260
4261 // Remove this record from the list of starting applications.
4262 mPersistentStartingProcesses.remove(app);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07004263 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
4264 "Attach application locked removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 mProcessesOnHold.remove(app);
4266
4267 boolean badApp = false;
4268 boolean didSomething = false;
4269
4270 // See if the top visible activity is waiting to run in this process...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004271 ActivityRecord hr = mMainStack.topRunningActivityLocked(null);
Christopher Tate04c0af82010-06-07 18:35:20 -07004272 if (hr != null && normalMode) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004273 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 && processName.equals(hr.processName)) {
4275 try {
Mike Lockwood3a74bd32011-08-12 13:55:22 -07004276 if (mHeadless) {
4277 Slog.e(TAG, "Starting activities not supported on headless device: " + hr);
4278 } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 didSomething = true;
4280 }
4281 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004282 Slog.w(TAG, "Exception in new application when starting activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 + hr.intent.getComponent().flattenToShortString(), e);
4284 badApp = true;
4285 }
4286 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004287 mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 }
4289 }
4290
4291 // Find any services that should be running in this process...
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004292 if (!badApp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 try {
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004294 didSomething |= mServices.attachApplicationLocked(app, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 } catch (Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 badApp = true;
4297 }
4298 }
4299
Christopher Tatef46723b2012-01-26 14:19:24 -08004300 // Check if a next-broadcast receiver is in this process...
4301 if (!badApp && isPendingBroadcastProcessLocked(pid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 try {
Christopher Tatef46723b2012-01-26 14:19:24 -08004303 didSomething = sendPendingBroadcastsLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 } catch (Exception e) {
Christopher Tatef46723b2012-01-26 14:19:24 -08004305 // If the app died trying to launch the receiver we declare it 'bad'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 badApp = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 }
4308 }
4309
Christopher Tate181fafa2009-05-14 11:12:14 -07004310 // Check whether the next backup agent is in this process...
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004311 if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004312 if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07004313 ensurePackageDexOpt(mBackupTarget.appInfo.packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -07004314 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004315 thread.scheduleCreateBackupAgent(mBackupTarget.appInfo,
4316 compatibilityInfoForPackageLocked(mBackupTarget.appInfo),
4317 mBackupTarget.backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -07004318 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004319 Slog.w(TAG, "Exception scheduling backup agent creation: ");
Christopher Tate181fafa2009-05-14 11:12:14 -07004320 e.printStackTrace();
4321 }
4322 }
4323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 if (badApp) {
4325 // todo: Also need to kill application to deal with all
4326 // kinds of exceptions.
Dianne Hackborn130b0d22011-07-26 22:07:48 -07004327 handleAppDiedLocked(app, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328 return false;
4329 }
4330
4331 if (!didSomething) {
4332 updateOomAdjLocked();
4333 }
4334
4335 return true;
4336 }
4337
4338 public final void attachApplication(IApplicationThread thread) {
4339 synchronized (this) {
4340 int callingPid = Binder.getCallingPid();
4341 final long origId = Binder.clearCallingIdentity();
4342 attachApplicationLocked(thread, callingPid);
4343 Binder.restoreCallingIdentity(origId);
4344 }
4345 }
4346
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004347 public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004349 ActivityRecord r = mMainStack.activityIdleInternal(token, false, config);
4350 if (stopProfiling) {
4351 synchronized (this) {
4352 if (mProfileProc == r.app) {
4353 if (mProfileFd != null) {
4354 try {
4355 mProfileFd.close();
4356 } catch (IOException e) {
4357 }
4358 clearProfilerLocked();
4359 }
4360 }
4361 }
4362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 Binder.restoreCallingIdentity(origId);
4364 }
4365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 void enableScreenAfterBoot() {
Doug Zongker2bec3d42009-12-04 12:52:44 -08004367 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004368 SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 mWindowManager.enableScreenAfterBoot();
Jeff Brownc042ee22012-05-08 13:03:42 -07004370
4371 synchronized (this) {
4372 updateEventDispatchingLocked();
4373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 }
4375
Dianne Hackborn661cd522011-08-22 00:26:20 -07004376 public void showBootMessage(final CharSequence msg, final boolean always) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004377 enforceNotIsolatedCaller("showBootMessage");
Dianne Hackborn661cd522011-08-22 00:26:20 -07004378 mWindowManager.showBootMessage(msg, always);
4379 }
4380
Dianne Hackborn90c52de2011-09-23 12:57:44 -07004381 public void dismissKeyguardOnNextActivity() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004382 enforceNotIsolatedCaller("dismissKeyguardOnNextActivity");
Jeff Sharkey7ffaa982012-04-30 16:59:05 -07004383 final long token = Binder.clearCallingIdentity();
4384 try {
4385 synchronized (this) {
4386 if (mLockScreenShown) {
4387 mLockScreenShown = false;
4388 comeOutOfSleepIfNeededLocked();
4389 }
4390 mMainStack.dismissKeyguardOnNextActivityLocked();
Dianne Hackborn1e88e982012-04-24 18:35:55 -07004391 }
Jeff Sharkey7ffaa982012-04-30 16:59:05 -07004392 } finally {
4393 Binder.restoreCallingIdentity(token);
Dianne Hackborn90c52de2011-09-23 12:57:44 -07004394 }
4395 }
4396
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004397 final void finishBooting() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004398 IntentFilter pkgFilter = new IntentFilter();
4399 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
4400 pkgFilter.addDataScheme("package");
4401 mContext.registerReceiver(new BroadcastReceiver() {
4402 @Override
4403 public void onReceive(Context context, Intent intent) {
4404 String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
4405 if (pkgs != null) {
4406 for (String pkg : pkgs) {
Vairavan Srinivasan61f07652010-07-22 13:36:40 -07004407 synchronized (ActivityManagerService.this) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07004408 if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) {
4409 setResultCode(Activity.RESULT_OK);
4410 return;
4411 }
4412 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004413 }
4414 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004415 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004416 }, pkgFilter);
Amith Yamasani13593602012-03-22 16:16:17 -07004417
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004418 synchronized (this) {
4419 // Ensure that any processes we had put on hold are now started
4420 // up.
4421 final int NP = mProcessesOnHold.size();
4422 if (NP > 0) {
4423 ArrayList<ProcessRecord> procs =
4424 new ArrayList<ProcessRecord>(mProcessesOnHold);
4425 for (int ip=0; ip<NP; ip++) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07004426 if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: "
4427 + procs.get(ip));
4428 startProcessLocked(procs.get(ip), "on-hold", null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004429 }
4430 }
4431
4432 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004433 // Start looking for apps that are abusing wake locks.
4434 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07004435 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004436 // Tell anyone interested that we are done booting!
Dianne Hackbornf4c454b2010-08-11 12:47:41 -07004437 SystemProperties.set("sys.boot_completed", "1");
Guang Zhu191713a2012-01-12 12:02:22 -08004438 SystemProperties.set("dev.bootcomplete", "1");
Dianne Hackborn80a4af22012-08-27 19:18:31 -07004439 for (int i=0; i<mStartedUsers.size(); i++) {
4440 UserStartedState uss = mStartedUsers.valueAt(i);
4441 if (uss.mState == UserStartedState.STATE_BOOTING) {
4442 uss.mState = UserStartedState.STATE_RUNNING;
Dianne Hackborn41203752012-08-31 14:05:51 -07004443 final int userId = mStartedUsers.keyAt(i);
4444 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null);
4445 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
4446 broadcastIntentLocked(null, null, intent,
Dianne Hackborn80a4af22012-08-27 19:18:31 -07004447 null, null, 0, null, null,
4448 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
Dianne Hackborn41203752012-08-31 14:05:51 -07004449 false, false, MY_PID, Process.SYSTEM_UID, userId);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07004450 }
Amith Yamasani4860cfc2012-08-08 19:14:18 -07004451 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004452 }
4453 }
4454 }
4455
4456 final void ensureBootCompleted() {
4457 boolean booting;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004458 boolean enableScreen;
4459 synchronized (this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004460 booting = mBooting;
4461 mBooting = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004462 enableScreen = !mBooted;
4463 mBooted = true;
4464 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004465
4466 if (booting) {
4467 finishBooting();
4468 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004469
4470 if (enableScreen) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004471 enableScreenAfterBoot();
4472 }
4473 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07004474
4475 public final void activityResumed(IBinder token) {
4476 final long origId = Binder.clearCallingIdentity();
4477 mMainStack.activityResumed(token);
4478 Binder.restoreCallingIdentity(origId);
4479 }
4480
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004481 public final void activityPaused(IBinder token) {
4482 final long origId = Binder.clearCallingIdentity();
4483 mMainStack.activityPaused(token, false);
4484 Binder.restoreCallingIdentity(origId);
4485 }
4486
4487 public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail,
4488 CharSequence description) {
4489 if (localLOGV) Slog.v(
4490 TAG, "Activity stopped: token=" + token);
4491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 // Refuse possible leaked file descriptors
4493 if (icicle != null && icicle.hasFileDescriptors()) {
4494 throw new IllegalArgumentException("File descriptors passed in Bundle");
4495 }
4496
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004497 ActivityRecord r = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498
4499 final long origId = Binder.clearCallingIdentity();
4500
4501 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004502 r = mMainStack.isInStackLocked(token);
4503 if (r != null) {
4504 r.stack.activityStoppedLocked(r, icicle, thumbnail, description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 }
4506 }
4507
4508 if (r != null) {
4509 sendPendingThumbnail(r, null, null, null, false);
4510 }
4511
4512 trimApplications();
4513
4514 Binder.restoreCallingIdentity(origId);
4515 }
4516
4517 public final void activityDestroyed(IBinder token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004518 if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004519 mMainStack.activityDestroyed(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 }
4521
4522 public String getCallingPackage(IBinder token) {
4523 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004524 ActivityRecord r = getCallingRecordLocked(token);
Dianne Hackborn9bbcb912009-10-20 15:42:38 -07004525 return r != null && r.app != null ? r.info.packageName : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 }
4527 }
4528
4529 public ComponentName getCallingActivity(IBinder token) {
4530 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004531 ActivityRecord r = getCallingRecordLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 return r != null ? r.intent.getComponent() : null;
4533 }
4534 }
4535
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004536 private ActivityRecord getCallingRecordLocked(IBinder token) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004537 ActivityRecord r = mMainStack.isInStackLocked(token);
4538 if (r == null) {
4539 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004541 return r.resultTo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 }
4543
4544 public ComponentName getActivityClassForToken(IBinder token) {
4545 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004546 ActivityRecord r = mMainStack.isInStackLocked(token);
4547 if (r == null) {
4548 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004550 return r.intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 }
4552 }
4553
4554 public String getPackageForToken(IBinder token) {
4555 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004556 ActivityRecord r = mMainStack.isInStackLocked(token);
4557 if (r == null) {
4558 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004560 return r.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 }
4562 }
4563
4564 public IIntentSender getIntentSender(int type,
4565 String packageName, IBinder token, String resultWho,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004566 int requestCode, Intent[] intents, String[] resolvedTypes,
Dianne Hackborn41203752012-08-31 14:05:51 -07004567 int flags, Bundle options, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004568 enforceNotIsolatedCaller("getIntentSender");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004569 // Refuse possible leaked file descriptors
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004570 if (intents != null) {
4571 if (intents.length < 1) {
4572 throw new IllegalArgumentException("Intents array length must be >= 1");
4573 }
4574 for (int i=0; i<intents.length; i++) {
4575 Intent intent = intents[i];
Dianne Hackborn52b0ce02011-04-14 13:09:32 -07004576 if (intent != null) {
4577 if (intent.hasFileDescriptors()) {
4578 throw new IllegalArgumentException("File descriptors passed in Intent");
4579 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004580 if (type == ActivityManager.INTENT_SENDER_BROADCAST &&
Dianne Hackborn52b0ce02011-04-14 13:09:32 -07004581 (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
4582 throw new IllegalArgumentException(
4583 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
4584 }
4585 intents[i] = new Intent(intent);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004586 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004587 }
4588 if (resolvedTypes != null && resolvedTypes.length != intents.length) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004589 throw new IllegalArgumentException(
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004590 "Intent array length does not match resolvedTypes length");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004591 }
4592 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004593 if (options != null) {
4594 if (options.hasFileDescriptors()) {
4595 throw new IllegalArgumentException("File descriptors passed in options");
4596 }
4597 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 synchronized(this) {
4600 int callingUid = Binder.getCallingUid();
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07004601 int origUserId = userId;
Dianne Hackborn41203752012-08-31 14:05:51 -07004602 userId = handleIncomingUserLocked(Binder.getCallingPid(), callingUid, userId,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07004603 type == ActivityManager.INTENT_SENDER_BROADCAST, true,
4604 "getIntentSender", null);
4605 if (origUserId == UserHandle.USER_CURRENT) {
4606 // We don't want to evaluate this until the pending intent is
4607 // actually executed. However, we do want to always do the
4608 // security checking for it above.
4609 userId = UserHandle.USER_CURRENT;
4610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 try {
Jeff Brown10e89712011-07-08 18:52:57 -07004612 if (callingUid != 0 && callingUid != Process.SYSTEM_UID) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004613 int uid = AppGlobals.getPackageManager()
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004614 .getPackageUid(packageName, UserHandle.getUserId(callingUid));
4615 if (!UserHandle.isSameApp(callingUid, uid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 String msg = "Permission Denial: getIntentSender() from pid="
4617 + Binder.getCallingPid()
4618 + ", uid=" + Binder.getCallingUid()
4619 + ", (need uid=" + uid + ")"
4620 + " is not allowed to send as package " + packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004621 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 throw new SecurityException(msg);
4623 }
4624 }
Dianne Hackborn41203752012-08-31 14:05:51 -07004625
4626 return getIntentSenderLocked(type, packageName, callingUid, userId,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004627 token, resultWho, requestCode, intents, resolvedTypes, flags, options);
Dianne Hackborn860755f2010-06-03 18:47:52 -07004628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004629 } catch (RemoteException e) {
4630 throw new SecurityException(e);
4631 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004632 }
4633 }
4634
Dianne Hackborn41203752012-08-31 14:05:51 -07004635 IIntentSender getIntentSenderLocked(int type, String packageName,
4636 int callingUid, int userId, IBinder token, String resultWho,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004637 int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
4638 Bundle options) {
Amith Yamasani742a6712011-05-04 14:49:28 -07004639 if (DEBUG_MU)
4640 Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004641 ActivityRecord activity = null;
Dianne Hackborna4972e92012-03-14 10:38:05 -07004642 if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004643 activity = mMainStack.isInStackLocked(token);
4644 if (activity == null) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07004645 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004647 if (activity.finishing) {
4648 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004650 }
4651
4652 final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
4653 final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
4654 final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
4655 flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
4656 |PendingIntent.FLAG_UPDATE_CURRENT);
4657
4658 PendingIntentRecord.Key key = new PendingIntentRecord.Key(
4659 type, packageName, activity, resultWho,
Dianne Hackborn41203752012-08-31 14:05:51 -07004660 requestCode, intents, resolvedTypes, flags, options, userId);
Dianne Hackborn860755f2010-06-03 18:47:52 -07004661 WeakReference<PendingIntentRecord> ref;
4662 ref = mIntentSenderRecords.get(key);
4663 PendingIntentRecord rec = ref != null ? ref.get() : null;
4664 if (rec != null) {
4665 if (!cancelCurrent) {
4666 if (updateCurrent) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004667 if (rec.key.requestIntent != null) {
Adam Powell501d4a52012-04-30 15:03:57 -07004668 rec.key.requestIntent.replaceExtras(intents != null ?
4669 intents[intents.length - 1] : null);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004670 }
4671 if (intents != null) {
4672 intents[intents.length-1] = rec.key.requestIntent;
4673 rec.key.allIntents = intents;
4674 rec.key.allResolvedTypes = resolvedTypes;
4675 } else {
4676 rec.key.allIntents = null;
4677 rec.key.allResolvedTypes = null;
4678 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 return rec;
4681 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004682 rec.canceled = true;
4683 mIntentSenderRecords.remove(key);
4684 }
4685 if (noCreate) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 return rec;
4687 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004688 rec = new PendingIntentRecord(this, key, callingUid);
4689 mIntentSenderRecords.put(key, rec.ref);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004690 if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07004691 if (activity.pendingResults == null) {
4692 activity.pendingResults
4693 = new HashSet<WeakReference<PendingIntentRecord>>();
4694 }
4695 activity.pendingResults.add(rec.ref);
4696 }
4697 return rec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 }
4699
4700 public void cancelIntentSender(IIntentSender sender) {
4701 if (!(sender instanceof PendingIntentRecord)) {
4702 return;
4703 }
4704 synchronized(this) {
4705 PendingIntentRecord rec = (PendingIntentRecord)sender;
4706 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004707 int uid = AppGlobals.getPackageManager()
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004708 .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId());
4709 if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 String msg = "Permission Denial: cancelIntentSender() from pid="
4711 + Binder.getCallingPid()
4712 + ", uid=" + Binder.getCallingUid()
4713 + " is not allowed to cancel packges "
4714 + rec.key.packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004715 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 throw new SecurityException(msg);
4717 }
4718 } catch (RemoteException e) {
4719 throw new SecurityException(e);
4720 }
4721 cancelIntentSenderLocked(rec, true);
4722 }
4723 }
4724
4725 void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) {
4726 rec.canceled = true;
4727 mIntentSenderRecords.remove(rec.key);
4728 if (cleanActivity && rec.key.activity != null) {
4729 rec.key.activity.pendingResults.remove(rec.ref);
4730 }
4731 }
4732
4733 public String getPackageForIntentSender(IIntentSender pendingResult) {
4734 if (!(pendingResult instanceof PendingIntentRecord)) {
4735 return null;
4736 }
Brad Fitzpatrickb213d102010-04-19 11:58:52 -07004737 try {
4738 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4739 return res.key.packageName;
4740 } catch (ClassCastException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 }
4742 return null;
4743 }
4744
Christopher Tatec4a07d12012-04-06 14:19:13 -07004745 public int getUidForIntentSender(IIntentSender sender) {
4746 if (sender instanceof PendingIntentRecord) {
4747 try {
4748 PendingIntentRecord res = (PendingIntentRecord)sender;
4749 return res.uid;
4750 } catch (ClassCastException e) {
4751 }
4752 }
4753 return -1;
4754 }
4755
Dianne Hackborn6c418d52011-06-29 14:05:33 -07004756 public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) {
4757 if (!(pendingResult instanceof PendingIntentRecord)) {
4758 return false;
4759 }
4760 try {
4761 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4762 if (res.key.allIntents == null) {
4763 return false;
4764 }
4765 for (int i=0; i<res.key.allIntents.length; i++) {
4766 Intent intent = res.key.allIntents[i];
4767 if (intent.getPackage() != null && intent.getComponent() != null) {
4768 return false;
4769 }
4770 }
4771 return true;
4772 } catch (ClassCastException e) {
4773 }
4774 return false;
4775 }
4776
Dianne Hackborn1927ae82012-06-22 15:21:36 -07004777 public boolean isIntentSenderAnActivity(IIntentSender pendingResult) {
4778 if (!(pendingResult instanceof PendingIntentRecord)) {
4779 return false;
4780 }
4781 try {
4782 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4783 if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) {
4784 return true;
4785 }
4786 return false;
4787 } catch (ClassCastException e) {
4788 }
4789 return false;
4790 }
4791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 public void setProcessLimit(int max) {
4793 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
4794 "setProcessLimit()");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004795 synchronized (this) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07004796 mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004797 mProcessLimitOverride = max;
4798 }
4799 trimApplications();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 }
4801
4802 public int getProcessLimit() {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004803 synchronized (this) {
4804 return mProcessLimitOverride;
4805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806 }
4807
4808 void foregroundTokenDied(ForegroundToken token) {
4809 synchronized (ActivityManagerService.this) {
4810 synchronized (mPidsSelfLocked) {
4811 ForegroundToken cur
4812 = mForegroundProcesses.get(token.pid);
4813 if (cur != token) {
4814 return;
4815 }
4816 mForegroundProcesses.remove(token.pid);
4817 ProcessRecord pr = mPidsSelfLocked.get(token.pid);
4818 if (pr == null) {
4819 return;
4820 }
4821 pr.forcingToForeground = null;
4822 pr.foregroundServices = false;
4823 }
4824 updateOomAdjLocked();
4825 }
4826 }
4827
4828 public void setProcessForeground(IBinder token, int pid, boolean isForeground) {
4829 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
4830 "setProcessForeground()");
4831 synchronized(this) {
4832 boolean changed = false;
4833
4834 synchronized (mPidsSelfLocked) {
4835 ProcessRecord pr = mPidsSelfLocked.get(pid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004836 if (pr == null && isForeground) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004837 Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 return;
4839 }
4840 ForegroundToken oldToken = mForegroundProcesses.get(pid);
4841 if (oldToken != null) {
4842 oldToken.token.unlinkToDeath(oldToken, 0);
4843 mForegroundProcesses.remove(pid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004844 if (pr != null) {
4845 pr.forcingToForeground = null;
4846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 changed = true;
4848 }
4849 if (isForeground && token != null) {
4850 ForegroundToken newToken = new ForegroundToken() {
4851 public void binderDied() {
4852 foregroundTokenDied(this);
4853 }
4854 };
4855 newToken.pid = pid;
4856 newToken.token = token;
4857 try {
4858 token.linkToDeath(newToken, 0);
4859 mForegroundProcesses.put(pid, newToken);
4860 pr.forcingToForeground = token;
4861 changed = true;
4862 } catch (RemoteException e) {
4863 // If the process died while doing this, we will later
4864 // do the cleanup with the process death link.
4865 }
4866 }
4867 }
4868
4869 if (changed) {
4870 updateOomAdjLocked();
4871 }
4872 }
4873 }
4874
4875 // =========================================================
4876 // PERMISSIONS
4877 // =========================================================
4878
4879 static class PermissionController extends IPermissionController.Stub {
4880 ActivityManagerService mActivityManagerService;
4881 PermissionController(ActivityManagerService activityManagerService) {
4882 mActivityManagerService = activityManagerService;
4883 }
4884
4885 public boolean checkPermission(String permission, int pid, int uid) {
4886 return mActivityManagerService.checkPermission(permission, pid,
4887 uid) == PackageManager.PERMISSION_GRANTED;
4888 }
4889 }
4890
4891 /**
4892 * This can be called with or without the global lock held.
4893 */
4894 int checkComponentPermission(String permission, int pid, int uid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08004895 int owningUid, boolean exported) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 // We might be performing an operation on behalf of an indirect binder
4897 // invocation, e.g. via {@link #openContentUri}. Check and adjust the
4898 // client identity accordingly before proceeding.
4899 Identity tlsIdentity = sCallerIdentity.get();
4900 if (tlsIdentity != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004901 Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 + tlsIdentity.pid + "," + tlsIdentity.uid + "}");
4903 uid = tlsIdentity.uid;
4904 pid = tlsIdentity.pid;
4905 }
4906
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004907 if (pid == MY_PID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 return PackageManager.PERMISSION_GRANTED;
4909 }
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004910
4911 return ActivityManager.checkComponentPermission(permission, uid,
4912 owningUid, exported);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913 }
4914
4915 /**
4916 * As the only public entry point for permissions checking, this method
4917 * can enforce the semantic that requesting a check on a null global
4918 * permission is automatically denied. (Internally a null permission
4919 * string is used when calling {@link #checkComponentPermission} in cases
4920 * when only uid-based security is needed.)
4921 *
4922 * This can be called with or without the global lock held.
4923 */
4924 public int checkPermission(String permission, int pid, int uid) {
4925 if (permission == null) {
4926 return PackageManager.PERMISSION_DENIED;
4927 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004928 return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 }
4930
4931 /**
4932 * Binder IPC calls go through the public entry point.
4933 * This can be called with or without the global lock held.
4934 */
4935 int checkCallingPermission(String permission) {
4936 return checkPermission(permission,
4937 Binder.getCallingPid(),
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004938 UserHandle.getAppId(Binder.getCallingUid()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 }
4940
4941 /**
4942 * This can be called with or without the global lock held.
4943 */
4944 void enforceCallingPermission(String permission, String func) {
4945 if (checkCallingPermission(permission)
4946 == PackageManager.PERMISSION_GRANTED) {
4947 return;
4948 }
4949
4950 String msg = "Permission Denial: " + func + " from pid="
4951 + Binder.getCallingPid()
4952 + ", uid=" + Binder.getCallingUid()
4953 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004954 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 throw new SecurityException(msg);
4956 }
4957
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004958 /**
4959 * Determine if UID is holding permissions required to access {@link Uri} in
4960 * the given {@link ProviderInfo}. Final permission checking is always done
4961 * in {@link ContentProvider}.
4962 */
4963 private final boolean checkHoldingPermissionsLocked(
4964 IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004965 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4966 "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid);
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004967
4968 if (pi.applicationInfo.uid == uid) {
4969 return true;
4970 } else if (!pi.exported) {
4971 return false;
4972 }
4973
4974 boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0;
4975 boolean writeMet = (modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 try {
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004977 // check if target holds top-level <provider> permissions
4978 if (!readMet && pi.readPermission != null
4979 && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) {
4980 readMet = true;
4981 }
4982 if (!writeMet && pi.writePermission != null
4983 && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) {
4984 writeMet = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004986
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004987 // track if unprotected read/write is allowed; any denied
4988 // <path-permission> below removes this ability
4989 boolean allowDefaultRead = pi.readPermission == null;
4990 boolean allowDefaultWrite = pi.writePermission == null;
Dianne Hackborn48058e82010-09-27 16:53:23 -07004991
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004992 // check if target holds any <path-permission> that match uri
4993 final PathPermission[] pps = pi.pathPermissions;
4994 if (pps != null) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004995 final String path = uri.getPath();
4996 int i = pps.length;
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004997 while (i > 0 && (!readMet || !writeMet)) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004998 i--;
4999 PathPermission pp = pps[i];
Jeff Sharkey110a6b62012-03-12 11:12:41 -07005000 if (pp.match(path)) {
5001 if (!readMet) {
5002 final String pprperm = pp.getReadPermission();
5003 if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for "
5004 + pprperm + " for " + pp.getPath()
5005 + ": match=" + pp.match(path)
5006 + " check=" + pm.checkUidPermission(pprperm, uid));
5007 if (pprperm != null) {
5008 if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) {
5009 readMet = true;
5010 } else {
5011 allowDefaultRead = false;
5012 }
5013 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07005014 }
Jeff Sharkey110a6b62012-03-12 11:12:41 -07005015 if (!writeMet) {
5016 final String ppwperm = pp.getWritePermission();
5017 if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm "
5018 + ppwperm + " for " + pp.getPath()
5019 + ": match=" + pp.match(path)
5020 + " check=" + pm.checkUidPermission(ppwperm, uid));
5021 if (ppwperm != null) {
5022 if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) {
5023 writeMet = true;
5024 } else {
5025 allowDefaultWrite = false;
5026 }
5027 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07005028 }
5029 }
5030 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07005031 }
Jeff Sharkey110a6b62012-03-12 11:12:41 -07005032
5033 // grant unprotected <provider> read/write, if not blocked by
5034 // <path-permission> above
5035 if (allowDefaultRead) readMet = true;
5036 if (allowDefaultWrite) writeMet = true;
5037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 } catch (RemoteException e) {
5039 return false;
5040 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07005041
Jeff Sharkey110a6b62012-03-12 11:12:41 -07005042 return readMet && writeMet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 }
5044
5045 private final boolean checkUriPermissionLocked(Uri uri, int uid,
5046 int modeFlags) {
5047 // Root gets to do everything.
Jeff Brown10e89712011-07-08 18:52:57 -07005048 if (uid == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005049 return true;
5050 }
5051 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid);
5052 if (perms == null) return false;
5053 UriPermission perm = perms.get(uri);
5054 if (perm == null) return false;
5055 return (modeFlags&perm.modeFlags) == modeFlags;
5056 }
5057
5058 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005059 enforceNotIsolatedCaller("checkUriPermission");
5060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061 // Another redirected-binder-call permissions check as in
5062 // {@link checkComponentPermission}.
5063 Identity tlsIdentity = sCallerIdentity.get();
5064 if (tlsIdentity != null) {
5065 uid = tlsIdentity.uid;
5066 pid = tlsIdentity.pid;
5067 }
5068
5069 // Our own process gets to do everything.
5070 if (pid == MY_PID) {
5071 return PackageManager.PERMISSION_GRANTED;
5072 }
5073 synchronized(this) {
5074 return checkUriPermissionLocked(uri, uid, modeFlags)
5075 ? PackageManager.PERMISSION_GRANTED
5076 : PackageManager.PERMISSION_DENIED;
5077 }
5078 }
5079
Dianne Hackborn39792d22010-08-19 18:01:52 -07005080 /**
5081 * Check if the targetPkg can be granted permission to access uri by
5082 * the callingUid using the given modeFlags. Throws a security exception
5083 * if callingUid is not allowed to do this. Returns the uid of the target
5084 * if the URI permission grant should be performed; returns -1 if it is not
5085 * needed (for example targetPkg already has permission to access the URI).
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005086 * If you already know the uid of the target, you can supply it in
5087 * lastTargetUid else set that to -1.
Dianne Hackborn39792d22010-08-19 18:01:52 -07005088 */
5089 int checkGrantUriPermissionLocked(int callingUid, String targetPkg,
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005090 Uri uri, int modeFlags, int lastTargetUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005091 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5092 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5093 if (modeFlags == 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07005094 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 }
5096
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005097 if (targetPkg != null) {
5098 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
5099 "Checking grant " + targetPkg + " permission to " + uri);
5100 }
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005101
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005102 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103
5104 // If this is not a content: uri, we can't do anything with it.
5105 if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005106 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005107 "Can't grant URI permission for non-content URI: " + uri);
Dianne Hackborn39792d22010-08-19 18:01:52 -07005108 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 }
5110
5111 String name = uri.getAuthority();
5112 ProviderInfo pi = null;
Amith Yamasani742a6712011-05-04 14:49:28 -07005113 ContentProviderRecord cpr = mProviderMap.getProviderByName(name,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005114 UserHandle.getUserId(callingUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 if (cpr != null) {
5116 pi = cpr.info;
5117 } else {
5118 try {
5119 pi = pm.resolveContentProvider(name,
Dianne Hackborn11f0cb72012-09-16 17:12:34 -07005120 PackageManager.GET_URI_PERMISSION_PATTERNS,
5121 UserHandle.getUserId(callingUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122 } catch (RemoteException ex) {
5123 }
5124 }
5125 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07005126 Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString());
Dianne Hackborn39792d22010-08-19 18:01:52 -07005127 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 }
5129
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005130 int targetUid = lastTargetUid;
5131 if (targetUid < 0 && targetPkg != null) {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005132 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005133 targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid));
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005134 if (targetUid < 0) {
5135 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
5136 "Can't grant URI permission no uid for: " + targetPkg);
5137 return -1;
5138 }
5139 } catch (RemoteException ex) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07005140 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 }
5143
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005144 if (targetUid >= 0) {
5145 // First... does the target actually need this permission?
5146 if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) {
5147 // No need to grant the target this permission.
5148 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
5149 "Target " + targetPkg + " already has full permission to " + uri);
5150 return -1;
5151 }
5152 } else {
5153 // First... there is no target package, so can anyone access it?
5154 boolean allowed = pi.exported;
5155 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
5156 if (pi.readPermission != null) {
5157 allowed = false;
5158 }
5159 }
5160 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
5161 if (pi.writePermission != null) {
5162 allowed = false;
5163 }
5164 }
5165 if (allowed) {
5166 return -1;
5167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 }
5169
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005170 // Second... is the provider allowing granting of URI permissions?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 if (!pi.grantUriPermissions) {
5172 throw new SecurityException("Provider " + pi.packageName
5173 + "/" + pi.name
5174 + " does not allow granting of Uri permissions (uri "
5175 + uri + ")");
5176 }
5177 if (pi.uriPermissionPatterns != null) {
5178 final int N = pi.uriPermissionPatterns.length;
5179 boolean allowed = false;
5180 for (int i=0; i<N; i++) {
5181 if (pi.uriPermissionPatterns[i] != null
5182 && pi.uriPermissionPatterns[i].match(uri.getPath())) {
5183 allowed = true;
5184 break;
5185 }
5186 }
5187 if (!allowed) {
5188 throw new SecurityException("Provider " + pi.packageName
5189 + "/" + pi.name
5190 + " does not allow granting of permission to path of Uri "
5191 + uri);
5192 }
5193 }
5194
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005195 // Third... does the caller itself have permission to access
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 // this uri?
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005197 if (callingUid != Process.myUid()) {
5198 if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) {
5199 if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
5200 throw new SecurityException("Uid " + callingUid
5201 + " does not have permission to uri " + uri);
5202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 }
5204 }
5205
Dianne Hackborn39792d22010-08-19 18:01:52 -07005206 return targetUid;
5207 }
5208
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005209 public int checkGrantUriPermission(int callingUid, String targetPkg,
5210 Uri uri, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005211 enforceNotIsolatedCaller("checkGrantUriPermission");
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005212 synchronized(this) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005213 return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1);
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005214 }
5215 }
5216
Dianne Hackborn39792d22010-08-19 18:01:52 -07005217 void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg,
5218 Uri uri, int modeFlags, UriPermissionOwner owner) {
5219 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5220 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5221 if (modeFlags == 0) {
5222 return;
5223 }
5224
5225 // So here we are: the caller has the assumed permission
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 // to the uri, and the target doesn't. Let's now give this to
5227 // the target.
5228
Joe Onorato8a9b2202010-02-26 18:56:32 -08005229 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn39792d22010-08-19 18:01:52 -07005230 "Granting " + targetPkg + "/" + targetUid + " permission to " + uri);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 HashMap<Uri, UriPermission> targetUris
5233 = mGrantedUriPermissions.get(targetUid);
5234 if (targetUris == null) {
5235 targetUris = new HashMap<Uri, UriPermission>();
5236 mGrantedUriPermissions.put(targetUid, targetUris);
5237 }
5238
5239 UriPermission perm = targetUris.get(uri);
5240 if (perm == null) {
5241 perm = new UriPermission(targetUid, uri);
5242 targetUris.put(uri, perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07005244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 perm.modeFlags |= modeFlags;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005246 if (owner == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 perm.globalModeFlags |= modeFlags;
Vairavan Srinivasan91c12c22011-01-21 18:26:06 -08005248 } else {
5249 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
5250 perm.readOwners.add(owner);
5251 owner.addReadPermission(perm);
5252 }
5253 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
5254 perm.writeOwners.add(owner);
5255 owner.addWritePermission(perm);
5256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 }
5258 }
5259
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005260 void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri,
5261 int modeFlags, UriPermissionOwner owner) {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005262 if (targetPkg == null) {
5263 throw new NullPointerException("targetPkg");
5264 }
5265
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005266 int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1);
Dianne Hackborn39792d22010-08-19 18:01:52 -07005267 if (targetUid < 0) {
5268 return;
5269 }
5270
5271 grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner);
5272 }
5273
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005274 static class NeededUriGrants extends ArrayList<Uri> {
5275 final String targetPkg;
5276 final int targetUid;
5277 final int flags;
5278
5279 NeededUriGrants(String _targetPkg, int _targetUid, int _flags) {
5280 targetPkg = _targetPkg;
5281 targetUid = _targetUid;
5282 flags = _flags;
5283 }
5284 }
5285
Dianne Hackborn39792d22010-08-19 18:01:52 -07005286 /**
5287 * Like checkGrantUriPermissionLocked, but takes an Intent.
5288 */
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005289 NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid,
5290 String targetPkg, Intent intent, int mode, NeededUriGrants needed) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07005291 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005292 "Checking URI perm to data=" + (intent != null ? intent.getData() : null)
5293 + " clip=" + (intent != null ? intent.getClipData() : null)
Dianne Hackbornb424b632010-08-18 15:59:05 -07005294 + " from " + intent + "; flags=0x"
5295 + Integer.toHexString(intent != null ? intent.getFlags() : 0));
5296
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005297 if (targetPkg == null) {
5298 throw new NullPointerException("targetPkg");
5299 }
5300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 if (intent == null) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005302 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 }
5304 Uri data = intent.getData();
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005305 ClipData clip = intent.getClipData();
5306 if (data == null && clip == null) {
5307 return null;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005308 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005309 if (data != null) {
5310 int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data,
5311 mode, needed != null ? needed.targetUid : -1);
5312 if (target > 0) {
5313 if (needed == null) {
5314 needed = new NeededUriGrants(targetPkg, target, mode);
5315 }
5316 needed.add(data);
5317 }
5318 }
5319 if (clip != null) {
5320 for (int i=0; i<clip.getItemCount(); i++) {
5321 Uri uri = clip.getItemAt(i).getUri();
5322 if (uri != null) {
5323 int target = -1;
5324 target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri,
5325 mode, needed != null ? needed.targetUid : -1);
5326 if (target > 0) {
5327 if (needed == null) {
5328 needed = new NeededUriGrants(targetPkg, target, mode);
5329 }
5330 needed.add(uri);
5331 }
5332 } else {
5333 Intent clipIntent = clip.getItemAt(i).getIntent();
5334 if (clipIntent != null) {
5335 NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked(
5336 callingUid, targetPkg, clipIntent, mode, needed);
5337 if (newNeeded != null) {
5338 needed = newNeeded;
5339 }
5340 }
5341 }
5342 }
5343 }
5344
5345 return needed;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005346 }
5347
5348 /**
5349 * Like grantUriPermissionUncheckedLocked, but takes an Intent.
5350 */
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005351 void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed,
5352 UriPermissionOwner owner) {
5353 if (needed != null) {
5354 for (int i=0; i<needed.size(); i++) {
5355 grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg,
5356 needed.get(i), needed.flags, owner);
5357 }
5358 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07005359 }
5360
5361 void grantUriPermissionFromIntentLocked(int callingUid,
5362 String targetPkg, Intent intent, UriPermissionOwner owner) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005363 NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg,
Dianne Hackbornd9781fe2012-03-08 18:04:18 -08005364 intent, intent != null ? intent.getFlags() : 0, null);
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005365 if (needed == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 return;
5367 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07005368
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005369 grantUriPermissionUncheckedFromIntentLocked(needed, owner);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 }
5371
5372 public void grantUriPermission(IApplicationThread caller, String targetPkg,
5373 Uri uri, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005374 enforceNotIsolatedCaller("grantUriPermission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 synchronized(this) {
5376 final ProcessRecord r = getRecordForAppLocked(caller);
5377 if (r == null) {
5378 throw new SecurityException("Unable to find app for caller "
5379 + caller
5380 + " when granting permission to uri " + uri);
5381 }
5382 if (targetPkg == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -07005383 throw new IllegalArgumentException("null target");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 }
5385 if (uri == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -07005386 throw new IllegalArgumentException("null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 }
5388
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005389 grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 null);
5391 }
5392 }
5393
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005394 void removeUriPermissionIfNeededLocked(UriPermission perm) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005395 if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION
5396 |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) {
5397 HashMap<Uri, UriPermission> perms
5398 = mGrantedUriPermissions.get(perm.uid);
5399 if (perms != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005400 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005401 "Removing " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 perms.remove(perm.uri);
5403 if (perms.size() == 0) {
5404 mGrantedUriPermissions.remove(perm.uid);
5405 }
5406 }
5407 }
5408 }
5409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 private void revokeUriPermissionLocked(int callingUid, Uri uri,
5411 int modeFlags) {
5412 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5413 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5414 if (modeFlags == 0) {
5415 return;
5416 }
5417
Joe Onorato8a9b2202010-02-26 18:56:32 -08005418 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005419 "Revoking all granted permissions to " + uri);
5420
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005421 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422
5423 final String authority = uri.getAuthority();
5424 ProviderInfo pi = null;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005425 int userId = UserHandle.getUserId(callingUid);
Amith Yamasani483f3b02012-03-13 16:08:00 -07005426 ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 if (cpr != null) {
5428 pi = cpr.info;
5429 } else {
5430 try {
5431 pi = pm.resolveContentProvider(authority,
Amith Yamasani483f3b02012-03-13 16:08:00 -07005432 PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 } catch (RemoteException ex) {
5434 }
5435 }
5436 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07005437 Slog.w(TAG, "No content provider found for permission revoke: " + uri.toSafeString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 return;
5439 }
5440
5441 // Does the caller have this permission on the URI?
Dianne Hackborn48058e82010-09-27 16:53:23 -07005442 if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 // Right now, if you are not the original owner of the permission,
5444 // you are not allowed to revoke it.
5445 //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
5446 throw new SecurityException("Uid " + callingUid
5447 + " does not have permission to uri " + uri);
5448 //}
5449 }
5450
5451 // Go through all of the permissions and remove any that match.
5452 final List<String> SEGMENTS = uri.getPathSegments();
5453 if (SEGMENTS != null) {
5454 final int NS = SEGMENTS.size();
5455 int N = mGrantedUriPermissions.size();
5456 for (int i=0; i<N; i++) {
5457 HashMap<Uri, UriPermission> perms
5458 = mGrantedUriPermissions.valueAt(i);
5459 Iterator<UriPermission> it = perms.values().iterator();
5460 toploop:
5461 while (it.hasNext()) {
5462 UriPermission perm = it.next();
5463 Uri targetUri = perm.uri;
5464 if (!authority.equals(targetUri.getAuthority())) {
5465 continue;
5466 }
5467 List<String> targetSegments = targetUri.getPathSegments();
5468 if (targetSegments == null) {
5469 continue;
5470 }
5471 if (targetSegments.size() < NS) {
5472 continue;
5473 }
5474 for (int j=0; j<NS; j++) {
5475 if (!SEGMENTS.get(j).equals(targetSegments.get(j))) {
5476 continue toploop;
5477 }
5478 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005479 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005480 "Revoking " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 perm.clearModes(modeFlags);
5482 if (perm.modeFlags == 0) {
5483 it.remove();
5484 }
5485 }
5486 if (perms.size() == 0) {
5487 mGrantedUriPermissions.remove(
5488 mGrantedUriPermissions.keyAt(i));
5489 N--;
5490 i--;
5491 }
5492 }
5493 }
5494 }
5495
5496 public void revokeUriPermission(IApplicationThread caller, Uri uri,
5497 int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005498 enforceNotIsolatedCaller("revokeUriPermission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 synchronized(this) {
5500 final ProcessRecord r = getRecordForAppLocked(caller);
5501 if (r == null) {
5502 throw new SecurityException("Unable to find app for caller "
5503 + caller
5504 + " when revoking permission to uri " + uri);
5505 }
5506 if (uri == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005507 Slog.w(TAG, "revokeUriPermission: null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 return;
5509 }
5510
5511 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5512 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5513 if (modeFlags == 0) {
5514 return;
5515 }
5516
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005517 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518
5519 final String authority = uri.getAuthority();
5520 ProviderInfo pi = null;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005521 ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 if (cpr != null) {
5523 pi = cpr.info;
5524 } else {
5525 try {
5526 pi = pm.resolveContentProvider(authority,
Amith Yamasani483f3b02012-03-13 16:08:00 -07005527 PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 } catch (RemoteException ex) {
5529 }
5530 }
5531 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07005532 Slog.w(TAG, "No content provider found for permission revoke: "
5533 + uri.toSafeString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 return;
5535 }
5536
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005537 revokeUriPermissionLocked(r.uid, uri, modeFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 }
5539 }
5540
Dianne Hackborn7e269642010-08-25 19:50:20 -07005541 @Override
5542 public IBinder newUriPermissionOwner(String name) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005543 enforceNotIsolatedCaller("newUriPermissionOwner");
Dianne Hackborn7e269642010-08-25 19:50:20 -07005544 synchronized(this) {
5545 UriPermissionOwner owner = new UriPermissionOwner(this, name);
5546 return owner.getExternalTokenLocked();
5547 }
5548 }
5549
5550 @Override
5551 public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg,
5552 Uri uri, int modeFlags) {
5553 synchronized(this) {
5554 UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
5555 if (owner == null) {
5556 throw new IllegalArgumentException("Unknown owner: " + token);
5557 }
5558 if (fromUid != Binder.getCallingUid()) {
5559 if (Binder.getCallingUid() != Process.myUid()) {
5560 // Only system code can grant URI permissions on behalf
5561 // of other users.
5562 throw new SecurityException("nice try");
5563 }
5564 }
5565 if (targetPkg == null) {
5566 throw new IllegalArgumentException("null target");
5567 }
5568 if (uri == null) {
5569 throw new IllegalArgumentException("null uri");
5570 }
5571
5572 grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner);
5573 }
5574 }
5575
5576 @Override
5577 public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) {
5578 synchronized(this) {
5579 UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
5580 if (owner == null) {
5581 throw new IllegalArgumentException("Unknown owner: " + token);
5582 }
5583
5584 if (uri == null) {
5585 owner.removeUriPermissionsLocked(mode);
5586 } else {
5587 owner.removeUriPermissionLocked(uri, mode);
5588 }
5589 }
5590 }
5591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 public void showWaitingForDebugger(IApplicationThread who, boolean waiting) {
5593 synchronized (this) {
5594 ProcessRecord app =
5595 who != null ? getRecordForAppLocked(who) : null;
5596 if (app == null) return;
5597
5598 Message msg = Message.obtain();
5599 msg.what = WAIT_FOR_DEBUGGER_MSG;
5600 msg.obj = app;
5601 msg.arg1 = waiting ? 1 : 0;
5602 mHandler.sendMessage(msg);
5603 }
5604 }
5605
5606 public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07005607 final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ);
5608 final long hiddenAppMem = mProcessList.getMemLevel(ProcessList.HIDDEN_APP_MIN_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 outInfo.availMem = Process.getFreeMemory();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07005610 outInfo.totalMem = Process.getTotalMemory();
Dianne Hackborn7d608422011-08-07 16:24:18 -07005611 outInfo.threshold = homeAppMem;
5612 outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2));
5613 outInfo.hiddenAppThreshold = hiddenAppMem;
5614 outInfo.secondaryServerThreshold = mProcessList.getMemLevel(
Dianne Hackborne02c88a2011-10-28 13:58:15 -07005615 ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07005616 outInfo.visibleAppThreshold = mProcessList.getMemLevel(
5617 ProcessList.VISIBLE_APP_ADJ);
5618 outInfo.foregroundAppThreshold = mProcessList.getMemLevel(
5619 ProcessList.FOREGROUND_APP_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 }
5621
5622 // =========================================================
5623 // TASK MANAGEMENT
5624 // =========================================================
5625
5626 public List getTasks(int maxNum, int flags,
5627 IThumbnailReceiver receiver) {
5628 ArrayList list = new ArrayList();
5629
5630 PendingThumbnailsRecord pending = null;
5631 IApplicationThread topThumbnail = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005632 ActivityRecord topRecord = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633
5634 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005635 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 TAG, "getTasks: max=" + maxNum + ", flags=" + flags
5637 + ", receiver=" + receiver);
5638
5639 if (checkCallingPermission(android.Manifest.permission.GET_TASKS)
5640 != PackageManager.PERMISSION_GRANTED) {
5641 if (receiver != null) {
5642 // If the caller wants to wait for pending thumbnails,
5643 // it ain't gonna get them.
5644 try {
5645 receiver.finished();
5646 } catch (RemoteException ex) {
5647 }
5648 }
5649 String msg = "Permission Denial: getTasks() from pid="
5650 + Binder.getCallingPid()
5651 + ", uid=" + Binder.getCallingUid()
5652 + " requires " + android.Manifest.permission.GET_TASKS;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005653 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 throw new SecurityException(msg);
5655 }
5656
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005657 int pos = mMainStack.mHistory.size()-1;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005658 ActivityRecord next =
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005659 pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005660 ActivityRecord top = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 TaskRecord curTask = null;
5662 int numActivities = 0;
5663 int numRunning = 0;
5664 while (pos >= 0 && maxNum > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005665 final ActivityRecord r = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 pos--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005667 next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668
5669 // Initialize state for next task if needed.
5670 if (top == null ||
5671 (top.state == ActivityState.INITIALIZING
5672 && top.task == r.task)) {
5673 top = r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 curTask = r.task;
5675 numActivities = numRunning = 0;
5676 }
5677
5678 // Add 'r' into the current task.
5679 numActivities++;
5680 if (r.app != null && r.app.thread != null) {
5681 numRunning++;
5682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683
Joe Onorato8a9b2202010-02-26 18:56:32 -08005684 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 TAG, r.intent.getComponent().flattenToShortString()
5686 + ": task=" + r.task);
5687
5688 // If the next one is a different task, generate a new
5689 // TaskInfo entry for what we have.
5690 if (next == null || next.task != curTask) {
5691 ActivityManager.RunningTaskInfo ci
5692 = new ActivityManager.RunningTaskInfo();
5693 ci.id = curTask.taskId;
5694 ci.baseActivity = r.intent.getComponent();
5695 ci.topActivity = top.intent.getComponent();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07005696 if (top.thumbHolder != null) {
5697 ci.description = top.thumbHolder.lastDescription;
5698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 ci.numActivities = numActivities;
5700 ci.numRunning = numRunning;
5701 //System.out.println(
5702 // "#" + maxNum + ": " + " descr=" + ci.description);
5703 if (ci.thumbnail == null && receiver != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005704 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 TAG, "State=" + top.state + "Idle=" + top.idle
5706 + " app=" + top.app
5707 + " thr=" + (top.app != null ? top.app.thread : null));
5708 if (top.state == ActivityState.RESUMED
5709 || top.state == ActivityState.PAUSING) {
5710 if (top.idle && top.app != null
5711 && top.app.thread != null) {
5712 topRecord = top;
5713 topThumbnail = top.app.thread;
5714 } else {
5715 top.thumbnailNeeded = true;
5716 }
5717 }
5718 if (pending == null) {
5719 pending = new PendingThumbnailsRecord(receiver);
5720 }
5721 pending.pendingRecords.add(top);
5722 }
5723 list.add(ci);
5724 maxNum--;
5725 top = null;
5726 }
5727 }
5728
5729 if (pending != null) {
5730 mPendingThumbnails.add(pending);
5731 }
5732 }
5733
Joe Onorato8a9b2202010-02-26 18:56:32 -08005734 if (localLOGV) Slog.v(TAG, "We have pending thumbnails: " + pending);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735
5736 if (topThumbnail != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005737 if (localLOGV) Slog.v(TAG, "Requesting top thumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08005739 topThumbnail.requestThumbnail(topRecord.appToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005741 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08005742 sendPendingThumbnail(null, topRecord.appToken, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 }
5744 }
5745
5746 if (pending == null && receiver != null) {
5747 // In this case all thumbnails were available and the client
5748 // is being asked to be told when the remaining ones come in...
5749 // which is unusually, since the top-most currently running
5750 // activity should never have a canned thumbnail! Oh well.
5751 try {
5752 receiver.finished();
5753 } catch (RemoteException ex) {
5754 }
5755 }
5756
5757 return list;
5758 }
5759
5760 public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
Amith Yamasani82644082012-08-03 13:09:11 -07005761 int flags, int userId) {
Amith Yamasani742a6712011-05-04 14:49:28 -07005762 final int callingUid = Binder.getCallingUid();
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005763 if (userId != UserHandle.getCallingUserId()) {
Amith Yamasani82644082012-08-03 13:09:11 -07005764 // Check if the caller is holding permissions for cross-user requests.
5765 if (checkComponentPermission(
5766 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
5767 Binder.getCallingPid(), callingUid, -1, true)
5768 != PackageManager.PERMISSION_GRANTED) {
5769 String msg = "Permission Denial: "
5770 + "Request to get recent tasks for user " + userId
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005771 + " but is calling from user " + UserHandle.getUserId(callingUid)
Amith Yamasani82644082012-08-03 13:09:11 -07005772 + "; this requires "
5773 + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
5774 Slog.w(TAG, msg);
5775 throw new SecurityException(msg);
5776 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005777 if (userId == UserHandle.USER_CURRENT) {
Amith Yamasani82644082012-08-03 13:09:11 -07005778 userId = mCurrentUserId;
5779 }
5780 }
5781 }
5782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 synchronized (this) {
5784 enforceCallingPermission(android.Manifest.permission.GET_TASKS,
5785 "getRecentTasks()");
Dianne Hackborn8238e712012-04-24 11:15:40 -07005786 final boolean detailed = checkCallingPermission(
5787 android.Manifest.permission.GET_DETAILED_TASKS)
5788 == PackageManager.PERMISSION_GRANTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005790 IPackageManager pm = AppGlobals.getPackageManager();
Amith Yamasani82644082012-08-03 13:09:11 -07005791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 final int N = mRecentTasks.size();
5793 ArrayList<ActivityManager.RecentTaskInfo> res
5794 = new ArrayList<ActivityManager.RecentTaskInfo>(
5795 maxNum < N ? maxNum : N);
5796 for (int i=0; i<N && maxNum > 0; i++) {
5797 TaskRecord tr = mRecentTasks.get(i);
Amith Yamasani742a6712011-05-04 14:49:28 -07005798 // Only add calling user's recent tasks
Amith Yamasani82644082012-08-03 13:09:11 -07005799 if (tr.userId != userId) continue;
Dianne Hackborn905577f2011-09-07 18:31:28 -07005800 // Return the entry if desired by the caller. We always return
5801 // the first entry, because callers always expect this to be the
Amith Yamasani742a6712011-05-04 14:49:28 -07005802 // foreground app. We may filter others if the caller has
Dianne Hackborn905577f2011-09-07 18:31:28 -07005803 // not supplied RECENT_WITH_EXCLUDED and there is some reason
5804 // we should exclude the entry.
Amith Yamasani742a6712011-05-04 14:49:28 -07005805
Dianne Hackborn905577f2011-09-07 18:31:28 -07005806 if (i == 0
5807 || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 || (tr.intent == null)
5809 || ((tr.intent.getFlags()
5810 &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) {
5811 ActivityManager.RecentTaskInfo rti
5812 = new ActivityManager.RecentTaskInfo();
5813 rti.id = tr.numActivities > 0 ? tr.taskId : -1;
Dianne Hackbornd94df452011-02-16 18:53:31 -08005814 rti.persistentId = tr.taskId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 rti.baseIntent = new Intent(
5816 tr.intent != null ? tr.intent : tr.affinityIntent);
Dianne Hackborn8238e712012-04-24 11:15:40 -07005817 if (!detailed) {
5818 rti.baseIntent.replaceExtras((Bundle)null);
5819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 rti.origActivity = tr.origActivity;
Dianne Hackbornd2835932010-12-13 16:28:46 -08005821 rti.description = tr.lastDescription;
5822
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005823 if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) {
5824 // Check whether this activity is currently available.
5825 try {
5826 if (rti.origActivity != null) {
Amith Yamasani82644082012-08-03 13:09:11 -07005827 if (pm.getActivityInfo(rti.origActivity, 0, userId)
Amith Yamasani483f3b02012-03-13 16:08:00 -07005828 == null) {
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005829 continue;
5830 }
5831 } else if (rti.baseIntent != null) {
5832 if (pm.queryIntentActivities(rti.baseIntent,
Amith Yamasani82644082012-08-03 13:09:11 -07005833 null, 0, userId) == null) {
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005834 continue;
5835 }
5836 }
5837 } catch (RemoteException e) {
5838 // Will never happen.
5839 }
5840 }
5841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 res.add(rti);
5843 maxNum--;
5844 }
5845 }
5846 return res;
5847 }
5848 }
5849
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005850 private TaskRecord taskForIdLocked(int id) {
5851 final int N = mRecentTasks.size();
5852 for (int i=0; i<N; i++) {
5853 TaskRecord tr = mRecentTasks.get(i);
5854 if (tr.taskId == id) {
5855 return tr;
Dianne Hackbornd94df452011-02-16 18:53:31 -08005856 }
5857 }
5858 return null;
5859 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005860
5861 public ActivityManager.TaskThumbnails getTaskThumbnails(int id) {
5862 synchronized (this) {
5863 enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER,
5864 "getTaskThumbnails()");
5865 TaskRecord tr = taskForIdLocked(id);
5866 if (tr != null) {
5867 return mMainStack.getTaskThumbnailsLocked(tr);
5868 }
5869 }
5870 return null;
5871 }
5872
Dianne Hackborn15491c62012-09-19 10:59:14 -07005873 public Bitmap getTaskTopThumbnail(int id) {
5874 synchronized (this) {
5875 enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER,
5876 "getTaskTopThumbnail()");
5877 TaskRecord tr = taskForIdLocked(id);
5878 if (tr != null) {
5879 return mMainStack.getTaskTopThumbnailLocked(tr);
5880 }
5881 }
5882 return null;
5883 }
5884
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005885 public boolean removeSubTask(int taskId, int subTaskIndex) {
5886 synchronized (this) {
5887 enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS,
5888 "removeSubTask()");
5889 long ident = Binder.clearCallingIdentity();
5890 try {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005891 return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex,
5892 true) != null;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005893 } finally {
5894 Binder.restoreCallingIdentity(ident);
5895 }
5896 }
5897 }
5898
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005899 private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) {
5900 final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005901 Intent baseIntent = new Intent(
5902 tr.intent != null ? tr.intent : tr.affinityIntent);
5903 ComponentName component = baseIntent.getComponent();
5904 if (component == null) {
5905 Slog.w(TAG, "Now component for base intent of task: " + tr);
5906 return;
5907 }
5908
5909 // Find any running services associated with this app.
Dianne Hackborn599db5c2012-08-03 19:28:48 -07005910 mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005911
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005912 if (killProcesses) {
5913 // Find any running processes associated with this app.
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005914 final String pkg = component.getPackageName();
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005915 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005916 HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap();
5917 for (SparseArray<ProcessRecord> uids : pmap.values()) {
5918 for (int i=0; i<uids.size(); i++) {
5919 ProcessRecord proc = uids.valueAt(i);
5920 if (proc.userId != tr.userId) {
5921 continue;
5922 }
5923 if (!proc.pkgList.contains(pkg)) {
5924 continue;
5925 }
5926 procs.add(proc);
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005927 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005928 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005929
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005930 // Kill the running processes.
5931 for (int i=0; i<procs.size(); i++) {
5932 ProcessRecord pr = procs.get(i);
5933 if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
5934 Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task");
5935 EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid,
5936 pr.processName, pr.setAdj, "remove task");
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005937 pr.killedBackground = true;
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005938 Process.killProcessQuiet(pr.pid);
5939 } else {
5940 pr.waitingToKill = "remove task";
5941 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005942 }
5943 }
5944 }
5945
5946 public boolean removeTask(int taskId, int flags) {
5947 synchronized (this) {
5948 enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS,
5949 "removeTask()");
5950 long ident = Binder.clearCallingIdentity();
5951 try {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005952 ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1,
5953 false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005954 if (r != null) {
5955 mRecentTasks.remove(r.task);
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005956 cleanUpRemovedTaskLocked(r.task, flags);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005957 return true;
Dianne Hackborneeb1dca2011-09-08 13:30:11 -07005958 } else {
5959 TaskRecord tr = null;
5960 int i=0;
5961 while (i < mRecentTasks.size()) {
5962 TaskRecord t = mRecentTasks.get(i);
5963 if (t.taskId == taskId) {
5964 tr = t;
5965 break;
5966 }
5967 i++;
5968 }
5969 if (tr != null) {
5970 if (tr.numActivities <= 0) {
5971 // Caller is just removing a recent task that is
5972 // not actively running. That is easy!
5973 mRecentTasks.remove(i);
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005974 cleanUpRemovedTaskLocked(tr, flags);
5975 return true;
Dianne Hackborneeb1dca2011-09-08 13:30:11 -07005976 } else {
5977 Slog.w(TAG, "removeTask: task " + taskId
5978 + " does not have activities to remove, "
5979 + " but numActivities=" + tr.numActivities
5980 + ": " + tr);
5981 }
5982 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005983 }
5984 } finally {
5985 Binder.restoreCallingIdentity(ident);
5986 }
5987 }
5988 return false;
5989 }
Dianne Hackbornd94df452011-02-16 18:53:31 -08005990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 private final int findAffinityTaskTopLocked(int startIndex, String affinity) {
5992 int j;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005993 TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 TaskRecord jt = startTask;
5995
5996 // First look backwards
5997 for (j=startIndex-1; j>=0; j--) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005998 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 if (r.task != jt) {
6000 jt = r.task;
6001 if (affinity.equals(jt.affinity)) {
6002 return j;
6003 }
6004 }
6005 }
6006
6007 // Now look forwards
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006008 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 jt = startTask;
6010 for (j=startIndex+1; j<N; j++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006011 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 if (r.task != jt) {
6013 if (affinity.equals(jt.affinity)) {
6014 return j;
6015 }
6016 jt = r.task;
6017 }
6018 }
6019
6020 // Might it be at the top?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006021 if (affinity.equals(((ActivityRecord)mMainStack.mHistory.get(N-1)).task.affinity)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 return N-1;
6023 }
6024
6025 return -1;
6026 }
6027
6028 /**
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006029 * TODO: Add mController hook
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 */
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07006031 public void moveTaskToFront(int task, int flags, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
6033 "moveTaskToFront()");
6034
6035 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07006036 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
6037 Binder.getCallingUid(), "Task to front")) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07006038 ActivityOptions.abort(options);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07006039 return;
6040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 final long origId = Binder.clearCallingIdentity();
6042 try {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07006043 TaskRecord tr = taskForIdLocked(task);
6044 if (tr != null) {
6045 if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
6046 mMainStack.mUserLeaving = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07006048 if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
6049 // Caller wants the home activity moved with it. To accomplish this,
6050 // we'll just move the home task to the top first.
6051 mMainStack.moveHomeToFrontLocked();
6052 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07006053 mMainStack.moveTaskToFrontLocked(tr, null, options);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07006054 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006056 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
6057 ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006058 if (hr.task.taskId == task) {
Dianne Hackbornd94df452011-02-16 18:53:31 -08006059 if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
6060 mMainStack.mUserLeaving = true;
6061 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08006062 if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
6063 // Caller wants the home activity moved with it. To accomplish this,
6064 // we'll just move the home task to the top first.
6065 mMainStack.moveHomeToFrontLocked();
6066 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07006067 mMainStack.moveTaskToFrontLocked(hr.task, null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 return;
6069 }
6070 }
6071 } finally {
6072 Binder.restoreCallingIdentity(origId);
6073 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07006074 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 }
6076 }
6077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 public void moveTaskToBack(int task) {
6079 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
6080 "moveTaskToBack()");
6081
6082 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006083 if (mMainStack.mResumedActivity != null
6084 && mMainStack.mResumedActivity.task.taskId == task) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07006085 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
6086 Binder.getCallingUid(), "Task to back")) {
6087 return;
6088 }
6089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006091 mMainStack.moveTaskToBackLocked(task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 Binder.restoreCallingIdentity(origId);
6093 }
6094 }
6095
6096 /**
6097 * Moves an activity, and all of the other activities within the same task, to the bottom
6098 * of the history stack. The activity's order within the task is unchanged.
6099 *
6100 * @param token A reference to the activity we wish to move
6101 * @param nonRoot If false then this only works if the activity is the root
6102 * of a task; if true it will work for any activity in a task.
6103 * @return Returns true if the move completed, false if not.
6104 */
6105 public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006106 enforceNotIsolatedCaller("moveActivityTaskToBack");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 synchronized(this) {
6108 final long origId = Binder.clearCallingIdentity();
6109 int taskId = getTaskForActivityLocked(token, !nonRoot);
6110 if (taskId >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006111 return mMainStack.moveTaskToBackLocked(taskId, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 }
6113 Binder.restoreCallingIdentity(origId);
6114 }
6115 return false;
6116 }
6117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 public void moveTaskBackwards(int task) {
6119 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
6120 "moveTaskBackwards()");
6121
6122 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07006123 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
6124 Binder.getCallingUid(), "Task backwards")) {
6125 return;
6126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 final long origId = Binder.clearCallingIdentity();
6128 moveTaskBackwardsLocked(task);
6129 Binder.restoreCallingIdentity(origId);
6130 }
6131 }
6132
6133 private final void moveTaskBackwardsLocked(int task) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006134 Slog.e(TAG, "moveTaskBackwards not yet implemented!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135 }
6136
6137 public int getTaskForActivity(IBinder token, boolean onlyRoot) {
6138 synchronized(this) {
6139 return getTaskForActivityLocked(token, onlyRoot);
6140 }
6141 }
6142
6143 int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006144 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 TaskRecord lastTask = null;
6146 for (int i=0; i<N; i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006147 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackbornbe707852011-11-11 14:32:10 -08006148 if (r.appToken == token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 if (!onlyRoot || lastTask != r.task) {
6150 return r.task.taskId;
6151 }
6152 return -1;
6153 }
6154 lastTask = r.task;
6155 }
6156
6157 return -1;
6158 }
6159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006160 // =========================================================
6161 // THUMBNAILS
6162 // =========================================================
6163
6164 public void reportThumbnail(IBinder token,
6165 Bitmap thumbnail, CharSequence description) {
6166 //System.out.println("Report thumbnail for " + token + ": " + thumbnail);
6167 final long origId = Binder.clearCallingIdentity();
6168 sendPendingThumbnail(null, token, thumbnail, description, true);
6169 Binder.restoreCallingIdentity(origId);
6170 }
6171
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006172 final void sendPendingThumbnail(ActivityRecord r, IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006173 Bitmap thumbnail, CharSequence description, boolean always) {
6174 TaskRecord task = null;
6175 ArrayList receivers = null;
6176
6177 //System.out.println("Send pending thumbnail: " + r);
6178
6179 synchronized(this) {
6180 if (r == null) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07006181 r = mMainStack.isInStackLocked(token);
6182 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 return;
6184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07006186 if (thumbnail == null && r.thumbHolder != null) {
6187 thumbnail = r.thumbHolder.lastThumbnail;
6188 description = r.thumbHolder.lastDescription;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 }
6190 if (thumbnail == null && !always) {
6191 // If there is no thumbnail, and this entry is not actually
6192 // going away, then abort for now and pick up the next
6193 // thumbnail we get.
6194 return;
6195 }
6196 task = r.task;
6197
6198 int N = mPendingThumbnails.size();
6199 int i=0;
6200 while (i<N) {
6201 PendingThumbnailsRecord pr =
6202 (PendingThumbnailsRecord)mPendingThumbnails.get(i);
6203 //System.out.println("Looking in " + pr.pendingRecords);
6204 if (pr.pendingRecords.remove(r)) {
6205 if (receivers == null) {
6206 receivers = new ArrayList();
6207 }
6208 receivers.add(pr);
6209 if (pr.pendingRecords.size() == 0) {
6210 pr.finished = true;
6211 mPendingThumbnails.remove(i);
6212 N--;
6213 continue;
6214 }
6215 }
6216 i++;
6217 }
6218 }
6219
6220 if (receivers != null) {
6221 final int N = receivers.size();
6222 for (int i=0; i<N; i++) {
6223 try {
6224 PendingThumbnailsRecord pr =
6225 (PendingThumbnailsRecord)receivers.get(i);
6226 pr.receiver.newThumbnail(
6227 task != null ? task.taskId : -1, thumbnail, description);
6228 if (pr.finished) {
6229 pr.receiver.finished();
6230 }
6231 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006232 Slog.w(TAG, "Exception thrown when sending thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 }
6234 }
6235 }
6236 }
6237
6238 // =========================================================
6239 // CONTENT PROVIDERS
6240 // =========================================================
6241
Jeff Brown10e89712011-07-08 18:52:57 -07006242 private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) {
6243 List<ProviderInfo> providers = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006245 providers = AppGlobals.getPackageManager().
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006246 queryContentProviders(app.processName, app.uid,
Dianne Hackborn1655be42009-05-08 14:29:01 -07006247 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 } catch (RemoteException ex) {
6249 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006250 if (DEBUG_MU)
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006251 Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid);
6252 int userId = app.userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 if (providers != null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006254 int N = providers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 for (int i=0; i<N; i++) {
6256 ProviderInfo cpi =
6257 (ProviderInfo)providers.get(i);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006258 boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo,
6259 cpi.name, cpi.flags);
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006260 if (singleton && UserHandle.getUserId(app.uid) != 0) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006261 // This is a singleton provider, but a user besides the
6262 // default user is asking to initialize a process it runs
6263 // in... well, no, it doesn't actually run in this process,
6264 // it runs in the process of the default user. Get rid of it.
6265 providers.remove(i);
6266 N--;
6267 continue;
6268 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006269
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006270 ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006271 ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 if (cpr == null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006273 cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton);
Amith Yamasani742a6712011-05-04 14:49:28 -07006274 mProviderMap.putProviderByClass(comp, cpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006276 if (DEBUG_MU)
6277 Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 app.pubProviders.put(cpi.name, cpr);
6279 app.addPackage(cpi.applicationInfo.packageName);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07006280 ensurePackageDexOpt(cpi.applicationInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 }
6282 }
6283 return providers;
6284 }
6285
Jeff Sharkey110a6b62012-03-12 11:12:41 -07006286 /**
6287 * Check if {@link ProcessRecord} has a possible chance at accessing the
6288 * given {@link ProviderInfo}. Final permission checking is always done
6289 * in {@link ContentProvider}.
6290 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006291 private final String checkContentProviderPermissionLocked(
Dianne Hackbornb424b632010-08-18 15:59:05 -07006292 ProviderInfo cpi, ProcessRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006293 final int callingPid = (r != null) ? r.pid : Binder.getCallingPid();
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006294 final int callingUid = (r != null) ? r.uid : Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 if (checkComponentPermission(cpi.readPermission, callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006296 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackbornb424b632010-08-18 15:59:05 -07006297 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 return null;
6299 }
6300 if (checkComponentPermission(cpi.writePermission, callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006301 cpi.applicationInfo.uid, cpi.exported)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 == PackageManager.PERMISSION_GRANTED) {
6303 return null;
6304 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006305
6306 PathPermission[] pps = cpi.pathPermissions;
6307 if (pps != null) {
6308 int i = pps.length;
6309 while (i > 0) {
6310 i--;
6311 PathPermission pp = pps[i];
6312 if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006313 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackbornb424b632010-08-18 15:59:05 -07006314 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006315 return null;
6316 }
6317 if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006318 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006319 == PackageManager.PERMISSION_GRANTED) {
6320 return null;
6321 }
6322 }
6323 }
6324
Dianne Hackbornb424b632010-08-18 15:59:05 -07006325 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid);
6326 if (perms != null) {
6327 for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) {
6328 if (uri.getKey().getAuthority().equals(cpi.authority)) {
6329 return null;
6330 }
6331 }
6332 }
6333
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006334 String msg;
6335 if (!cpi.exported) {
6336 msg = "Permission Denial: opening provider " + cpi.name
6337 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
6338 + ", uid=" + callingUid + ") that is not exported from uid "
6339 + cpi.applicationInfo.uid;
6340 } else {
6341 msg = "Permission Denial: opening provider " + cpi.name
6342 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
6343 + ", uid=" + callingUid + ") requires "
6344 + cpi.readPermission + " or " + cpi.writePermission;
6345 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08006346 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 return msg;
6348 }
6349
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006350 ContentProviderConnection incProviderCountLocked(ProcessRecord r,
6351 final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006352 if (r != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006353 for (int i=0; i<r.conProviders.size(); i++) {
6354 ContentProviderConnection conn = r.conProviders.get(i);
6355 if (conn.provider == cpr) {
6356 if (DEBUG_PROVIDER) Slog.v(TAG,
6357 "Adding provider requested by "
6358 + r.processName + " from process "
6359 + cpr.info.processName + ": " + cpr.name.flattenToShortString()
6360 + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
6361 if (stable) {
6362 conn.stableCount++;
6363 conn.numStableIncs++;
6364 } else {
6365 conn.unstableCount++;
6366 conn.numUnstableIncs++;
6367 }
6368 return conn;
6369 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006370 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006371 ContentProviderConnection conn = new ContentProviderConnection(cpr, r);
6372 if (stable) {
6373 conn.stableCount = 1;
6374 conn.numStableIncs = 1;
6375 } else {
6376 conn.unstableCount = 1;
6377 conn.numUnstableIncs = 1;
6378 }
6379 cpr.connections.add(conn);
6380 r.conProviders.add(conn);
6381 return conn;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006382 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006383 cpr.addExternalProcessHandleLocked(externalProcessToken);
6384 return null;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006385 }
6386
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006387 boolean decProviderCountLocked(ContentProviderConnection conn,
6388 ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
6389 if (conn != null) {
6390 cpr = conn.provider;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006391 if (DEBUG_PROVIDER) Slog.v(TAG,
6392 "Removing provider requested by "
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006393 + conn.client.processName + " from process "
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006394 + cpr.info.processName + ": " + cpr.name.flattenToShortString()
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006395 + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
6396 if (stable) {
6397 conn.stableCount--;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006398 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006399 conn.unstableCount--;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006400 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006401 if (conn.stableCount == 0 && conn.unstableCount == 0) {
6402 cpr.connections.remove(conn);
6403 conn.client.conProviders.remove(conn);
6404 return true;
6405 }
6406 return false;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006407 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006408 cpr.removeExternalProcessHandleLocked(externalProcessToken);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006409 return false;
6410 }
6411
Amith Yamasani742a6712011-05-04 14:49:28 -07006412 private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller,
Dianne Hackborn41203752012-08-31 14:05:51 -07006413 String name, IBinder token, boolean stable, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 ContentProviderRecord cpr;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006415 ContentProviderConnection conn = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 ProviderInfo cpi = null;
6417
6418 synchronized(this) {
6419 ProcessRecord r = null;
6420 if (caller != null) {
6421 r = getRecordForAppLocked(caller);
6422 if (r == null) {
6423 throw new SecurityException(
6424 "Unable to find app for caller " + caller
6425 + " (pid=" + Binder.getCallingPid()
6426 + ") when getting content provider " + name);
6427 }
6428 }
6429
6430 // First check if this content provider has been published...
Amith Yamasani742a6712011-05-04 14:49:28 -07006431 cpr = mProviderMap.getProviderByName(name, userId);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006432 boolean providerRunning = cpr != null;
6433 if (providerRunning) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006434 cpi = cpr.info;
Dianne Hackbornb424b632010-08-18 15:59:05 -07006435 String msg;
6436 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
6437 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 }
6439
6440 if (r != null && cpr.canRunHere(r)) {
6441 // This provider has been published or is in the process
6442 // of being published... but it is also allowed to run
6443 // in the caller's process, so don't make a connection
6444 // and just let the caller instantiate its own instance.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006445 ContentProviderHolder holder = cpr.newHolder(null);
6446 // don't give caller the provider object, it needs
6447 // to make its own.
6448 holder.provider = null;
6449 return holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006450 }
6451
6452 final long origId = Binder.clearCallingIdentity();
6453
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006454 // In this case the provider instance already exists, so we can
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 // return it right away.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006456 conn = incProviderCountLocked(r, cpr, token, stable);
6457 if (conn != null && (conn.stableCount+conn.unstableCount) == 1) {
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006458 if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006459 // If this is a perceptible app accessing the provider,
Dianne Hackborn906497c2010-05-10 15:57:38 -07006460 // make sure to count it as being accessed and thus
6461 // back up on the LRU list. This is good because
6462 // content providers are often expensive to start.
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006463 updateLruProcessLocked(cpr.proc, false, true);
Dianne Hackborn906497c2010-05-10 15:57:38 -07006464 }
Dianne Hackborn6f86c0e2010-05-11 14:20:52 -07006465 }
6466
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006467 if (cpr.proc != null) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006468 if (false) {
6469 if (cpr.name.flattenToShortString().equals(
6470 "com.android.providers.calendar/.CalendarProvider2")) {
6471 Slog.v(TAG, "****************** KILLING "
6472 + cpr.name.flattenToShortString());
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006473 Process.killProcess(cpr.proc.pid);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006474 }
6475 }
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006476 boolean success = updateOomAdjLocked(cpr.proc);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006477 if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success);
6478 // NOTE: there is still a race here where a signal could be
6479 // pending on the process even though we managed to update its
6480 // adj level. Not sure what to do about this, but at least
6481 // the race is now smaller.
6482 if (!success) {
6483 // Uh oh... it looks like the provider's process
6484 // has been killed on us. We need to wait for a new
6485 // process to be started, and make sure its death
6486 // doesn't kill our process.
6487 Slog.i(TAG,
6488 "Existing provider " + cpr.name.flattenToShortString()
6489 + " is crashing; detaching " + r);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006490 boolean lastRef = decProviderCountLocked(conn, cpr, token, stable);
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006491 appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006492 if (!lastRef) {
6493 // This wasn't the last ref our process had on
6494 // the provider... we have now been killed, bail.
6495 return null;
6496 }
6497 providerRunning = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006498 conn = null;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 }
6501
6502 Binder.restoreCallingIdentity(origId);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006505 boolean singleton;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006506 if (!providerRunning) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006508 cpi = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07006509 resolveContentProvider(name,
Amith Yamasani483f3b02012-03-13 16:08:00 -07006510 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 } catch (RemoteException ex) {
6512 }
6513 if (cpi == null) {
6514 return null;
6515 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006516 singleton = isSingleton(cpi.processName, cpi.applicationInfo,
6517 cpi.name, cpi.flags);
6518 if (singleton) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07006519 userId = 0;
6520 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07006521 cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07006522
Dianne Hackbornb424b632010-08-18 15:59:05 -07006523 String msg;
6524 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
6525 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 }
6527
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07006528 if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08006529 && !cpi.processName.equals("system")) {
6530 // If this content provider does not run in the system
6531 // process, and the system is not yet ready to run other
6532 // processes, then fail fast instead of hanging.
6533 throw new IllegalArgumentException(
6534 "Attempt to launch content provider before system ready");
6535 }
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006536
Dianne Hackborn80a4af22012-08-27 19:18:31 -07006537 // Make sure that the user who owns this provider is started. If not,
6538 // we don't want to allow it to run.
6539 if (mStartedUsers.get(userId) == null) {
6540 Slog.w(TAG, "Unable to launch app "
6541 + cpi.applicationInfo.packageName + "/"
6542 + cpi.applicationInfo.uid + " for provider "
6543 + name + ": user " + userId + " is stopped");
6544 return null;
6545 }
6546
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006547 ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
Amith Yamasani483f3b02012-03-13 16:08:00 -07006548 cpr = mProviderMap.getProviderByClass(comp, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 final boolean firstClass = cpr == null;
6550 if (firstClass) {
6551 try {
6552 ApplicationInfo ai =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006553 AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 getApplicationInfo(
6555 cpi.applicationInfo.packageName,
Amith Yamasani483f3b02012-03-13 16:08:00 -07006556 STOCK_PM_FLAGS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 if (ai == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006558 Slog.w(TAG, "No package info for content provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 + cpi.name);
6560 return null;
6561 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07006562 ai = getAppInfoForUser(ai, userId);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006563 cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006564 } catch (RemoteException ex) {
6565 // pm is in same process, this will never happen.
6566 }
6567 }
6568
6569 if (r != null && cpr.canRunHere(r)) {
6570 // If this is a multiprocess provider, then just return its
6571 // info and allow the caller to instantiate it. Only do
6572 // this if the provider is the same user as the caller's
6573 // process, or can run as root (so can be in any process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006574 return cpr.newHolder(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006575 }
6576
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006577 if (DEBUG_PROVIDER) {
6578 RuntimeException e = new RuntimeException("here");
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006579 Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006580 + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 }
6582
6583 // This is single process, and our app is now connecting to it.
6584 // See if we are already in the process of launching this
6585 // provider.
6586 final int N = mLaunchingProviders.size();
6587 int i;
6588 for (i=0; i<N; i++) {
6589 if (mLaunchingProviders.get(i) == cpr) {
6590 break;
6591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006592 }
6593
6594 // If the provider is not already being launched, then get it
6595 // started.
6596 if (i >= N) {
6597 final long origId = Binder.clearCallingIdentity();
Dianne Hackborne7f97212011-02-24 14:40:20 -08006598
6599 try {
6600 // Content provider is now in use, its package can't be stopped.
6601 try {
6602 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07006603 cpr.appInfo.packageName, false, userId);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006604 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08006605 } catch (IllegalArgumentException e) {
6606 Slog.w(TAG, "Failed trying to unstop package "
6607 + cpr.appInfo.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006608 }
6609
6610 ProcessRecord proc = startProcessLocked(cpi.processName,
6611 cpr.appInfo, false, 0, "content provider",
6612 new ComponentName(cpi.applicationInfo.packageName,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006613 cpi.name), false, false);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006614 if (proc == null) {
6615 Slog.w(TAG, "Unable to launch app "
6616 + cpi.applicationInfo.packageName + "/"
6617 + cpi.applicationInfo.uid + " for provider "
6618 + name + ": process is bad");
6619 return null;
6620 }
6621 cpr.launchingApp = proc;
6622 mLaunchingProviders.add(cpr);
6623 } finally {
6624 Binder.restoreCallingIdentity(origId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626 }
6627
6628 // Make sure the provider is published (the same provider class
6629 // may be published under multiple names).
6630 if (firstClass) {
Amith Yamasani742a6712011-05-04 14:49:28 -07006631 mProviderMap.putProviderByClass(comp, cpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 }
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006633
Amith Yamasani742a6712011-05-04 14:49:28 -07006634 mProviderMap.putProviderByName(name, cpr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006635 conn = incProviderCountLocked(r, cpr, token, stable);
6636 if (conn != null) {
6637 conn.waiting = true;
6638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 }
6640 }
6641
6642 // Wait for the provider to be published...
6643 synchronized (cpr) {
6644 while (cpr.provider == null) {
6645 if (cpr.launchingApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006646 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006647 + cpi.applicationInfo.packageName + "/"
6648 + cpi.applicationInfo.uid + " for provider "
6649 + name + ": launching app became null");
Doug Zongker2bec3d42009-12-04 12:52:44 -08006650 EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 cpi.applicationInfo.packageName,
6652 cpi.applicationInfo.uid, name);
6653 return null;
6654 }
6655 try {
Amith Yamasani742a6712011-05-04 14:49:28 -07006656 if (DEBUG_MU) {
6657 Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp="
6658 + cpr.launchingApp);
6659 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006660 if (conn != null) {
6661 conn.waiting = true;
6662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 cpr.wait();
6664 } catch (InterruptedException ex) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006665 } finally {
6666 if (conn != null) {
6667 conn.waiting = false;
6668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 }
6670 }
6671 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006672 return cpr != null ? cpr.newHolder(conn) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 }
6674
6675 public final ContentProviderHolder getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07006676 IApplicationThread caller, String name, int userId, boolean stable) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006677 enforceNotIsolatedCaller("getContentProvider");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 if (caller == null) {
6679 String msg = "null IApplicationThread when getting content provider "
6680 + name;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006681 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006682 throw new SecurityException(msg);
6683 }
6684
Jeff Sharkey6d515712012-09-20 16:06:08 -07006685 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId,
6686 false, true, "getContentProvider", null);
6687 return getContentProviderImpl(caller, name, null, stable, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 }
6689
Jeff Sharkey6d515712012-09-20 16:06:08 -07006690 public ContentProviderHolder getContentProviderExternal(
6691 String name, int userId, IBinder token) {
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006692 enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
6693 "Do not have permission in call getContentProviderExternal()");
Jeff Sharkey6d515712012-09-20 16:06:08 -07006694 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId,
6695 false, true, "getContentProvider", null);
6696 return getContentProviderExternalUnchecked(name, token, userId);
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006697 }
6698
Dianne Hackborn41203752012-08-31 14:05:51 -07006699 private ContentProviderHolder getContentProviderExternalUnchecked(String name,
6700 IBinder token, int userId) {
6701 return getContentProviderImpl(null, name, token, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006702 }
6703
6704 /**
6705 * Drop a content provider from a ProcessRecord's bookkeeping
6706 * @param cpr
6707 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006708 public void removeContentProvider(IBinder connection, boolean stable) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006709 enforceNotIsolatedCaller("removeContentProvider");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006710 synchronized (this) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006711 ContentProviderConnection conn;
6712 try {
6713 conn = (ContentProviderConnection)connection;
6714 } catch (ClassCastException e) {
6715 String msg ="removeContentProvider: " + connection
6716 + " not a ContentProviderConnection";
6717 Slog.w(TAG, msg);
6718 throw new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006719 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006720 if (conn == null) {
6721 throw new NullPointerException("connection is null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006723 if (decProviderCountLocked(conn, null, null, stable)) {
6724 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006726 }
6727 }
6728
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006729 public void removeContentProviderExternal(String name, IBinder token) {
6730 enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
6731 "Do not have permission in call removeContentProviderExternal()");
Dianne Hackborn41203752012-08-31 14:05:51 -07006732 removeContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId());
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006733 }
6734
Dianne Hackborn41203752012-08-31 14:05:51 -07006735 private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736 synchronized (this) {
Dianne Hackborn41203752012-08-31 14:05:51 -07006737 ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006738 if(cpr == null) {
6739 //remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08006740 if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006741 return;
6742 }
6743
6744 //update content provider record entry info
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006745 ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name);
Dianne Hackborn41203752012-08-31 14:05:51 -07006746 ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId);
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006747 if (localCpr.hasExternalProcessHandles()) {
6748 if (localCpr.removeExternalProcessHandleLocked(token)) {
6749 updateOomAdjLocked();
6750 } else {
6751 Slog.e(TAG, "Attmpt to remove content provider " + localCpr
6752 + " with no external reference for token: "
6753 + token + ".");
6754 }
6755 } else {
6756 Slog.e(TAG, "Attmpt to remove content provider: " + localCpr
6757 + " with no external references.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006759 }
6760 }
6761
6762 public final void publishContentProviders(IApplicationThread caller,
6763 List<ContentProviderHolder> providers) {
6764 if (providers == null) {
6765 return;
6766 }
6767
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006768 enforceNotIsolatedCaller("publishContentProviders");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006769 synchronized (this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 final ProcessRecord r = getRecordForAppLocked(caller);
Amith Yamasani742a6712011-05-04 14:49:28 -07006771 if (DEBUG_MU)
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006772 Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773 if (r == null) {
6774 throw new SecurityException(
6775 "Unable to find app for caller " + caller
6776 + " (pid=" + Binder.getCallingPid()
6777 + ") when publishing content providers");
6778 }
6779
6780 final long origId = Binder.clearCallingIdentity();
6781
6782 final int N = providers.size();
6783 for (int i=0; i<N; i++) {
6784 ContentProviderHolder src = providers.get(i);
6785 if (src == null || src.info == null || src.provider == null) {
6786 continue;
6787 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07006788 ContentProviderRecord dst = r.pubProviders.get(src.info.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006789 if (DEBUG_MU)
6790 Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 if (dst != null) {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006792 ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006793 mProviderMap.putProviderByClass(comp, dst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 String names[] = dst.info.authority.split(";");
6795 for (int j = 0; j < names.length; j++) {
Amith Yamasani742a6712011-05-04 14:49:28 -07006796 mProviderMap.putProviderByName(names[j], dst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 }
6798
6799 int NL = mLaunchingProviders.size();
6800 int j;
6801 for (j=0; j<NL; j++) {
6802 if (mLaunchingProviders.get(j) == dst) {
6803 mLaunchingProviders.remove(j);
6804 j--;
6805 NL--;
6806 }
6807 }
6808 synchronized (dst) {
6809 dst.provider = src.provider;
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006810 dst.proc = r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 dst.notifyAll();
6812 }
6813 updateOomAdjLocked(r);
6814 }
6815 }
6816
6817 Binder.restoreCallingIdentity(origId);
6818 }
6819 }
6820
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006821 public boolean refContentProvider(IBinder connection, int stable, int unstable) {
6822 ContentProviderConnection conn;
6823 try {
6824 conn = (ContentProviderConnection)connection;
6825 } catch (ClassCastException e) {
6826 String msg ="refContentProvider: " + connection
6827 + " not a ContentProviderConnection";
6828 Slog.w(TAG, msg);
6829 throw new IllegalArgumentException(msg);
6830 }
6831 if (conn == null) {
6832 throw new NullPointerException("connection is null");
6833 }
6834
6835 synchronized (this) {
6836 if (stable > 0) {
6837 conn.numStableIncs += stable;
6838 }
6839 stable = conn.stableCount + stable;
6840 if (stable < 0) {
6841 throw new IllegalStateException("stableCount < 0: " + stable);
6842 }
6843
6844 if (unstable > 0) {
6845 conn.numUnstableIncs += unstable;
6846 }
6847 unstable = conn.unstableCount + unstable;
6848 if (unstable < 0) {
6849 throw new IllegalStateException("unstableCount < 0: " + unstable);
6850 }
6851
6852 if ((stable+unstable) <= 0) {
6853 throw new IllegalStateException("ref counts can't go to zero here: stable="
6854 + stable + " unstable=" + unstable);
6855 }
6856 conn.stableCount = stable;
6857 conn.unstableCount = unstable;
6858 return !conn.dead;
6859 }
6860 }
6861
6862 public void unstableProviderDied(IBinder connection) {
6863 ContentProviderConnection conn;
6864 try {
6865 conn = (ContentProviderConnection)connection;
6866 } catch (ClassCastException e) {
6867 String msg ="refContentProvider: " + connection
6868 + " not a ContentProviderConnection";
6869 Slog.w(TAG, msg);
6870 throw new IllegalArgumentException(msg);
6871 }
6872 if (conn == null) {
6873 throw new NullPointerException("connection is null");
6874 }
6875
6876 // Safely retrieve the content provider associated with the connection.
6877 IContentProvider provider;
6878 synchronized (this) {
6879 provider = conn.provider.provider;
6880 }
6881
6882 if (provider == null) {
6883 // Um, yeah, we're way ahead of you.
6884 return;
6885 }
6886
6887 // Make sure the caller is being honest with us.
6888 if (provider.asBinder().pingBinder()) {
6889 // Er, no, still looks good to us.
6890 synchronized (this) {
6891 Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid()
6892 + " says " + conn + " died, but we don't agree");
6893 return;
6894 }
6895 }
6896
6897 // Well look at that! It's dead!
6898 synchronized (this) {
6899 if (conn.provider.provider != provider) {
6900 // But something changed... good enough.
6901 return;
6902 }
6903
6904 ProcessRecord proc = conn.provider.proc;
6905 if (proc == null || proc.thread == null) {
6906 // Seems like the process is already cleaned up.
6907 return;
6908 }
6909
6910 // As far as we're concerned, this is just like receiving a
6911 // death notification... just a bit prematurely.
6912 Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid
6913 + ") early provider death");
Dianne Hackbornbd145db2012-06-05 16:20:46 -07006914 final long ident = Binder.clearCallingIdentity();
6915 try {
6916 appDiedLocked(proc, proc.pid, proc.thread);
6917 } finally {
6918 Binder.restoreCallingIdentity(ident);
6919 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006920 }
6921 }
6922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923 public static final void installSystemProviders() {
Jeff Brown10e89712011-07-08 18:52:57 -07006924 List<ProviderInfo> providers;
Josh Bartel2ecce342010-02-25 10:55:48 -06006925 synchronized (mSelf) {
6926 ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID);
6927 providers = mSelf.generateApplicationProvidersLocked(app);
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08006928 if (providers != null) {
6929 for (int i=providers.size()-1; i>=0; i--) {
6930 ProviderInfo pi = (ProviderInfo)providers.get(i);
6931 if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
6932 Slog.w(TAG, "Not installing system proc provider " + pi.name
6933 + ": not system .apk");
6934 providers.remove(i);
6935 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08006936 }
6937 }
6938 }
Josh Bartel2ecce342010-02-25 10:55:48 -06006939 if (providers != null) {
6940 mSystemThread.installSystemProviders(providers);
6941 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006942
6943 mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf);
Mark Brophyc6350272011-08-05 16:16:39 +01006944
6945 mSelf.mUsageStatsService.monitorPackages();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 }
6947
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006948 /**
6949 * Allows app to retrieve the MIME type of a URI without having permission
6950 * to access its content provider.
6951 *
6952 * CTS tests for this functionality can be run with "runtest cts-appsecurity".
6953 *
6954 * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/
6955 * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java
6956 */
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07006957 public String getProviderMimeType(Uri uri, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006958 enforceNotIsolatedCaller("getProviderMimeType");
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07006959 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(),
6960 userId, false, true, "getProviderMimeType", null);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006961 final String name = uri.getAuthority();
6962 final long ident = Binder.clearCallingIdentity();
6963 ContentProviderHolder holder = null;
6964
6965 try {
Dianne Hackborn41203752012-08-31 14:05:51 -07006966 holder = getContentProviderExternalUnchecked(name, null, userId);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006967 if (holder != null) {
6968 return holder.provider.getType(uri);
6969 }
6970 } catch (RemoteException e) {
6971 Log.w(TAG, "Content provider dead retrieving " + uri, e);
6972 return null;
6973 } finally {
6974 if (holder != null) {
Dianne Hackborn41203752012-08-31 14:05:51 -07006975 removeContentProviderExternalUnchecked(name, null, userId);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006976 }
6977 Binder.restoreCallingIdentity(ident);
6978 }
6979
6980 return null;
6981 }
6982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 // =========================================================
6984 // GLOBAL MANAGEMENT
6985 // =========================================================
6986
6987 final ProcessRecord newProcessRecordLocked(IApplicationThread thread,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006988 ApplicationInfo info, String customProcess, boolean isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 String proc = customProcess != null ? customProcess : info.processName;
6990 BatteryStatsImpl.Uid.Proc ps = null;
6991 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006992 int uid = info.uid;
6993 if (isolated) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006994 int userId = UserHandle.getUserId(uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006995 int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1;
6996 uid = 0;
6997 while (true) {
6998 if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID
6999 || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) {
7000 mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID;
7001 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07007002 uid = UserHandle.getUid(userId, mNextIsolatedProcessUid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007003 mNextIsolatedProcessUid++;
7004 if (mIsolatedProcesses.indexOfKey(uid) < 0) {
7005 // No process for this uid, use it.
7006 break;
7007 }
7008 stepsLeft--;
7009 if (stepsLeft <= 0) {
7010 return null;
7011 }
7012 }
7013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 synchronized (stats) {
7015 ps = stats.getProcessStatsLocked(info.uid, proc);
7016 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007017 return new ProcessRecord(ps, thread, info, proc, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007018 }
7019
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007020 final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) {
7021 ProcessRecord app;
7022 if (!isolated) {
7023 app = getProcessRecordLocked(info.processName, info.uid);
7024 } else {
7025 app = null;
7026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027
7028 if (app == null) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007029 app = newProcessRecordLocked(null, info, null, isolated);
7030 mProcessNames.put(info.processName, app.uid, app);
7031 if (isolated) {
7032 mIsolatedProcesses.put(app.uid, app);
7033 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007034 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 }
7036
Dianne Hackborne7f97212011-02-24 14:40:20 -08007037 // This package really, really can not be stopped.
7038 try {
7039 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07007040 info.packageName, false, UserHandle.getUserId(app.uid));
Dianne Hackborne7f97212011-02-24 14:40:20 -08007041 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08007042 } catch (IllegalArgumentException e) {
7043 Slog.w(TAG, "Failed trying to unstop package "
7044 + info.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08007045 }
7046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
7048 == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
7049 app.persistent = true;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07007050 app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 }
7052 if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
7053 mPersistentStartingProcesses.add(app);
7054 startProcessLocked(app, "added application", app.processName);
7055 }
7056
7057 return app;
7058 }
7059
7060 public void unhandledBack() {
7061 enforceCallingPermission(android.Manifest.permission.FORCE_BACK,
7062 "unhandledBack()");
7063
7064 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007065 int count = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007066 if (DEBUG_SWITCH) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 TAG, "Performing unhandledBack(): stack size = " + count);
7068 if (count > 1) {
7069 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007070 mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1),
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07007071 count-1, Activity.RESULT_CANCELED, null, "unhandled-back", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 Binder.restoreCallingIdentity(origId);
7073 }
7074 }
7075 }
7076
7077 public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08007078 enforceNotIsolatedCaller("openContentUri");
Dianne Hackborn41203752012-08-31 14:05:51 -07007079 final int userId = UserHandle.getCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007080 String name = uri.getAuthority();
Dianne Hackborn41203752012-08-31 14:05:51 -07007081 ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 ParcelFileDescriptor pfd = null;
7083 if (cph != null) {
7084 // We record the binder invoker's uid in thread-local storage before
7085 // going to the content provider to open the file. Later, in the code
7086 // that handles all permissions checks, we look for this uid and use
7087 // that rather than the Activity Manager's own uid. The effect is that
7088 // we do the check against the caller's permissions even though it looks
7089 // to the content provider like the Activity Manager itself is making
7090 // the request.
7091 sCallerIdentity.set(new Identity(
7092 Binder.getCallingPid(), Binder.getCallingUid()));
7093 try {
7094 pfd = cph.provider.openFile(uri, "r");
7095 } catch (FileNotFoundException e) {
7096 // do nothing; pfd will be returned null
7097 } finally {
7098 // Ensure that whatever happens, we clean up the identity state
7099 sCallerIdentity.remove();
7100 }
7101
7102 // We've got the fd now, so we're done with the provider.
Dianne Hackborn41203752012-08-31 14:05:51 -07007103 removeContentProviderExternalUnchecked(name, null, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007105 Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 }
7107 return pfd;
7108 }
7109
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007110 // Actually is sleeping or shutting down or whatever else in the future
7111 // is an inactive state.
7112 public boolean isSleeping() {
7113 return mSleeping || mShuttingDown;
7114 }
7115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007116 public void goingToSleep() {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007117 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
7118 != PackageManager.PERMISSION_GRANTED) {
7119 throw new SecurityException("Requires permission "
7120 + android.Manifest.permission.DEVICE_POWER);
7121 }
7122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 synchronized(this) {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007124 mWentToSleep = true;
Jeff Brownc042ee22012-05-08 13:03:42 -07007125 updateEventDispatchingLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007127 if (!mSleeping) {
7128 mSleeping = true;
7129 mMainStack.stopIfSleepingLocked();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007130
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007131 // Initialize the wake times of all processes.
7132 checkExcessivePowerUsageLocked(false);
7133 mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
7134 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
7135 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
7136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137 }
7138 }
7139
Dianne Hackborn55280a92009-05-07 15:53:46 -07007140 public boolean shutdown(int timeout) {
7141 if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
7142 != PackageManager.PERMISSION_GRANTED) {
7143 throw new SecurityException("Requires permission "
7144 + android.Manifest.permission.SHUTDOWN);
7145 }
7146
7147 boolean timedout = false;
7148
7149 synchronized(this) {
7150 mShuttingDown = true;
Jeff Brownc042ee22012-05-08 13:03:42 -07007151 updateEventDispatchingLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -07007152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007153 if (mMainStack.mResumedActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007154 mMainStack.stopIfSleepingLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -07007155 final long endTime = System.currentTimeMillis() + timeout;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007156 while (mMainStack.mResumedActivity != null
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08007157 || mMainStack.mPausingActivity != null) {
Dianne Hackborn55280a92009-05-07 15:53:46 -07007158 long delay = endTime - System.currentTimeMillis();
7159 if (delay <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007160 Slog.w(TAG, "Activity manager shutdown timed out");
Dianne Hackborn55280a92009-05-07 15:53:46 -07007161 timedout = true;
7162 break;
7163 }
7164 try {
7165 this.wait();
7166 } catch (InterruptedException e) {
7167 }
7168 }
7169 }
7170 }
7171
7172 mUsageStatsService.shutdown();
7173 mBatteryStatsService.shutdown();
7174
7175 return timedout;
7176 }
7177
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007178 public final void activitySlept(IBinder token) {
7179 if (localLOGV) Slog.v(
7180 TAG, "Activity slept: token=" + token);
7181
7182 ActivityRecord r = null;
7183
7184 final long origId = Binder.clearCallingIdentity();
7185
7186 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07007187 r = mMainStack.isInStackLocked(token);
7188 if (r != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007189 mMainStack.activitySleptLocked(r);
7190 }
7191 }
7192
7193 Binder.restoreCallingIdentity(origId);
7194 }
7195
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007196 private void comeOutOfSleepIfNeededLocked() {
7197 if (!mWentToSleep && !mLockScreenShown) {
7198 if (mSleeping) {
7199 mSleeping = false;
7200 mMainStack.awakeFromSleepingLocked();
7201 mMainStack.resumeTopActivityLocked(null);
7202 }
7203 }
7204 }
7205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007206 public void wakingUp() {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007207 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
7208 != PackageManager.PERMISSION_GRANTED) {
7209 throw new SecurityException("Requires permission "
7210 + android.Manifest.permission.DEVICE_POWER);
7211 }
7212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007213 synchronized(this) {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007214 mWentToSleep = false;
Jeff Brownc042ee22012-05-08 13:03:42 -07007215 updateEventDispatchingLocked();
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007216 comeOutOfSleepIfNeededLocked();
7217 }
7218 }
7219
Jeff Brownc042ee22012-05-08 13:03:42 -07007220 private void updateEventDispatchingLocked() {
7221 mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown);
7222 }
7223
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007224 public void setLockScreenShown(boolean shown) {
7225 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
7226 != PackageManager.PERMISSION_GRANTED) {
7227 throw new SecurityException("Requires permission "
7228 + android.Manifest.permission.DEVICE_POWER);
7229 }
7230
7231 synchronized(this) {
7232 mLockScreenShown = shown;
7233 comeOutOfSleepIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007234 }
7235 }
7236
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007237 public void stopAppSwitches() {
7238 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
7239 != PackageManager.PERMISSION_GRANTED) {
7240 throw new SecurityException("Requires permission "
7241 + android.Manifest.permission.STOP_APP_SWITCHES);
7242 }
7243
7244 synchronized(this) {
7245 mAppSwitchesAllowedTime = SystemClock.uptimeMillis()
7246 + APP_SWITCH_DELAY_TIME;
7247 mDidAppSwitch = false;
7248 mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
7249 Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
7250 mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME);
7251 }
7252 }
7253
7254 public void resumeAppSwitches() {
7255 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
7256 != PackageManager.PERMISSION_GRANTED) {
7257 throw new SecurityException("Requires permission "
7258 + android.Manifest.permission.STOP_APP_SWITCHES);
7259 }
7260
7261 synchronized(this) {
7262 // Note that we don't execute any pending app switches... we will
7263 // let those wait until either the timeout, or the next start
7264 // activity request.
7265 mAppSwitchesAllowedTime = 0;
7266 }
7267 }
7268
7269 boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid,
7270 String name) {
7271 if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
7272 return true;
7273 }
7274
7275 final int perm = checkComponentPermission(
7276 android.Manifest.permission.STOP_APP_SWITCHES, callingPid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08007277 callingUid, -1, true);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007278 if (perm == PackageManager.PERMISSION_GRANTED) {
7279 return true;
7280 }
7281
Joe Onorato8a9b2202010-02-26 18:56:32 -08007282 Slog.w(TAG, name + " request from " + callingUid + " stopped");
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007283 return false;
7284 }
7285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 public void setDebugApp(String packageName, boolean waitForDebugger,
7287 boolean persistent) {
7288 enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
7289 "setDebugApp()");
7290
7291 // Note that this is not really thread safe if there are multiple
7292 // callers into it at the same time, but that's not a situation we
7293 // care about.
7294 if (persistent) {
7295 final ContentResolver resolver = mContext.getContentResolver();
7296 Settings.System.putString(
7297 resolver, Settings.System.DEBUG_APP,
7298 packageName);
7299 Settings.System.putInt(
7300 resolver, Settings.System.WAIT_FOR_DEBUGGER,
7301 waitForDebugger ? 1 : 0);
7302 }
7303
7304 synchronized (this) {
7305 if (!persistent) {
7306 mOrigDebugApp = mDebugApp;
7307 mOrigWaitForDebugger = mWaitForDebugger;
7308 }
7309 mDebugApp = packageName;
7310 mWaitForDebugger = waitForDebugger;
7311 mDebugTransient = !persistent;
7312 if (packageName != null) {
7313 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07007314 forceStopPackageLocked(packageName, -1, false, false, true, true,
7315 UserHandle.USER_ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007316 Binder.restoreCallingIdentity(origId);
7317 }
7318 }
7319 }
7320
Siva Velusamy92a8b222012-03-09 16:24:04 -08007321 void setOpenGlTraceApp(ApplicationInfo app, String processName) {
7322 synchronized (this) {
7323 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
7324 if (!isDebuggable) {
7325 if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
7326 throw new SecurityException("Process not debuggable: " + app.packageName);
7327 }
7328 }
7329
7330 mOpenGlTraceApp = processName;
7331 }
7332 }
7333
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07007334 void setProfileApp(ApplicationInfo app, String processName, String profileFile,
7335 ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
7336 synchronized (this) {
7337 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
7338 if (!isDebuggable) {
7339 if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
7340 throw new SecurityException("Process not debuggable: " + app.packageName);
7341 }
7342 }
7343 mProfileApp = processName;
7344 mProfileFile = profileFile;
7345 if (mProfileFd != null) {
7346 try {
7347 mProfileFd.close();
7348 } catch (IOException e) {
7349 }
7350 mProfileFd = null;
7351 }
7352 mProfileFd = profileFd;
7353 mProfileType = 0;
7354 mAutoStopProfiler = autoStopProfiler;
7355 }
7356 }
7357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 public void setAlwaysFinish(boolean enabled) {
7359 enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH,
7360 "setAlwaysFinish()");
7361
7362 Settings.System.putInt(
7363 mContext.getContentResolver(),
7364 Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0);
7365
7366 synchronized (this) {
7367 mAlwaysFinishActivities = enabled;
7368 }
7369 }
7370
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007371 public void setActivityController(IActivityController controller) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007372 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007373 "setActivityController()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007375 mController = controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 }
7377 }
7378
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08007379 public boolean isUserAMonkey() {
7380 // For now the fact that there is a controller implies
7381 // we have a monkey.
7382 synchronized (this) {
7383 return mController != null;
7384 }
7385 }
7386
Jeff Sharkeya4620792011-05-20 15:29:23 -07007387 public void registerProcessObserver(IProcessObserver observer) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08007388 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
7389 "registerProcessObserver()");
7390 synchronized (this) {
7391 mProcessObservers.register(observer);
7392 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07007393 }
7394
7395 public void unregisterProcessObserver(IProcessObserver observer) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08007396 synchronized (this) {
7397 mProcessObservers.unregister(observer);
7398 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07007399 }
7400
Daniel Sandler69a48172010-06-23 16:29:36 -04007401 public void setImmersive(IBinder token, boolean immersive) {
7402 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07007403 ActivityRecord r = mMainStack.isInStackLocked(token);
7404 if (r == null) {
Daniel Sandler69a48172010-06-23 16:29:36 -04007405 throw new IllegalArgumentException();
7406 }
Daniel Sandler69a48172010-06-23 16:29:36 -04007407 r.immersive = immersive;
7408 }
7409 }
7410
7411 public boolean isImmersive(IBinder token) {
7412 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07007413 ActivityRecord r = mMainStack.isInStackLocked(token);
7414 if (r == null) {
Daniel Sandler69a48172010-06-23 16:29:36 -04007415 throw new IllegalArgumentException();
7416 }
Daniel Sandler69a48172010-06-23 16:29:36 -04007417 return r.immersive;
7418 }
7419 }
7420
7421 public boolean isTopActivityImmersive() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08007422 enforceNotIsolatedCaller("startActivity");
Daniel Sandler69a48172010-06-23 16:29:36 -04007423 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007424 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Daniel Sandler69a48172010-06-23 16:29:36 -04007425 return (r != null) ? r.immersive : false;
7426 }
7427 }
7428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 public final void enterSafeMode() {
7430 synchronized(this) {
7431 // It only makes sense to do this before the system is ready
7432 // and started launching other packages.
7433 if (!mSystemReady) {
7434 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007435 AppGlobals.getPackageManager().enterSafeMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007436 } catch (RemoteException e) {
7437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 }
7439 }
7440 }
7441
Jeff Brownb09abc12011-01-13 21:08:27 -08007442 public final void showSafeModeOverlay() {
7443 View v = LayoutInflater.from(mContext).inflate(
7444 com.android.internal.R.layout.safe_mode, null);
7445 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
7446 lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
7447 lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
7448 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
Fabrice Di Meglioaac0d4e2012-07-19 19:21:26 -07007449 lp.gravity = Gravity.BOTTOM | Gravity.START;
Jeff Brownb09abc12011-01-13 21:08:27 -08007450 lp.format = v.getBackground().getOpacity();
7451 lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
7452 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
7453 ((WindowManager)mContext.getSystemService(
7454 Context.WINDOW_SERVICE)).addView(v, lp);
7455 }
7456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 public void noteWakeupAlarm(IIntentSender sender) {
7458 if (!(sender instanceof PendingIntentRecord)) {
7459 return;
7460 }
7461 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
7462 synchronized (stats) {
7463 if (mBatteryStatsService.isOnBattery()) {
7464 mBatteryStatsService.enforceCallingPermission();
7465 PendingIntentRecord rec = (PendingIntentRecord)sender;
7466 int MY_UID = Binder.getCallingUid();
7467 int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
7468 BatteryStatsImpl.Uid.Pkg pkg =
7469 stats.getPackageStatsLocked(uid, rec.key.packageName);
7470 pkg.incWakeupsLocked();
7471 }
7472 }
7473 }
7474
Dianne Hackborn64825172011-03-02 21:32:58 -08007475 public boolean killPids(int[] pids, String pReason, boolean secure) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007477 throw new SecurityException("killPids only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007478 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007479 String reason = (pReason == null) ? "Unknown" : pReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 // XXX Note: don't acquire main activity lock here, because the window
7481 // manager calls in with its locks held.
7482
7483 boolean killed = false;
7484 synchronized (mPidsSelfLocked) {
7485 int[] types = new int[pids.length];
7486 int worstType = 0;
7487 for (int i=0; i<pids.length; i++) {
7488 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
7489 if (proc != null) {
7490 int type = proc.setAdj;
7491 types[i] = type;
7492 if (type > worstType) {
7493 worstType = type;
7494 }
7495 }
7496 }
7497
Dianne Hackborn64825172011-03-02 21:32:58 -08007498 // If the worst oom_adj is somewhere in the hidden proc LRU range,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007499 // then constrain it so we will kill all hidden procs.
Dianne Hackborne02c88a2011-10-28 13:58:15 -07007500 if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ
7501 && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07007502 worstType = ProcessList.HIDDEN_APP_MIN_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 }
Dianne Hackborn64825172011-03-02 21:32:58 -08007504
7505 // If this is not a secure call, don't let it kill processes that
7506 // are important.
Dianne Hackborne02c88a2011-10-28 13:58:15 -07007507 if (!secure && worstType < ProcessList.SERVICE_ADJ) {
7508 worstType = ProcessList.SERVICE_ADJ;
Dianne Hackborn64825172011-03-02 21:32:58 -08007509 }
7510
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007511 Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 for (int i=0; i<pids.length; i++) {
7513 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
7514 if (proc == null) {
7515 continue;
7516 }
7517 int adj = proc.setAdj;
Dianne Hackborn906497c2010-05-10 15:57:38 -07007518 if (adj >= worstType && !proc.killedBackground) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07007519 Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007520 EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid,
7521 proc.processName, adj, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 killed = true;
Dianne Hackborn906497c2010-05-10 15:57:38 -07007523 proc.killedBackground = true;
7524 Process.killProcessQuiet(pids[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 }
7526 }
7527 }
7528 return killed;
7529 }
Jeff Sharkeyb9a07012012-03-22 17:00:04 -07007530
7531 @Override
7532 public boolean killProcessesBelowForeground(String reason) {
7533 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
7534 throw new SecurityException("killProcessesBelowForeground() only available to system");
7535 }
7536
7537 return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason);
7538 }
7539
7540 private boolean killProcessesBelowAdj(int belowAdj, String reason) {
7541 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
7542 throw new SecurityException("killProcessesBelowAdj() only available to system");
7543 }
7544
7545 boolean killed = false;
7546 synchronized (mPidsSelfLocked) {
7547 final int size = mPidsSelfLocked.size();
7548 for (int i = 0; i < size; i++) {
7549 final int pid = mPidsSelfLocked.keyAt(i);
7550 final ProcessRecord proc = mPidsSelfLocked.valueAt(i);
7551 if (proc == null) continue;
7552
7553 final int adj = proc.setAdj;
7554 if (adj > belowAdj && !proc.killedBackground) {
7555 Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
7556 EventLog.writeEvent(
7557 EventLogTags.AM_KILL, proc.pid, proc.processName, adj, reason);
7558 killed = true;
7559 proc.killedBackground = true;
7560 Process.killProcessQuiet(pid);
7561 }
7562 }
7563 }
7564 return killed;
7565 }
7566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567 public final void startRunning(String pkg, String cls, String action,
7568 String data) {
7569 synchronized(this) {
7570 if (mStartRunning) {
7571 return;
7572 }
7573 mStartRunning = true;
7574 mTopComponent = pkg != null && cls != null
7575 ? new ComponentName(pkg, cls) : null;
7576 mTopAction = action != null ? action : Intent.ACTION_MAIN;
7577 mTopData = data;
7578 if (!mSystemReady) {
7579 return;
7580 }
7581 }
7582
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007583 systemReady(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007584 }
7585
7586 private void retrieveSettings() {
7587 final ContentResolver resolver = mContext.getContentResolver();
7588 String debugApp = Settings.System.getString(
7589 resolver, Settings.System.DEBUG_APP);
7590 boolean waitForDebugger = Settings.System.getInt(
7591 resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0;
7592 boolean alwaysFinishActivities = Settings.System.getInt(
7593 resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0;
7594
7595 Configuration configuration = new Configuration();
7596 Settings.System.getConfiguration(resolver, configuration);
7597
7598 synchronized (this) {
7599 mDebugApp = mOrigDebugApp = debugApp;
7600 mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger;
7601 mAlwaysFinishActivities = alwaysFinishActivities;
7602 // This happens before any activities are started, so we can
7603 // change mConfiguration in-place.
Dianne Hackborn813075a62011-11-14 17:45:19 -08007604 updateConfigurationLocked(configuration, null, false, true);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007605 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 }
7607 }
7608
7609 public boolean testIsSystemReady() {
7610 // no need to synchronize(this) just to read & return the value
7611 return mSystemReady;
7612 }
7613
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007614 private static File getCalledPreBootReceiversFile() {
7615 File dataDir = Environment.getDataDirectory();
7616 File systemDir = new File(dataDir, "system");
7617 File fname = new File(systemDir, "called_pre_boots.dat");
7618 return fname;
7619 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07007620
7621 static final int LAST_DONE_VERSION = 10000;
7622
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007623 private static ArrayList<ComponentName> readLastDonePreBootReceivers() {
7624 ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>();
7625 File file = getCalledPreBootReceiversFile();
7626 FileInputStream fis = null;
7627 try {
7628 fis = new FileInputStream(file);
7629 DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048));
Dianne Hackborn661cd522011-08-22 00:26:20 -07007630 int fvers = dis.readInt();
7631 if (fvers == LAST_DONE_VERSION) {
7632 String vers = dis.readUTF();
7633 String codename = dis.readUTF();
7634 String build = dis.readUTF();
7635 if (android.os.Build.VERSION.RELEASE.equals(vers)
7636 && android.os.Build.VERSION.CODENAME.equals(codename)
7637 && android.os.Build.VERSION.INCREMENTAL.equals(build)) {
7638 int num = dis.readInt();
7639 while (num > 0) {
7640 num--;
7641 String pkg = dis.readUTF();
7642 String cls = dis.readUTF();
7643 lastDoneReceivers.add(new ComponentName(pkg, cls));
7644 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007645 }
7646 }
7647 } catch (FileNotFoundException e) {
7648 } catch (IOException e) {
7649 Slog.w(TAG, "Failure reading last done pre-boot receivers", e);
7650 } finally {
7651 if (fis != null) {
7652 try {
7653 fis.close();
7654 } catch (IOException e) {
7655 }
7656 }
7657 }
7658 return lastDoneReceivers;
7659 }
7660
7661 private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) {
7662 File file = getCalledPreBootReceiversFile();
7663 FileOutputStream fos = null;
7664 DataOutputStream dos = null;
7665 try {
7666 Slog.i(TAG, "Writing new set of last done pre-boot receivers...");
7667 fos = new FileOutputStream(file);
7668 dos = new DataOutputStream(new BufferedOutputStream(fos, 2048));
Dianne Hackborn661cd522011-08-22 00:26:20 -07007669 dos.writeInt(LAST_DONE_VERSION);
7670 dos.writeUTF(android.os.Build.VERSION.RELEASE);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007671 dos.writeUTF(android.os.Build.VERSION.CODENAME);
Dianne Hackborn661cd522011-08-22 00:26:20 -07007672 dos.writeUTF(android.os.Build.VERSION.INCREMENTAL);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007673 dos.writeInt(list.size());
7674 for (int i=0; i<list.size(); i++) {
7675 dos.writeUTF(list.get(i).getPackageName());
7676 dos.writeUTF(list.get(i).getClassName());
7677 }
7678 } catch (IOException e) {
7679 Slog.w(TAG, "Failure writing last done pre-boot receivers", e);
7680 file.delete();
7681 } finally {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07007682 FileUtils.sync(fos);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007683 if (dos != null) {
7684 try {
7685 dos.close();
7686 } catch (IOException e) {
7687 // TODO Auto-generated catch block
7688 e.printStackTrace();
7689 }
7690 }
7691 }
7692 }
7693
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007694 public void systemReady(final Runnable goingCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 synchronized(this) {
7696 if (mSystemReady) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007697 if (goingCallback != null) goingCallback.run();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 return;
7699 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007700
7701 // Check to see if there are any update receivers to run.
7702 if (!mDidUpdate) {
7703 if (mWaitingUpdate) {
7704 return;
7705 }
7706 Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
7707 List<ResolveInfo> ris = null;
7708 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007709 ris = AppGlobals.getPackageManager().queryIntentReceivers(
Amith Yamasani483f3b02012-03-13 16:08:00 -07007710 intent, null, 0, 0);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007711 } catch (RemoteException e) {
7712 }
7713 if (ris != null) {
7714 for (int i=ris.size()-1; i>=0; i--) {
7715 if ((ris.get(i).activityInfo.applicationInfo.flags
7716 &ApplicationInfo.FLAG_SYSTEM) == 0) {
7717 ris.remove(i);
7718 }
7719 }
7720 intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007721
7722 ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers();
7723
7724 final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>();
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007725 for (int i=0; i<ris.size(); i++) {
7726 ActivityInfo ai = ris.get(i).activityInfo;
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007727 ComponentName comp = new ComponentName(ai.packageName, ai.name);
7728 if (lastDoneReceivers.contains(comp)) {
7729 ris.remove(i);
7730 i--;
7731 }
7732 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07007733
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07007734 final int[] users = getUsersLocked();
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007735 for (int i=0; i<ris.size(); i++) {
7736 ActivityInfo ai = ris.get(i).activityInfo;
7737 ComponentName comp = new ComponentName(ai.packageName, ai.name);
7738 doneReceivers.add(comp);
7739 intent.setComponent(comp);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07007740 for (int j=0; j<users.length; j++) {
7741 IIntentReceiver finisher = null;
7742 if (i == ris.size()-1 && j == users.length-1) {
7743 finisher = new IIntentReceiver.Stub() {
7744 public void performReceive(Intent intent, int resultCode,
7745 String data, Bundle extras, boolean ordered,
7746 boolean sticky, int sendingUser) {
7747 // The raw IIntentReceiver interface is called
7748 // with the AM lock held, so redispatch to
7749 // execute our code without the lock.
7750 mHandler.post(new Runnable() {
7751 public void run() {
7752 synchronized (ActivityManagerService.this) {
7753 mDidUpdate = true;
7754 }
7755 writeLastDonePreBootReceivers(doneReceivers);
7756 showBootMessage(mContext.getText(
7757 R.string.android_upgrading_complete),
7758 false);
7759 systemReady(goingCallback);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07007760 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07007761 });
7762 }
7763 };
7764 }
7765 Slog.i(TAG, "Sending system update to " + intent.getComponent()
7766 + " for user " + users[j]);
7767 broadcastIntentLocked(null, null, intent, null, finisher,
7768 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID,
7769 users[j]);
7770 if (finisher != null) {
7771 mWaitingUpdate = true;
7772 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007773 }
7774 }
7775 }
7776 if (mWaitingUpdate) {
7777 return;
7778 }
7779 mDidUpdate = true;
7780 }
7781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 mSystemReady = true;
7783 if (!mStartRunning) {
7784 return;
7785 }
7786 }
7787
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007788 ArrayList<ProcessRecord> procsToKill = null;
7789 synchronized(mPidsSelfLocked) {
7790 for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
7791 ProcessRecord proc = mPidsSelfLocked.valueAt(i);
7792 if (!isAllowedWhileBooting(proc.info)){
7793 if (procsToKill == null) {
7794 procsToKill = new ArrayList<ProcessRecord>();
7795 }
7796 procsToKill.add(proc);
7797 }
7798 }
7799 }
7800
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007801 synchronized(this) {
7802 if (procsToKill != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007803 for (int i=procsToKill.size()-1; i>=0; i--) {
7804 ProcessRecord proc = procsToKill.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007805 Slog.i(TAG, "Removing system update proc: " + proc);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08007806 removeProcessLocked(proc, true, false, "system update done");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007807 }
7808 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007809
7810 // Now that we have cleaned up any update processes, we
7811 // are ready to start launching real processes and know that
7812 // we won't trample on them any more.
7813 mProcessesReady = true;
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007814 }
7815
Joe Onorato8a9b2202010-02-26 18:56:32 -08007816 Slog.i(TAG, "System now ready");
Doug Zongker2bec3d42009-12-04 12:52:44 -08007817 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 SystemClock.uptimeMillis());
7819
7820 synchronized(this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007821 // Make sure we have no pre-ready processes sitting around.
7822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
7824 ResolveInfo ri = mContext.getPackageManager()
7825 .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
Dianne Hackborn1655be42009-05-08 14:29:01 -07007826 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 CharSequence errorMsg = null;
7828 if (ri != null) {
7829 ActivityInfo ai = ri.activityInfo;
7830 ApplicationInfo app = ai.applicationInfo;
7831 if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
7832 mTopAction = Intent.ACTION_FACTORY_TEST;
7833 mTopData = null;
7834 mTopComponent = new ComponentName(app.packageName,
7835 ai.name);
7836 } else {
7837 errorMsg = mContext.getResources().getText(
7838 com.android.internal.R.string.factorytest_not_system);
7839 }
7840 } else {
7841 errorMsg = mContext.getResources().getText(
7842 com.android.internal.R.string.factorytest_no_action);
7843 }
7844 if (errorMsg != null) {
7845 mTopAction = null;
7846 mTopData = null;
7847 mTopComponent = null;
7848 Message msg = Message.obtain();
7849 msg.what = SHOW_FACTORY_ERROR_MSG;
7850 msg.getData().putCharSequence("msg", errorMsg);
7851 mHandler.sendMessage(msg);
7852 }
7853 }
7854 }
7855
7856 retrieveSettings();
7857
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007858 if (goingCallback != null) goingCallback.run();
7859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 synchronized (this) {
7861 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
7862 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007863 List apps = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07007864 getPersistentApplications(STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865 if (apps != null) {
7866 int N = apps.size();
7867 int i;
7868 for (i=0; i<N; i++) {
7869 ApplicationInfo info
7870 = (ApplicationInfo)apps.get(i);
7871 if (info != null &&
7872 !info.packageName.equals("android")) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007873 addAppLocked(info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 }
7875 }
7876 }
7877 } catch (RemoteException ex) {
7878 // pm is in same process, this will never happen.
7879 }
7880 }
7881
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007882 // Start up initial activity.
7883 mBooting = true;
7884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007886 if (AppGlobals.getPackageManager().hasSystemUidErrors()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 Message msg = Message.obtain();
7888 msg.what = SHOW_UID_ERROR_MSG;
7889 mHandler.sendMessage(msg);
7890 }
7891 } catch (RemoteException e) {
7892 }
7893
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07007894 long ident = Binder.clearCallingIdentity();
7895 try {
7896 Intent intent = new Intent(Intent.ACTION_USER_STARTED);
7897 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
7898 intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId);
7899 broadcastIntentLocked(null, null, intent,
7900 null, null, 0, null, null, null,
7901 false, false, MY_PID, Process.SYSTEM_UID, mCurrentUserId);
7902 } finally {
7903 Binder.restoreCallingIdentity(ident);
7904 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007905 mMainStack.resumeTopActivityLocked(null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07007906 sendUserSwitchBroadcastsLocked(-1, mCurrentUserId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 }
7908 }
7909
Dan Egnorb7f03672009-12-09 16:22:32 -08007910 private boolean makeAppCrashingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007911 String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 app.crashing = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08007913 app.crashingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08007914 ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 startAppProblemLocked(app);
7916 app.stopFreezingAllLocked();
7917 return handleAppCrashLocked(app);
7918 }
7919
Dan Egnorb7f03672009-12-09 16:22:32 -08007920 private void makeAppNotRespondingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007921 String activity, String shortMsg, String longMsg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 app.notResponding = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08007923 app.notRespondingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08007924 ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
7925 activity, shortMsg, longMsg, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007926 startAppProblemLocked(app);
7927 app.stopFreezingAllLocked();
7928 }
7929
7930 /**
7931 * Generate a process error record, suitable for attachment to a ProcessRecord.
7932 *
7933 * @param app The ProcessRecord in which the error occurred.
7934 * @param condition Crashing, Application Not Responding, etc. Values are defined in
7935 * ActivityManager.AppErrorStateInfo
Dan Egnor60d87622009-12-16 16:32:58 -08007936 * @param activity The activity associated with the crash, if known.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 * @param shortMsg Short message describing the crash.
7938 * @param longMsg Long message describing the crash.
Dan Egnorb7f03672009-12-09 16:22:32 -08007939 * @param stackTrace Full crash stack trace, may be null.
7940 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 * @return Returns a fully-formed AppErrorStateInfo record.
7942 */
7943 private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007944 int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007945 ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
Dan Egnorb7f03672009-12-09 16:22:32 -08007946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 report.condition = condition;
7948 report.processName = app.processName;
7949 report.pid = app.pid;
7950 report.uid = app.info.uid;
Dan Egnor60d87622009-12-16 16:32:58 -08007951 report.tag = activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 report.shortMsg = shortMsg;
7953 report.longMsg = longMsg;
Dan Egnorb7f03672009-12-09 16:22:32 -08007954 report.stackTrace = stackTrace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007955
7956 return report;
7957 }
7958
Dan Egnor42471dd2010-01-07 17:25:22 -08007959 void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007960 synchronized (this) {
7961 app.crashing = false;
7962 app.crashingReport = null;
7963 app.notResponding = false;
7964 app.notRespondingReport = null;
7965 if (app.anrDialog == fromDialog) {
7966 app.anrDialog = null;
7967 }
7968 if (app.waitDialog == fromDialog) {
7969 app.waitDialog = null;
7970 }
7971 if (app.pid > 0 && app.pid != MY_PID) {
Dan Egnor42471dd2010-01-07 17:25:22 -08007972 handleAppCrashLocked(app);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07007973 Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request");
Dianne Hackborn8633e682010-04-22 16:03:41 -07007974 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
7975 app.processName, app.setAdj, "user's request after error");
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07007976 Process.killProcessQuiet(app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 }
7979 }
Dan Egnor42471dd2010-01-07 17:25:22 -08007980
Dan Egnorb7f03672009-12-09 16:22:32 -08007981 private boolean handleAppCrashLocked(ProcessRecord app) {
Mike Lockwood86548c42011-09-13 17:21:46 -04007982 if (mHeadless) {
7983 Log.e(TAG, "handleAppCrashLocked: " + app.processName);
7984 return false;
7985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 long now = SystemClock.uptimeMillis();
7987
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007988 Long crashTime;
7989 if (!app.isolated) {
7990 crashTime = mProcessCrashTimes.get(app.info.processName, app.uid);
7991 } else {
7992 crashTime = null;
7993 }
Dianne Hackborn7d608422011-08-07 16:24:18 -07007994 if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007995 // This process loses!
Joe Onorato8a9b2202010-02-26 18:56:32 -08007996 Slog.w(TAG, "Process " + app.info.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 + " has crashed too many times: killing!");
Doug Zongker2bec3d42009-12-04 12:52:44 -08007998 EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007999 app.info.processName, app.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008000 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
8001 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 if (r.app == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008003 Slog.w(TAG, " Force finishing activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07008005 r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED,
8006 null, "crashed", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 }
8008 }
8009 if (!app.persistent) {
8010 // We don't want to start this process again until the user
8011 // explicitly does so... but for persistent process, we really
8012 // need to keep it running. If a persistent process is actually
8013 // repeatedly crashing, then badness for everyone.
Dianne Hackborna0c283e2012-02-09 10:47:01 -08008014 EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 app.info.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08008016 if (!app.isolated) {
8017 // XXX We don't have a way to mark isolated processes
8018 // as bad, since they don't have a peristent identity.
8019 mBadProcesses.put(app.info.processName, app.uid, now);
8020 mProcessCrashTimes.remove(app.info.processName, app.uid);
8021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 app.bad = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 app.removed = true;
Dianne Hackborn130b0d22011-07-26 22:07:48 -07008024 // Don't let services in this process be restarted and potentially
8025 // annoy the user repeatedly. Unless it is persistent, since those
8026 // processes run critical code.
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08008027 removeProcessLocked(app, false, false, "crash");
Dianne Hackborncb44d962011-03-10 17:02:27 -08008028 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 return false;
8030 }
Dianne Hackborncb44d962011-03-10 17:02:27 -08008031 mMainStack.resumeTopActivityLocked(null);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07008032 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008033 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Kevin Hester-Chowd87a9be2012-03-05 08:01:00 -08008034 if (r != null && r.app == app) {
Dianne Hackbornf83c5552010-03-31 22:19:32 -07008035 // If the top running activity is from this crashing
8036 // process, then terminate it to avoid getting in a loop.
8037 Slog.w(TAG, " Force finishing activity "
8038 + r.intent.getComponent().flattenToShortString());
Dianne Hackbornbe707852011-11-11 14:32:10 -08008039 int index = mMainStack.indexOfActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008040 r.stack.finishActivityLocked(r, index,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07008041 Activity.RESULT_CANCELED, null, "crashed", false);
Dianne Hackborn070783f2010-12-29 16:46:28 -08008042 // Also terminate any activities below it that aren't yet
Dianne Hackbornf83c5552010-03-31 22:19:32 -07008043 // stopped, to avoid a situation where one will get
8044 // re-start our crashing activity once it gets resumed again.
8045 index--;
8046 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008047 r = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07008048 if (r.state == ActivityState.RESUMED
8049 || r.state == ActivityState.PAUSING
8050 || r.state == ActivityState.PAUSED) {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08008051 if (!r.isHomeActivity || mHomeProcess != r.app) {
Dianne Hackbornf83c5552010-03-31 22:19:32 -07008052 Slog.w(TAG, " Force finishing activity "
8053 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008054 r.stack.finishActivityLocked(r, index,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07008055 Activity.RESULT_CANCELED, null, "crashed", false);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07008056 }
8057 }
8058 }
8059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 }
8061
8062 // Bump up the crash count of any services currently running in the proc.
8063 if (app.services.size() != 0) {
8064 // Any services running in the application need to be placed
8065 // back in the pending list.
Dianne Hackborn860755f2010-06-03 18:47:52 -07008066 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008067 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008068 ServiceRecord sr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 sr.crashCount++;
8070 }
8071 }
Mattias Larssona4fd0072010-06-22 22:37:03 +02008072
8073 // If the crashing process is what we consider to be the "home process" and it has been
8074 // replaced by a third-party app, clear the package preferred activities from packages
8075 // with a home activity running in the process to prevent a repeatedly crashing app
8076 // from blocking the user to manually clear the list.
8077 if (app == mHomeProcess && mHomeProcess.activities.size() > 0
8078 && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
8079 Iterator it = mHomeProcess.activities.iterator();
8080 while (it.hasNext()) {
Jean-Baptiste Queru5ea89f72010-07-30 09:30:31 -07008081 ActivityRecord r = (ActivityRecord)it.next();
Mattias Larssona4fd0072010-06-22 22:37:03 +02008082 if (r.isHomeActivity) {
8083 Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
8084 try {
8085 ActivityThread.getPackageManager()
8086 .clearPackagePreferredActivities(r.packageName);
8087 } catch (RemoteException c) {
8088 // pm is in same process, this will never happen.
8089 }
8090 }
8091 }
8092 }
8093
Dianne Hackborna0c283e2012-02-09 10:47:01 -08008094 if (!app.isolated) {
8095 // XXX Can't keep track of crash times for isolated processes,
8096 // because they don't have a perisistent identity.
8097 mProcessCrashTimes.put(app.info.processName, app.uid, now);
8098 }
8099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 return true;
8101 }
8102
8103 void startAppProblemLocked(ProcessRecord app) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08008104 app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
8105 mContext, app.info.packageName, app.info.flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008106 skipCurrentReceiverLocked(app);
8107 }
8108
8109 void skipCurrentReceiverLocked(ProcessRecord app) {
Christopher Tatef46723b2012-01-26 14:19:24 -08008110 for (BroadcastQueue queue : mBroadcastQueues) {
8111 queue.skipCurrentReceiverLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008112 }
8113 }
8114
Dan Egnor60d87622009-12-16 16:32:58 -08008115 /**
8116 * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes.
8117 * The application process will exit immediately after this call returns.
8118 * @param app object of the crashing app, null for the system server
8119 * @param crashInfo describing the exception
8120 */
8121 public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08008122 ProcessRecord r = findAppProcess(app, "Crash");
Jeff Sharkeya353d262011-10-28 11:12:06 -07008123 final String processName = app == null ? "system_server"
8124 : (r == null ? "unknown" : r.processName);
Dan Egnor60d87622009-12-16 16:32:58 -08008125
8126 EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(),
Jeff Sharkeya353d262011-10-28 11:12:06 -07008127 processName,
Dan Egnor2780e732010-01-22 14:47:35 -08008128 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08008129 crashInfo.exceptionClassName,
8130 crashInfo.exceptionMessage,
8131 crashInfo.throwFileName,
8132 crashInfo.throwLineNumber);
8133
Jeff Sharkeya353d262011-10-28 11:12:06 -07008134 addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08008135
8136 crashApplication(r, crashInfo);
8137 }
8138
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07008139 public void handleApplicationStrictModeViolation(
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008140 IBinder app,
8141 int violationMask,
8142 StrictMode.ViolationInfo info) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08008143 ProcessRecord r = findAppProcess(app, "StrictMode");
8144 if (r == null) {
8145 return;
8146 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07008147
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07008148 if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) {
Brad Fitzpatrickf3d86be2010-11-23 10:31:52 -08008149 Integer stackFingerprint = info.hashCode();
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07008150 boolean logIt = true;
8151 synchronized (mAlreadyLoggedViolatedStacks) {
8152 if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) {
8153 logIt = false;
8154 // TODO: sub-sample into EventLog for these, with
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008155 // the info.durationMillis? Then we'd get
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07008156 // the relative pain numbers, without logging all
8157 // the stack traces repeatedly. We'd want to do
8158 // likewise in the client code, which also does
8159 // dup suppression, before the Binder call.
8160 } else {
8161 if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) {
8162 mAlreadyLoggedViolatedStacks.clear();
8163 }
8164 mAlreadyLoggedViolatedStacks.add(stackFingerprint);
8165 }
8166 }
8167 if (logIt) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008168 logStrictModeViolationToDropBox(r, info);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07008169 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07008170 }
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07008171
8172 if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) {
8173 AppErrorResult result = new AppErrorResult();
8174 synchronized (this) {
8175 final long origId = Binder.clearCallingIdentity();
8176
8177 Message msg = Message.obtain();
8178 msg.what = SHOW_STRICT_MODE_VIOLATION_MSG;
8179 HashMap<String, Object> data = new HashMap<String, Object>();
8180 data.put("result", result);
8181 data.put("app", r);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07008182 data.put("violationMask", violationMask);
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008183 data.put("info", info);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07008184 msg.obj = data;
8185 mHandler.sendMessage(msg);
8186
8187 Binder.restoreCallingIdentity(origId);
8188 }
8189 int res = result.get();
Dianne Hackbornb424b632010-08-18 15:59:05 -07008190 Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07008191 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07008192 }
8193
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008194 // Depending on the policy in effect, there could be a bunch of
8195 // these in quick succession so we try to batch these together to
8196 // minimize disk writes, number of dropbox entries, and maximize
8197 // compression, by having more fewer, larger records.
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008198 private void logStrictModeViolationToDropBox(
8199 ProcessRecord process,
8200 StrictMode.ViolationInfo info) {
8201 if (info == null) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008202 return;
8203 }
8204 final boolean isSystemApp = process == null ||
8205 (process.info.flags & (ApplicationInfo.FLAG_SYSTEM |
8206 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
Jeff Sharkeya353d262011-10-28 11:12:06 -07008207 final String processName = process == null ? "unknown" : process.processName;
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008208 final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode";
8209 final DropBoxManager dbox = (DropBoxManager)
8210 mContext.getSystemService(Context.DROPBOX_SERVICE);
8211
8212 // Exit early if the dropbox isn't configured to accept this report type.
8213 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
8214
8215 boolean bufferWasEmpty;
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008216 boolean needsFlush;
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008217 final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024);
8218 synchronized (sb) {
8219 bufferWasEmpty = sb.length() == 0;
Jeff Sharkeya353d262011-10-28 11:12:06 -07008220 appendDropBoxProcessHeaders(process, processName, sb);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008221 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
8222 sb.append("System-App: ").append(isSystemApp).append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008223 sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n");
8224 if (info.violationNumThisLoop != 0) {
8225 sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n");
8226 }
Brad Fitzpatrick599ca292010-10-22 14:47:03 -07008227 if (info.numAnimationsRunning != 0) {
8228 sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n");
8229 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07008230 if (info.broadcastIntentAction != null) {
8231 sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n");
8232 }
Brad Fitzpatricke7520d82010-11-10 18:08:36 -08008233 if (info.durationMillis != -1) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008234 sb.append("Duration-Millis: ").append(info.durationMillis).append("\n");
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008235 }
Brad Fitzpatrickbfbe5772011-01-19 00:10:58 -08008236 if (info.numInstances != -1) {
8237 sb.append("Instance-Count: ").append(info.numInstances).append("\n");
8238 }
Brad Fitzpatricke7520d82010-11-10 18:08:36 -08008239 if (info.tags != null) {
8240 for (String tag : info.tags) {
8241 sb.append("Span-Tag: ").append(tag).append("\n");
8242 }
8243 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008244 sb.append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008245 if (info.crashInfo != null && info.crashInfo.stackTrace != null) {
8246 sb.append(info.crashInfo.stackTrace);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008247 }
8248 sb.append("\n");
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008249
8250 // Only buffer up to ~64k. Various logging bits truncate
8251 // things at 128k.
8252 needsFlush = (sb.length() > 64 * 1024);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008253 }
8254
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008255 // Flush immediately if the buffer's grown too large, or this
8256 // is a non-system app. Non-system apps are isolated with a
8257 // different tag & policy and not batched.
8258 //
8259 // Batching is useful during internal testing with
8260 // StrictMode settings turned up high. Without batching,
8261 // thousands of separate files could be created on boot.
8262 if (!isSystemApp || needsFlush) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008263 new Thread("Error dump: " + dropboxTag) {
8264 @Override
8265 public void run() {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008266 String report;
8267 synchronized (sb) {
8268 report = sb.toString();
8269 sb.delete(0, sb.length());
8270 sb.trimToSize();
8271 }
8272 if (report.length() != 0) {
8273 dbox.addText(dropboxTag, report);
8274 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008275 }
8276 }.start();
8277 return;
8278 }
8279
8280 // System app batching:
8281 if (!bufferWasEmpty) {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008282 // An existing dropbox-writing thread is outstanding, so
8283 // we don't need to start it up. The existing thread will
8284 // catch the buffer appends we just did.
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008285 return;
8286 }
8287
8288 // Worker thread to both batch writes and to avoid blocking the caller on I/O.
8289 // (After this point, we shouldn't access AMS internal data structures.)
8290 new Thread("Error dump: " + dropboxTag) {
8291 @Override
8292 public void run() {
8293 // 5 second sleep to let stacks arrive and be batched together
8294 try {
8295 Thread.sleep(5000); // 5 seconds
8296 } catch (InterruptedException e) {}
8297
8298 String errorReport;
8299 synchronized (mStrictModeBuffer) {
8300 errorReport = mStrictModeBuffer.toString();
8301 if (errorReport.length() == 0) {
8302 return;
8303 }
8304 mStrictModeBuffer.delete(0, mStrictModeBuffer.length());
8305 mStrictModeBuffer.trimToSize();
8306 }
8307 dbox.addText(dropboxTag, errorReport);
8308 }
8309 }.start();
8310 }
8311
Dan Egnor60d87622009-12-16 16:32:58 -08008312 /**
8313 * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors.
8314 * @param app object of the crashing app, null for the system server
8315 * @param tag reported by the caller
8316 * @param crashInfo describing the context of the error
8317 * @return true if the process should exit immediately (WTF is fatal)
8318 */
8319 public boolean handleApplicationWtf(IBinder app, String tag,
Dan Egnorb7f03672009-12-09 16:22:32 -08008320 ApplicationErrorReport.CrashInfo crashInfo) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08008321 ProcessRecord r = findAppProcess(app, "WTF");
Jeff Sharkeya353d262011-10-28 11:12:06 -07008322 final String processName = app == null ? "system_server"
8323 : (r == null ? "unknown" : r.processName);
Dan Egnor60d87622009-12-16 16:32:58 -08008324
8325 EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(),
Jeff Sharkeya353d262011-10-28 11:12:06 -07008326 processName,
Dan Egnor2780e732010-01-22 14:47:35 -08008327 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08008328 tag, crashInfo.exceptionMessage);
8329
Jeff Sharkeya353d262011-10-28 11:12:06 -07008330 addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08008331
Dianne Hackborn1ab43772011-03-15 14:38:02 -07008332 if (r != null && r.pid != Process.myPid() &&
8333 Settings.Secure.getInt(mContext.getContentResolver(),
8334 Settings.Secure.WTF_IS_FATAL, 0) != 0) {
Dan Egnor60d87622009-12-16 16:32:58 -08008335 crashApplication(r, crashInfo);
8336 return true;
8337 } else {
8338 return false;
8339 }
8340 }
8341
8342 /**
8343 * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit})
8344 * @return the corresponding {@link ProcessRecord} object, or null if none could be found
8345 */
Dianne Hackborncb44d962011-03-10 17:02:27 -08008346 private ProcessRecord findAppProcess(IBinder app, String reason) {
Dan Egnor60d87622009-12-16 16:32:58 -08008347 if (app == null) {
8348 return null;
8349 }
8350
8351 synchronized (this) {
8352 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
8353 final int NA = apps.size();
8354 for (int ia=0; ia<NA; ia++) {
8355 ProcessRecord p = apps.valueAt(ia);
8356 if (p.thread != null && p.thread.asBinder() == app) {
8357 return p;
8358 }
8359 }
8360 }
8361
Dianne Hackborncb44d962011-03-10 17:02:27 -08008362 Slog.w(TAG, "Can't find mystery application for " + reason
8363 + " from pid=" + Binder.getCallingPid()
8364 + " uid=" + Binder.getCallingUid() + ": " + app);
Dan Egnor60d87622009-12-16 16:32:58 -08008365 return null;
8366 }
8367 }
8368
8369 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008370 * Utility function for addErrorToDropBox and handleStrictModeViolation's logging
8371 * to append various headers to the dropbox log text.
Dan Egnor60d87622009-12-16 16:32:58 -08008372 */
Jeff Sharkeya353d262011-10-28 11:12:06 -07008373 private void appendDropBoxProcessHeaders(ProcessRecord process, String processName,
8374 StringBuilder sb) {
Vairavan Srinivasan68a4e0a2011-02-14 20:45:59 -08008375 // Watchdog thread ends up invoking this function (with
8376 // a null ProcessRecord) to add the stack file to dropbox.
8377 // Do not acquire a lock on this (am) in such cases, as it
8378 // could cause a potential deadlock, if and when watchdog
8379 // is invoked due to unavailability of lock on am and it
8380 // would prevent watchdog from killing system_server.
8381 if (process == null) {
Jeff Sharkeya353d262011-10-28 11:12:06 -07008382 sb.append("Process: ").append(processName).append("\n");
Vairavan Srinivasan68a4e0a2011-02-14 20:45:59 -08008383 return;
8384 }
Brad Fitzpatrick1e02d362010-09-10 09:19:50 -07008385 // Note: ProcessRecord 'process' is guarded by the service
8386 // instance. (notably process.pkgList, which could otherwise change
8387 // concurrently during execution of this method)
8388 synchronized (this) {
Jeff Sharkeya353d262011-10-28 11:12:06 -07008389 sb.append("Process: ").append(processName).append("\n");
Dan Egnora455d192010-03-12 08:52:28 -08008390 int flags = process.info.flags;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008391 IPackageManager pm = AppGlobals.getPackageManager();
Dan Egnora455d192010-03-12 08:52:28 -08008392 sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
8393 for (String pkg : process.pkgList) {
8394 sb.append("Package: ").append(pkg);
Dan Egnor42471dd2010-01-07 17:25:22 -08008395 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07008396 PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId());
Dan Egnora455d192010-03-12 08:52:28 -08008397 if (pi != null) {
8398 sb.append(" v").append(pi.versionCode);
8399 if (pi.versionName != null) {
8400 sb.append(" (").append(pi.versionName).append(")");
8401 }
8402 }
8403 } catch (RemoteException e) {
8404 Slog.e(TAG, "Error getting package info: " + pkg, e);
Dan Egnor60d87622009-12-16 16:32:58 -08008405 }
Dan Egnora455d192010-03-12 08:52:28 -08008406 sb.append("\n");
Dan Egnor60d87622009-12-16 16:32:58 -08008407 }
Dan Egnora455d192010-03-12 08:52:28 -08008408 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008409 }
8410
8411 private static String processClass(ProcessRecord process) {
8412 if (process == null || process.pid == MY_PID) {
8413 return "system_server";
8414 } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
8415 return "system_app";
8416 } else {
8417 return "data_app";
8418 }
8419 }
8420
8421 /**
8422 * Write a description of an error (crash, WTF, ANR) to the drop box.
8423 * @param eventType to include in the drop box tag ("crash", "wtf", etc.)
8424 * @param process which caused the error, null means the system server
8425 * @param activity which triggered the error, null if unknown
8426 * @param parent activity related to the error, null if unknown
8427 * @param subject line related to the error, null if absent
8428 * @param report in long form describing the error, null if absent
8429 * @param logFile to include in the report, null if none
8430 * @param crashInfo giving an application stack trace, null if absent
8431 */
8432 public void addErrorToDropBox(String eventType,
Jeff Sharkeya353d262011-10-28 11:12:06 -07008433 ProcessRecord process, String processName, ActivityRecord activity,
8434 ActivityRecord parent, String subject,
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008435 final String report, final File logFile,
8436 final ApplicationErrorReport.CrashInfo crashInfo) {
8437 // NOTE -- this must never acquire the ActivityManagerService lock,
8438 // otherwise the watchdog may be prevented from resetting the system.
8439
8440 final String dropboxTag = processClass(process) + "_" + eventType;
8441 final DropBoxManager dbox = (DropBoxManager)
8442 mContext.getSystemService(Context.DROPBOX_SERVICE);
8443
8444 // Exit early if the dropbox isn't configured to accept this report type.
8445 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
8446
8447 final StringBuilder sb = new StringBuilder(1024);
Jeff Sharkeya353d262011-10-28 11:12:06 -07008448 appendDropBoxProcessHeaders(process, processName, sb);
Dan Egnora455d192010-03-12 08:52:28 -08008449 if (activity != null) {
8450 sb.append("Activity: ").append(activity.shortComponentName).append("\n");
8451 }
8452 if (parent != null && parent.app != null && parent.app.pid != process.pid) {
8453 sb.append("Parent-Process: ").append(parent.app.processName).append("\n");
8454 }
8455 if (parent != null && parent != activity) {
8456 sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n");
8457 }
8458 if (subject != null) {
8459 sb.append("Subject: ").append(subject).append("\n");
8460 }
8461 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
Christian Lindeberg03d2ca62010-09-28 14:52:20 +02008462 if (Debug.isDebuggerConnected()) {
8463 sb.append("Debugger: Connected\n");
8464 }
Dan Egnora455d192010-03-12 08:52:28 -08008465 sb.append("\n");
8466
8467 // Do the rest in a worker thread to avoid blocking the caller on I/O
8468 // (After this point, we shouldn't access AMS internal data structures.)
8469 Thread worker = new Thread("Error dump: " + dropboxTag) {
8470 @Override
8471 public void run() {
8472 if (report != null) {
8473 sb.append(report);
8474 }
8475 if (logFile != null) {
8476 try {
8477 sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]"));
8478 } catch (IOException e) {
8479 Slog.e(TAG, "Error reading " + logFile, e);
8480 }
8481 }
8482 if (crashInfo != null && crashInfo.stackTrace != null) {
8483 sb.append(crashInfo.stackTrace);
8484 }
8485
8486 String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag;
8487 int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0);
8488 if (lines > 0) {
8489 sb.append("\n");
8490
8491 // Merge several logcat streams, and take the last N lines
8492 InputStreamReader input = null;
8493 try {
8494 java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat",
8495 "-v", "time", "-b", "events", "-b", "system", "-b", "main",
8496 "-t", String.valueOf(lines)).redirectErrorStream(true).start();
8497
8498 try { logcat.getOutputStream().close(); } catch (IOException e) {}
8499 try { logcat.getErrorStream().close(); } catch (IOException e) {}
8500 input = new InputStreamReader(logcat.getInputStream());
8501
8502 int num;
8503 char[] buf = new char[8192];
8504 while ((num = input.read(buf)) > 0) sb.append(buf, 0, num);
8505 } catch (IOException e) {
8506 Slog.e(TAG, "Error running logcat", e);
8507 } finally {
8508 if (input != null) try { input.close(); } catch (IOException e) {}
8509 }
8510 }
8511
8512 dbox.addText(dropboxTag, sb.toString());
Dan Egnor60d87622009-12-16 16:32:58 -08008513 }
Dan Egnora455d192010-03-12 08:52:28 -08008514 };
8515
Dianne Hackborn56385cc2012-04-30 15:07:47 -07008516 if (process == null) {
8517 // If process is null, we are being called from some internal code
8518 // and may be about to die -- run this synchronously.
8519 worker.run();
Dan Egnora455d192010-03-12 08:52:28 -08008520 } else {
8521 worker.start();
Dan Egnor60d87622009-12-16 16:32:58 -08008522 }
8523 }
8524
8525 /**
8526 * Bring up the "unexpected error" dialog box for a crashing app.
8527 * Deal with edge cases (intercepts from instrumented applications,
8528 * ActivityController, error intent receivers, that sort of thing).
8529 * @param r the application crashing
8530 * @param crashInfo describing the failure
8531 */
8532 private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnorb7f03672009-12-09 16:22:32 -08008533 long timeMillis = System.currentTimeMillis();
8534 String shortMsg = crashInfo.exceptionClassName;
8535 String longMsg = crashInfo.exceptionMessage;
8536 String stackTrace = crashInfo.stackTrace;
8537 if (shortMsg != null && longMsg != null) {
8538 longMsg = shortMsg + ": " + longMsg;
8539 } else if (shortMsg != null) {
8540 longMsg = shortMsg;
8541 }
8542
Dan Egnor60d87622009-12-16 16:32:58 -08008543 AppErrorResult result = new AppErrorResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008544 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008545 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 try {
8547 String name = r != null ? r.processName : null;
8548 int pid = r != null ? r.pid : Binder.getCallingPid();
Dan Egnor60d87622009-12-16 16:32:58 -08008549 if (!mController.appCrashed(name, pid,
Dan Egnorb7f03672009-12-09 16:22:32 -08008550 shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008551 Slog.w(TAG, "Force-killing crashed app " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008552 + " at watcher's request");
8553 Process.killProcess(pid);
Dan Egnorb7f03672009-12-09 16:22:32 -08008554 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 }
8556 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008557 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008558 }
8559 }
8560
8561 final long origId = Binder.clearCallingIdentity();
8562
8563 // If this process is running instrumentation, finish it.
8564 if (r != null && r.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008565 Slog.w(TAG, "Error in app " + r.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 + " running instrumentation " + r.instrumentationClass + ":");
Joe Onorato8a9b2202010-02-26 18:56:32 -08008567 if (shortMsg != null) Slog.w(TAG, " " + shortMsg);
8568 if (longMsg != null) Slog.w(TAG, " " + longMsg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008569 Bundle info = new Bundle();
8570 info.putString("shortMsg", shortMsg);
8571 info.putString("longMsg", longMsg);
8572 finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info);
8573 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08008574 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 }
8576
Dan Egnor60d87622009-12-16 16:32:58 -08008577 // If we can't identify the process or it's already exceeded its crash quota,
8578 // quit right away without showing a crash dialog.
8579 if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008580 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08008581 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 }
8583
8584 Message msg = Message.obtain();
8585 msg.what = SHOW_ERROR_MSG;
8586 HashMap data = new HashMap();
8587 data.put("result", result);
8588 data.put("app", r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 msg.obj = data;
8590 mHandler.sendMessage(msg);
8591
8592 Binder.restoreCallingIdentity(origId);
8593 }
8594
8595 int res = result.get();
8596
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008597 Intent appErrorIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008598 synchronized (this) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08008599 if (r != null && !r.isolated) {
8600 // XXX Can't keep track of crash time for isolated processes,
8601 // since they don't have a persistent identity.
8602 mProcessCrashTimes.put(r.info.processName, r.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 SystemClock.uptimeMillis());
8604 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008605 if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
Dan Egnorb7f03672009-12-09 16:22:32 -08008606 appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008607 }
8608 }
8609
8610 if (appErrorIntent != null) {
8611 try {
8612 mContext.startActivity(appErrorIntent);
8613 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008614 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008617 }
Dan Egnorb7f03672009-12-09 16:22:32 -08008618
8619 Intent createAppErrorIntentLocked(ProcessRecord r,
8620 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
8621 ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008622 if (report == null) {
8623 return null;
8624 }
8625 Intent result = new Intent(Intent.ACTION_APP_ERROR);
8626 result.setComponent(r.errorReportReceiver);
8627 result.putExtra(Intent.EXTRA_BUG_REPORT, report);
8628 result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
8629 return result;
8630 }
8631
Dan Egnorb7f03672009-12-09 16:22:32 -08008632 private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
8633 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008634 if (r.errorReportReceiver == null) {
8635 return null;
8636 }
8637
8638 if (!r.crashing && !r.notResponding) {
8639 return null;
8640 }
8641
Dan Egnorb7f03672009-12-09 16:22:32 -08008642 ApplicationErrorReport report = new ApplicationErrorReport();
8643 report.packageName = r.info.packageName;
8644 report.installerPackageName = r.errorReportReceiver.getPackageName();
8645 report.processName = r.processName;
8646 report.time = timeMillis;
Jacek Surazskie0ee6ef2010-01-07 16:23:03 +01008647 report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008648
Dan Egnorb7f03672009-12-09 16:22:32 -08008649 if (r.crashing) {
8650 report.type = ApplicationErrorReport.TYPE_CRASH;
8651 report.crashInfo = crashInfo;
8652 } else if (r.notResponding) {
8653 report.type = ApplicationErrorReport.TYPE_ANR;
8654 report.anrInfo = new ApplicationErrorReport.AnrInfo();
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008655
Dan Egnorb7f03672009-12-09 16:22:32 -08008656 report.anrInfo.activity = r.notRespondingReport.tag;
8657 report.anrInfo.cause = r.notRespondingReport.shortMsg;
8658 report.anrInfo.info = r.notRespondingReport.longMsg;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008659 }
8660
Dan Egnorb7f03672009-12-09 16:22:32 -08008661 return report;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008662 }
8663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008665 enforceNotIsolatedCaller("getProcessesInErrorState");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 // assume our apps are happy - lazy create the list
8667 List<ActivityManager.ProcessErrorStateInfo> errList = null;
8668
Dianne Hackborn0c380492012-08-20 17:23:30 -07008669 final boolean allUsers = ActivityManager.checkUidPermission(
8670 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
8671 Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED;
8672 int userId = UserHandle.getUserId(Binder.getCallingUid());
8673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 synchronized (this) {
8675
8676 // iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008677 for (int i=mLruProcesses.size()-1; i>=0; i--) {
8678 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn0c380492012-08-20 17:23:30 -07008679 if (!allUsers && app.userId != userId) {
8680 continue;
8681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 if ((app.thread != null) && (app.crashing || app.notResponding)) {
8683 // This one's in trouble, so we'll generate a report for it
8684 // crashes are higher priority (in case there's a crash *and* an anr)
8685 ActivityManager.ProcessErrorStateInfo report = null;
8686 if (app.crashing) {
8687 report = app.crashingReport;
8688 } else if (app.notResponding) {
8689 report = app.notRespondingReport;
8690 }
8691
8692 if (report != null) {
8693 if (errList == null) {
8694 errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1);
8695 }
8696 errList.add(report);
8697 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008698 Slog.w(TAG, "Missing app error report, app = " + app.processName +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 " crashing = " + app.crashing +
8700 " notResponding = " + app.notResponding);
8701 }
8702 }
8703 }
8704 }
8705
8706 return errList;
8707 }
Dianne Hackborn905577f2011-09-07 18:31:28 -07008708
8709 static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) {
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008710 if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn905577f2011-09-07 18:31:28 -07008711 if (currApp != null) {
8712 currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1;
8713 }
8714 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008715 } else if (adj >= ProcessList.SERVICE_B_ADJ) {
8716 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
Dianne Hackborn905577f2011-09-07 18:31:28 -07008717 } else if (adj >= ProcessList.HOME_APP_ADJ) {
8718 if (currApp != null) {
8719 currApp.lru = 0;
8720 }
8721 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008722 } else if (adj >= ProcessList.SERVICE_ADJ) {
Dianne Hackborn905577f2011-09-07 18:31:28 -07008723 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
8724 } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
8725 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
8726 } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
8727 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
8728 } else if (adj >= ProcessList.VISIBLE_APP_ADJ) {
8729 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
8730 } else {
8731 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
8732 }
8733 }
8734
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008735 private void fillInProcMemInfo(ProcessRecord app,
8736 ActivityManager.RunningAppProcessInfo outInfo) {
8737 outInfo.pid = app.pid;
8738 outInfo.uid = app.info.uid;
8739 if (mHeavyWeightProcess == app) {
8740 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE;
8741 }
8742 if (app.persistent) {
8743 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT;
8744 }
Dianne Hackborn0c380492012-08-20 17:23:30 -07008745 if (app.hasActivities) {
8746 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES;
8747 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008748 outInfo.lastTrimLevel = app.trimMemoryLevel;
8749 int adj = app.curAdj;
8750 outInfo.importance = oomAdjToImportance(adj, outInfo);
8751 outInfo.importanceReasonCode = app.adjTypeCode;
8752 }
8753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008755 enforceNotIsolatedCaller("getRunningAppProcesses");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 // Lazy instantiation of list
8757 List<ActivityManager.RunningAppProcessInfo> runList = null;
Dianne Hackborn0c380492012-08-20 17:23:30 -07008758 final boolean allUsers = ActivityManager.checkUidPermission(
8759 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
8760 Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED;
8761 int userId = UserHandle.getUserId(Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 synchronized (this) {
8763 // Iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008764 for (int i=mLruProcesses.size()-1; i>=0; i--) {
8765 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn0c380492012-08-20 17:23:30 -07008766 if (!allUsers && app.userId != userId) {
8767 continue;
8768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008769 if ((app.thread != null) && (!app.crashing && !app.notResponding)) {
8770 // Generate process state info for running application
8771 ActivityManager.RunningAppProcessInfo currApp =
8772 new ActivityManager.RunningAppProcessInfo(app.processName,
8773 app.pid, app.getPackageList());
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008774 fillInProcMemInfo(app, currApp);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008775 if (app.adjSource instanceof ProcessRecord) {
8776 currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid;
Dianne Hackborn905577f2011-09-07 18:31:28 -07008777 currApp.importanceReasonImportance = oomAdjToImportance(
8778 app.adjSourceOom, null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008779 } else if (app.adjSource instanceof ActivityRecord) {
8780 ActivityRecord r = (ActivityRecord)app.adjSource;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008781 if (r.app != null) currApp.importanceReasonPid = r.app.pid;
8782 }
8783 if (app.adjTarget instanceof ComponentName) {
8784 currApp.importanceReasonComponent = (ComponentName)app.adjTarget;
8785 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008786 //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 // + " lru=" + currApp.lru);
8788 if (runList == null) {
8789 runList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
8790 }
8791 runList.add(currApp);
8792 }
8793 }
8794 }
8795 return runList;
8796 }
8797
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008798 public List<ApplicationInfo> getRunningExternalApplications() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008799 enforceNotIsolatedCaller("getRunningExternalApplications");
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008800 List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses();
8801 List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>();
8802 if (runningApps != null && runningApps.size() > 0) {
8803 Set<String> extList = new HashSet<String>();
8804 for (ActivityManager.RunningAppProcessInfo app : runningApps) {
8805 if (app.pkgList != null) {
8806 for (String pkg : app.pkgList) {
8807 extList.add(pkg);
8808 }
8809 }
8810 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008811 IPackageManager pm = AppGlobals.getPackageManager();
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008812 for (String pkg : extList) {
8813 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07008814 ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId());
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008815 if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
8816 retList.add(info);
8817 }
8818 } catch (RemoteException e) {
8819 }
8820 }
8821 }
8822 return retList;
8823 }
8824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008825 @Override
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008826 public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) {
8827 enforceNotIsolatedCaller("getMyMemoryState");
8828 synchronized (this) {
8829 ProcessRecord proc;
8830 synchronized (mPidsSelfLocked) {
8831 proc = mPidsSelfLocked.get(Binder.getCallingPid());
8832 }
8833 fillInProcMemInfo(proc, outInfo);
8834 }
8835 }
8836
8837 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008838 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008839 if (checkCallingPermission(android.Manifest.permission.DUMP)
8840 != PackageManager.PERMISSION_GRANTED) {
8841 pw.println("Permission Denial: can't dump ActivityManager from from pid="
8842 + Binder.getCallingPid()
8843 + ", uid=" + Binder.getCallingUid()
8844 + " without permission "
8845 + android.Manifest.permission.DUMP);
8846 return;
8847 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008848
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008849 boolean dumpAll = false;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008850 boolean dumpClient = false;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008851 String dumpPackage = null;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008852
8853 int opti = 0;
8854 while (opti < args.length) {
8855 String opt = args[opti];
8856 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
8857 break;
8858 }
8859 opti++;
8860 if ("-a".equals(opt)) {
8861 dumpAll = true;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008862 } else if ("-c".equals(opt)) {
8863 dumpClient = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008864 } else if ("-h".equals(opt)) {
8865 pw.println("Activity manager dump options:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008866 pw.println(" [-a] [-c] [-h] [cmd] ...");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008867 pw.println(" cmd may be one of:");
Dianne Hackborn287952c2010-09-22 22:34:31 -07008868 pw.println(" a[ctivities]: activity stack state");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008869 pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state");
8870 pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state");
8871 pw.println(" p[rocesses] [PACKAGE_NAME]: process state");
Dianne Hackborn287952c2010-09-22 22:34:31 -07008872 pw.println(" o[om]: out of memory management");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008873 pw.println(" prov[iders] [COMP_SPEC ...]: content provider state");
Marco Nelissen18cb2872011-11-15 11:19:53 -08008874 pw.println(" provider [COMP_SPEC]: provider client-side state");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008875 pw.println(" s[ervices] [COMP_SPEC ...]: service state");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008876 pw.println(" service [COMP_SPEC]: service client-side state");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008877 pw.println(" package [PACKAGE_NAME]: all state related to given package");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008878 pw.println(" all: dump all activities");
8879 pw.println(" top: dump the top activity");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008880 pw.println(" cmd may also be a COMP_SPEC to dump activities.");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008881 pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),");
8882 pw.println(" a partial substring in a component name, a");
8883 pw.println(" hex object identifier.");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008884 pw.println(" -a: include all available server state.");
8885 pw.println(" -c: include client state.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008886 return;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008887 } else {
8888 pw.println("Unknown argument: " + opt + "; use -h for help");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008890 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008891
8892 long origId = Binder.clearCallingIdentity();
8893 boolean more = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008894 // Is the caller requesting to dump a particular piece of data?
8895 if (opti < args.length) {
8896 String cmd = args[opti];
8897 opti++;
8898 if ("activities".equals(cmd) || "a".equals(cmd)) {
8899 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008900 dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008901 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008902 } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008903 String[] newArgs;
8904 String name;
8905 if (opti >= args.length) {
8906 name = null;
8907 newArgs = EMPTY_STRING_ARRAY;
8908 } else {
8909 name = args[opti];
8910 opti++;
8911 newArgs = new String[args.length - opti];
8912 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8913 args.length - opti);
8914 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008915 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008916 dumpBroadcastsLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008917 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008918 } else if ("intents".equals(cmd) || "i".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008919 String[] newArgs;
8920 String name;
8921 if (opti >= args.length) {
8922 name = null;
8923 newArgs = EMPTY_STRING_ARRAY;
8924 } else {
8925 name = args[opti];
8926 opti++;
8927 newArgs = new String[args.length - opti];
8928 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8929 args.length - opti);
8930 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008931 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008932 dumpPendingIntentsLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008933 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008934 } else if ("processes".equals(cmd) || "p".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008935 String[] newArgs;
8936 String name;
8937 if (opti >= args.length) {
8938 name = null;
8939 newArgs = EMPTY_STRING_ARRAY;
8940 } else {
8941 name = args[opti];
8942 opti++;
8943 newArgs = new String[args.length - opti];
8944 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8945 args.length - opti);
8946 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008947 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008948 dumpProcessesLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008949 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07008950 } else if ("oom".equals(cmd) || "o".equals(cmd)) {
8951 synchronized (this) {
8952 dumpOomLocked(fd, pw, args, opti, true);
8953 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08008954 } else if ("provider".equals(cmd)) {
8955 String[] newArgs;
8956 String name;
8957 if (opti >= args.length) {
8958 name = null;
8959 newArgs = EMPTY_STRING_ARRAY;
8960 } else {
8961 name = args[opti];
8962 opti++;
8963 newArgs = new String[args.length - opti];
8964 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
8965 }
8966 if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) {
8967 pw.println("No providers match: " + name);
8968 pw.println("Use -h for help.");
8969 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008970 } else if ("providers".equals(cmd) || "prov".equals(cmd)) {
8971 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008972 dumpProvidersLocked(fd, pw, args, opti, true, null);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008973 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008974 } else if ("service".equals(cmd)) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008975 String[] newArgs;
8976 String name;
8977 if (opti >= args.length) {
8978 name = null;
8979 newArgs = EMPTY_STRING_ARRAY;
8980 } else {
8981 name = args[opti];
8982 opti++;
8983 newArgs = new String[args.length - opti];
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008984 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8985 args.length - opti);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008986 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07008987 if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008988 pw.println("No services match: " + name);
8989 pw.println("Use -h for help.");
8990 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008991 } else if ("package".equals(cmd)) {
8992 String[] newArgs;
8993 if (opti >= args.length) {
8994 pw.println("package: no package name specified");
8995 pw.println("Use -h for help.");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008996 } else {
8997 dumpPackage = args[opti];
8998 opti++;
8999 newArgs = new String[args.length - opti];
9000 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
9001 args.length - opti);
9002 args = newArgs;
9003 opti = 0;
Amith Yamasani7463ada2012-04-11 15:02:39 -07009004 more = true;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009005 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009006 } else if ("services".equals(cmd) || "s".equals(cmd)) {
9007 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -07009008 mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009009 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07009010 } else {
9011 // Dumping a single activity?
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009012 if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) {
9013 pw.println("Bad activity command, or no activities match: " + cmd);
9014 pw.println("Use -h for help.");
Dianne Hackborn625ac272010-09-17 18:29:22 -07009015 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07009016 }
9017 if (!more) {
9018 Binder.restoreCallingIdentity(origId);
Dianne Hackborn30d71892010-12-11 10:37:55 -08009019 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009020 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009021 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07009022
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009023 // No piece of data specified, dump everything.
9024 synchronized (this) {
9025 boolean needSep;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009026 needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009027 if (needSep) {
9028 pw.println(" ");
9029 }
9030 if (dumpAll) {
9031 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009032 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009033 needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009034 if (needSep) {
9035 pw.println(" ");
9036 }
9037 if (dumpAll) {
9038 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009039 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009040 needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009041 if (needSep) {
9042 pw.println(" ");
9043 }
9044 if (dumpAll) {
9045 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009046 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07009047 needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009048 if (needSep) {
9049 pw.println(" ");
9050 }
9051 if (dumpAll) {
9052 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009053 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009054 needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009055 if (needSep) {
9056 pw.println(" ");
9057 }
9058 if (dumpAll) {
9059 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009060 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009061 dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009062 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07009063 Binder.restoreCallingIdentity(origId);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009064 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07009065
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009066 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009067 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009068 pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)");
9069 pw.println(" Main stack:");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009070 dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient,
9071 dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009072 pw.println(" ");
9073 pw.println(" Running activities (most recent first):");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009074 dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false,
9075 dumpPackage);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009076 if (mMainStack.mWaitingVisibleActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009077 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009078 pw.println(" Activities waiting for another to become visible:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009079 dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009080 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009081 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009082 if (mMainStack.mStoppingActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009084 pw.println(" Activities waiting to stop:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009085 dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009086 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009087 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08009088 if (mMainStack.mGoingToSleepActivities.size() > 0) {
9089 pw.println(" ");
9090 pw.println(" Activities waiting to sleep:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009091 dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009092 !dumpAll, false, dumpPackage);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08009093 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009094 if (mMainStack.mFinishingActivities.size() > 0) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009095 pw.println(" ");
9096 pw.println(" Activities waiting to finish:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009097 dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009098 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009100
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009101 pw.println(" ");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08009102 if (mMainStack.mPausingActivity != null) {
9103 pw.println(" mPausingActivity: " + mMainStack.mPausingActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009104 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009105 pw.println(" mResumedActivity: " + mMainStack.mResumedActivity);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009106 pw.println(" mFocusedActivity: " + mFocusedActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009107 if (dumpAll) {
9108 pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity);
9109 pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout);
Dianne Hackborn90c52de2011-09-23 12:57:44 -07009110 pw.println(" mDismissKeyguardOnNextActivity: "
9111 + mMainStack.mDismissKeyguardOnNextActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009114 if (mRecentTasks.size() > 0) {
9115 pw.println();
9116 pw.println(" Recent tasks:");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009117
9118 final int N = mRecentTasks.size();
9119 for (int i=0; i<N; i++) {
9120 TaskRecord tr = mRecentTasks.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009121 if (dumpPackage != null) {
9122 if (tr.realActivity == null ||
9123 !dumpPackage.equals(tr.realActivity)) {
9124 continue;
9125 }
9126 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009127 pw.print(" * Recent #"); pw.print(i); pw.print(": ");
9128 pw.println(tr);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009129 if (dumpAll) {
9130 mRecentTasks.get(i).dump(pw, " ");
9131 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009132 }
9133 }
9134
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009135 if (dumpAll) {
9136 pw.println(" ");
9137 pw.println(" mCurTask: " + mCurTask);
9138 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009139
9140 return true;
9141 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07009142
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009143 boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009144 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009145 boolean needSep = false;
9146 int numPers = 0;
9147
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009148 pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)");
9149
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009150 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009151 for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) {
9152 final int NA = procs.size();
9153 for (int ia=0; ia<NA; ia++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009154 ProcessRecord r = procs.valueAt(ia);
9155 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9156 continue;
9157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009158 if (!needSep) {
9159 pw.println(" All known processes:");
9160 needSep = true;
9161 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009162 pw.print(r.persistent ? " *PERS*" : " *APP*");
9163 pw.print(" UID "); pw.print(procs.keyAt(ia));
9164 pw.print(" "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009165 r.dump(pw, " ");
9166 if (r.persistent) {
9167 numPers++;
9168 }
9169 }
9170 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009171 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08009172
9173 if (mIsolatedProcesses.size() > 0) {
9174 if (needSep) pw.println(" ");
9175 needSep = true;
9176 pw.println(" Isolated process list (sorted by uid):");
9177 for (int i=0; i<mIsolatedProcesses.size(); i++) {
9178 ProcessRecord r = mIsolatedProcesses.valueAt(i);
9179 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9180 continue;
9181 }
9182 pw.println(String.format("%sIsolated #%2d: %s",
9183 " ", i, r.toString()));
9184 }
9185 }
9186
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009187 if (mLruProcesses.size() > 0) {
9188 if (needSep) pw.println(" ");
9189 needSep = true;
Dianne Hackborn905577f2011-09-07 18:31:28 -07009190 pw.println(" Process LRU list (sorted by oom_adj):");
Dianne Hackborn287952c2010-09-22 22:34:31 -07009191 dumpProcessOomList(pw, this, mLruProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009192 "Proc", "PERS", false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009193 needSep = true;
9194 }
9195
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009196 if (dumpAll) {
9197 synchronized (mPidsSelfLocked) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009198 boolean printed = false;
9199 for (int i=0; i<mPidsSelfLocked.size(); i++) {
9200 ProcessRecord r = mPidsSelfLocked.valueAt(i);
9201 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9202 continue;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009203 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009204 if (!printed) {
9205 if (needSep) pw.println(" ");
9206 needSep = true;
9207 pw.println(" PID mappings:");
9208 printed = true;
9209 }
9210 pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i));
9211 pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009212 }
9213 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009214 }
9215
9216 if (mForegroundProcesses.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009217 synchronized (mPidsSelfLocked) {
9218 boolean printed = false;
9219 for (int i=0; i<mForegroundProcesses.size(); i++) {
9220 ProcessRecord r = mPidsSelfLocked.get(
9221 mForegroundProcesses.valueAt(i).pid);
9222 if (dumpPackage != null && (r == null
9223 || !dumpPackage.equals(r.info.packageName))) {
9224 continue;
9225 }
9226 if (!printed) {
9227 if (needSep) pw.println(" ");
9228 needSep = true;
9229 pw.println(" Foreground Processes:");
9230 printed = true;
9231 }
9232 pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i));
9233 pw.print(": "); pw.println(mForegroundProcesses.valueAt(i));
9234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009236 }
9237
9238 if (mPersistentStartingProcesses.size() > 0) {
9239 if (needSep) pw.println(" ");
9240 needSep = true;
9241 pw.println(" Persisent processes that are starting:");
9242 dumpProcessList(pw, this, mPersistentStartingProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009243 "Starting Norm", "Restarting PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009246 if (mRemovedProcesses.size() > 0) {
9247 if (needSep) pw.println(" ");
9248 needSep = true;
9249 pw.println(" Processes that are being removed:");
9250 dumpProcessList(pw, this, mRemovedProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009251 "Removed Norm", "Removed PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009252 }
9253
9254 if (mProcessesOnHold.size() > 0) {
9255 if (needSep) pw.println(" ");
9256 needSep = true;
9257 pw.println(" Processes that are on old until the system is ready:");
9258 dumpProcessList(pw, this, mProcessesOnHold, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009259 "OnHold Norm", "OnHold PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009261
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009262 needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009263
9264 if (mProcessCrashTimes.getMap().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009265 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009266 long now = SystemClock.uptimeMillis();
9267 for (Map.Entry<String, SparseArray<Long>> procs
9268 : mProcessCrashTimes.getMap().entrySet()) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009269 String pname = procs.getKey();
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009270 SparseArray<Long> uids = procs.getValue();
9271 final int N = uids.size();
9272 for (int i=0; i<N; i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009273 int puid = uids.keyAt(i);
9274 ProcessRecord r = mProcessNames.get(pname, puid);
9275 if (dumpPackage != null && (r == null
9276 || !dumpPackage.equals(r.info.packageName))) {
9277 continue;
9278 }
9279 if (!printed) {
9280 if (needSep) pw.println(" ");
9281 needSep = true;
9282 pw.println(" Time since processes crashed:");
9283 printed = true;
9284 }
9285 pw.print(" Process "); pw.print(pname);
9286 pw.print(" uid "); pw.print(puid);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009287 pw.print(": last crashed ");
Dianne Hackborn27ff9132012-03-06 14:57:58 -08009288 TimeUtils.formatDuration(now-uids.valueAt(i), pw);
9289 pw.println(" ago");
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009290 }
9291 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009294 if (mBadProcesses.getMap().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009295 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009296 for (Map.Entry<String, SparseArray<Long>> procs
9297 : mBadProcesses.getMap().entrySet()) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009298 String pname = procs.getKey();
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009299 SparseArray<Long> uids = procs.getValue();
9300 final int N = uids.size();
9301 for (int i=0; i<N; i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009302 int puid = uids.keyAt(i);
9303 ProcessRecord r = mProcessNames.get(pname, puid);
9304 if (dumpPackage != null && (r == null
9305 || !dumpPackage.equals(r.info.packageName))) {
9306 continue;
9307 }
9308 if (!printed) {
9309 if (needSep) pw.println(" ");
9310 needSep = true;
9311 pw.println(" Bad processes:");
9312 }
9313 pw.print(" Bad process "); pw.print(pname);
9314 pw.print(" uid "); pw.print(puid);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009315 pw.print(": crashed at time ");
9316 pw.println(uids.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009317 }
9318 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009321 pw.println();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07009322 pw.println(" mStartedUsers:");
9323 for (int i=0; i<mStartedUsers.size(); i++) {
9324 UserStartedState uss = mStartedUsers.valueAt(i);
9325 pw.print(" User #"); pw.print(uss.mHandle.getIdentifier());
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07009326 pw.print(": "); uss.dump("", pw);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07009327 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07009328 pw.print(" mUserLru: [");
9329 for (int i=0; i<mUserLru.size(); i++) {
9330 if (i > 0) pw.print(", ");
9331 pw.print(mUserLru.get(i));
9332 }
9333 pw.println("]");
Dianne Hackbornc72fc672012-09-20 13:12:03 -07009334 if (dumpAll) {
9335 pw.print(" mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray));
9336 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009337 pw.println(" mHomeProcess: " + mHomeProcess);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009338 pw.println(" mPreviousProcess: " + mPreviousProcess);
Dianne Hackborn50685602011-12-01 12:23:37 -08009339 if (dumpAll) {
9340 StringBuilder sb = new StringBuilder(128);
9341 sb.append(" mPreviousProcessVisibleTime: ");
9342 TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb);
9343 pw.println(sb);
9344 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07009345 if (mHeavyWeightProcess != null) {
9346 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
9347 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009348 pw.println(" mConfiguration: " + mConfiguration);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009349 if (dumpAll) {
9350 pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange);
Dianne Hackborn3d0724d2011-05-12 15:39:41 -07009351 if (mCompatModePackages.getPackages().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009352 boolean printed = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07009353 for (Map.Entry<String, Integer> entry
9354 : mCompatModePackages.getPackages().entrySet()) {
9355 String pkg = entry.getKey();
9356 int mode = entry.getValue();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009357 if (dumpPackage != null && !dumpPackage.equals(pkg)) {
9358 continue;
9359 }
9360 if (!printed) {
9361 pw.println(" mScreenCompatPackages:");
9362 printed = true;
9363 }
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07009364 pw.print(" "); pw.print(pkg); pw.print(": ");
9365 pw.print(mode); pw.println();
9366 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -07009367 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009368 }
Dianne Hackbornff5b1582012-04-12 17:24:07 -07009369 if (mSleeping || mWentToSleep || mLockScreenShown) {
9370 pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep
9371 + " mLockScreenShown " + mLockScreenShown);
9372 }
9373 if (mShuttingDown) {
9374 pw.println(" mShuttingDown=" + mShuttingDown);
9375 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009376 if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient
9377 || mOrigWaitForDebugger) {
9378 pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp
9379 + " mDebugTransient=" + mDebugTransient
9380 + " mOrigWaitForDebugger=" + mOrigWaitForDebugger);
9381 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08009382 if (mOpenGlTraceApp != null) {
9383 pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp);
9384 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07009385 if (mProfileApp != null || mProfileProc != null || mProfileFile != null
9386 || mProfileFd != null) {
9387 pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc);
9388 pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd);
9389 pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler="
9390 + mAutoStopProfiler);
9391 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009392 if (mAlwaysFinishActivities || mController != null) {
9393 pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities
9394 + " mController=" + mController);
9395 }
9396 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 pw.println(" Total persistent processes: " + numPers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 pw.println(" mStartRunning=" + mStartRunning
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07009399 + " mProcessesReady=" + mProcessesReady
9400 + " mSystemReady=" + mSystemReady);
9401 pw.println(" mBooting=" + mBooting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402 + " mBooted=" + mBooted
9403 + " mFactoryTest=" + mFactoryTest);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009404 pw.print(" mLastPowerCheckRealtime=");
9405 TimeUtils.formatDuration(mLastPowerCheckRealtime, pw);
9406 pw.println("");
9407 pw.print(" mLastPowerCheckUptime=");
9408 TimeUtils.formatDuration(mLastPowerCheckUptime, pw);
9409 pw.println("");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009410 pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep);
9411 pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity);
Dianne Hackborn906497c2010-05-10 15:57:38 -07009412 pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq);
Dianne Hackbornee7621c2012-08-13 16:42:18 -07009413 pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs
9414 + " mNumHiddenProcs=" + mNumHiddenProcs
9415 + " mNumServiceProcs=" + mNumServiceProcs
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009416 + " mNewNumServiceProcs=" + mNewNumServiceProcs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009418
9419 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009420 }
9421
Dianne Hackborn287952c2010-09-22 22:34:31 -07009422 boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009423 int opti, boolean needSep, boolean dumpAll, String dumpPackage) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009424 if (mProcessesToGc.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009425 boolean printed = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009426 long now = SystemClock.uptimeMillis();
9427 for (int i=0; i<mProcessesToGc.size(); i++) {
9428 ProcessRecord proc = mProcessesToGc.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009429 if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) {
9430 continue;
9431 }
9432 if (!printed) {
9433 if (needSep) pw.println(" ");
9434 needSep = true;
9435 pw.println(" Processes that are waiting to GC:");
9436 printed = true;
9437 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07009438 pw.print(" Process "); pw.println(proc);
9439 pw.print(" lowMem="); pw.print(proc.reportLowMemory);
9440 pw.print(", last gced=");
9441 pw.print(now-proc.lastRequestedGc);
9442 pw.print(" ms ago, last lowMem=");
9443 pw.print(now-proc.lastLowMemory);
9444 pw.println(" ms ago");
9445
9446 }
9447 }
9448 return needSep;
9449 }
9450
9451 boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args,
9452 int opti, boolean dumpAll) {
9453 boolean needSep = false;
9454
9455 if (mLruProcesses.size() > 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009456 if (needSep) pw.println(" ");
9457 needSep = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07009458 pw.println(" OOM levels:");
Dianne Hackborn7d608422011-08-07 16:24:18 -07009459 pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009460 pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009461 pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ);
9462 pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ);
9463 pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ);
9464 pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ);
9465 pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009466 pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009467 pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009468 pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009469 pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009470 pw.print(" HIDDEN_APP_MIN_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MIN_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009471 pw.print(" HIDDEN_APP_MAX_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MAX_ADJ);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07009472
9473 if (needSep) pw.println(" ");
9474 needSep = true;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009475 pw.println(" Process OOM control:");
Dianne Hackborn905577f2011-09-07 18:31:28 -07009476 dumpProcessOomList(pw, this, mLruProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009477 "Proc", "PERS", true, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009478 needSep = true;
9479 }
9480
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009481 needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009482
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009483 pw.println();
Dianne Hackborn287952c2010-09-22 22:34:31 -07009484 pw.println(" mHomeProcess: " + mHomeProcess);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009485 pw.println(" mPreviousProcess: " + mPreviousProcess);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009486 if (mHeavyWeightProcess != null) {
9487 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
9488 }
9489
9490 return true;
9491 }
9492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 /**
9494 * There are three ways to call this:
Marco Nelissen18cb2872011-11-15 11:19:53 -08009495 * - no provider specified: dump all the providers
9496 * - a flattened component name that matched an existing provider was specified as the
9497 * first arg: dump that one provider
9498 * - the first arg isn't the flattened component name of an existing provider:
9499 * dump all providers whose component contains the first arg as a substring
9500 */
9501 protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args,
9502 int opti, boolean dumpAll) {
Marco Nelissende7408c2012-02-08 14:57:38 -08009503 return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll);
Marco Nelissen18cb2872011-11-15 11:19:53 -08009504 }
9505
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009506 static class ItemMatcher {
9507 ArrayList<ComponentName> components;
9508 ArrayList<String> strings;
9509 ArrayList<Integer> objects;
9510 boolean all;
9511
9512 ItemMatcher() {
9513 all = true;
9514 }
9515
9516 void build(String name) {
9517 ComponentName componentName = ComponentName.unflattenFromString(name);
9518 if (componentName != null) {
9519 if (components == null) {
9520 components = new ArrayList<ComponentName>();
9521 }
9522 components.add(componentName);
9523 all = false;
9524 } else {
9525 int objectId = 0;
9526 // Not a '/' separated full component name; maybe an object ID?
9527 try {
9528 objectId = Integer.parseInt(name, 16);
9529 if (objects == null) {
9530 objects = new ArrayList<Integer>();
9531 }
9532 objects.add(objectId);
9533 all = false;
9534 } catch (RuntimeException e) {
9535 // Not an integer; just do string match.
9536 if (strings == null) {
9537 strings = new ArrayList<String>();
9538 }
9539 strings.add(name);
9540 all = false;
9541 }
9542 }
9543 }
9544
9545 int build(String[] args, int opti) {
9546 for (; opti<args.length; opti++) {
9547 String name = args[opti];
9548 if ("--".equals(name)) {
9549 return opti+1;
9550 }
9551 build(name);
9552 }
9553 return opti;
9554 }
9555
9556 boolean match(Object object, ComponentName comp) {
9557 if (all) {
9558 return true;
9559 }
9560 if (components != null) {
9561 for (int i=0; i<components.size(); i++) {
9562 if (components.get(i).equals(comp)) {
9563 return true;
9564 }
9565 }
9566 }
9567 if (objects != null) {
9568 for (int i=0; i<objects.size(); i++) {
9569 if (System.identityHashCode(object) == objects.get(i)) {
9570 return true;
9571 }
9572 }
9573 }
9574 if (strings != null) {
9575 String flat = comp.flattenToString();
9576 for (int i=0; i<strings.size(); i++) {
9577 if (flat.contains(strings.get(i))) {
9578 return true;
9579 }
9580 }
9581 }
9582 return false;
9583 }
9584 }
9585
Dianne Hackborn625ac272010-09-17 18:29:22 -07009586 /**
9587 * There are three things that cmd can be:
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009588 * - a flattened component name that matches an existing activity
Dianne Hackborn625ac272010-09-17 18:29:22 -07009589 * - the cmd arg isn't the flattened component name of an existing activity:
9590 * dump all activity whose component contains the cmd as a substring
9591 * - A hex number of the ActivityRecord object instance.
9592 */
9593 protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args,
9594 int opti, boolean dumpAll) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07009595 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009596
9597 if ("all".equals(name)) {
9598 synchronized (this) {
9599 for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07009600 activities.add(r1);
9601 }
9602 }
Dianne Hackbornf9302322011-06-14 18:36:14 -07009603 } else if ("top".equals(name)) {
9604 synchronized (this) {
9605 final int N = mMainStack.mHistory.size();
9606 if (N > 0) {
9607 activities.add((ActivityRecord)mMainStack.mHistory.get(N-1));
9608 }
9609 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009610 } else {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009611 ItemMatcher matcher = new ItemMatcher();
9612 matcher.build(name);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009613
9614 synchronized (this) {
9615 for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009616 if (matcher.match(r1, r1.intent.getComponent())) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009617 activities.add(r1);
9618 }
9619 }
9620 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07009621 }
9622
9623 if (activities.size() <= 0) {
9624 return false;
9625 }
9626
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009627 String[] newArgs = new String[args.length - opti];
9628 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
9629
Dianne Hackborn30d71892010-12-11 10:37:55 -08009630 TaskRecord lastTask = null;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009631 boolean needSep = false;
Dianne Hackborn30d71892010-12-11 10:37:55 -08009632 for (int i=activities.size()-1; i>=0; i--) {
9633 ActivityRecord r = (ActivityRecord)activities.get(i);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009634 if (needSep) {
9635 pw.println();
9636 }
9637 needSep = true;
9638 synchronized (this) {
9639 if (lastTask != r.task) {
9640 lastTask = r.task;
9641 pw.print("TASK "); pw.print(lastTask.affinity);
9642 pw.print(" id="); pw.println(lastTask.taskId);
9643 if (dumpAll) {
9644 lastTask.dump(pw, " ");
9645 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08009646 }
9647 }
9648 dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009649 }
9650 return true;
9651 }
9652
9653 /**
9654 * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if
9655 * there is a thread associated with the activity.
9656 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08009657 private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw,
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009658 final ActivityRecord r, String[] args, boolean dumpAll) {
9659 String innerPrefix = prefix + " ";
Dianne Hackborn30d71892010-12-11 10:37:55 -08009660 synchronized (this) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009661 pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName);
9662 pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r)));
9663 pw.print(" pid=");
Dianne Hackborn30d71892010-12-11 10:37:55 -08009664 if (r.app != null) pw.println(r.app.pid);
9665 else pw.println("(not running)");
9666 if (dumpAll) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009667 r.dump(pw, innerPrefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009668 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07009669 }
9670 if (r.app != null && r.app.thread != null) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009671 // flush anything that is already in the PrintWriter since the thread is going
9672 // to write to the file descriptor directly
9673 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07009674 try {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009675 TransferPipe tp = new TransferPipe();
9676 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08009677 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
9678 r.appToken, innerPrefix, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009679 tp.go(fd);
9680 } finally {
9681 tp.kill();
9682 }
9683 } catch (IOException e) {
9684 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009685 } catch (RemoteException e) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009686 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
Dianne Hackborn625ac272010-09-17 18:29:22 -07009687 }
9688 }
9689 }
9690
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009691 boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009692 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009693 boolean needSep = false;
Dianne Hackborn786b4402012-08-27 15:14:02 -07009694 boolean onlyHistory = false;
9695
9696 if ("history".equals(dumpPackage)) {
9697 onlyHistory = true;
9698 dumpPackage = null;
9699 }
9700
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009701 pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)");
Dianne Hackborn786b4402012-08-27 15:14:02 -07009702 if (!onlyHistory && dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 if (mRegisteredReceivers.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009704 boolean printed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 Iterator it = mRegisteredReceivers.values().iterator();
9706 while (it.hasNext()) {
9707 ReceiverList r = (ReceiverList)it.next();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009708 if (dumpPackage != null && (r.app == null ||
9709 !dumpPackage.equals(r.app.info.packageName))) {
9710 continue;
9711 }
9712 if (!printed) {
9713 pw.println(" Registered Receivers:");
9714 needSep = true;
9715 printed = true;
9716 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009717 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009718 r.dump(pw, " ");
9719 }
9720 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009721
9722 if (mReceiverResolver.dump(pw, needSep ?
9723 "\n Receiver Resolver Table:" : " Receiver Resolver Table:",
9724 " ", dumpPackage, false)) {
9725 needSep = true;
9726 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009727 }
Christopher Tatef46723b2012-01-26 14:19:24 -08009728
9729 for (BroadcastQueue q : mBroadcastQueues) {
9730 needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009733 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009734
Dianne Hackborn786b4402012-08-27 15:14:02 -07009735 if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07009736 for (int user=0; user<mStickyBroadcasts.size(); user++) {
9737 if (needSep) {
9738 pw.println();
9739 }
9740 needSep = true;
9741 pw.print(" Sticky broadcasts for user ");
9742 pw.print(mStickyBroadcasts.keyAt(user)); pw.println(":");
9743 StringBuilder sb = new StringBuilder(128);
9744 for (Map.Entry<String, ArrayList<Intent>> ent
9745 : mStickyBroadcasts.valueAt(user).entrySet()) {
9746 pw.print(" * Sticky action "); pw.print(ent.getKey());
9747 if (dumpAll) {
9748 pw.println(":");
9749 ArrayList<Intent> intents = ent.getValue();
9750 final int N = intents.size();
9751 for (int i=0; i<N; i++) {
9752 sb.setLength(0);
9753 sb.append(" Intent: ");
9754 intents.get(i).toShortString(sb, false, true, false, false);
9755 pw.println(sb.toString());
9756 Bundle bundle = intents.get(i).getExtras();
9757 if (bundle != null) {
9758 pw.print(" ");
9759 pw.println(bundle.toString());
9760 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009761 }
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07009762 } else {
9763 pw.println("");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 }
9765 }
9766 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009767 }
9768
Dianne Hackborn786b4402012-08-27 15:14:02 -07009769 if (!onlyHistory && dumpAll) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009770 pw.println();
Christopher Tatef46723b2012-01-26 14:19:24 -08009771 for (BroadcastQueue queue : mBroadcastQueues) {
9772 pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]="
9773 + queue.mBroadcastsScheduled);
9774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 pw.println(" mHandler:");
9776 mHandler.dump(new PrintWriterPrinter(pw), " ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009777 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009779
9780 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009781 }
9782
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009783 boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009784 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07009785 boolean needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009787 ItemMatcher matcher = new ItemMatcher();
9788 matcher.build(args, opti);
9789
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009790 pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)");
Amith Yamasani742a6712011-05-04 14:49:28 -07009791
9792 mProviderMap.dumpProvidersLocked(pw, dumpAll);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009793
9794 if (mLaunchingProviders.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009795 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009796 for (int i=mLaunchingProviders.size()-1; i>=0; i--) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009797 ContentProviderRecord r = mLaunchingProviders.get(i);
9798 if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) {
9799 continue;
9800 }
9801 if (!printed) {
9802 if (needSep) pw.println(" ");
9803 needSep = true;
9804 pw.println(" Launching content providers:");
9805 printed = true;
9806 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009807 pw.print(" Launching #"); pw.print(i); pw.print(": ");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009808 pw.println(r);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009809 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009810 }
9811
9812 if (mGrantedUriPermissions.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009813 if (needSep) pw.println();
9814 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009815 pw.println("Granted Uri Permissions:");
9816 for (int i=0; i<mGrantedUriPermissions.size(); i++) {
9817 int uid = mGrantedUriPermissions.keyAt(i);
9818 HashMap<Uri, UriPermission> perms
9819 = mGrantedUriPermissions.valueAt(i);
9820 pw.print(" * UID "); pw.print(uid);
9821 pw.println(" holds:");
9822 for (UriPermission perm : perms.values()) {
9823 pw.print(" "); pw.println(perm);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009824 if (dumpAll) {
9825 perm.dump(pw, " ");
9826 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009827 }
9828 }
9829 needSep = true;
9830 }
9831
9832 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 }
9834
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009835 boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009836 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009837 boolean needSep = false;
9838
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009839 if (mIntentSenderRecords.size() > 0) {
9840 boolean printed = false;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009841 Iterator<WeakReference<PendingIntentRecord>> it
9842 = mIntentSenderRecords.values().iterator();
9843 while (it.hasNext()) {
9844 WeakReference<PendingIntentRecord> ref = it.next();
9845 PendingIntentRecord rec = ref != null ? ref.get(): null;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009846 if (dumpPackage != null && (rec == null
9847 || !dumpPackage.equals(rec.key.packageName))) {
9848 continue;
9849 }
9850 if (!printed) {
9851 pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)");
9852 printed = true;
9853 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009854 needSep = true;
9855 if (rec != null) {
9856 pw.print(" * "); pw.println(rec);
9857 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009858 rec.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009860 } else {
9861 pw.print(" * "); pw.println(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 }
9863 }
9864 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009865
9866 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 }
9868
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009869 private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009870 String prefix, String label, boolean complete, boolean brief, boolean client,
9871 String dumpPackage) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 TaskRecord lastTask = null;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009873 boolean needNL = false;
9874 final String innerPrefix = prefix + " ";
9875 final String[] args = new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876 for (int i=list.size()-1; i>=0; i--) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009877 final ActivityRecord r = (ActivityRecord)list.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009878 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
9879 continue;
9880 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07009881 final boolean full = !brief && (complete || !r.isInHistory());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009882 if (needNL) {
9883 pw.println(" ");
9884 needNL = false;
9885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 if (lastTask != r.task) {
9887 lastTask = r.task;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009888 pw.print(prefix);
9889 pw.print(full ? "* " : " ");
9890 pw.println(lastTask);
9891 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009892 lastTask.dump(pw, prefix + " ");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009893 } else if (complete) {
9894 // Complete + brief == give a summary. Isn't that obvious?!?
9895 if (lastTask.intent != null) {
Dianne Hackborn90c52de2011-09-23 12:57:44 -07009896 pw.print(prefix); pw.print(" ");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08009897 pw.println(lastTask.intent.toInsecureStringWithClip());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009898 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009901 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
9902 pw.print(" #"); pw.print(i); pw.print(": ");
9903 pw.println(r);
9904 if (full) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009905 r.dump(pw, innerPrefix);
9906 } else if (complete) {
9907 // Complete + brief == give a summary. Isn't that obvious?!?
Dianne Hackborn90c52de2011-09-23 12:57:44 -07009908 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009909 if (r.app != null) {
9910 pw.print(innerPrefix); pw.println(r.app);
9911 }
9912 }
9913 if (client && r.app != null && r.app.thread != null) {
9914 // flush anything that is already in the PrintWriter since the thread is going
9915 // to write to the file descriptor directly
9916 pw.flush();
9917 try {
9918 TransferPipe tp = new TransferPipe();
9919 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08009920 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
9921 r.appToken, innerPrefix, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009922 // Short timeout, since blocking here can
9923 // deadlock with the application.
9924 tp.go(fd, 2000);
9925 } finally {
9926 tp.kill();
9927 }
9928 } catch (IOException e) {
9929 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
9930 } catch (RemoteException e) {
9931 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
9932 }
9933 needNL = true;
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009935 }
9936 }
9937
Dianne Hackborn09c916b2009-12-08 14:50:51 -08009938 private static String buildOomTag(String prefix, String space, int val, int base) {
9939 if (val == base) {
9940 if (space == null) return prefix;
9941 return prefix + " ";
9942 }
9943 return prefix + "+" + Integer.toString(val-base);
9944 }
9945
9946 private static final int dumpProcessList(PrintWriter pw,
9947 ActivityManagerService service, List list,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009948 String prefix, String normalLabel, String persistentLabel,
9949 String dumpPackage) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009950 int numPers = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009951 final int N = list.size()-1;
9952 for (int i=N; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 ProcessRecord r = (ProcessRecord)list.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009954 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9955 continue;
9956 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07009957 pw.println(String.format("%s%s #%2d: %s",
9958 prefix, (r.persistent ? persistentLabel : normalLabel),
9959 i, r.toString()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009960 if (r.persistent) {
9961 numPers++;
9962 }
9963 }
9964 return numPers;
9965 }
9966
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009967 private static final boolean dumpProcessOomList(PrintWriter pw,
Dianne Hackborn905577f2011-09-07 18:31:28 -07009968 ActivityManagerService service, List<ProcessRecord> origList,
Dianne Hackborn287952c2010-09-22 22:34:31 -07009969 String prefix, String normalLabel, String persistentLabel,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009970 boolean inclDetails, String dumpPackage) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009971
Dianne Hackborn905577f2011-09-07 18:31:28 -07009972 ArrayList<Pair<ProcessRecord, Integer>> list
9973 = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size());
9974 for (int i=0; i<origList.size(); i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009975 ProcessRecord r = origList.get(i);
9976 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9977 continue;
9978 }
Dianne Hackborn905577f2011-09-07 18:31:28 -07009979 list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i));
9980 }
9981
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009982 if (list.size() <= 0) {
9983 return false;
9984 }
9985
Dianne Hackborn905577f2011-09-07 18:31:28 -07009986 Comparator<Pair<ProcessRecord, Integer>> comparator
9987 = new Comparator<Pair<ProcessRecord, Integer>>() {
9988 @Override
9989 public int compare(Pair<ProcessRecord, Integer> object1,
9990 Pair<ProcessRecord, Integer> object2) {
9991 if (object1.first.setAdj != object2.first.setAdj) {
9992 return object1.first.setAdj > object2.first.setAdj ? -1 : 1;
9993 }
9994 if (object1.second.intValue() != object2.second.intValue()) {
9995 return object1.second.intValue() > object2.second.intValue() ? -1 : 1;
9996 }
9997 return 0;
9998 }
9999 };
10000
10001 Collections.sort(list, comparator);
10002
Dianne Hackborn287952c2010-09-22 22:34:31 -070010003 final long curRealtime = SystemClock.elapsedRealtime();
10004 final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime;
10005 final long curUptime = SystemClock.uptimeMillis();
10006 final long uptimeSince = curUptime - service.mLastPowerCheckUptime;
10007
Dianne Hackbornee9aef02011-11-16 13:21:46 -080010008 for (int i=list.size()-1; i>=0; i--) {
Dianne Hackborn905577f2011-09-07 18:31:28 -070010009 ProcessRecord r = list.get(i).first;
Dianne Hackborn287952c2010-09-22 22:34:31 -070010010 String oomAdj;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070010011 if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070010012 oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -070010013 } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) {
10014 oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ);
Dianne Hackbornf35fe232011-11-01 19:25:20 -070010015 } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) {
10016 oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -070010017 } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) {
10018 oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -070010019 } else if (r.setAdj >= ProcessList.SERVICE_ADJ) {
10020 oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -070010021 } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) {
Dianne Hackborn672342c2011-11-29 11:29:02 -080010022 oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -070010023 } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
10024 oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ);
10025 } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
10026 oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ);
10027 } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) {
10028 oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ);
10029 } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) {
10030 oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -070010031 } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) {
10032 oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -070010033 } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) {
10034 oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ);
Dianne Hackborn287952c2010-09-22 22:34:31 -070010035 } else {
10036 oomAdj = Integer.toString(r.setAdj);
10037 }
10038 String schedGroup;
10039 switch (r.setSchedGroup) {
10040 case Process.THREAD_GROUP_BG_NONINTERACTIVE:
10041 schedGroup = "B";
10042 break;
10043 case Process.THREAD_GROUP_DEFAULT:
10044 schedGroup = "F";
10045 break;
10046 default:
10047 schedGroup = Integer.toString(r.setSchedGroup);
10048 break;
10049 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070010050 String foreground;
10051 if (r.foregroundActivities) {
10052 foreground = "A";
10053 } else if (r.foregroundServices) {
10054 foreground = "S";
10055 } else {
10056 foreground = " ";
10057 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070010058 pw.println(String.format("%s%s #%2d: adj=%s/%s%s trm=%2d %s (%s)",
Dianne Hackborn287952c2010-09-22 22:34:31 -070010059 prefix, (r.persistent ? persistentLabel : normalLabel),
Dianne Hackbornee9aef02011-11-16 13:21:46 -080010060 (origList.size()-1)-list.get(i).second, oomAdj, schedGroup,
10061 foreground, r.trimMemoryLevel, r.toShortString(), r.adjType));
Dianne Hackborn287952c2010-09-22 22:34:31 -070010062 if (r.adjSource != null || r.adjTarget != null) {
10063 pw.print(prefix);
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010064 pw.print(" ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070010065 if (r.adjTarget instanceof ComponentName) {
10066 pw.print(((ComponentName)r.adjTarget).flattenToShortString());
10067 } else if (r.adjTarget != null) {
10068 pw.print(r.adjTarget.toString());
10069 } else {
10070 pw.print("{null}");
10071 }
10072 pw.print("<=");
10073 if (r.adjSource instanceof ProcessRecord) {
10074 pw.print("Proc{");
10075 pw.print(((ProcessRecord)r.adjSource).toShortString());
10076 pw.println("}");
10077 } else if (r.adjSource != null) {
10078 pw.println(r.adjSource.toString());
10079 } else {
10080 pw.println("{null}");
10081 }
10082 }
10083 if (inclDetails) {
10084 pw.print(prefix);
10085 pw.print(" ");
10086 pw.print("oom: max="); pw.print(r.maxAdj);
10087 pw.print(" hidden="); pw.print(r.hiddenAdj);
Dianne Hackbornee7621c2012-08-13 16:42:18 -070010088 pw.print(" empty="); pw.print(r.emptyAdj);
Dianne Hackborn287952c2010-09-22 22:34:31 -070010089 pw.print(" curRaw="); pw.print(r.curRawAdj);
10090 pw.print(" setRaw="); pw.print(r.setRawAdj);
10091 pw.print(" cur="); pw.print(r.curAdj);
10092 pw.print(" set="); pw.println(r.setAdj);
10093 pw.print(prefix);
10094 pw.print(" ");
10095 pw.print("keeping="); pw.print(r.keeping);
10096 pw.print(" hidden="); pw.print(r.hidden);
Dianne Hackbornc68c9132011-07-29 01:25:18 -070010097 pw.print(" empty="); pw.print(r.empty);
10098 pw.print(" hasAboveClient="); pw.println(r.hasAboveClient);
Dianne Hackborn287952c2010-09-22 22:34:31 -070010099
10100 if (!r.keeping) {
10101 if (r.lastWakeTime != 0) {
10102 long wtime;
10103 BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics();
10104 synchronized (stats) {
10105 wtime = stats.getProcessWakeTime(r.info.uid,
10106 r.pid, curRealtime);
10107 }
10108 long timeUsed = wtime - r.lastWakeTime;
10109 pw.print(prefix);
10110 pw.print(" ");
10111 pw.print("keep awake over ");
10112 TimeUtils.formatDuration(realtimeSince, pw);
10113 pw.print(" used ");
10114 TimeUtils.formatDuration(timeUsed, pw);
10115 pw.print(" (");
10116 pw.print((timeUsed*100)/realtimeSince);
10117 pw.println("%)");
10118 }
10119 if (r.lastCpuTime != 0) {
10120 long timeUsed = r.curCpuTime - r.lastCpuTime;
10121 pw.print(prefix);
10122 pw.print(" ");
10123 pw.print("run cpu over ");
10124 TimeUtils.formatDuration(uptimeSince, pw);
10125 pw.print(" used ");
10126 TimeUtils.formatDuration(timeUsed, pw);
10127 pw.print(" (");
10128 pw.print((timeUsed*100)/uptimeSince);
10129 pw.println("%)");
10130 }
10131 }
10132 }
10133 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -080010134 return true;
Dianne Hackborn287952c2010-09-22 22:34:31 -070010135 }
10136
Dianne Hackbornb437e092011-08-05 17:50:29 -070010137 ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010138 ArrayList<ProcessRecord> procs;
10139 synchronized (this) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010140 if (args != null && args.length > start
10141 && args[start].charAt(0) != '-') {
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010142 procs = new ArrayList<ProcessRecord>();
10143 int pid = -1;
10144 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010145 pid = Integer.parseInt(args[start]);
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010146 } catch (NumberFormatException e) {
10147
10148 }
10149 for (int i=mLruProcesses.size()-1; i>=0; i--) {
10150 ProcessRecord proc = mLruProcesses.get(i);
10151 if (proc.pid == pid) {
10152 procs.add(proc);
Dianne Hackbornb437e092011-08-05 17:50:29 -070010153 } else if (proc.processName.equals(args[start])) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010154 procs.add(proc);
10155 }
10156 }
10157 if (procs.size() <= 0) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010158 pw.println("No process found for: " + args[start]);
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010159 return null;
10160 }
10161 } else {
10162 procs = new ArrayList<ProcessRecord>(mLruProcesses);
10163 }
10164 }
10165 return procs;
10166 }
10167
10168 final void dumpGraphicsHardwareUsage(FileDescriptor fd,
10169 PrintWriter pw, String[] args) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010170 ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010171 if (procs == null) {
10172 return;
10173 }
10174
10175 long uptime = SystemClock.uptimeMillis();
10176 long realtime = SystemClock.elapsedRealtime();
10177 pw.println("Applications Graphics Acceleration Info:");
10178 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
10179
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010180 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
10181 ProcessRecord r = procs.get(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -070010182 if (r.thread != null) {
10183 pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **");
10184 pw.flush();
10185 try {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010186 TransferPipe tp = new TransferPipe();
10187 try {
10188 r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args);
10189 tp.go(fd);
10190 } finally {
10191 tp.kill();
10192 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010193 } catch (IOException e) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010194 pw.println("Failure while dumping the app: " + r);
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010195 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -070010196 } catch (RemoteException e) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010197 pw.println("Got a RemoteException while dumping the app " + r);
Chet Haase9c1e23b2011-03-24 10:51:31 -070010198 pw.flush();
10199 }
10200 }
10201 }
Chet Haase9c1e23b2011-03-24 10:51:31 -070010202 }
10203
Jeff Brown6754ba22011-12-14 20:20:01 -080010204 final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) {
10205 ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args);
10206 if (procs == null) {
10207 return;
10208 }
10209
10210 pw.println("Applications Database Info:");
10211
10212 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
10213 ProcessRecord r = procs.get(i);
10214 if (r.thread != null) {
10215 pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **");
10216 pw.flush();
10217 try {
10218 TransferPipe tp = new TransferPipe();
10219 try {
10220 r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args);
10221 tp.go(fd);
10222 } finally {
10223 tp.kill();
10224 }
10225 } catch (IOException e) {
10226 pw.println("Failure while dumping the app: " + r);
10227 pw.flush();
10228 } catch (RemoteException e) {
10229 pw.println("Got a RemoteException while dumping the app " + r);
10230 pw.flush();
10231 }
10232 }
10233 }
10234 }
10235
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010236 final static class MemItem {
10237 final String label;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010238 final String shortLabel;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010239 final long pss;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010240 final int id;
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010241 ArrayList<MemItem> subitems;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010242
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010243 public MemItem(String _label, String _shortLabel, long _pss, int _id) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010244 label = _label;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010245 shortLabel = _shortLabel;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010246 pss = _pss;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010247 id = _id;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010248 }
10249 }
10250
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010251 static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items,
Dianne Hackbornb437e092011-08-05 17:50:29 -070010252 boolean sort) {
10253 if (sort) {
10254 Collections.sort(items, new Comparator<MemItem>() {
10255 @Override
10256 public int compare(MemItem lhs, MemItem rhs) {
10257 if (lhs.pss < rhs.pss) {
10258 return 1;
10259 } else if (lhs.pss > rhs.pss) {
10260 return -1;
10261 }
10262 return 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010263 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010264 });
10265 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010266
10267 for (int i=0; i<items.size(); i++) {
10268 MemItem mi = items.get(i);
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010269 pw.print(prefix); pw.printf("%7d kB: ", mi.pss); pw.println(mi.label);
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010270 if (mi.subitems != null) {
10271 dumpMemItems(pw, prefix + " ", mi.subitems, true);
10272 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010273 }
10274 }
10275
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010276 // These are in KB.
10277 static final long[] DUMP_MEM_BUCKETS = new long[] {
10278 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024,
10279 120*1024, 160*1024, 200*1024,
10280 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024,
10281 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024
10282 };
10283
Dianne Hackborn672342c2011-11-29 11:29:02 -080010284 static final void appendMemBucket(StringBuilder out, long memKB, String label,
10285 boolean stackLike) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010286 int start = label.lastIndexOf('.');
10287 if (start >= 0) start++;
10288 else start = 0;
10289 int end = label.length();
10290 for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) {
10291 if (DUMP_MEM_BUCKETS[i] >= memKB) {
10292 long bucket = DUMP_MEM_BUCKETS[i]/1024;
10293 out.append(bucket);
Dianne Hackborn672342c2011-11-29 11:29:02 -080010294 out.append(stackLike ? "MB." : "MB ");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010295 out.append(label, start, end);
10296 return;
10297 }
10298 }
10299 out.append(memKB/1024);
Dianne Hackborn672342c2011-11-29 11:29:02 -080010300 out.append(stackLike ? "MB." : "MB ");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010301 out.append(label, start, end);
10302 }
10303
10304 static final int[] DUMP_MEM_OOM_ADJ = new int[] {
10305 ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ,
10306 ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ,
10307 ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ,
10308 ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ
10309 };
10310 static final String[] DUMP_MEM_OOM_LABEL = new String[] {
10311 "System", "Persistent", "Foreground",
10312 "Visible", "Perceptible", "Heavy Weight",
10313 "Backup", "A Services", "Home", "Previous",
10314 "B Services", "Background"
10315 };
10316
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010317 final void dumpApplicationMemoryUsage(FileDescriptor fd,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010318 PrintWriter pw, String prefix, String[] args, boolean brief,
Dianne Hackborn672342c2011-11-29 11:29:02 -080010319 PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010320 boolean dumpAll = false;
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010321 boolean oomOnly = false;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010322
10323 int opti = 0;
10324 while (opti < args.length) {
10325 String opt = args[opti];
10326 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
10327 break;
10328 }
10329 opti++;
10330 if ("-a".equals(opt)) {
10331 dumpAll = true;
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010332 } else if ("--oom".equals(opt)) {
10333 oomOnly = true;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010334 } else if ("-h".equals(opt)) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010335 pw.println("meminfo dump options: [-a] [--oom] [process]");
Dianne Hackbornb437e092011-08-05 17:50:29 -070010336 pw.println(" -a: include all available information for each process.");
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010337 pw.println(" --oom: only show processes organized by oom adj.");
Dianne Hackbornb437e092011-08-05 17:50:29 -070010338 pw.println("If [process] is specified it can be the name or ");
10339 pw.println("pid of a specific process to dump.");
10340 return;
10341 } else {
10342 pw.println("Unknown argument: " + opt + "; use -h for help");
10343 }
10344 }
10345
10346 ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010347 if (procs == null) {
10348 return;
10349 }
10350
Dianne Hackborn6447ca32009-04-07 19:50:08 -070010351 final boolean isCheckinRequest = scanArgs(args, "--checkin");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 long uptime = SystemClock.uptimeMillis();
10353 long realtime = SystemClock.elapsedRealtime();
Dianne Hackbornb437e092011-08-05 17:50:29 -070010354
10355 if (procs.size() == 1 || isCheckinRequest) {
10356 dumpAll = true;
10357 }
10358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010359 if (isCheckinRequest) {
10360 // short checkin version
10361 pw.println(uptime + "," + realtime);
10362 pw.flush();
10363 } else {
10364 pw.println("Applications Memory Usage (kB):");
10365 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
10366 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010367
Dianne Hackbornb437e092011-08-05 17:50:29 -070010368 String[] innerArgs = new String[args.length-opti];
10369 System.arraycopy(args, opti, innerArgs, 0, args.length-opti);
10370
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010371 ArrayList<MemItem> procMems = new ArrayList<MemItem>();
10372 long nativePss=0, dalvikPss=0, otherPss=0;
10373 long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS];
10374
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010375 long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length];
10376 ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[])
10377 new ArrayList[DUMP_MEM_OOM_LABEL.length];
Dianne Hackbornb437e092011-08-05 17:50:29 -070010378
10379 long totalPss = 0;
10380
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010381 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
10382 ProcessRecord r = procs.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010383 if (r.thread != null) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010384 if (!isCheckinRequest && dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **");
10386 pw.flush();
10387 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010388 Debug.MemoryInfo mi = null;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010389 if (dumpAll) {
10390 try {
10391 mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs);
10392 } catch (RemoteException e) {
10393 if (!isCheckinRequest) {
10394 pw.println("Got RemoteException!");
10395 pw.flush();
10396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010397 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010398 } else {
10399 mi = new Debug.MemoryInfo();
10400 Debug.getMemoryInfo(r.pid, mi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010401 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010402
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010403 if (!isCheckinRequest && mi != null) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010404 long myTotalPss = mi.getTotalPss();
10405 totalPss += myTotalPss;
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010406 MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")",
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010407 r.processName, myTotalPss, 0);
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010408 procMems.add(pssItem);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010409
10410 nativePss += mi.nativePss;
10411 dalvikPss += mi.dalvikPss;
10412 otherPss += mi.otherPss;
10413 for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
10414 long mem = mi.getOtherPss(j);
10415 miscPss[j] += mem;
10416 otherPss -= mem;
10417 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010418
10419 for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010420 if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex]
10421 || oomIndex == (oomPss.length-1)) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010422 oomPss[oomIndex] += myTotalPss;
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010423 if (oomProcs[oomIndex] == null) {
10424 oomProcs[oomIndex] = new ArrayList<MemItem>();
10425 }
10426 oomProcs[oomIndex].add(pssItem);
Dianne Hackbornb437e092011-08-05 17:50:29 -070010427 break;
10428 }
10429 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 }
10432 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010433
10434 if (!isCheckinRequest && procs.size() > 1) {
10435 ArrayList<MemItem> catMems = new ArrayList<MemItem>();
10436
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010437 catMems.add(new MemItem("Native", "Native", nativePss, -1));
10438 catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2));
10439 catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3));
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010440 for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010441 String label = Debug.MemoryInfo.getOtherLabel(j);
10442 catMems.add(new MemItem(label, label, miscPss[j], j));
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010443 }
10444
Dianne Hackbornb437e092011-08-05 17:50:29 -070010445 ArrayList<MemItem> oomMems = new ArrayList<MemItem>();
10446 for (int j=0; j<oomPss.length; j++) {
10447 if (oomPss[j] != 0) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010448 String label = DUMP_MEM_OOM_LABEL[j];
10449 MemItem item = new MemItem(label, label, oomPss[j],
10450 DUMP_MEM_OOM_ADJ[j]);
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010451 item.subitems = oomProcs[j];
10452 oomMems.add(item);
Dianne Hackbornb437e092011-08-05 17:50:29 -070010453 }
10454 }
10455
Dianne Hackborn672342c2011-11-29 11:29:02 -080010456 if (outTag != null || outStack != null) {
10457 if (outTag != null) {
10458 appendMemBucket(outTag, totalPss, "total", false);
10459 }
10460 if (outStack != null) {
10461 appendMemBucket(outStack, totalPss, "total", true);
10462 }
10463 boolean firstLine = true;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010464 for (int i=0; i<oomMems.size(); i++) {
10465 MemItem miCat = oomMems.get(i);
10466 if (miCat.subitems == null || miCat.subitems.size() < 1) {
10467 continue;
10468 }
10469 if (miCat.id < ProcessList.SERVICE_ADJ
10470 || miCat.id == ProcessList.HOME_APP_ADJ
10471 || miCat.id == ProcessList.PREVIOUS_APP_ADJ) {
Dianne Hackborn672342c2011-11-29 11:29:02 -080010472 if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) {
10473 outTag.append(" / ");
10474 }
10475 if (outStack != null) {
10476 if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) {
10477 if (firstLine) {
10478 outStack.append(":");
10479 firstLine = false;
10480 }
10481 outStack.append("\n\t at ");
10482 } else {
10483 outStack.append("$");
10484 }
10485 }
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010486 for (int j=0; j<miCat.subitems.size(); j++) {
10487 MemItem mi = miCat.subitems.get(j);
10488 if (j > 0) {
Dianne Hackborn672342c2011-11-29 11:29:02 -080010489 if (outTag != null) {
10490 outTag.append(" ");
10491 }
10492 if (outStack != null) {
10493 outStack.append("$");
10494 }
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010495 }
Dianne Hackborn672342c2011-11-29 11:29:02 -080010496 if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) {
10497 appendMemBucket(outTag, mi.pss, mi.shortLabel, false);
10498 }
10499 if (outStack != null) {
10500 appendMemBucket(outStack, mi.pss, mi.shortLabel, true);
10501 }
10502 }
10503 if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) {
10504 outStack.append("(");
10505 for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) {
10506 if (DUMP_MEM_OOM_ADJ[k] == miCat.id) {
10507 outStack.append(DUMP_MEM_OOM_LABEL[k]);
10508 outStack.append(":");
10509 outStack.append(DUMP_MEM_OOM_ADJ[k]);
10510 }
10511 }
10512 outStack.append(")");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010513 }
10514 }
10515 }
10516 }
10517
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010518 if (!brief && !oomOnly) {
Dianne Hackborn04d6db32011-11-04 20:07:24 -070010519 pw.println();
10520 pw.println("Total PSS by process:");
10521 dumpMemItems(pw, " ", procMems, true);
10522 pw.println();
10523 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010524 pw.println("Total PSS by OOM adjustment:");
10525 dumpMemItems(pw, " ", oomMems, false);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010526 if (!oomOnly) {
10527 PrintWriter out = categoryPw != null ? categoryPw : pw;
10528 out.println();
10529 out.println("Total PSS by category:");
10530 dumpMemItems(out, " ", catMems, true);
Dianne Hackborn04d6db32011-11-04 20:07:24 -070010531 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010532 pw.println();
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010533 pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB");
Dianne Hackbornd3e677b2012-04-05 14:58:18 -070010534 final int[] SINGLE_LONG_FORMAT = new int[] {
10535 Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG
10536 };
10537 long[] longOut = new long[1];
10538 Process.readProcFile("/sys/kernel/mm/ksm/pages_shared",
10539 SINGLE_LONG_FORMAT, null, longOut, null);
10540 long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10541 longOut[0] = 0;
10542 Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing",
10543 SINGLE_LONG_FORMAT, null, longOut, null);
10544 long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10545 longOut[0] = 0;
10546 Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared",
10547 SINGLE_LONG_FORMAT, null, longOut, null);
10548 long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10549 longOut[0] = 0;
10550 Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile",
10551 SINGLE_LONG_FORMAT, null, longOut, null);
10552 long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10553 pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared ");
10554 pw.print(shared); pw.println(" kB");
10555 pw.print(" "); pw.print(unshared); pw.print(" kB unshared; ");
10556 pw.print(voltile); pw.println(" kB volatile");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558 }
10559
10560 /**
10561 * Searches array of arguments for the specified string
10562 * @param args array of argument strings
10563 * @param value value to search for
10564 * @return true if the value is contained in the array
10565 */
10566 private static boolean scanArgs(String[] args, String value) {
10567 if (args != null) {
10568 for (String arg : args) {
10569 if (value.equals(arg)) {
10570 return true;
10571 }
10572 }
10573 }
10574 return false;
10575 }
10576
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010577 private final boolean removeDyingProviderLocked(ProcessRecord proc,
10578 ContentProviderRecord cpr, boolean always) {
10579 final boolean inLaunching = mLaunchingProviders.contains(cpr);
10580
10581 if (!inLaunching || always) {
10582 synchronized (cpr) {
10583 cpr.launchingApp = null;
10584 cpr.notifyAll();
10585 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010586 mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010587 String names[] = cpr.info.authority.split(";");
10588 for (int j = 0; j < names.length; j++) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010589 mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 }
10591 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010592
10593 for (int i=0; i<cpr.connections.size(); i++) {
10594 ContentProviderConnection conn = cpr.connections.get(i);
10595 if (conn.waiting) {
10596 // If this connection is waiting for the provider, then we don't
10597 // need to mess with its process unless we are always removing
10598 // or for some reason the provider is not currently launching.
10599 if (inLaunching && !always) {
10600 continue;
10601 }
10602 }
10603 ProcessRecord capp = conn.client;
10604 conn.dead = true;
10605 if (conn.stableCount > 0) {
10606 if (!capp.persistent && capp.thread != null
10607 && capp.pid != 0
10608 && capp.pid != MY_PID) {
10609 Slog.i(TAG, "Kill " + capp.processName
10610 + " (pid " + capp.pid + "): provider " + cpr.info.name
10611 + " in dying process " + (proc != null ? proc.processName : "??"));
10612 EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid,
10613 capp.processName, capp.setAdj, "dying provider "
10614 + cpr.name.toShortString());
10615 Process.killProcessQuiet(capp.pid);
10616 }
10617 } else if (capp.thread != null && conn.provider.provider != null) {
10618 try {
10619 capp.thread.unstableProviderDied(conn.provider.provider.asBinder());
10620 } catch (RemoteException e) {
10621 }
10622 // In the protocol here, we don't expect the client to correctly
10623 // clean up this connection, we'll just remove it.
10624 cpr.connections.remove(i);
10625 conn.client.conProviders.remove(conn);
10626 }
10627 }
10628
10629 if (inLaunching && always) {
10630 mLaunchingProviders.remove(cpr);
10631 }
10632 return inLaunching;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633 }
10634
10635 /**
10636 * Main code for cleaning up a process when it has gone away. This is
10637 * called both as a result of the process dying, or directly when stopping
10638 * a process when running in single process mode.
10639 */
10640 private final void cleanUpApplicationRecordLocked(ProcessRecord app,
Dianne Hackborn130b0d22011-07-26 22:07:48 -070010641 boolean restarting, boolean allowRestart, int index) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642 if (index >= 0) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010643 mLruProcesses.remove(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 }
10645
Dianne Hackborn36124872009-10-08 16:22:03 -070010646 mProcessesToGc.remove(app);
10647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 // Dismiss any open dialogs.
10649 if (app.crashDialog != null) {
10650 app.crashDialog.dismiss();
10651 app.crashDialog = null;
10652 }
10653 if (app.anrDialog != null) {
10654 app.anrDialog.dismiss();
10655 app.anrDialog = null;
10656 }
10657 if (app.waitDialog != null) {
10658 app.waitDialog.dismiss();
10659 app.waitDialog = null;
10660 }
10661
10662 app.crashing = false;
10663 app.notResponding = false;
10664
10665 app.resetPackageList();
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -070010666 app.unlinkDeathRecipient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010667 app.thread = null;
10668 app.forcingToForeground = null;
10669 app.foregroundServices = false;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070010670 app.foregroundActivities = false;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070010671 app.hasShownUi = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070010672 app.hasAboveClient = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010673
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010674 mServices.killServicesLocked(app, allowRestart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675
10676 boolean restart = false;
10677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010678 // Remove published content providers.
10679 if (!app.pubProviders.isEmpty()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010680 Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010682 ContentProviderRecord cpr = it.next();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010683
10684 final boolean always = app.bad || !allowRestart;
10685 if (removeDyingProviderLocked(app, cpr, always) || always) {
10686 // We left the provider in the launching list, need to
10687 // restart it.
10688 restart = true;
10689 }
10690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 cpr.provider = null;
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -070010692 cpr.proc = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 }
10694 app.pubProviders.clear();
10695 }
10696
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010697 // Take care of any launching providers waiting for this process.
10698 if (checkAppInLaunchingProvidersLocked(app, false)) {
10699 restart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010700 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 // Unregister from connected content providers.
10703 if (!app.conProviders.isEmpty()) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010704 for (int i=0; i<app.conProviders.size(); i++) {
10705 ContentProviderConnection conn = app.conProviders.get(i);
10706 conn.provider.connections.remove(conn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 }
10708 app.conProviders.clear();
10709 }
10710
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010711 // At this point there may be remaining entries in mLaunchingProviders
10712 // where we were the only one waiting, so they are no longer of use.
10713 // Look for these and clean up if found.
10714 // XXX Commented out for now. Trying to figure out a way to reproduce
10715 // the actual situation to identify what is actually going on.
10716 if (false) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010717 for (int i=0; i<mLaunchingProviders.size(); i++) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010718 ContentProviderRecord cpr = (ContentProviderRecord)
10719 mLaunchingProviders.get(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010720 if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010721 synchronized (cpr) {
10722 cpr.launchingApp = null;
10723 cpr.notifyAll();
10724 }
10725 }
10726 }
10727 }
10728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 skipCurrentReceiverLocked(app);
10730
10731 // Unregister any receivers.
10732 if (app.receivers.size() > 0) {
10733 Iterator<ReceiverList> it = app.receivers.iterator();
10734 while (it.hasNext()) {
10735 removeReceiverLocked(it.next());
10736 }
10737 app.receivers.clear();
10738 }
10739
Christopher Tate181fafa2009-05-14 11:12:14 -070010740 // If the app is undergoing backup, tell the backup manager about it
10741 if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010742 if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup");
Christopher Tate181fafa2009-05-14 11:12:14 -070010743 try {
10744 IBackupManager bm = IBackupManager.Stub.asInterface(
10745 ServiceManager.getService(Context.BACKUP_SERVICE));
10746 bm.agentDisconnected(app.info.packageName);
10747 } catch (RemoteException e) {
10748 // can't happen; backup manager is local
10749 }
10750 }
10751
Dianne Hackborna93c2c12012-05-31 15:29:36 -070010752 for (int i = mPendingProcessChanges.size()-1; i>=0; i--) {
10753 ProcessChangeItem item = mPendingProcessChanges.get(i);
10754 if (item.pid == app.pid) {
10755 mPendingProcessChanges.remove(i);
10756 mAvailProcessChanges.add(item);
10757 }
10758 }
Jeff Sharkey287bd832011-05-28 19:36:26 -070010759 mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070010760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 // If the caller is restarting this app, then leave it in its
10762 // current lists and let the caller take care of it.
10763 if (restarting) {
10764 return;
10765 }
10766
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010767 if (!app.persistent || app.isolated) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010768 if (DEBUG_PROCESSES) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 "Removing non-persistent process during cleanup: " + app);
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010770 mProcessNames.remove(app.processName, app.uid);
10771 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -070010772 if (mHeavyWeightProcess == app) {
Dianne Hackborn41203752012-08-31 14:05:51 -070010773 mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
10774 mHeavyWeightProcess.userId, 0));
Dianne Hackborn860755f2010-06-03 18:47:52 -070010775 mHeavyWeightProcess = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -070010776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777 } else if (!app.removed) {
10778 // This app is persistent, so we need to keep its record around.
10779 // If it is not already on the pending app list, add it there
10780 // and start a new process for it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010781 if (mPersistentStartingProcesses.indexOf(app) < 0) {
10782 mPersistentStartingProcesses.add(app);
10783 restart = true;
10784 }
10785 }
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070010786 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
10787 "Clean-up removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010788 mProcessesOnHold.remove(app);
10789
The Android Open Source Project4df24232009-03-05 14:34:35 -080010790 if (app == mHomeProcess) {
10791 mHomeProcess = null;
10792 }
Dianne Hackbornf35fe232011-11-01 19:25:20 -070010793 if (app == mPreviousProcess) {
10794 mPreviousProcess = null;
10795 }
10796
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010797 if (restart && !app.isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010798 // We have components that still need to be running in the
10799 // process, so re-launch it.
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010800 mProcessNames.put(app.processName, app.uid, app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 startProcessLocked(app, "restart", app.processName);
10802 } else if (app.pid > 0 && app.pid != MY_PID) {
10803 // Goodbye!
10804 synchronized (mPidsSelfLocked) {
10805 mPidsSelfLocked.remove(app.pid);
10806 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
10807 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010808 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010809 }
10810 }
10811
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010812 boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) {
10813 // Look through the content providers we are waiting to have launched,
10814 // and if any run in this process then either schedule a restart of
10815 // the process or kill the client waiting for it if this process has
10816 // gone bad.
10817 int NL = mLaunchingProviders.size();
10818 boolean restart = false;
10819 for (int i=0; i<NL; i++) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010820 ContentProviderRecord cpr = mLaunchingProviders.get(i);
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010821 if (cpr.launchingApp == app) {
10822 if (!alwaysBad && !app.bad) {
10823 restart = true;
10824 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010825 removeDyingProviderLocked(app, cpr, true);
Vairavan Srinivasan4ee36492012-09-03 17:09:58 -070010826 // cpr should have been removed from mLaunchingProviders
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010827 NL = mLaunchingProviders.size();
Vairavan Srinivasan4ee36492012-09-03 17:09:58 -070010828 i--;
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010829 }
10830 }
10831 }
10832 return restart;
10833 }
10834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 // =========================================================
10836 // SERVICES
10837 // =========================================================
10838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 public List<ActivityManager.RunningServiceInfo> getServices(int maxNum,
10840 int flags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010841 enforceNotIsolatedCaller("getServices");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010843 return mServices.getRunningServiceInfoLocked(maxNum, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010844 }
10845 }
10846
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010847 public PendingIntent getRunningServiceControlPanel(ComponentName name) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010848 enforceNotIsolatedCaller("getRunningServiceControlPanel");
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010849 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010850 return mServices.getRunningServiceControlPanelLocked(name);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010851 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010852 }
10853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854 public ComponentName startService(IApplicationThread caller, Intent service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010855 String resolvedType, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010856 enforceNotIsolatedCaller("startService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010857 // Refuse possible leaked file descriptors
10858 if (service != null && service.hasFileDescriptors() == true) {
10859 throw new IllegalArgumentException("File descriptors passed in Intent");
10860 }
10861
Amith Yamasani742a6712011-05-04 14:49:28 -070010862 if (DEBUG_SERVICE)
10863 Slog.v(TAG, "startService: " + service + " type=" + resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010864 synchronized(this) {
10865 final int callingPid = Binder.getCallingPid();
10866 final int callingUid = Binder.getCallingUid();
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010867 checkValidCaller(callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010868 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010869 ComponentName res = mServices.startServiceLocked(caller, service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010870 resolvedType, callingPid, callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010871 Binder.restoreCallingIdentity(origId);
10872 return res;
10873 }
10874 }
10875
10876 ComponentName startServiceInPackage(int uid,
Dianne Hackborn41203752012-08-31 14:05:51 -070010877 Intent service, String resolvedType, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010878 synchronized(this) {
Amith Yamasani742a6712011-05-04 14:49:28 -070010879 if (DEBUG_SERVICE)
10880 Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010882 ComponentName res = mServices.startServiceLocked(null, service,
Dianne Hackborn41203752012-08-31 14:05:51 -070010883 resolvedType, -1, uid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010884 Binder.restoreCallingIdentity(origId);
10885 return res;
10886 }
10887 }
10888
10889 public int stopService(IApplicationThread caller, Intent service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010890 String resolvedType, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010891 enforceNotIsolatedCaller("stopService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010892 // Refuse possible leaked file descriptors
10893 if (service != null && service.hasFileDescriptors() == true) {
10894 throw new IllegalArgumentException("File descriptors passed in Intent");
10895 }
10896
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010897 checkValidCaller(Binder.getCallingUid(), userId);
10898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010899 synchronized(this) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010900 return mServices.stopServiceLocked(caller, service, resolvedType, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010902 }
10903
10904 public IBinder peekService(Intent service, String resolvedType) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010905 enforceNotIsolatedCaller("peekService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010906 // Refuse possible leaked file descriptors
10907 if (service != null && service.hasFileDescriptors() == true) {
10908 throw new IllegalArgumentException("File descriptors passed in Intent");
10909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010910 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010911 return mServices.peekServiceLocked(service, resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010913 }
10914
10915 public boolean stopServiceToken(ComponentName className, IBinder token,
10916 int startId) {
10917 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010918 return mServices.stopServiceTokenLocked(className, token, startId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 }
10921
10922 public void setServiceForeground(ComponentName className, IBinder token,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070010923 int id, Notification notification, boolean removeNotification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010924 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010925 mServices.setServiceForegroundLocked(className, token, id, notification,
10926 removeNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010927 }
10928 }
Amith Yamasania4a54e22012-04-16 15:44:19 -070010929
Dianne Hackborn41203752012-08-31 14:05:51 -070010930 public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll,
10931 boolean requireFull, String name, String callerPackage) {
10932 synchronized(this) {
10933 return handleIncomingUserLocked(callingPid, callingUid, userId, allowAll,
10934 requireFull, name, callerPackage);
10935 }
10936 }
10937
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070010938 int handleIncomingUserLocked(int callingPid, int callingUid, int userId, boolean allowAll,
10939 boolean requireFull, String name, String callerPackage) {
10940 final int callingUserId = UserHandle.getUserId(callingUid);
10941 if (callingUserId != userId) {
10942 if (callingUid != 0 && callingUid != Process.SYSTEM_UID) {
10943 if ((requireFull || checkComponentPermission(
10944 android.Manifest.permission.INTERACT_ACROSS_USERS,
10945 callingPid, callingUid, -1, true) != PackageManager.PERMISSION_GRANTED)
10946 && checkComponentPermission(
10947 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
10948 callingPid, callingUid, -1, true)
10949 != PackageManager.PERMISSION_GRANTED) {
10950 if (userId == UserHandle.USER_CURRENT_OR_SELF) {
10951 // In this case, they would like to just execute as their
10952 // owner user instead of failing.
10953 userId = callingUserId;
10954 } else {
Dianne Hackborn41203752012-08-31 14:05:51 -070010955 StringBuilder builder = new StringBuilder(128);
10956 builder.append("Permission Denial: ");
10957 builder.append(name);
10958 if (callerPackage != null) {
10959 builder.append(" from ");
10960 builder.append(callerPackage);
10961 }
10962 builder.append(" asks to run as user ");
10963 builder.append(userId);
10964 builder.append(" but is calling from user ");
10965 builder.append(UserHandle.getUserId(callingUid));
10966 builder.append("; this requires ");
10967 builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL);
10968 if (!requireFull) {
Dianne Hackborn74ee8652012-09-07 18:33:18 -070010969 builder.append(" or ");
Dianne Hackborn41203752012-08-31 14:05:51 -070010970 builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS);
10971 }
10972 String msg = builder.toString();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070010973 Slog.w(TAG, msg);
10974 throw new SecurityException(msg);
10975 }
10976 }
10977 }
10978 if (userId == UserHandle.USER_CURRENT
10979 || userId == UserHandle.USER_CURRENT_OR_SELF) {
10980 userId = mCurrentUserId;
10981 }
10982 if (!allowAll && userId < 0) {
10983 throw new IllegalArgumentException(
10984 "Call does not support special user #" + userId);
10985 }
10986 }
10987 return userId;
10988 }
10989
Dianne Hackborn7d19e022012-08-07 19:12:33 -070010990 boolean isSingleton(String componentProcessName, ApplicationInfo aInfo,
10991 String className, int flags) {
Amith Yamasania4a54e22012-04-16 15:44:19 -070010992 boolean result = false;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010993 if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -070010994 if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) {
10995 if (ActivityManager.checkUidPermission(
10996 android.Manifest.permission.INTERACT_ACROSS_USERS,
10997 aInfo.uid) != PackageManager.PERMISSION_GRANTED) {
10998 ComponentName comp = new ComponentName(aInfo.packageName, className);
10999 String msg = "Permission Denial: Component " + comp.flattenToShortString()
11000 + " requests FLAG_SINGLE_USER, but app does not hold "
11001 + android.Manifest.permission.INTERACT_ACROSS_USERS;
11002 Slog.w(TAG, msg);
11003 throw new SecurityException(msg);
11004 }
11005 result = true;
11006 }
Amith Yamasania4a54e22012-04-16 15:44:19 -070011007 } else if (componentProcessName == aInfo.packageName) {
11008 result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0;
11009 } else if ("system".equals(componentProcessName)) {
11010 result = true;
11011 }
11012 if (DEBUG_MU) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -070011013 Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo
11014 + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result);
Amith Yamasania4a54e22012-04-16 15:44:19 -070011015 }
11016 return result;
11017 }
11018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011019 public int bindService(IApplicationThread caller, IBinder token,
11020 Intent service, String resolvedType,
Amith Yamasani37ce3a82012-02-06 12:04:42 -080011021 IServiceConnection connection, int flags, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011022 enforceNotIsolatedCaller("bindService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011023 // Refuse possible leaked file descriptors
11024 if (service != null && service.hasFileDescriptors() == true) {
11025 throw new IllegalArgumentException("File descriptors passed in Intent");
11026 }
11027
11028 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070011029 return mServices.bindServiceLocked(caller, token, service, resolvedType,
11030 connection, flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011031 }
11032 }
11033
11034 public boolean unbindService(IServiceConnection connection) {
11035 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070011036 return mServices.unbindServiceLocked(connection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011038 }
11039
11040 public void publishService(IBinder token, Intent intent, IBinder service) {
11041 // Refuse possible leaked file descriptors
11042 if (intent != null && intent.hasFileDescriptors() == true) {
11043 throw new IllegalArgumentException("File descriptors passed in Intent");
11044 }
11045
11046 synchronized(this) {
11047 if (!(token instanceof ServiceRecord)) {
11048 throw new IllegalArgumentException("Invalid service token");
11049 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -070011050 mServices.publishServiceLocked((ServiceRecord)token, intent, service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011051 }
11052 }
11053
11054 public void unbindFinished(IBinder token, Intent intent, boolean doRebind) {
11055 // Refuse possible leaked file descriptors
11056 if (intent != null && intent.hasFileDescriptors() == true) {
11057 throw new IllegalArgumentException("File descriptors passed in Intent");
11058 }
11059
11060 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070011061 mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 }
11063 }
11064
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011065 public void serviceDoneExecuting(IBinder token, int type, int startId, int res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 synchronized(this) {
11067 if (!(token instanceof ServiceRecord)) {
11068 throw new IllegalArgumentException("Invalid service token");
11069 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -070011070 mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res);
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011072 }
11073
11074 // =========================================================
Christopher Tate181fafa2009-05-14 11:12:14 -070011075 // BACKUP AND RESTORE
11076 // =========================================================
11077
11078 // Cause the target app to be launched if necessary and its backup agent
11079 // instantiated. The backup agent will invoke backupAgentCreated() on the
11080 // activity manager to announce its creation.
11081 public boolean bindBackupAgent(ApplicationInfo app, int backupMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011082 if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -070011083 enforceCallingPermission("android.permission.BACKUP", "startBackupAgent");
11084
11085 synchronized(this) {
11086 // !!! TODO: currently no check here that we're already bound
11087 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
11088 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
11089 synchronized (stats) {
11090 ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name);
11091 }
11092
Dianne Hackborne7f97212011-02-24 14:40:20 -080011093 // Backup agent is now in use, its package can't be stopped.
11094 try {
11095 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011096 app.packageName, false, UserHandle.getUserId(app.uid));
Dianne Hackborne7f97212011-02-24 14:40:20 -080011097 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -080011098 } catch (IllegalArgumentException e) {
11099 Slog.w(TAG, "Failed trying to unstop package "
11100 + app.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -080011101 }
11102
Christopher Tate181fafa2009-05-14 11:12:14 -070011103 BackupRecord r = new BackupRecord(ss, app, backupMode);
Christopher Tate4a627c72011-04-01 14:43:32 -070011104 ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL)
11105 ? new ComponentName(app.packageName, app.backupAgentName)
11106 : new ComponentName("android", "FullBackupAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -070011107 // startProcessLocked() returns existing proc's record if it's already running
11108 ProcessRecord proc = startProcessLocked(app.processName, app,
Dianne Hackborna0c283e2012-02-09 10:47:01 -080011109 false, 0, "backup", hostingName, false, false);
Christopher Tate181fafa2009-05-14 11:12:14 -070011110 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011111 Slog.e(TAG, "Unable to start backup agent process " + r);
Christopher Tate181fafa2009-05-14 11:12:14 -070011112 return false;
11113 }
11114
11115 r.app = proc;
11116 mBackupTarget = r;
11117 mBackupAppName = app.packageName;
11118
Christopher Tate6fa95972009-06-05 18:43:55 -070011119 // Try not to kill the process during backup
11120 updateOomAdjLocked(proc);
11121
Christopher Tate181fafa2009-05-14 11:12:14 -070011122 // If the process is already attached, schedule the creation of the backup agent now.
11123 // If it is not yet live, this will be done when it attaches to the framework.
11124 if (proc.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011125 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc);
Christopher Tate181fafa2009-05-14 11:12:14 -070011126 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -040011127 proc.thread.scheduleCreateBackupAgent(app,
11128 compatibilityInfoForPackageLocked(app), backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -070011129 } catch (RemoteException e) {
Christopher Tate436344a2009-09-30 16:17:37 -070011130 // Will time out on the backup manager side
Christopher Tate181fafa2009-05-14 11:12:14 -070011131 }
11132 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011133 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc not running, waiting for attach");
Christopher Tate181fafa2009-05-14 11:12:14 -070011134 }
11135 // Invariants: at this point, the target app process exists and the application
11136 // is either already running or in the process of coming up. mBackupTarget and
11137 // mBackupAppName describe the app, so that when it binds back to the AM we
11138 // know that it's scheduled for a backup-agent operation.
11139 }
11140
11141 return true;
11142 }
11143
11144 // A backup agent has just come up
11145 public void backupAgentCreated(String agentPackageName, IBinder agent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011146 if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName
Christopher Tate181fafa2009-05-14 11:12:14 -070011147 + " = " + agent);
11148
11149 synchronized(this) {
11150 if (!agentPackageName.equals(mBackupAppName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011151 Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!");
Christopher Tate181fafa2009-05-14 11:12:14 -070011152 return;
11153 }
Dianne Hackborn06740692010-09-22 22:46:21 -070011154 }
Christopher Tate181fafa2009-05-14 11:12:14 -070011155
Dianne Hackborn06740692010-09-22 22:46:21 -070011156 long oldIdent = Binder.clearCallingIdentity();
11157 try {
11158 IBackupManager bm = IBackupManager.Stub.asInterface(
11159 ServiceManager.getService(Context.BACKUP_SERVICE));
11160 bm.agentConnected(agentPackageName, agent);
11161 } catch (RemoteException e) {
11162 // can't happen; the backup manager service is local
11163 } catch (Exception e) {
11164 Slog.w(TAG, "Exception trying to deliver BackupAgent binding: ");
11165 e.printStackTrace();
11166 } finally {
11167 Binder.restoreCallingIdentity(oldIdent);
Christopher Tate181fafa2009-05-14 11:12:14 -070011168 }
11169 }
11170
11171 // done with this agent
11172 public void unbindBackupAgent(ApplicationInfo appInfo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011173 if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo);
Christopher Tate8a27f922009-06-26 11:49:18 -070011174 if (appInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011175 Slog.w(TAG, "unbind backup agent for null app");
Christopher Tate8a27f922009-06-26 11:49:18 -070011176 return;
11177 }
Christopher Tate181fafa2009-05-14 11:12:14 -070011178
11179 synchronized(this) {
Christopher Tate8a27f922009-06-26 11:49:18 -070011180 if (mBackupAppName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011181 Slog.w(TAG, "Unbinding backup agent with no active backup");
Christopher Tate8a27f922009-06-26 11:49:18 -070011182 return;
11183 }
11184
Christopher Tate181fafa2009-05-14 11:12:14 -070011185 if (!mBackupAppName.equals(appInfo.packageName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011186 Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target");
Christopher Tate181fafa2009-05-14 11:12:14 -070011187 return;
11188 }
11189
Christopher Tate6fa95972009-06-05 18:43:55 -070011190 ProcessRecord proc = mBackupTarget.app;
11191 mBackupTarget = null;
11192 mBackupAppName = null;
11193
11194 // Not backing this app up any more; reset its OOM adjustment
11195 updateOomAdjLocked(proc);
11196
Christopher Tatec7b31e32009-06-10 15:49:30 -070011197 // If the app crashed during backup, 'thread' will be null here
11198 if (proc.thread != null) {
11199 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -040011200 proc.thread.scheduleDestroyBackupAgent(appInfo,
11201 compatibilityInfoForPackageLocked(appInfo));
Christopher Tatec7b31e32009-06-10 15:49:30 -070011202 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011203 Slog.e(TAG, "Exception when unbinding backup agent:");
Christopher Tatec7b31e32009-06-10 15:49:30 -070011204 e.printStackTrace();
11205 }
Christopher Tate181fafa2009-05-14 11:12:14 -070011206 }
Christopher Tate181fafa2009-05-14 11:12:14 -070011207 }
11208 }
11209 // =========================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 // BROADCASTS
11211 // =========================================================
11212
Josh Bartel7f208742010-02-25 11:01:44 -060011213 private final List getStickiesLocked(String action, IntentFilter filter,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011214 List cur, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011215 final ContentResolver resolver = mContext.getContentResolver();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011216 HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId);
11217 if (stickies == null) {
11218 return cur;
11219 }
11220 final ArrayList<Intent> list = stickies.get(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011221 if (list == null) {
11222 return cur;
11223 }
11224 int N = list.size();
11225 for (int i=0; i<N; i++) {
11226 Intent intent = list.get(i);
11227 if (filter.match(resolver, intent, true, TAG) >= 0) {
11228 if (cur == null) {
11229 cur = new ArrayList<Intent>();
11230 }
11231 cur.add(intent);
11232 }
11233 }
11234 return cur;
11235 }
11236
Christopher Tatef46723b2012-01-26 14:19:24 -080011237 boolean isPendingBroadcastProcessLocked(int pid) {
11238 return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid)
11239 || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid);
11240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011241
Christopher Tatef46723b2012-01-26 14:19:24 -080011242 void skipPendingBroadcastLocked(int pid) {
11243 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
11244 for (BroadcastQueue queue : mBroadcastQueues) {
11245 queue.skipPendingBroadcastLocked(pid);
11246 }
11247 }
11248
11249 // The app just attached; send any pending broadcasts that it should receive
11250 boolean sendPendingBroadcastsLocked(ProcessRecord app) {
11251 boolean didSomething = false;
11252 for (BroadcastQueue queue : mBroadcastQueues) {
11253 didSomething |= queue.sendPendingBroadcastsLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011254 }
Christopher Tatef46723b2012-01-26 14:19:24 -080011255 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011256 }
11257
Dianne Hackborn6c418d52011-06-29 14:05:33 -070011258 public Intent registerReceiver(IApplicationThread caller, String callerPackage,
Dianne Hackborn20e80982012-08-31 19:00:44 -070011259 IIntentReceiver receiver, IntentFilter filter, String permission, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011260 enforceNotIsolatedCaller("registerReceiver");
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011261 int callingUid;
Dianne Hackborn20e80982012-08-31 19:00:44 -070011262 int callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011263 synchronized(this) {
11264 ProcessRecord callerApp = null;
11265 if (caller != null) {
11266 callerApp = getRecordForAppLocked(caller);
11267 if (callerApp == null) {
11268 throw new SecurityException(
11269 "Unable to find app for caller " + caller
11270 + " (pid=" + Binder.getCallingPid()
11271 + ") when registering receiver " + receiver);
11272 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -070011273 if (callerApp.info.uid != Process.SYSTEM_UID &&
11274 !callerApp.pkgList.contains(callerPackage)) {
11275 throw new SecurityException("Given caller package " + callerPackage
11276 + " is not running in process " + callerApp);
11277 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011278 callingUid = callerApp.info.uid;
Dianne Hackborn20e80982012-08-31 19:00:44 -070011279 callingPid = callerApp.pid;
Dianne Hackborn6c418d52011-06-29 14:05:33 -070011280 } else {
11281 callerPackage = null;
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011282 callingUid = Binder.getCallingUid();
Dianne Hackborn20e80982012-08-31 19:00:44 -070011283 callingPid = Binder.getCallingPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011284 }
11285
Dianne Hackborn20e80982012-08-31 19:00:44 -070011286 userId = this.handleIncomingUserLocked(callingPid, callingUid, userId,
11287 true, true, "registerReceiver", callerPackage);
11288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011289 List allSticky = null;
11290
11291 // Look for any matching sticky broadcasts...
11292 Iterator actions = filter.actionsIterator();
11293 if (actions != null) {
11294 while (actions.hasNext()) {
11295 String action = (String)actions.next();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011296 allSticky = getStickiesLocked(action, filter, allSticky,
11297 UserHandle.USER_ALL);
11298 allSticky = getStickiesLocked(action, filter, allSticky,
11299 UserHandle.getUserId(callingUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011300 }
11301 } else {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011302 allSticky = getStickiesLocked(null, filter, allSticky,
11303 UserHandle.USER_ALL);
11304 allSticky = getStickiesLocked(null, filter, allSticky,
11305 UserHandle.getUserId(callingUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011306 }
11307
11308 // The first sticky in the list is returned directly back to
11309 // the client.
11310 Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null;
11311
Joe Onorato8a9b2202010-02-26 18:56:32 -080011312 if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011313 + ": " + sticky);
11314
11315 if (receiver == null) {
11316 return sticky;
11317 }
11318
11319 ReceiverList rl
11320 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
11321 if (rl == null) {
Dianne Hackborn20e80982012-08-31 19:00:44 -070011322 rl = new ReceiverList(this, callerApp, callingPid, callingUid,
11323 userId, receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011324 if (rl.app != null) {
11325 rl.app.receivers.add(rl);
11326 } else {
11327 try {
11328 receiver.asBinder().linkToDeath(rl, 0);
11329 } catch (RemoteException e) {
11330 return sticky;
11331 }
11332 rl.linkedToDeath = true;
11333 }
11334 mRegisteredReceivers.put(receiver.asBinder(), rl);
Dianne Hackborn20e80982012-08-31 19:00:44 -070011335 } else if (rl.uid != callingUid) {
11336 throw new IllegalArgumentException(
11337 "Receiver requested to register for uid " + callingUid
11338 + " was previously registered for uid " + rl.uid);
11339 } else if (rl.pid != callingPid) {
11340 throw new IllegalArgumentException(
11341 "Receiver requested to register for pid " + callingPid
11342 + " was previously registered for pid " + rl.pid);
11343 } else if (rl.userId != userId) {
11344 throw new IllegalArgumentException(
11345 "Receiver requested to register for user " + userId
11346 + " was previously registered for user " + rl.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011347 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011348 BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,
Dianne Hackborn20e80982012-08-31 19:00:44 -070011349 permission, callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011350 rl.add(bf);
11351 if (!bf.debugCheck()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011352 Slog.w(TAG, "==> For Dynamic broadast");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011353 }
11354 mReceiverResolver.addFilter(bf);
11355
11356 // Enqueue broadcasts for all existing stickies that match
11357 // this filter.
11358 if (allSticky != null) {
11359 ArrayList receivers = new ArrayList();
11360 receivers.add(bf);
11361
11362 int N = allSticky.size();
11363 for (int i=0; i<N; i++) {
11364 Intent intent = (Intent)allSticky.get(i);
Christopher Tatef46723b2012-01-26 14:19:24 -080011365 BroadcastQueue queue = broadcastQueueForIntent(intent);
11366 BroadcastRecord r = new BroadcastRecord(queue, intent, null,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011367 null, -1, -1, null, receivers, null, 0, null, null,
Amith Yamasani8bf06ed2012-08-27 19:30:30 -070011368 false, true, true, -1);
Christopher Tatef46723b2012-01-26 14:19:24 -080011369 queue.enqueueParallelBroadcastLocked(r);
11370 queue.scheduleBroadcastsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011371 }
11372 }
11373
11374 return sticky;
11375 }
11376 }
11377
11378 public void unregisterReceiver(IIntentReceiver receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011379 if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011380
Christopher Tatef46723b2012-01-26 14:19:24 -080011381 final long origId = Binder.clearCallingIdentity();
11382 try {
11383 boolean doTrim = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011384
Christopher Tatef46723b2012-01-26 14:19:24 -080011385 synchronized(this) {
11386 ReceiverList rl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
Christopher Tatef46723b2012-01-26 14:19:24 -080011388 if (rl != null) {
11389 if (rl.curBroadcast != null) {
11390 BroadcastRecord r = rl.curBroadcast;
11391 final boolean doNext = finishReceiverLocked(
11392 receiver.asBinder(), r.resultCode, r.resultData,
11393 r.resultExtras, r.resultAbort, true);
11394 if (doNext) {
11395 doTrim = true;
11396 r.queue.processNextBroadcast(false);
11397 }
11398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011399
Christopher Tatef46723b2012-01-26 14:19:24 -080011400 if (rl.app != null) {
11401 rl.app.receivers.remove(rl);
11402 }
11403 removeReceiverLocked(rl);
11404 if (rl.linkedToDeath) {
11405 rl.linkedToDeath = false;
11406 rl.receiver.asBinder().unlinkToDeath(rl, 0);
11407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011408 }
11409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410
Christopher Tatef46723b2012-01-26 14:19:24 -080011411 // If we actually concluded any broadcasts, we might now be able
11412 // to trim the recipients' apps from our working set
11413 if (doTrim) {
11414 trimApplications();
11415 return;
11416 }
11417
11418 } finally {
11419 Binder.restoreCallingIdentity(origId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011421 }
11422
11423 void removeReceiverLocked(ReceiverList rl) {
11424 mRegisteredReceivers.remove(rl.receiver.asBinder());
11425 int N = rl.size();
11426 for (int i=0; i<N; i++) {
11427 mReceiverResolver.removeFilter(rl.get(i));
11428 }
11429 }
11430
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011431 private final void sendPackageBroadcastLocked(int cmd, String[] packages, int userId) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011432 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
11433 ProcessRecord r = mLruProcesses.get(i);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011434 if (r.thread != null && (userId == UserHandle.USER_ALL || r.userId == userId)) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011435 try {
11436 r.thread.dispatchPackageBroadcast(cmd, packages);
11437 } catch (RemoteException ex) {
11438 }
11439 }
11440 }
11441 }
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011442
11443 private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType,
11444 int[] users) {
11445 List<ResolveInfo> receivers = null;
11446 try {
11447 HashSet<ComponentName> singleUserReceivers = null;
11448 boolean scannedFirstReceivers = false;
11449 for (int user : users) {
11450 List<ResolveInfo> newReceivers = AppGlobals.getPackageManager()
11451 .queryIntentReceivers(intent, resolvedType, STOCK_PM_FLAGS, user);
11452 if (newReceivers != null && newReceivers.size() == 0) {
11453 newReceivers = null;
11454 }
11455 if (receivers == null) {
11456 receivers = newReceivers;
11457 } else if (newReceivers != null) {
11458 // We need to concatenate the additional receivers
11459 // found with what we have do far. This would be easy,
11460 // but we also need to de-dup any receivers that are
11461 // singleUser.
11462 if (!scannedFirstReceivers) {
11463 // Collect any single user receivers we had already retrieved.
11464 scannedFirstReceivers = true;
11465 for (int i=0; i<receivers.size(); i++) {
11466 ResolveInfo ri = receivers.get(i);
11467 if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
11468 ComponentName cn = new ComponentName(
11469 ri.activityInfo.packageName, ri.activityInfo.name);
11470 if (singleUserReceivers == null) {
11471 singleUserReceivers = new HashSet<ComponentName>();
11472 }
11473 singleUserReceivers.add(cn);
11474 }
11475 }
11476 }
11477 // Add the new results to the existing results, tracking
11478 // and de-dupping single user receivers.
11479 for (int i=0; i<newReceivers.size(); i++) {
Dianne Hackborn6cbd33f2012-09-17 18:28:24 -070011480 ResolveInfo ri = newReceivers.get(i);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011481 if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {
11482 ComponentName cn = new ComponentName(
11483 ri.activityInfo.packageName, ri.activityInfo.name);
11484 if (singleUserReceivers == null) {
11485 singleUserReceivers = new HashSet<ComponentName>();
11486 }
11487 if (!singleUserReceivers.contains(cn)) {
11488 singleUserReceivers.add(cn);
11489 receivers.add(ri);
11490 }
11491 } else {
11492 receivers.add(ri);
11493 }
11494 }
11495 }
11496 }
11497 } catch (RemoteException ex) {
11498 // pm is in same process, this will never happen.
11499 }
11500 return receivers;
11501 }
11502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011503 private final int broadcastIntentLocked(ProcessRecord callerApp,
11504 String callerPackage, Intent intent, String resolvedType,
11505 IIntentReceiver resultTo, int resultCode, String resultData,
11506 Bundle map, String requiredPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -070011507 boolean ordered, boolean sticky, int callingPid, int callingUid,
11508 int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011509 intent = new Intent(intent);
11510
Dianne Hackborne7f97212011-02-24 14:40:20 -080011511 // By default broadcasts do not go to stopped apps.
11512 intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
11513
Joe Onorato8a9b2202010-02-26 18:56:32 -080011514 if (DEBUG_BROADCAST_LIGHT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011515 TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent
Amith Yamasani13593602012-03-22 16:16:17 -070011516 + " ordered=" + ordered + " userid=" + userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011517 if ((resultTo != null) && !ordered) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011518 Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011519 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011520
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011521 userId = handleIncomingUserLocked(callingPid, callingUid, userId,
11522 true, false, "broadcast", callerPackage);
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011523
Dianne Hackbornc72fc672012-09-20 13:12:03 -070011524 // Make sure that the user who is receiving this broadcast is started.
Dianne Hackborn80a4af22012-08-27 19:18:31 -070011525 // If not, we will just skip it.
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011526 if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070011527 if (callingUid != Process.SYSTEM_UID || (intent.getFlags()
11528 & Intent.FLAG_RECEIVER_BOOT_UPGRADE) == 0) {
11529 Slog.w(TAG, "Skipping broadcast of " + intent
11530 + ": user " + userId + " is stopped");
11531 return ActivityManager.BROADCAST_SUCCESS;
11532 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070011533 }
11534
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011535 /*
11536 * Prevent non-system code (defined here to be non-persistent
11537 * processes) from sending protected broadcasts.
11538 */
11539 if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID
11540 || callingUid == Process.SHELL_UID || callingUid == Process.BLUETOOTH_UID ||
11541 callingUid == 0) {
11542 // Always okay.
11543 } else if (callerApp == null || !callerApp.persistent) {
11544 try {
11545 if (AppGlobals.getPackageManager().isProtectedBroadcast(
11546 intent.getAction())) {
11547 String msg = "Permission Denial: not allowed to send broadcast "
11548 + intent.getAction() + " from pid="
11549 + callingPid + ", uid=" + callingUid;
11550 Slog.w(TAG, msg);
11551 throw new SecurityException(msg);
11552 }
11553 } catch (RemoteException e) {
11554 Slog.w(TAG, "Remote exception", e);
11555 return ActivityManager.BROADCAST_SUCCESS;
11556 }
11557 }
11558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011559 // Handle special intents: if this broadcast is from the package
11560 // manager about a package being removed, we need to remove all of
11561 // its activities from the history stack.
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011562 final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011563 intent.getAction());
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011564 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
11565 || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080011566 || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011567 || uidRemoved) {
11568 if (checkComponentPermission(
11569 android.Manifest.permission.BROADCAST_PACKAGE_REMOVED,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -080011570 callingPid, callingUid, -1, true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011571 == PackageManager.PERMISSION_GRANTED) {
11572 if (uidRemoved) {
11573 final Bundle intentExtras = intent.getExtras();
11574 final int uid = intentExtras != null
11575 ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
11576 if (uid >= 0) {
11577 BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
11578 synchronized (bs) {
11579 bs.removeUidStatsLocked(uid);
11580 }
11581 }
11582 } else {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011583 // If resources are unavailable just force stop all
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011584 // those packages and flush the attribute cache as well.
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080011585 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011586 String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
11587 if (list != null && (list.length > 0)) {
11588 for (String pkg : list) {
Amith Yamasani483f3b02012-03-13 16:08:00 -070011589 forceStopPackageLocked(pkg, -1, false, true, true, false, userId);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011590 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011591 sendPackageBroadcastLocked(
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011592 IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011593 }
11594 } else {
11595 Uri data = intent.getData();
11596 String ssp;
11597 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
11598 if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
11599 forceStopPackageLocked(ssp,
Amith Yamasani483f3b02012-03-13 16:08:00 -070011600 intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true,
11601 false, userId);
Dianne Hackbornde7faf62009-06-30 13:27:30 -070011602 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011603 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011604 sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011605 new String[] {ssp}, userId);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011607 }
11608 }
11609 }
11610 } else {
11611 String msg = "Permission Denial: " + intent.getAction()
11612 + " broadcast from " + callerPackage + " (pid=" + callingPid
11613 + ", uid=" + callingUid + ")"
11614 + " requires "
11615 + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011616 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 throw new SecurityException(msg);
11618 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011619
11620 // Special case for adding a package: by default turn on compatibility
11621 // mode.
11622 } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070011623 Uri data = intent.getData();
11624 String ssp;
11625 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
11626 mCompatModePackages.handlePackageAddedLocked(ssp,
11627 intent.getBooleanExtra(Intent.EXTRA_REPLACING, false));
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011629 }
11630
11631 /*
11632 * If this is the time zone changed action, queue up a message that will reset the timezone
11633 * of all currently running processes. This message will get queued up before the broadcast
11634 * happens.
11635 */
11636 if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {
11637 mHandler.sendEmptyMessage(UPDATE_TIME_ZONE);
11638 }
11639
Robert Greenwalt03595d02010-11-02 14:08:23 -070011640 if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) {
11641 mHandler.sendEmptyMessage(CLEAR_DNS_CACHE);
11642 }
11643
Robert Greenwalt434203a2010-10-11 16:00:27 -070011644 if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) {
11645 ProxyProperties proxy = intent.getParcelableExtra("proxy");
11646 mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy));
11647 }
11648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011649 // Add to the sticky list if requested.
11650 if (sticky) {
11651 if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,
11652 callingPid, callingUid)
11653 != PackageManager.PERMISSION_GRANTED) {
11654 String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid="
11655 + callingPid + ", uid=" + callingUid
11656 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011657 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011658 throw new SecurityException(msg);
11659 }
11660 if (requiredPermission != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011661 Slog.w(TAG, "Can't broadcast sticky intent " + intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011662 + " and enforce permission " + requiredPermission);
Dianne Hackborna4972e92012-03-14 10:38:05 -070011663 return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011664 }
11665 if (intent.getComponent() != null) {
11666 throw new SecurityException(
11667 "Sticky broadcasts can't target a specific component");
11668 }
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011669 // We use userId directly here, since the "all" target is maintained
11670 // as a separate set of sticky broadcasts.
11671 if (userId != UserHandle.USER_ALL) {
11672 // But first, if this is not a broadcast to all users, then
11673 // make sure it doesn't conflict with an existing broadcast to
11674 // all users.
11675 HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(
11676 UserHandle.USER_ALL);
11677 if (stickies != null) {
11678 ArrayList<Intent> list = stickies.get(intent.getAction());
11679 if (list != null) {
11680 int N = list.size();
11681 int i;
11682 for (i=0; i<N; i++) {
11683 if (intent.filterEquals(list.get(i))) {
11684 throw new IllegalArgumentException(
11685 "Sticky broadcast " + intent + " for user "
11686 + userId + " conflicts with existing global broadcast");
11687 }
11688 }
11689 }
11690 }
11691 }
11692 HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId);
11693 if (stickies == null) {
11694 stickies = new HashMap<String, ArrayList<Intent>>();
11695 mStickyBroadcasts.put(userId, stickies);
11696 }
11697 ArrayList<Intent> list = stickies.get(intent.getAction());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011698 if (list == null) {
11699 list = new ArrayList<Intent>();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011700 stickies.put(intent.getAction(), list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011701 }
11702 int N = list.size();
11703 int i;
11704 for (i=0; i<N; i++) {
11705 if (intent.filterEquals(list.get(i))) {
11706 // This sticky already exists, replace it.
11707 list.set(i, new Intent(intent));
11708 break;
11709 }
11710 }
11711 if (i >= N) {
11712 list.add(new Intent(intent));
11713 }
11714 }
11715
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011716 int[] users;
11717 if (userId == UserHandle.USER_ALL) {
11718 // Caller wants broadcast to go to all started users.
Dianne Hackbornc72fc672012-09-20 13:12:03 -070011719 users = mStartedUserArray;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011720 } else {
11721 // Caller wants broadcast to go to one specific user.
Dianne Hackbornc72fc672012-09-20 13:12:03 -070011722 users = mCurrentUserArray;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011723 }
11724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011725 // Figure out who all will receive this broadcast.
11726 List receivers = null;
11727 List<BroadcastFilter> registeredReceivers = null;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011728 // Need to resolve the intent to interested receivers...
11729 if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)
11730 == 0) {
11731 receivers = collectReceiverComponents(intent, resolvedType, users);
11732 }
11733 if (intent.getComponent() == null) {
11734 registeredReceivers = mReceiverResolver.queryIntent(intent,
11735 resolvedType, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011736 }
11737
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011738 final boolean replacePending =
11739 (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;
11740
Joe Onorato8a9b2202010-02-26 18:56:32 -080011741 if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction()
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011742 + " replacePending=" + replacePending);
11743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011744 int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
11745 if (!ordered && NR > 0) {
11746 // If we are not serializing this broadcast, then send the
11747 // registered receivers separately so they don't wait for the
11748 // components to be launched.
Christopher Tatef46723b2012-01-26 14:19:24 -080011749 final BroadcastQueue queue = broadcastQueueForIntent(intent);
11750 BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011751 callerPackage, callingPid, callingUid, requiredPermission,
11752 registeredReceivers, resultTo, resultCode, resultData, map,
Amith Yamasani8bf06ed2012-08-27 19:30:30 -070011753 ordered, sticky, false, userId);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011754 if (DEBUG_BROADCAST) Slog.v(
Christopher Tatef46723b2012-01-26 14:19:24 -080011755 TAG, "Enqueueing parallel broadcast " + r);
11756 final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011757 if (!replaced) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011758 queue.enqueueParallelBroadcastLocked(r);
11759 queue.scheduleBroadcastsLocked();
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011761 registeredReceivers = null;
11762 NR = 0;
11763 }
11764
11765 // Merge into one list.
11766 int ir = 0;
11767 if (receivers != null) {
11768 // A special case for PACKAGE_ADDED: do not allow the package
11769 // being added to see this broadcast. This prevents them from
11770 // using this as a back door to get run as soon as they are
11771 // installed. Maybe in the future we want to have a special install
11772 // broadcast or such for apps, but we'd like to deliberately make
11773 // this decision.
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011774 String skipPackages[] = null;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011775 if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())
11776 || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())
11777 || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011778 Uri data = intent.getData();
11779 if (data != null) {
11780 String pkgName = data.getSchemeSpecificPart();
11781 if (pkgName != null) {
11782 skipPackages = new String[] { pkgName };
11783 }
11784 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011785 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011786 skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
The Android Open Source Project10592532009-03-18 17:39:46 -070011787 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011788 if (skipPackages != null && (skipPackages.length > 0)) {
11789 for (String skipPackage : skipPackages) {
11790 if (skipPackage != null) {
11791 int NT = receivers.size();
11792 for (int it=0; it<NT; it++) {
11793 ResolveInfo curt = (ResolveInfo)receivers.get(it);
11794 if (curt.activityInfo.packageName.equals(skipPackage)) {
11795 receivers.remove(it);
11796 it--;
11797 NT--;
11798 }
11799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011800 }
11801 }
11802 }
11803
11804 int NT = receivers != null ? receivers.size() : 0;
11805 int it = 0;
11806 ResolveInfo curt = null;
11807 BroadcastFilter curr = null;
11808 while (it < NT && ir < NR) {
11809 if (curt == null) {
11810 curt = (ResolveInfo)receivers.get(it);
11811 }
11812 if (curr == null) {
11813 curr = registeredReceivers.get(ir);
11814 }
11815 if (curr.getPriority() >= curt.priority) {
11816 // Insert this broadcast record into the final list.
11817 receivers.add(it, curr);
11818 ir++;
11819 curr = null;
11820 it++;
11821 NT++;
11822 } else {
11823 // Skip to the next ResolveInfo in the final list.
11824 it++;
11825 curt = null;
11826 }
11827 }
11828 }
11829 while (ir < NR) {
11830 if (receivers == null) {
11831 receivers = new ArrayList();
11832 }
11833 receivers.add(registeredReceivers.get(ir));
11834 ir++;
11835 }
11836
11837 if ((receivers != null && receivers.size() > 0)
11838 || resultTo != null) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011839 BroadcastQueue queue = broadcastQueueForIntent(intent);
11840 BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011841 callerPackage, callingPid, callingUid, requiredPermission,
Dianne Hackborn12527f92009-11-11 17:39:50 -080011842 receivers, resultTo, resultCode, resultData, map, ordered,
Amith Yamasani8bf06ed2012-08-27 19:30:30 -070011843 sticky, false, userId);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011844 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011845 TAG, "Enqueueing ordered broadcast " + r
Christopher Tatef46723b2012-01-26 14:19:24 -080011846 + ": prev had " + queue.mOrderedBroadcasts.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011847 if (DEBUG_BROADCAST) {
11848 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011849 Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850 }
Christopher Tatef46723b2012-01-26 14:19:24 -080011851 boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011852 if (!replaced) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011853 queue.enqueueOrderedBroadcastLocked(r);
11854 queue.scheduleBroadcastsLocked();
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 }
11857
Dianne Hackborna4972e92012-03-14 10:38:05 -070011858 return ActivityManager.BROADCAST_SUCCESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 }
11860
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011861 final Intent verifyBroadcastLocked(Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011862 // Refuse possible leaked file descriptors
11863 if (intent != null && intent.hasFileDescriptors() == true) {
11864 throw new IllegalArgumentException("File descriptors passed in Intent");
11865 }
11866
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011867 int flags = intent.getFlags();
11868
11869 if (!mProcessesReady) {
11870 // if the caller really truly claims to know what they're doing, go
11871 // ahead and allow the broadcast without launching any receivers
11872 if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) {
11873 intent = new Intent(intent);
11874 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
11875 } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
11876 Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent
11877 + " before boot completion");
11878 throw new IllegalStateException("Cannot broadcast before boot completed");
11879 }
11880 }
11881
11882 if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
11883 throw new IllegalArgumentException(
11884 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
11885 }
11886
11887 return intent;
11888 }
11889
11890 public final int broadcastIntent(IApplicationThread caller,
11891 Intent intent, String resolvedType, IIntentReceiver resultTo,
11892 int resultCode, String resultData, Bundle map,
Amith Yamasani742a6712011-05-04 14:49:28 -070011893 String requiredPermission, boolean serialized, boolean sticky, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011894 enforceNotIsolatedCaller("broadcastIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011895 synchronized(this) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011896 intent = verifyBroadcastLocked(intent);
Dianne Hackborn9acc0302009-08-25 00:27:12 -070011897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011898 final ProcessRecord callerApp = getRecordForAppLocked(caller);
11899 final int callingPid = Binder.getCallingPid();
11900 final int callingUid = Binder.getCallingUid();
11901 final long origId = Binder.clearCallingIdentity();
11902 int res = broadcastIntentLocked(callerApp,
11903 callerApp != null ? callerApp.info.packageName : null,
11904 intent, resolvedType, resultTo,
Amith Yamasani742a6712011-05-04 14:49:28 -070011905 resultCode, resultData, map, requiredPermission, serialized, sticky,
11906 callingPid, callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011907 Binder.restoreCallingIdentity(origId);
11908 return res;
11909 }
11910 }
11911
11912 int broadcastIntentInPackage(String packageName, int uid,
11913 Intent intent, String resolvedType, IIntentReceiver resultTo,
11914 int resultCode, String resultData, Bundle map,
Amith Yamasani742a6712011-05-04 14:49:28 -070011915 String requiredPermission, boolean serialized, boolean sticky, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011916 synchronized(this) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011917 intent = verifyBroadcastLocked(intent);
11918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011919 final long origId = Binder.clearCallingIdentity();
11920 int res = broadcastIntentLocked(null, packageName, intent, resolvedType,
11921 resultTo, resultCode, resultData, map, requiredPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -070011922 serialized, sticky, -1, uid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011923 Binder.restoreCallingIdentity(origId);
11924 return res;
11925 }
11926 }
11927
Amith Yamasani742a6712011-05-04 14:49:28 -070011928 public final void unbroadcastIntent(IApplicationThread caller, Intent intent, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011929 // Refuse possible leaked file descriptors
11930 if (intent != null && intent.hasFileDescriptors() == true) {
11931 throw new IllegalArgumentException("File descriptors passed in Intent");
11932 }
11933
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011934 userId = handleIncomingUserLocked(Binder.getCallingPid(),
11935 Binder.getCallingUid(), userId, true, false, "removeStickyBroadcast", null);
11936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 synchronized(this) {
11938 if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY)
11939 != PackageManager.PERMISSION_GRANTED) {
11940 String msg = "Permission Denial: unbroadcastIntent() from pid="
11941 + Binder.getCallingPid()
11942 + ", uid=" + Binder.getCallingUid()
11943 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011944 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011945 throw new SecurityException(msg);
11946 }
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011947 HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId);
11948 if (stickies != null) {
11949 ArrayList<Intent> list = stickies.get(intent.getAction());
11950 if (list != null) {
11951 int N = list.size();
11952 int i;
11953 for (i=0; i<N; i++) {
11954 if (intent.filterEquals(list.get(i))) {
11955 list.remove(i);
11956 break;
11957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011958 }
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070011959 if (list.size() <= 0) {
11960 stickies.remove(intent.getAction());
11961 }
11962 }
11963 if (stickies.size() <= 0) {
11964 mStickyBroadcasts.remove(userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011965 }
11966 }
11967 }
11968 }
11969
11970 private final boolean finishReceiverLocked(IBinder receiver, int resultCode,
11971 String resultData, Bundle resultExtras, boolean resultAbort,
11972 boolean explicit) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011973 final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver);
11974 if (r == null) {
11975 Slog.w(TAG, "finishReceiver called but not found on queue");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011976 return false;
11977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011978
Christopher Tatef46723b2012-01-26 14:19:24 -080011979 return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort,
11980 explicit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011981 }
11982
11983 public void finishReceiver(IBinder who, int resultCode, String resultData,
11984 Bundle resultExtras, boolean resultAbort) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011985 if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011986
11987 // Refuse possible leaked file descriptors
11988 if (resultExtras != null && resultExtras.hasFileDescriptors()) {
11989 throw new IllegalArgumentException("File descriptors passed in Bundle");
11990 }
11991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011992 final long origId = Binder.clearCallingIdentity();
Christopher Tatef46723b2012-01-26 14:19:24 -080011993 try {
11994 boolean doNext = false;
11995 BroadcastRecord r = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996
Christopher Tatef46723b2012-01-26 14:19:24 -080011997 synchronized(this) {
11998 r = broadcastRecordForReceiverLocked(who);
11999 if (r != null) {
12000 doNext = r.queue.finishReceiverLocked(r, resultCode,
12001 resultData, resultExtras, resultAbort, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012003 }
Jeff Brown4d94a762010-09-23 11:33:28 -070012004
Christopher Tatef46723b2012-01-26 14:19:24 -080012005 if (doNext) {
12006 r.queue.processNextBroadcast(false);
12007 }
12008 trimApplications();
12009 } finally {
12010 Binder.restoreCallingIdentity(origId);
Dianne Hackbornad5499d2010-03-29 18:08:45 -070012011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014 // =========================================================
12015 // INSTRUMENTATION
12016 // =========================================================
12017
12018 public boolean startInstrumentation(ComponentName className,
12019 String profileFile, int flags, Bundle arguments,
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070012020 IInstrumentationWatcher watcher, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080012021 enforceNotIsolatedCaller("startInstrumentation");
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070012022 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(),
12023 userId, false, true, "startInstrumentation", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 // Refuse possible leaked file descriptors
12025 if (arguments != null && arguments.hasFileDescriptors()) {
12026 throw new IllegalArgumentException("File descriptors passed in Bundle");
12027 }
12028
12029 synchronized(this) {
12030 InstrumentationInfo ii = null;
12031 ApplicationInfo ai = null;
12032 try {
12033 ii = mContext.getPackageManager().getInstrumentationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070012034 className, STOCK_PM_FLAGS);
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070012035 ai = AppGlobals.getPackageManager().getApplicationInfo(
12036 ii.targetPackage, STOCK_PM_FLAGS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012037 } catch (PackageManager.NameNotFoundException e) {
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070012038 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012039 }
12040 if (ii == null) {
12041 reportStartInstrumentationFailure(watcher, className,
12042 "Unable to find instrumentation info for: " + className);
12043 return false;
12044 }
12045 if (ai == null) {
12046 reportStartInstrumentationFailure(watcher, className,
12047 "Unable to find instrumentation target package: " + ii.targetPackage);
12048 return false;
12049 }
12050
12051 int match = mContext.getPackageManager().checkSignatures(
12052 ii.targetPackage, ii.packageName);
12053 if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) {
12054 String msg = "Permission Denial: starting instrumentation "
12055 + className + " from pid="
12056 + Binder.getCallingPid()
12057 + ", uid=" + Binder.getCallingPid()
12058 + " not allowed because package " + ii.packageName
12059 + " does not have a signature matching the target "
12060 + ii.targetPackage;
12061 reportStartInstrumentationFailure(watcher, className, msg);
12062 throw new SecurityException(msg);
12063 }
12064
12065 final long origId = Binder.clearCallingIdentity();
Christopher Tate3dacd842011-08-19 14:56:15 -070012066 // Instrumentation can kill and relaunch even persistent processes
Amith Yamasani483f3b02012-03-13 16:08:00 -070012067 forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId);
Dianne Hackborna0c283e2012-02-09 10:47:01 -080012068 ProcessRecord app = addAppLocked(ai, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012069 app.instrumentationClass = className;
Dianne Hackborn1655be42009-05-08 14:29:01 -070012070 app.instrumentationInfo = ai;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012071 app.instrumentationProfileFile = profileFile;
12072 app.instrumentationArguments = arguments;
12073 app.instrumentationWatcher = watcher;
12074 app.instrumentationResultClass = className;
12075 Binder.restoreCallingIdentity(origId);
12076 }
12077
12078 return true;
12079 }
12080
12081 /**
12082 * Report errors that occur while attempting to start Instrumentation. Always writes the
12083 * error to the logs, but if somebody is watching, send the report there too. This enables
12084 * the "am" command to report errors with more information.
12085 *
12086 * @param watcher The IInstrumentationWatcher. Null if there isn't one.
12087 * @param cn The component name of the instrumentation.
12088 * @param report The error report.
12089 */
12090 private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher,
12091 ComponentName cn, String report) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012092 Slog.w(TAG, report);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012093 try {
12094 if (watcher != null) {
12095 Bundle results = new Bundle();
12096 results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService");
12097 results.putString("Error", report);
12098 watcher.instrumentationStatus(cn, -1, results);
12099 }
12100 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012101 Slog.w(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012102 }
12103 }
12104
12105 void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) {
12106 if (app.instrumentationWatcher != null) {
12107 try {
12108 // NOTE: IInstrumentationWatcher *must* be oneway here
12109 app.instrumentationWatcher.instrumentationFinished(
12110 app.instrumentationClass,
12111 resultCode,
12112 results);
12113 } catch (RemoteException e) {
12114 }
12115 }
12116 app.instrumentationWatcher = null;
12117 app.instrumentationClass = null;
Dianne Hackborn1655be42009-05-08 14:29:01 -070012118 app.instrumentationInfo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012119 app.instrumentationProfileFile = null;
12120 app.instrumentationArguments = null;
12121
Dianne Hackborn80a4af22012-08-27 19:18:31 -070012122 forceStopPackageLocked(app.info.packageName, -1, false, false, true, true, app.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 }
12124
12125 public void finishInstrumentation(IApplicationThread target,
12126 int resultCode, Bundle results) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070012127 int userId = UserHandle.getCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012128 // Refuse possible leaked file descriptors
12129 if (results != null && results.hasFileDescriptors()) {
12130 throw new IllegalArgumentException("File descriptors passed in Intent");
12131 }
12132
12133 synchronized(this) {
12134 ProcessRecord app = getRecordForAppLocked(target);
12135 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012136 Slog.w(TAG, "finishInstrumentation: no app for " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012137 return;
12138 }
12139 final long origId = Binder.clearCallingIdentity();
12140 finishInstrumentationLocked(app, resultCode, results);
12141 Binder.restoreCallingIdentity(origId);
12142 }
12143 }
12144
12145 // =========================================================
12146 // CONFIGURATION
12147 // =========================================================
12148
12149 public ConfigurationInfo getDeviceConfigurationInfo() {
12150 ConfigurationInfo config = new ConfigurationInfo();
12151 synchronized (this) {
12152 config.reqTouchScreen = mConfiguration.touchscreen;
12153 config.reqKeyboardType = mConfiguration.keyboard;
12154 config.reqNavigation = mConfiguration.navigation;
Dianne Hackbornfae76f52009-07-16 13:41:23 -070012155 if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD
12156 || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012157 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
12158 }
Dianne Hackbornfae76f52009-07-16 13:41:23 -070012159 if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED
12160 && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012161 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
12162 }
Jack Palevichb90d28c2009-07-22 15:35:24 -070012163 config.reqGlEsVersion = GL_ES_VERSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012164 }
12165 return config;
12166 }
12167
12168 public Configuration getConfiguration() {
12169 Configuration ci;
12170 synchronized(this) {
12171 ci = new Configuration(mConfiguration);
12172 }
12173 return ci;
12174 }
12175
Dianne Hackborn31ca8542011-07-19 14:58:28 -070012176 public void updatePersistentConfiguration(Configuration values) {
12177 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
12178 "updateConfiguration()");
12179 enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS,
12180 "updateConfiguration()");
12181 if (values == null) {
12182 throw new NullPointerException("Configuration must not be null");
12183 }
12184
12185 synchronized(this) {
12186 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn813075a62011-11-14 17:45:19 -080012187 updateConfigurationLocked(values, null, true, false);
Dianne Hackborn31ca8542011-07-19 14:58:28 -070012188 Binder.restoreCallingIdentity(origId);
12189 }
12190 }
12191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012192 public void updateConfiguration(Configuration values) {
12193 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
12194 "updateConfiguration()");
12195
12196 synchronized(this) {
12197 if (values == null && mWindowManager != null) {
12198 // sentinel: fetch the current configuration from the window manager
12199 values = mWindowManager.computeNewConfiguration();
12200 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070012201
12202 if (mWindowManager != null) {
12203 mProcessList.applyDisplaySize(mWindowManager);
12204 }
12205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012206 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn31ca8542011-07-19 14:58:28 -070012207 if (values != null) {
12208 Settings.System.clearConfiguration(values);
12209 }
Dianne Hackborn813075a62011-11-14 17:45:19 -080012210 updateConfigurationLocked(values, null, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012211 Binder.restoreCallingIdentity(origId);
12212 }
12213 }
12214
12215 /**
12216 * Do either or both things: (1) change the current configuration, and (2)
12217 * make sure the given activity is running with the (now) current
12218 * configuration. Returns true if the activity has been left running, or
12219 * false if <var>starting</var> is being destroyed to match the new
12220 * configuration.
Dianne Hackborn31ca8542011-07-19 14:58:28 -070012221 * @param persistent TODO
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012222 */
Dianne Hackborna573f6a2012-02-09 16:12:18 -080012223 boolean updateConfigurationLocked(Configuration values,
Dianne Hackborn813075a62011-11-14 17:45:19 -080012224 ActivityRecord starting, boolean persistent, boolean initLocale) {
Mike Lockwood3a74bd32011-08-12 13:55:22 -070012225 // do nothing if we are headless
12226 if (mHeadless) return true;
12227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 int changes = 0;
12229
12230 boolean kept = true;
12231
12232 if (values != null) {
12233 Configuration newConfig = new Configuration(mConfiguration);
12234 changes = newConfig.updateFrom(values);
12235 if (changes != 0) {
Dianne Hackborndc6b6352009-09-30 14:20:09 -070012236 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012237 Slog.i(TAG, "Updating configuration to: " + values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012238 }
12239
Doug Zongker2bec3d42009-12-04 12:52:44 -080012240 EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012241
Dianne Hackborn813075a62011-11-14 17:45:19 -080012242 if (values.locale != null && !initLocale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012243 saveLocaleLocked(values.locale,
12244 !values.locale.equals(mConfiguration.locale),
12245 values.userSetLocale);
12246 }
12247
Dianne Hackborne36d6e22010-02-17 19:46:25 -080012248 mConfigurationSeq++;
12249 if (mConfigurationSeq <= 0) {
12250 mConfigurationSeq = 1;
12251 }
12252 newConfig.seq = mConfigurationSeq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012253 mConfiguration = newConfig;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012254 Slog.i(TAG, "Config changed: " + newConfig);
Dianne Hackborn813075a62011-11-14 17:45:19 -080012255
12256 final Configuration configCopy = new Configuration(mConfiguration);
Joe Onorato54a4a412011-11-02 20:50:08 -070012257
12258 // TODO: If our config changes, should we auto dismiss any currently
12259 // showing dialogs?
12260 mShowDialogs = shouldShowDialogs(newConfig);
Dianne Hackborn813075a62011-11-14 17:45:19 -080012261
Dianne Hackborn826d17c2009-11-12 12:55:51 -080012262 AttributeCache ac = AttributeCache.instance();
12263 if (ac != null) {
Dianne Hackborn813075a62011-11-14 17:45:19 -080012264 ac.updateConfiguration(configCopy);
Dianne Hackborn826d17c2009-11-12 12:55:51 -080012265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012266
Dianne Hackborn2f0b1752011-05-31 17:59:49 -070012267 // Make sure all resources in our process are updated
12268 // right now, so that anyone who is going to retrieve
12269 // resource values after we return will be sure to get
12270 // the new ones. This is especially important during
12271 // boot, where the first config change needs to guarantee
12272 // all resources have that config before following boot
12273 // code is executed.
Dianne Hackborn813075a62011-11-14 17:45:19 -080012274 mSystemThread.applyConfigurationToResources(configCopy);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -070012275
Dianne Hackborn31ca8542011-07-19 14:58:28 -070012276 if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080012277 Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG);
Dianne Hackborn813075a62011-11-14 17:45:19 -080012278 msg.obj = new Configuration(configCopy);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080012279 mHandler.sendMessage(msg);
12280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012281
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012282 for (int i=mLruProcesses.size()-1; i>=0; i--) {
12283 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012284 try {
12285 if (app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012286 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -070012287 + app.processName + " new config " + mConfiguration);
Dianne Hackborn813075a62011-11-14 17:45:19 -080012288 app.thread.scheduleConfigurationChanged(configCopy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012289 }
12290 } catch (Exception e) {
12291 }
12292 }
12293 Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080012294 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
12295 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012296 broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070012297 null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080012298 if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
12299 broadcastIntentLocked(null, null,
12300 new Intent(Intent.ACTION_LOCALE_CHANGED),
12301 null, null, 0, null, null,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070012302 null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080012303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012304 }
12305 }
12306
12307 if (changes != 0 && starting == null) {
12308 // If the configuration changed, and the caller is not already
12309 // in the process of starting an activity, then find the top
12310 // activity to check if its configuration needs to change.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012311 starting = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012312 }
12313
12314 if (starting != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012315 kept = mMainStack.ensureActivityConfigurationLocked(starting, changes);
Dianne Hackborn5f4d6432010-12-21 20:40:11 -080012316 // And we need to make sure at this point that all other activities
12317 // are made visible with the correct configuration.
12318 mMainStack.ensureActivitiesVisibleLocked(starting, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012319 }
12320
Dianne Hackborne36d6e22010-02-17 19:46:25 -080012321 if (values != null && mWindowManager != null) {
12322 mWindowManager.setNewConfiguration(mConfiguration);
12323 }
12324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012325 return kept;
12326 }
Joe Onorato54a4a412011-11-02 20:50:08 -070012327
12328 /**
12329 * Decide based on the configuration whether we should shouw the ANR,
12330 * crash, etc dialogs. The idea is that if there is no affordnace to
12331 * press the on-screen buttons, we shouldn't show the dialog.
12332 *
12333 * A thought: SystemUI might also want to get told about this, the Power
12334 * dialog / global actions also might want different behaviors.
12335 */
12336 private static final boolean shouldShowDialogs(Configuration config) {
12337 return !(config.keyboard == Configuration.KEYBOARD_NOKEYS
12338 && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH);
12339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012340
12341 /**
12342 * Save the locale. You must be inside a synchronized (this) block.
12343 */
12344 private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) {
12345 if(isDiff) {
12346 SystemProperties.set("user.language", l.getLanguage());
12347 SystemProperties.set("user.region", l.getCountry());
12348 }
12349
12350 if(isPersist) {
12351 SystemProperties.set("persist.sys.language", l.getLanguage());
12352 SystemProperties.set("persist.sys.country", l.getCountry());
12353 SystemProperties.set("persist.sys.localevar", l.getVariant());
12354 }
12355 }
12356
Adam Powelldd8fab22012-03-22 17:47:27 -070012357 @Override
12358 public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) {
12359 ActivityRecord srec = ActivityRecord.forToken(token);
Adam Powellb71a5bc2012-04-24 14:20:57 -070012360 return srec != null && srec.task.affinity != null &&
12361 srec.task.affinity.equals(destAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -070012362 }
12363
12364 public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode,
12365 Intent resultData) {
12366 ComponentName dest = destIntent.getComponent();
12367
12368 synchronized (this) {
12369 ActivityRecord srec = ActivityRecord.forToken(token);
Adam Powellb71a5bc2012-04-24 14:20:57 -070012370 if (srec == null) {
12371 return false;
12372 }
Adam Powelldd8fab22012-03-22 17:47:27 -070012373 ArrayList<ActivityRecord> history = srec.stack.mHistory;
12374 final int start = history.indexOf(srec);
12375 if (start < 0) {
12376 // Current activity is not in history stack; do nothing.
12377 return false;
12378 }
12379 int finishTo = start - 1;
12380 ActivityRecord parent = null;
12381 boolean foundParentInTask = false;
12382 if (dest != null) {
12383 TaskRecord tr = srec.task;
12384 for (int i = start - 1; i >= 0; i--) {
12385 ActivityRecord r = history.get(i);
12386 if (tr != r.task) {
12387 // Couldn't find parent in the same task; stop at the one above this.
12388 // (Root of current task; in-app "home" behavior)
12389 // Always at least finish the current activity.
12390 finishTo = Math.min(start - 1, i + 1);
12391 parent = history.get(finishTo);
12392 break;
12393 } else if (r.info.packageName.equals(dest.getPackageName()) &&
12394 r.info.name.equals(dest.getClassName())) {
12395 finishTo = i;
12396 parent = r;
12397 foundParentInTask = true;
12398 break;
12399 }
12400 }
12401 }
12402
12403 if (mController != null) {
12404 ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
12405 if (next != null) {
12406 // ask watcher if this is allowed
12407 boolean resumeOK = true;
12408 try {
12409 resumeOK = mController.activityResuming(next.packageName);
12410 } catch (RemoteException e) {
12411 mController = null;
12412 }
12413
12414 if (!resumeOK) {
12415 return false;
12416 }
12417 }
12418 }
12419 final long origId = Binder.clearCallingIdentity();
12420 for (int i = start; i > finishTo; i--) {
12421 ActivityRecord r = history.get(i);
12422 mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -070012423 "navigate-up", true);
Adam Powelldd8fab22012-03-22 17:47:27 -070012424 // Only return the supplied result for the first activity finished
12425 resultCode = Activity.RESULT_CANCELED;
12426 resultData = null;
12427 }
12428
12429 if (parent != null && foundParentInTask) {
12430 final int parentLaunchMode = parent.info.launchMode;
12431 final int destIntentFlags = destIntent.getFlags();
12432 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
12433 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
12434 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
12435 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Adam Powell69de7e12012-05-07 18:42:24 -070012436 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
Adam Powelldd8fab22012-03-22 17:47:27 -070012437 } else {
12438 try {
12439 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
Amith Yamasaniea7e9152012-09-24 16:11:18 -070012440 destIntent.getComponent(), 0, srec.userId);
Adam Powelldd8fab22012-03-22 17:47:27 -070012441 int res = mMainStack.startActivityLocked(srec.app.thread, destIntent,
12442 null, aInfo, parent.appToken, null,
12443 0, -1, parent.launchedFromUid, 0, null, true, null);
12444 foundParentInTask = res == ActivityManager.START_SUCCESS;
12445 } catch (RemoteException e) {
12446 foundParentInTask = false;
12447 }
12448 mMainStack.requestFinishActivityLocked(parent.appToken, resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -070012449 resultData, "navigate-up", true);
Adam Powelldd8fab22012-03-22 17:47:27 -070012450 }
12451 }
12452 Binder.restoreCallingIdentity(origId);
12453 return foundParentInTask;
12454 }
12455 }
12456
Dianne Hackborn5320eb82012-05-18 12:05:04 -070012457 public int getLaunchedFromUid(IBinder activityToken) {
12458 ActivityRecord srec = ActivityRecord.forToken(activityToken);
12459 if (srec == null) {
12460 return -1;
12461 }
12462 return srec.launchedFromUid;
12463 }
12464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012465 // =========================================================
12466 // LIFETIME MANAGEMENT
12467 // =========================================================
12468
Christopher Tatef46723b2012-01-26 14:19:24 -080012469 // Returns which broadcast queue the app is the current [or imminent] receiver
12470 // on, or 'null' if the app is not an active broadcast recipient.
12471 private BroadcastQueue isReceivingBroadcast(ProcessRecord app) {
12472 BroadcastRecord r = app.curReceiver;
12473 if (r != null) {
12474 return r.queue;
12475 }
12476
12477 // It's not the current receiver, but it might be starting up to become one
12478 synchronized (this) {
12479 for (BroadcastQueue queue : mBroadcastQueues) {
12480 r = queue.mPendingBroadcast;
12481 if (r != null && r.curApp == app) {
12482 // found it; report which queue it's in
12483 return queue;
12484 }
12485 }
12486 }
12487
12488 return null;
12489 }
12490
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012491 private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012492 int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012493 if (mAdjSeq == app.adjSeq) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012494 // This adjustment has already been computed. If we are calling
12495 // from the top, we may have already computed our adjustment with
12496 // an earlier hidden adjustment that isn't really for us... if
12497 // so, use the new hidden adjustment.
12498 if (!recursed && app.hidden) {
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012499 app.curAdj = app.curRawAdj = app.nonStoppingAdj =
12500 app.hasActivities ? hiddenAdj : emptyAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012501 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012502 return app.curRawAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012503 }
12504
12505 if (app.thread == null) {
12506 app.adjSeq = mAdjSeq;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012507 app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012508 return (app.curAdj=app.curRawAdj=ProcessList.HIDDEN_APP_MAX_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012509 }
12510
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012511 app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN;
12512 app.adjSource = null;
12513 app.adjTarget = null;
12514 app.empty = false;
12515 app.hidden = false;
12516
12517 final int activitiesSize = app.activities.size();
12518
Dianne Hackborn7d608422011-08-07 16:24:18 -070012519 if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012520 // The max adjustment doesn't allow this app to be anything
12521 // below foreground, so it is not worth doing work for it.
12522 app.adjType = "fixed";
12523 app.adjSeq = mAdjSeq;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012524 app.curRawAdj = app.nonStoppingAdj = app.maxAdj;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012525 app.hasActivities = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012526 app.foregroundActivities = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012527 app.keeping = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012528 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012529 // System process can do UI, and when they do we want to have
12530 // them trim their memory after the user leaves the UI. To
12531 // facilitate this, here we need to determine whether or not it
12532 // is currently showing UI.
12533 app.systemNoUi = true;
12534 if (app == TOP_APP) {
12535 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012536 app.hasActivities = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012537 } else if (activitiesSize > 0) {
12538 for (int j = 0; j < activitiesSize; j++) {
12539 final ActivityRecord r = app.activities.get(j);
12540 if (r.visible) {
12541 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012542 }
12543 if (r.app == app) {
12544 app.hasActivities = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012545 }
12546 }
12547 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012548 return (app.curAdj=app.maxAdj);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012549 }
12550
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012551 app.keeping = false;
12552 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012553 app.hasActivities = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012554
The Android Open Source Project4df24232009-03-05 14:34:35 -080012555 // Determine the importance of the process, starting with most
12556 // important to least, and assign an appropriate OOM adjustment.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012557 int adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012558 int schedGroup;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012559 boolean foregroundActivities = false;
12560 boolean interesting = false;
Christopher Tatef46723b2012-01-26 14:19:24 -080012561 BroadcastQueue queue;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012562 if (app == TOP_APP) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012563 // The last app on the list is the foreground app.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012564 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012565 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012566 app.adjType = "top-activity";
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012567 foregroundActivities = true;
12568 interesting = true;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012569 app.hasActivities = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012570 } else if (app.instrumentationClass != null) {
12571 // Don't want to kill running instrumentation.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012572 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012573 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012574 app.adjType = "instrumentation";
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012575 interesting = true;
Christopher Tatef46723b2012-01-26 14:19:24 -080012576 } else if ((queue = isReceivingBroadcast(app)) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012577 // An app that is currently receiving a broadcast also
Christopher Tatef46723b2012-01-26 14:19:24 -080012578 // counts as being in the foreground for OOM killer purposes.
12579 // It's placed in a sched group based on the nature of the
12580 // broadcast as reflected by which queue it's active in.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012581 adj = ProcessList.FOREGROUND_APP_ADJ;
Christopher Tatef46723b2012-01-26 14:19:24 -080012582 schedGroup = (queue == mFgBroadcastQueue)
12583 ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012584 app.adjType = "broadcast";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012585 } else if (app.executingServices.size() > 0) {
12586 // An app that is currently executing a service callback also
12587 // counts as being in the foreground.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012588 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012589 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012590 app.adjType = "exec-service";
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012591 } else {
12592 // Assume process is hidden (has activities); we will correct
12593 // later if this is not the case.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012594 adj = hiddenAdj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012595 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012596 app.hidden = true;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012597 app.adjType = "bg-activities";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012598 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012599
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012600 boolean hasStoppingActivities = false;
12601
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012602 // Examine all activities if not already foreground.
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012603 if (!foregroundActivities && activitiesSize > 0) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012604 for (int j = 0; j < activitiesSize; j++) {
12605 final ActivityRecord r = app.activities.get(j);
12606 if (r.visible) {
12607 // App has a visible activity; only upgrade adjustment.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012608 if (adj > ProcessList.VISIBLE_APP_ADJ) {
12609 adj = ProcessList.VISIBLE_APP_ADJ;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012610 app.adjType = "visible";
12611 }
12612 schedGroup = Process.THREAD_GROUP_DEFAULT;
12613 app.hidden = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012614 app.hasActivities = true;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012615 foregroundActivities = true;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012616 break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012617 } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012618 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12619 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012620 app.adjType = "pausing";
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012621 }
Dianne Hackborn8bf0aa92011-11-29 13:54:43 -080012622 app.hidden = false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012623 foregroundActivities = true;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012624 } else if (r.state == ActivityState.STOPPING) {
12625 // We will apply the actual adjustment later, because
12626 // we want to allow this process to immediately go through
12627 // any memory trimming that is in effect.
12628 app.hidden = false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012629 foregroundActivities = true;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012630 hasStoppingActivities = true;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012631 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012632 if (r.app == app) {
12633 app.hasActivities = true;
12634 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012635 }
12636 }
12637
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012638 if (adj == hiddenAdj && !app.hasActivities) {
12639 // Whoops, this process is completely empty as far as we know
12640 // at this point.
12641 adj = emptyAdj;
12642 app.empty = true;
12643 app.adjType = "bg-empty";
12644 }
12645
Dianne Hackborn7d608422011-08-07 16:24:18 -070012646 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012647 if (app.foregroundServices) {
12648 // The user is aware of this app, so make it visible.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012649 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012650 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012651 app.adjType = "foreground-service";
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012652 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012653 } else if (app.forcingToForeground != null) {
12654 // The user is aware of this app, so make it visible.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012655 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012656 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012657 app.adjType = "force-foreground";
12658 app.adjSource = app.forcingToForeground;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012659 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012660 }
12661 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012662
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012663 if (app.foregroundServices) {
12664 interesting = true;
12665 }
12666
Dianne Hackborn7d608422011-08-07 16:24:18 -070012667 if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012668 // We don't want to kill the current heavy-weight process.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012669 adj = ProcessList.HEAVY_WEIGHT_APP_ADJ;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012670 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012671 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012672 app.adjType = "heavy";
12673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012674
Dianne Hackborn7d608422011-08-07 16:24:18 -070012675 if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012676 // This process is hosting what we currently consider to be the
12677 // home app, so we don't want to let it go into the background.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012678 adj = ProcessList.HOME_APP_ADJ;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012679 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012680 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012681 app.adjType = "home";
12682 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012683
Dianne Hackbornf35fe232011-11-01 19:25:20 -070012684 if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess
12685 && app.activities.size() > 0) {
12686 // This was the previous process that showed UI to the user.
12687 // We want to try to keep it around more aggressively, to give
12688 // a good experience around switching between two apps.
12689 adj = ProcessList.PREVIOUS_APP_ADJ;
12690 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
12691 app.hidden = false;
12692 app.adjType = "previous";
12693 }
12694
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012695 if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj
12696 + " reason=" + app.adjType);
12697
The Android Open Source Project4df24232009-03-05 14:34:35 -080012698 // By default, we use the computed adjustment. It may be changed if
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012699 // there are applications dependent on our services or providers, but
12700 // this gives us a baseline and makes sure we don't get into an
12701 // infinite recursion.
12702 app.adjSeq = mAdjSeq;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012703 app.curRawAdj = app.nonStoppingAdj = adj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012704
Christopher Tate6fa95972009-06-05 18:43:55 -070012705 if (mBackupTarget != null && app == mBackupTarget.app) {
12706 // If possible we want to avoid killing apps while they're being backed up
Dianne Hackborn7d608422011-08-07 16:24:18 -070012707 if (adj > ProcessList.BACKUP_APP_ADJ) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012708 if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app);
Dianne Hackborn7d608422011-08-07 16:24:18 -070012709 adj = ProcessList.BACKUP_APP_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012710 app.adjType = "backup";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012711 app.hidden = false;
Christopher Tate6fa95972009-06-05 18:43:55 -070012712 }
12713 }
12714
Dianne Hackborn7d608422011-08-07 16:24:18 -070012715 if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012716 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012717 final long now = SystemClock.uptimeMillis();
12718 // This process is more important if the top activity is
12719 // bound to the service.
Dianne Hackborn860755f2010-06-03 18:47:52 -070012720 Iterator<ServiceRecord> jt = app.services.iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012721 while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012722 ServiceRecord s = jt.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012723 if (s.startRequested) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012724 if (app.hasShownUi && app != mHomeProcess) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012725 // If this process has shown some UI, let it immediately
12726 // go to the LRU list because it may be pretty heavy with
12727 // UI stuff. We'll tag it with a label just to help
12728 // debug and understand what is going on.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012729 if (adj > ProcessList.SERVICE_ADJ) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012730 app.adjType = "started-bg-ui-services";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012731 }
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012732 } else {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070012733 if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012734 // This service has seen some activity within
12735 // recent memory, so we will keep its process ahead
12736 // of the background processes.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012737 if (adj > ProcessList.SERVICE_ADJ) {
12738 adj = ProcessList.SERVICE_ADJ;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012739 app.adjType = "started-services";
12740 app.hidden = false;
12741 }
12742 }
12743 // If we have let the service slide into the background
12744 // state, still have some text describing what it is doing
12745 // even though the service no longer has an impact.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012746 if (adj > ProcessList.SERVICE_ADJ) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012747 app.adjType = "started-bg-services";
12748 }
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080012749 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012750 // Don't kill this process because it is doing work; it
12751 // has said it is doing work.
12752 app.keeping = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012753 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070012754 if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012755 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012756 Iterator<ArrayList<ConnectionRecord>> kt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012757 = s.connections.values().iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012758 while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012759 ArrayList<ConnectionRecord> clist = kt.next();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012760 for (int i=0; i<clist.size() && adj > ProcessList.FOREGROUND_APP_ADJ; i++) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012761 // XXX should compute this based on the max of
12762 // all connected clients.
12763 ConnectionRecord cr = clist.get(i);
12764 if (cr.binding.client == app) {
12765 // Binding to ourself is not interesting.
12766 continue;
12767 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012768 if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012769 ProcessRecord client = cr.binding.client;
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012770 int clientAdj = adj;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012771 int myHiddenAdj = hiddenAdj;
12772 if (myHiddenAdj > client.hiddenAdj) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012773 if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012774 myHiddenAdj = client.hiddenAdj;
12775 } else {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012776 myHiddenAdj = ProcessList.VISIBLE_APP_ADJ;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012777 }
12778 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012779 int myEmptyAdj = emptyAdj;
12780 if (myEmptyAdj > client.emptyAdj) {
12781 if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) {
12782 myEmptyAdj = client.emptyAdj;
12783 } else {
12784 myEmptyAdj = ProcessList.VISIBLE_APP_ADJ;
12785 }
12786 }
12787 clientAdj = computeOomAdjLocked(client, myHiddenAdj,
12788 myEmptyAdj, TOP_APP, true, doingAll);
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012789 String adjType = null;
12790 if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) {
12791 // Not doing bind OOM management, so treat
12792 // this guy more like a started service.
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012793 if (app.hasShownUi && app != mHomeProcess) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012794 // If this process has shown some UI, let it immediately
12795 // go to the LRU list because it may be pretty heavy with
12796 // UI stuff. We'll tag it with a label just to help
12797 // debug and understand what is going on.
12798 if (adj > clientAdj) {
12799 adjType = "bound-bg-ui-services";
12800 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012801 app.hidden = false;
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012802 clientAdj = adj;
12803 } else {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070012804 if (now >= (s.lastActivity
12805 + ActiveServices.MAX_SERVICE_INACTIVITY)) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012806 // This service has not seen activity within
12807 // recent memory, so allow it to drop to the
12808 // LRU list if there is no other reason to keep
12809 // it around. We'll also tag it with a label just
12810 // to help debug and undertand what is going on.
12811 if (adj > clientAdj) {
12812 adjType = "bound-bg-services";
12813 }
12814 clientAdj = adj;
12815 }
12816 }
12817 }
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012818 if (adj > clientAdj) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012819 // If this process has recently shown UI, and
12820 // the process that is binding to it is less
12821 // important than being visible, then we don't
12822 // care about the binding as much as we care
12823 // about letting this process get into the LRU
12824 // list to be killed and restarted if needed for
12825 // memory.
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012826 if (app.hasShownUi && app != mHomeProcess
12827 && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012828 adjType = "bound-bg-ui-services";
12829 } else {
12830 if ((cr.flags&(Context.BIND_ABOVE_CLIENT
12831 |Context.BIND_IMPORTANT)) != 0) {
12832 adj = clientAdj;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -070012833 } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0
12834 && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ
12835 && adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12836 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
12837 } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012838 adj = clientAdj;
12839 } else {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -070012840 app.pendingUiClean = true;
12841 if (adj > ProcessList.VISIBLE_APP_ADJ) {
12842 adj = ProcessList.VISIBLE_APP_ADJ;
12843 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012844 }
12845 if (!client.hidden) {
12846 app.hidden = false;
12847 }
12848 if (client.keeping) {
12849 app.keeping = true;
12850 }
12851 adjType = "service";
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012852 }
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012853 }
12854 if (adjType != null) {
12855 app.adjType = adjType;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012856 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12857 .REASON_SERVICE_IN_USE;
12858 app.adjSource = cr.binding.client;
Dianne Hackborn905577f2011-09-07 18:31:28 -070012859 app.adjSourceOom = clientAdj;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012860 app.adjTarget = s.name;
12861 }
12862 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
12863 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
12864 schedGroup = Process.THREAD_GROUP_DEFAULT;
12865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012866 }
12867 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012868 if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) {
12869 ActivityRecord a = cr.activity;
Dianne Hackborn7d608422011-08-07 16:24:18 -070012870 if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ &&
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012871 (a.visible || a.state == ActivityState.RESUMED
12872 || a.state == ActivityState.PAUSING)) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012873 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012874 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
12875 schedGroup = Process.THREAD_GROUP_DEFAULT;
12876 }
12877 app.hidden = false;
12878 app.adjType = "service";
12879 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12880 .REASON_SERVICE_IN_USE;
12881 app.adjSource = a;
Dianne Hackborn905577f2011-09-07 18:31:28 -070012882 app.adjSourceOom = adj;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012883 app.adjTarget = s.name;
12884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012886 }
12887 }
12888 }
12889 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012890
Dianne Hackborn287952c2010-09-22 22:34:31 -070012891 // Finally, if this process has active services running in it, we
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012892 // would like to avoid killing it unless it would prevent the current
12893 // application from running. By default we put the process in
12894 // with the rest of the background processes; as we scan through
12895 // its services we may bump it up from there.
12896 if (adj > hiddenAdj) {
12897 adj = hiddenAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012898 app.hidden = false;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012899 app.adjType = "bg-services";
12900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012901 }
12902
Dianne Hackborn7d608422011-08-07 16:24:18 -070012903 if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012904 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012905 Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012906 while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012907 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012908 ContentProviderRecord cpr = jt.next();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012909 for (int i = cpr.connections.size()-1;
12910 i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
12911 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE);
12912 i--) {
12913 ContentProviderConnection conn = cpr.connections.get(i);
12914 ProcessRecord client = conn.client;
12915 if (client == app) {
12916 // Being our own client is not interesting.
12917 continue;
12918 }
12919 int myHiddenAdj = hiddenAdj;
12920 if (myHiddenAdj > client.hiddenAdj) {
12921 if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) {
12922 myHiddenAdj = client.hiddenAdj;
12923 } else {
12924 myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ;
The Android Open Source Project10592532009-03-18 17:39:46 -070012925 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012926 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012927 int myEmptyAdj = emptyAdj;
12928 if (myEmptyAdj > client.emptyAdj) {
12929 if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) {
12930 myEmptyAdj = client.emptyAdj;
12931 } else {
12932 myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ;
12933 }
12934 }
12935 int clientAdj = computeOomAdjLocked(client, myHiddenAdj,
12936 myEmptyAdj, TOP_APP, true, doingAll);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012937 if (adj > clientAdj) {
12938 if (app.hasShownUi && app != mHomeProcess
12939 && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12940 app.adjType = "bg-ui-provider";
12941 } else {
12942 adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ
12943 ? clientAdj : ProcessList.FOREGROUND_APP_ADJ;
12944 app.adjType = "provider";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012945 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012946 if (!client.hidden) {
12947 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012948 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012949 if (client.keeping) {
12950 app.keeping = true;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012951 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012952 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12953 .REASON_PROVIDER_IN_USE;
12954 app.adjSource = client;
12955 app.adjSourceOom = clientAdj;
12956 app.adjTarget = cpr.name;
12957 }
12958 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
12959 schedGroup = Process.THREAD_GROUP_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012960 }
12961 }
12962 // If the provider has external (non-framework) process
12963 // dependencies, ensure that its adjustment is at least
12964 // FOREGROUND_APP_ADJ.
Svetoslav Ganov25872aa2012-02-03 19:19:09 -080012965 if (cpr.hasExternalProcessHandles()) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012966 if (adj > ProcessList.FOREGROUND_APP_ADJ) {
12967 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012968 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012969 app.hidden = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012970 app.keeping = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012971 app.adjType = "provider";
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070012972 app.adjTarget = cpr.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012973 }
12974 }
12975 }
12976 }
12977
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012978 if (adj == ProcessList.SERVICE_ADJ) {
12979 if (doingAll) {
12980 app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3);
12981 mNewNumServiceProcs++;
12982 }
12983 if (app.serviceb) {
12984 adj = ProcessList.SERVICE_B_ADJ;
12985 }
12986 } else {
12987 app.serviceb = false;
12988 }
12989
12990 app.nonStoppingAdj = adj;
12991
12992 if (hasStoppingActivities) {
12993 // Only upgrade adjustment.
12994 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12995 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
12996 app.adjType = "stopping";
12997 }
12998 }
12999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013000 app.curRawAdj = adj;
13001
Joe Onorato8a9b2202010-02-26 18:56:32 -080013002 //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013003 // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj);
13004 if (adj > app.maxAdj) {
13005 adj = app.maxAdj;
Dianne Hackborn7d608422011-08-07 16:24:18 -070013006 if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013007 schedGroup = Process.THREAD_GROUP_DEFAULT;
13008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013009 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070013010 if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070013011 app.keeping = true;
13012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013013
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013014 if (app.hasAboveClient) {
13015 // If this process has bound to any services with BIND_ABOVE_CLIENT,
13016 // then we need to drop its adjustment to be lower than the service's
13017 // in order to honor the request. We want to drop it by one adjustment
13018 // level... but there is special meaning applied to various levels so
13019 // we will skip some of them.
Dianne Hackborn7d608422011-08-07 16:24:18 -070013020 if (adj < ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013021 // System process will not get dropped, ever
Dianne Hackborn7d608422011-08-07 16:24:18 -070013022 } else if (adj < ProcessList.VISIBLE_APP_ADJ) {
13023 adj = ProcessList.VISIBLE_APP_ADJ;
13024 } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) {
13025 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
13026 } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) {
13027 adj = ProcessList.HIDDEN_APP_MIN_ADJ;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013028 } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013029 adj++;
13030 }
13031 }
13032
Dianne Hackborna93c2c12012-05-31 15:29:36 -070013033 int importance = app.memImportance;
13034 if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) {
13035 app.curAdj = adj;
13036 app.curSchedGroup = schedGroup;
13037 if (!interesting) {
13038 // For this reporting, if there is not something explicitly
13039 // interesting in this process then we will push it to the
13040 // background importance.
13041 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
13042 } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
13043 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
13044 } else if (adj >= ProcessList.SERVICE_B_ADJ) {
13045 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
13046 } else if (adj >= ProcessList.HOME_APP_ADJ) {
13047 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
13048 } else if (adj >= ProcessList.SERVICE_ADJ) {
13049 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
13050 } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
13051 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
13052 } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
13053 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
13054 } else if (adj >= ProcessList.VISIBLE_APP_ADJ) {
13055 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
13056 } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) {
13057 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
13058 } else {
13059 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT;
13060 }
13061 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070013062
Dianne Hackborna93c2c12012-05-31 15:29:36 -070013063 int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0;
13064 if (foregroundActivities != app.foregroundActivities) {
13065 changes |= ProcessChangeItem.CHANGE_ACTIVITIES;
13066 }
13067 if (changes != 0) {
13068 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes);
13069 app.memImportance = importance;
13070 app.foregroundActivities = foregroundActivities;
13071 int i = mPendingProcessChanges.size()-1;
13072 ProcessChangeItem item = null;
13073 while (i >= 0) {
13074 item = mPendingProcessChanges.get(i);
13075 if (item.pid == app.pid) {
13076 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item);
13077 break;
13078 }
13079 i--;
13080 }
13081 if (i < 0) {
13082 // No existing item in pending changes; need a new one.
13083 final int NA = mAvailProcessChanges.size();
13084 if (NA > 0) {
13085 item = mAvailProcessChanges.remove(NA-1);
13086 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item);
13087 } else {
13088 item = new ProcessChangeItem();
13089 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item);
13090 }
13091 item.changes = 0;
13092 item.pid = app.pid;
13093 item.uid = app.info.uid;
13094 if (mPendingProcessChanges.size() == 0) {
13095 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG,
13096 "*** Enqueueing dispatch processes changed!");
13097 mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget();
13098 }
13099 mPendingProcessChanges.add(item);
13100 }
13101 item.changes |= changes;
13102 item.importance = importance;
13103 item.foregroundActivities = foregroundActivities;
13104 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item "
13105 + Integer.toHexString(System.identityHashCode(item))
13106 + " " + app.toShortString() + ": changes=" + item.changes
13107 + " importance=" + item.importance
13108 + " foreground=" + item.foregroundActivities
13109 + " type=" + app.adjType + " source=" + app.adjSource
13110 + " target=" + app.adjTarget);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070013111 }
13112
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013113 return app.curRawAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013114 }
13115
13116 /**
13117 * Ask a given process to GC right now.
13118 */
13119 final void performAppGcLocked(ProcessRecord app) {
13120 try {
13121 app.lastRequestedGc = SystemClock.uptimeMillis();
13122 if (app.thread != null) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013123 if (app.reportLowMemory) {
13124 app.reportLowMemory = false;
13125 app.thread.scheduleLowMemory();
13126 } else {
13127 app.thread.processInBackground();
13128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013129 }
13130 } catch (Exception e) {
13131 // whatever.
13132 }
13133 }
13134
13135 /**
13136 * Returns true if things are idle enough to perform GCs.
13137 */
Josh Bartel7f208742010-02-25 11:01:44 -060013138 private final boolean canGcNowLocked() {
Christopher Tatef46723b2012-01-26 14:19:24 -080013139 boolean processingBroadcasts = false;
13140 for (BroadcastQueue q : mBroadcastQueues) {
13141 if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) {
13142 processingBroadcasts = true;
13143 }
13144 }
13145 return !processingBroadcasts
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070013146 && (mSleeping || (mMainStack.mResumedActivity != null &&
13147 mMainStack.mResumedActivity.idle));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013148 }
13149
13150 /**
13151 * Perform GCs on all processes that are waiting for it, but only
13152 * if things are idle.
13153 */
13154 final void performAppGcsLocked() {
13155 final int N = mProcessesToGc.size();
13156 if (N <= 0) {
13157 return;
13158 }
Josh Bartel7f208742010-02-25 11:01:44 -060013159 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013160 while (mProcessesToGc.size() > 0) {
13161 ProcessRecord proc = mProcessesToGc.remove(0);
Dianne Hackborn7d608422011-08-07 16:24:18 -070013162 if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013163 if ((proc.lastRequestedGc+GC_MIN_INTERVAL)
13164 <= SystemClock.uptimeMillis()) {
13165 // To avoid spamming the system, we will GC processes one
13166 // at a time, waiting a few seconds between each.
13167 performAppGcLocked(proc);
13168 scheduleAppGcsLocked();
13169 return;
13170 } else {
13171 // It hasn't been long enough since we last GCed this
13172 // process... put it in the list to wait for its time.
13173 addProcessToGcListLocked(proc);
13174 break;
13175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013176 }
13177 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013178
13179 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013180 }
13181 }
13182
13183 /**
13184 * If all looks good, perform GCs on all processes waiting for them.
13185 */
13186 final void performAppGcsIfAppropriateLocked() {
Josh Bartel7f208742010-02-25 11:01:44 -060013187 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013188 performAppGcsLocked();
13189 return;
13190 }
13191 // Still not idle, wait some more.
13192 scheduleAppGcsLocked();
13193 }
13194
13195 /**
13196 * Schedule the execution of all pending app GCs.
13197 */
13198 final void scheduleAppGcsLocked() {
13199 mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG);
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013200
13201 if (mProcessesToGc.size() > 0) {
13202 // Schedule a GC for the time to the next process.
13203 ProcessRecord proc = mProcessesToGc.get(0);
13204 Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG);
13205
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013206 long when = proc.lastRequestedGc + GC_MIN_INTERVAL;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013207 long now = SystemClock.uptimeMillis();
13208 if (when < (now+GC_TIMEOUT)) {
13209 when = now + GC_TIMEOUT;
13210 }
13211 mHandler.sendMessageAtTime(msg, when);
13212 }
13213 }
13214
13215 /**
13216 * Add a process to the array of processes waiting to be GCed. Keeps the
13217 * list in sorted order by the last GC time. The process can't already be
13218 * on the list.
13219 */
13220 final void addProcessToGcListLocked(ProcessRecord proc) {
13221 boolean added = false;
13222 for (int i=mProcessesToGc.size()-1; i>=0; i--) {
13223 if (mProcessesToGc.get(i).lastRequestedGc <
13224 proc.lastRequestedGc) {
13225 added = true;
13226 mProcessesToGc.add(i+1, proc);
13227 break;
13228 }
13229 }
13230 if (!added) {
13231 mProcessesToGc.add(0, proc);
13232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013233 }
13234
13235 /**
13236 * Set up to ask a process to GC itself. This will either do it
13237 * immediately, or put it on the list of processes to gc the next
13238 * time things are idle.
13239 */
13240 final void scheduleAppGcLocked(ProcessRecord app) {
13241 long now = SystemClock.uptimeMillis();
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013242 if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013243 return;
13244 }
13245 if (!mProcessesToGc.contains(app)) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013246 addProcessToGcListLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013247 scheduleAppGcsLocked();
13248 }
13249 }
13250
Dianne Hackborn287952c2010-09-22 22:34:31 -070013251 final void checkExcessivePowerUsageLocked(boolean doKills) {
13252 updateCpuStatsNow();
13253
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013254 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
Dianne Hackborn287952c2010-09-22 22:34:31 -070013255 boolean doWakeKills = doKills;
13256 boolean doCpuKills = doKills;
13257 if (mLastPowerCheckRealtime == 0) {
13258 doWakeKills = false;
13259 }
13260 if (mLastPowerCheckUptime == 0) {
13261 doCpuKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013262 }
13263 if (stats.isScreenOn()) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070013264 doWakeKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013265 }
13266 final long curRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn287952c2010-09-22 22:34:31 -070013267 final long realtimeSince = curRealtime - mLastPowerCheckRealtime;
13268 final long curUptime = SystemClock.uptimeMillis();
13269 final long uptimeSince = curUptime - mLastPowerCheckUptime;
13270 mLastPowerCheckRealtime = curRealtime;
13271 mLastPowerCheckUptime = curUptime;
13272 if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) {
13273 doWakeKills = false;
13274 }
13275 if (uptimeSince < CPU_MIN_CHECK_DURATION) {
13276 doCpuKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013277 }
13278 int i = mLruProcesses.size();
13279 while (i > 0) {
13280 i--;
13281 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn287952c2010-09-22 22:34:31 -070013282 if (!app.keeping) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013283 long wtime;
13284 synchronized (stats) {
13285 wtime = stats.getProcessWakeTime(app.info.uid,
13286 app.pid, curRealtime);
13287 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070013288 long wtimeUsed = wtime - app.lastWakeTime;
13289 long cputimeUsed = app.curCpuTime - app.lastCpuTime;
13290 if (DEBUG_POWER) {
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070013291 StringBuilder sb = new StringBuilder(128);
13292 sb.append("Wake for ");
13293 app.toShortString(sb);
13294 sb.append(": over ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070013295 TimeUtils.formatDuration(realtimeSince, sb);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070013296 sb.append(" used ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070013297 TimeUtils.formatDuration(wtimeUsed, sb);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070013298 sb.append(" (");
Dianne Hackborn287952c2010-09-22 22:34:31 -070013299 sb.append((wtimeUsed*100)/realtimeSince);
13300 sb.append("%)");
13301 Slog.i(TAG, sb.toString());
13302 sb.setLength(0);
13303 sb.append("CPU for ");
13304 app.toShortString(sb);
13305 sb.append(": over ");
13306 TimeUtils.formatDuration(uptimeSince, sb);
13307 sb.append(" used ");
13308 TimeUtils.formatDuration(cputimeUsed, sb);
13309 sb.append(" (");
13310 sb.append((cputimeUsed*100)/uptimeSince);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070013311 sb.append("%)");
13312 Slog.i(TAG, sb.toString());
13313 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013314 // If a process has held a wake lock for more
13315 // than 50% of the time during this period,
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013316 // that sounds bad. Kill!
Dianne Hackborn287952c2010-09-22 22:34:31 -070013317 if (doWakeKills && realtimeSince > 0
13318 && ((wtimeUsed*100)/realtimeSince) >= 50) {
13319 synchronized (stats) {
13320 stats.reportExcessiveWakeLocked(app.info.uid, app.processName,
13321 realtimeSince, wtimeUsed);
13322 }
13323 Slog.w(TAG, "Excessive wake lock in " + app.processName
13324 + " (pid " + app.pid + "): held " + wtimeUsed
13325 + " during " + realtimeSince);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013326 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13327 app.processName, app.setAdj, "excessive wake lock");
13328 Process.killProcessQuiet(app.pid);
Dianne Hackborn287952c2010-09-22 22:34:31 -070013329 } else if (doCpuKills && uptimeSince > 0
13330 && ((cputimeUsed*100)/uptimeSince) >= 50) {
13331 synchronized (stats) {
13332 stats.reportExcessiveCpuLocked(app.info.uid, app.processName,
13333 uptimeSince, cputimeUsed);
13334 }
13335 Slog.w(TAG, "Excessive CPU in " + app.processName
13336 + " (pid " + app.pid + "): used " + cputimeUsed
13337 + " during " + uptimeSince);
13338 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13339 app.processName, app.setAdj, "excessive cpu");
13340 Process.killProcessQuiet(app.pid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013341 } else {
13342 app.lastWakeTime = wtime;
Dianne Hackborn287952c2010-09-22 22:34:31 -070013343 app.lastCpuTime = app.curCpuTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013344 }
13345 }
13346 }
13347 }
13348
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013349 private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj,
13350 int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013351 app.hiddenAdj = hiddenAdj;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013352 app.emptyAdj = emptyAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013353
13354 if (app.thread == null) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013355 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013356 }
13357
Dianne Hackborn287952c2010-09-22 22:34:31 -070013358 final boolean wasKeeping = app.keeping;
13359
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013360 boolean success = true;
13361
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013362 computeOomAdjLocked(app, hiddenAdj, emptyAdj, TOP_APP, false, doingAll);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013363
Jeff Brown10e89712011-07-08 18:52:57 -070013364 if (app.curRawAdj != app.setRawAdj) {
Jeff Brown10e89712011-07-08 18:52:57 -070013365 if (wasKeeping && !app.keeping) {
13366 // This app is no longer something we want to keep. Note
13367 // its current wake lock time to later know to kill it if
13368 // it is not behaving well.
13369 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
13370 synchronized (stats) {
13371 app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
13372 app.pid, SystemClock.elapsedRealtime());
13373 }
13374 app.lastCpuTime = app.curCpuTime;
13375 }
13376
13377 app.setRawAdj = app.curRawAdj;
13378 }
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013379
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013380 if (app.curAdj != app.setAdj) {
13381 if (Process.setOomAdj(app.pid, app.curAdj)) {
Dianne Hackbornbbb09ac2011-11-30 11:31:29 -080013382 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013383 TAG, "Set " + app.pid + " " + app.processName +
13384 " adj " + app.curAdj + ": " + app.adjType);
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013385 app.setAdj = app.curAdj;
Jeff Brown10e89712011-07-08 18:52:57 -070013386 } else {
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013387 success = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013388 Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj);
Jeff Brown10e89712011-07-08 18:52:57 -070013389 }
13390 }
13391 if (app.setSchedGroup != app.curSchedGroup) {
13392 app.setSchedGroup = app.curSchedGroup;
13393 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
13394 "Setting process group of " + app.processName
13395 + " to " + app.curSchedGroup);
13396 if (app.waitingToKill != null &&
13397 app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
13398 Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill);
13399 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13400 app.processName, app.setAdj, app.waitingToKill);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -070013401 app.killedBackground = true;
Jeff Brown10e89712011-07-08 18:52:57 -070013402 Process.killProcessQuiet(app.pid);
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013403 success = false;
Jeff Brown10e89712011-07-08 18:52:57 -070013404 } else {
13405 if (true) {
13406 long oldId = Binder.clearCallingIdentity();
13407 try {
13408 Process.setProcessGroup(app.pid, app.curSchedGroup);
13409 } catch (Exception e) {
13410 Slog.w(TAG, "Failed setting process group of " + app.pid
13411 + " to " + app.curSchedGroup);
13412 e.printStackTrace();
13413 } finally {
13414 Binder.restoreCallingIdentity(oldId);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013415 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013416 } else {
Jeff Brown10e89712011-07-08 18:52:57 -070013417 if (app.thread != null) {
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070013418 try {
Jeff Brown10e89712011-07-08 18:52:57 -070013419 app.thread.setSchedulingGroup(app.curSchedGroup);
13420 } catch (RemoteException e) {
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070013421 }
13422 }
13423 }
13424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013425 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013426 return success;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013427 }
13428
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070013429 private final ActivityRecord resumedAppLocked() {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070013430 ActivityRecord resumedActivity = mMainStack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013431 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -080013432 resumedActivity = mMainStack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013433 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070013434 resumedActivity = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013435 }
13436 }
13437 return resumedActivity;
13438 }
13439
Dianne Hackborn599db5c2012-08-03 19:28:48 -070013440 final boolean updateOomAdjLocked(ProcessRecord app) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070013441 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013442 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
13443 int curAdj = app.curAdj;
Dianne Hackborn7d608422011-08-07 16:24:18 -070013444 final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ
13445 && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013446
13447 mAdjSeq++;
13448
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013449 boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.emptyAdj,
13450 TOP_APP, false);
Dianne Hackborn7d608422011-08-07 16:24:18 -070013451 final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ
13452 && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013453 if (nowHidden != wasHidden) {
13454 // Changed to/from hidden state, so apps after it in the LRU
13455 // list may also be changed.
13456 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013457 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013458 return success;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013459 }
13460
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013461 final void updateOomAdjLocked() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070013462 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013463 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
13464
13465 if (false) {
13466 RuntimeException e = new RuntimeException();
13467 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -080013468 Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013469 }
13470
13471 mAdjSeq++;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013472 mNewNumServiceProcs = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013473
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080013474 // Let's determine how many processes we have running vs.
13475 // how many slots we have for background processes; we may want
13476 // to put multiple processes in a slot of there are enough of
13477 // them.
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013478 int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ
13479 - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2;
13480 int emptyFactor = (mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs)/numSlots;
13481 if (emptyFactor < 1) emptyFactor = 1;
13482 int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots;
13483 if (hiddenFactor < 1) hiddenFactor = 1;
13484 int stepHidden = 0;
13485 int stepEmpty = 0;
13486 final int emptyProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit;
13487 final int hiddenProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit;
Dianne Hackborn8633e682010-04-22 16:03:41 -070013488 int numHidden = 0;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013489 int numEmpty = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013490 int numTrimming = 0;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013491
13492 mNumNonHiddenProcs = 0;
13493 mNumHiddenProcs = 0;
13494
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013495 // First update the OOM adjustment for each of the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013496 // application processes based on their current state.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013497 int i = mLruProcesses.size();
Dianne Hackborn7d608422011-08-07 16:24:18 -070013498 int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013499 int nextHiddenAdj = curHiddenAdj+1;
13500 int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
13501 int nextEmptyAdj = curEmptyAdj+2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013502 while (i > 0) {
13503 i--;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013504 ProcessRecord app = mLruProcesses.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080013505 //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj);
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013506 updateOomAdjLocked(app, curHiddenAdj, curEmptyAdj, TOP_APP, true);
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013507 if (!app.killedBackground) {
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013508 if (app.curRawAdj == curHiddenAdj && app.hasActivities) {
13509 // This process was assigned as a hidden process... step the
13510 // hidden level.
13511 mNumHiddenProcs++;
13512 if (curHiddenAdj != nextHiddenAdj) {
13513 stepHidden++;
13514 if (stepHidden >= hiddenFactor) {
13515 stepHidden = 0;
13516 curHiddenAdj = nextHiddenAdj;
13517 nextHiddenAdj += 2;
13518 if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) {
13519 nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ;
13520 }
13521 }
13522 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013523 numHidden++;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013524 if (numHidden > hiddenProcessLimit) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013525 Slog.i(TAG, "No longer want " + app.processName
13526 + " (pid " + app.pid + "): hidden #" + numHidden);
13527 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13528 app.processName, app.setAdj, "too many background");
13529 app.killedBackground = true;
13530 Process.killProcessQuiet(app.pid);
Dianne Hackborn8633e682010-04-22 16:03:41 -070013531 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013532 } else {
13533 if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) {
13534 // This process was assigned as an empty process... step the
13535 // empty level.
13536 if (curEmptyAdj != nextEmptyAdj) {
13537 stepEmpty++;
13538 if (stepEmpty >= emptyFactor) {
13539 stepEmpty = 0;
13540 curEmptyAdj = nextEmptyAdj;
13541 nextEmptyAdj += 2;
13542 if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) {
13543 nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ;
13544 }
13545 }
13546 }
13547 } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) {
13548 mNumNonHiddenProcs++;
13549 }
13550 if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
13551 numEmpty++;
13552 if (numEmpty > emptyProcessLimit) {
13553 Slog.i(TAG, "No longer want " + app.processName
13554 + " (pid " + app.pid + "): empty #" + numEmpty);
13555 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13556 app.processName, app.setAdj, "too many background");
13557 app.killedBackground = true;
13558 Process.killProcessQuiet(app.pid);
13559 }
13560 }
Dianne Hackborn8633e682010-04-22 16:03:41 -070013561 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013562 if (app.isolated && app.services.size() <= 0) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -080013563 // If this is an isolated process, and there are no
13564 // services running in it, then the process is no longer
13565 // needed. We agressively kill these because we can by
13566 // definition not re-use the same process again, and it is
13567 // good to avoid having whatever code was running in them
13568 // left sitting around after no longer needed.
13569 Slog.i(TAG, "Isolated process " + app.processName
13570 + " (pid " + app.pid + ") no longer needed");
13571 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13572 app.processName, app.setAdj, "isolated not needed");
13573 app.killedBackground = true;
13574 Process.killProcessQuiet(app.pid);
13575 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013576 if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ
13577 && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ
13578 && !app.killedBackground) {
13579 numTrimming++;
13580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013581 }
13582 }
13583
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013584 mNumServiceProcs = mNewNumServiceProcs;
13585
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013586 // Now determine the memory trimming level of background processes.
13587 // Unfortunately we need to start at the back of the list to do this
13588 // properly. We only do this if the number of background apps we
13589 // are managing to keep around is less than half the maximum we desire;
13590 // if we are keeping a good number around, we'll let them use whatever
13591 // memory they want.
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013592 if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/4)
13593 && numEmpty <= (ProcessList.MAX_HIDDEN_APPS/4)) {
13594 final int numHiddenAndEmpty = numHidden + numEmpty;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013595 final int N = mLruProcesses.size();
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013596 int factor = numTrimming/3;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080013597 int minFactor = 2;
13598 if (mHomeProcess != null) minFactor++;
13599 if (mPreviousProcess != null) minFactor++;
13600 if (factor < minFactor) factor = minFactor;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013601 int step = 0;
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013602 int fgTrimLevel;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013603 if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/5)) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013604 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013605 } else if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/3)) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013606 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
13607 } else {
13608 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE;
13609 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013610 int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013611 for (i=0; i<N; i++) {
13612 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013613 if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ
13614 && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080013615 && !app.killedBackground) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013616 if (app.trimMemoryLevel < curLevel && app.thread != null) {
13617 try {
13618 app.thread.scheduleTrimMemory(curLevel);
13619 } catch (RemoteException e) {
13620 }
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013621 if (false) {
13622 // For now we won't do this; our memory trimming seems
13623 // to be good enough at this point that destroying
13624 // activities causes more harm than good.
13625 if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE
13626 && app != mHomeProcess && app != mPreviousProcess) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013627 // Need to do this on its own message because the stack may not
13628 // be in a consistent state at this point.
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013629 // For these apps we will also finish their activities
13630 // to help them free memory.
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013631 mMainStack.scheduleDestroyActivities(app, false, "trim");
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013632 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013633 }
13634 }
13635 app.trimMemoryLevel = curLevel;
13636 step++;
13637 if (step >= factor) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013638 step = 0;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013639 switch (curLevel) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013640 case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
13641 curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013642 break;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013643 case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
13644 curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013645 break;
13646 }
13647 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013648 } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013649 if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013650 && app.thread != null) {
13651 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013652 app.thread.scheduleTrimMemory(
13653 ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013654 } catch (RemoteException e) {
13655 }
13656 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013657 app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013658 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013659 if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi)
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013660 && app.pendingUiClean) {
13661 // If this application is now in the background and it
13662 // had done UI, then give it the special trim level to
13663 // have it free UI resources.
13664 final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
13665 if (app.trimMemoryLevel < level && app.thread != null) {
13666 try {
13667 app.thread.scheduleTrimMemory(level);
13668 } catch (RemoteException e) {
13669 }
13670 }
13671 app.pendingUiClean = false;
13672 }
13673 if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013674 try {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013675 app.thread.scheduleTrimMemory(fgTrimLevel);
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013676 } catch (RemoteException e) {
13677 }
13678 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013679 app.trimMemoryLevel = fgTrimLevel;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013680 }
13681 }
13682 } else {
13683 final int N = mLruProcesses.size();
13684 for (i=0; i<N; i++) {
13685 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013686 if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi)
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013687 && app.pendingUiClean) {
13688 if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
13689 && app.thread != null) {
13690 try {
13691 app.thread.scheduleTrimMemory(
13692 ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
13693 } catch (RemoteException e) {
13694 }
13695 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013696 app.pendingUiClean = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013697 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013698 app.trimMemoryLevel = 0;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013699 }
13700 }
13701
13702 if (mAlwaysFinishActivities) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013703 // Need to do this on its own message because the stack may not
13704 // be in a consistent state at this point.
13705 mMainStack.scheduleDestroyActivities(null, false, "always-finish");
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013707 }
13708
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070013709 final void trimApplications() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013710 synchronized (this) {
13711 int i;
13712
13713 // First remove any unused application processes whose package
13714 // has been removed.
13715 for (i=mRemovedProcesses.size()-1; i>=0; i--) {
13716 final ProcessRecord app = mRemovedProcesses.get(i);
13717 if (app.activities.size() == 0
13718 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013719 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013720 TAG, "Exiting empty application process "
13721 + app.processName + " ("
13722 + (app.thread != null ? app.thread.asBinder() : null)
13723 + ")\n");
13724 if (app.pid > 0 && app.pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070013725 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13726 app.processName, app.setAdj, "empty");
13727 Process.killProcessQuiet(app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013728 } else {
13729 try {
13730 app.thread.scheduleExit();
13731 } catch (Exception e) {
13732 // Ignore exceptions.
13733 }
13734 }
Dianne Hackborn130b0d22011-07-26 22:07:48 -070013735 cleanUpApplicationRecordLocked(app, false, true, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013736 mRemovedProcesses.remove(i);
13737
13738 if (app.persistent) {
13739 if (app.persistent) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -080013740 addAppLocked(app.info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013741 }
13742 }
13743 }
13744 }
13745
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013746 // Now update the oom adj for all processes.
13747 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013748 }
13749 }
13750
13751 /** This method sends the specified signal to each of the persistent apps */
13752 public void signalPersistentProcesses(int sig) throws RemoteException {
13753 if (sig != Process.SIGNAL_USR1) {
13754 throw new SecurityException("Only SIGNAL_USR1 is allowed");
13755 }
13756
13757 synchronized (this) {
13758 if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES)
13759 != PackageManager.PERMISSION_GRANTED) {
13760 throw new SecurityException("Requires permission "
13761 + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES);
13762 }
13763
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013764 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
13765 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013766 if (r.thread != null && r.persistent) {
13767 Process.sendSignal(r.pid, sig);
13768 }
13769 }
13770 }
13771 }
13772
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013773 private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) {
13774 if (proc == null || proc == mProfileProc) {
13775 proc = mProfileProc;
13776 path = mProfileFile;
13777 profileType = mProfileType;
13778 clearProfilerLocked();
13779 }
13780 if (proc == null) {
13781 return;
13782 }
13783 try {
13784 proc.thread.profilerControl(false, path, null, profileType);
13785 } catch (RemoteException e) {
13786 throw new IllegalStateException("Process disappeared");
13787 }
13788 }
13789
13790 private void clearProfilerLocked() {
13791 if (mProfileFd != null) {
13792 try {
13793 mProfileFd.close();
13794 } catch (IOException e) {
13795 }
13796 }
13797 mProfileApp = null;
13798 mProfileProc = null;
13799 mProfileFile = null;
13800 mProfileType = 0;
13801 mAutoStopProfiler = false;
13802 }
13803
Dianne Hackborn1676c852012-09-10 14:52:30 -070013804 public boolean profileControl(String process, int userId, boolean start,
Romain Guy7eabe552011-07-21 14:56:34 -070013805 String path, ParcelFileDescriptor fd, int profileType) throws RemoteException {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013806
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013807 try {
13808 synchronized (this) {
13809 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
13810 // its own permission.
13811 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
13812 != PackageManager.PERMISSION_GRANTED) {
13813 throw new SecurityException("Requires permission "
13814 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013815 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013816
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013817 if (start && fd == null) {
13818 throw new IllegalArgumentException("null fd");
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013819 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013820
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013821 ProcessRecord proc = null;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013822 if (process != null) {
Dianne Hackborn1676c852012-09-10 14:52:30 -070013823 proc = findProcessLocked(process, userId, "profileControl");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013824 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013825
13826 if (start && (proc == null || proc.thread == null)) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013827 throw new IllegalArgumentException("Unknown process: " + process);
13828 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013829
13830 if (start) {
13831 stopProfilerLocked(null, null, 0);
13832 setProfileApp(proc.info, proc.processName, path, fd, false);
13833 mProfileProc = proc;
13834 mProfileType = profileType;
13835 try {
13836 fd = fd.dup();
13837 } catch (IOException e) {
13838 fd = null;
13839 }
13840 proc.thread.profilerControl(start, path, fd, profileType);
13841 fd = null;
13842 mProfileFd = null;
13843 } else {
13844 stopProfilerLocked(proc, path, profileType);
13845 if (fd != null) {
13846 try {
13847 fd.close();
13848 } catch (IOException e) {
13849 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013850 }
13851 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013852
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013853 return true;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013854 }
13855 } catch (RemoteException e) {
13856 throw new IllegalStateException("Process disappeared");
13857 } finally {
13858 if (fd != null) {
13859 try {
13860 fd.close();
13861 } catch (IOException e) {
13862 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013863 }
13864 }
13865 }
Andy McFadden824c5102010-07-09 16:26:57 -070013866
Dianne Hackborn1676c852012-09-10 14:52:30 -070013867 private ProcessRecord findProcessLocked(String process, int userId, String callName) {
13868 userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(),
13869 userId, true, true, callName, null);
13870 ProcessRecord proc = null;
13871 try {
13872 int pid = Integer.parseInt(process);
13873 synchronized (mPidsSelfLocked) {
13874 proc = mPidsSelfLocked.get(pid);
13875 }
13876 } catch (NumberFormatException e) {
13877 }
13878
13879 if (proc == null) {
13880 HashMap<String, SparseArray<ProcessRecord>> all
13881 = mProcessNames.getMap();
13882 SparseArray<ProcessRecord> procs = all.get(process);
13883 if (procs != null && procs.size() > 0) {
13884 proc = procs.valueAt(0);
13885 if (userId != UserHandle.USER_ALL && proc.userId != userId) {
13886 for (int i=1; i<procs.size(); i++) {
13887 ProcessRecord thisProc = procs.valueAt(i);
13888 if (thisProc.userId == userId) {
13889 proc = thisProc;
13890 break;
13891 }
13892 }
13893 }
13894 }
13895 }
13896
13897 return proc;
13898 }
13899
13900 public boolean dumpHeap(String process, int userId, boolean managed,
Andy McFadden824c5102010-07-09 16:26:57 -070013901 String path, ParcelFileDescriptor fd) throws RemoteException {
13902
13903 try {
13904 synchronized (this) {
13905 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
13906 // its own permission (same as profileControl).
13907 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
13908 != PackageManager.PERMISSION_GRANTED) {
13909 throw new SecurityException("Requires permission "
13910 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
13911 }
13912
13913 if (fd == null) {
13914 throw new IllegalArgumentException("null fd");
13915 }
13916
Dianne Hackborn1676c852012-09-10 14:52:30 -070013917 ProcessRecord proc = findProcessLocked(process, userId, "dumpHeap");
Andy McFadden824c5102010-07-09 16:26:57 -070013918 if (proc == null || proc.thread == null) {
13919 throw new IllegalArgumentException("Unknown process: " + process);
13920 }
13921
Dianne Hackbornf02e57b2011-03-01 22:21:04 -080013922 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
13923 if (!isDebuggable) {
Andy McFadden824c5102010-07-09 16:26:57 -070013924 if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
13925 throw new SecurityException("Process not debuggable: " + proc);
13926 }
13927 }
13928
13929 proc.thread.dumpHeap(managed, path, fd);
13930 fd = null;
13931 return true;
13932 }
13933 } catch (RemoteException e) {
13934 throw new IllegalStateException("Process disappeared");
13935 } finally {
13936 if (fd != null) {
13937 try {
13938 fd.close();
13939 } catch (IOException e) {
13940 }
13941 }
13942 }
13943 }
13944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 /** In this method we try to acquire our lock to make sure that we have not deadlocked */
13946 public void monitor() {
13947 synchronized (this) { }
13948 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080013949
Dianne Hackborna573f6a2012-02-09 16:12:18 -080013950 void onCoreSettingsChange(Bundle settings) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080013951 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
13952 ProcessRecord processRecord = mLruProcesses.get(i);
13953 try {
13954 if (processRecord.thread != null) {
13955 processRecord.thread.setCoreSettings(settings);
13956 }
13957 } catch (RemoteException re) {
13958 /* ignore */
13959 }
13960 }
13961 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070013962
13963 // Multi-user methods
13964
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013965 @Override
Amith Yamasani742a6712011-05-04 14:49:28 -070013966 public boolean switchUser(int userId) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013967 if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
13968 != PackageManager.PERMISSION_GRANTED) {
13969 String msg = "Permission Denial: switchUser() from pid="
13970 + Binder.getCallingPid()
13971 + ", uid=" + Binder.getCallingUid()
13972 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
13973 Slog.w(TAG, msg);
13974 throw new SecurityException(msg);
Amith Yamasani742a6712011-05-04 14:49:28 -070013975 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013976
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070013977 final long ident = Binder.clearCallingIdentity();
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013978 try {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070013979 synchronized (this) {
13980 final int oldUserId = mCurrentUserId;
13981 if (oldUserId == userId) {
13982 return true;
13983 }
13984
13985 final UserInfo userInfo = getUserManagerLocked().getUserInfo(userId);
13986 if (userInfo == null) {
13987 Slog.w(TAG, "No user info for user #" + userId);
13988 return false;
13989 }
13990
13991 mWindowManager.startFreezingScreen(R.anim.screen_user_exit,
13992 R.anim.screen_user_enter);
13993
13994 // If the user we are switching to is not currently started, then
13995 // we need to start it now.
13996 if (mStartedUsers.get(userId) == null) {
13997 mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false));
Dianne Hackbornc72fc672012-09-20 13:12:03 -070013998 updateStartedUserArrayLocked();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070013999 }
14000
14001 mCurrentUserId = userId;
Dianne Hackbornc72fc672012-09-20 13:12:03 -070014002 mCurrentUserArray = new int[] { userId };
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070014003 final Integer userIdInt = Integer.valueOf(userId);
14004 mUserLru.remove(userIdInt);
14005 mUserLru.add(userIdInt);
14006
Craig Mautnerf1b67412012-09-19 13:18:29 -070014007 mWindowManager.setCurrentUser(userId);
14008
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070014009 final UserStartedState uss = mStartedUsers.get(userId);
14010
14011 mHandler.removeMessages(REPORT_USER_SWITCH_MSG);
14012 mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG);
14013 mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG,
14014 oldUserId, userId, uss));
14015 mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG,
14016 oldUserId, userId, uss), USER_SWITCH_TIMEOUT);
14017 Intent intent = new Intent(Intent.ACTION_USER_STARTED);
14018 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
14019 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
14020 broadcastIntentLocked(null, null, intent,
14021 null, null, 0, null, null, null,
14022 false, false, MY_PID, Process.SYSTEM_UID, userId);
14023
14024 if ((userInfo.flags&UserInfo.FLAG_INITIALIZED) == 0) {
14025 if (userId != 0) {
14026 intent = new Intent(Intent.ACTION_USER_INITIALIZE);
14027 broadcastIntentLocked(null, null, intent, null,
14028 new IIntentReceiver.Stub() {
14029 public void performReceive(Intent intent, int resultCode,
14030 String data, Bundle extras, boolean ordered,
14031 boolean sticky, int sendingUser) {
14032 synchronized (ActivityManagerService.this) {
14033 getUserManagerLocked().makeInitialized(userInfo.id);
14034 }
14035 }
14036 }, 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID,
14037 userId);
14038 } else {
14039 getUserManagerLocked().makeInitialized(userInfo.id);
14040 }
14041 }
14042
14043 boolean haveActivities = mMainStack.switchUserLocked(userId, uss);
14044 if (!haveActivities) {
14045 startHomeActivityLocked(userId);
14046 }
Craig Mautnerf1b67412012-09-19 13:18:29 -070014047
Amith Yamasani920ace02012-09-20 22:15:37 -070014048 getUserManagerLocked().userForeground(userId);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070014049 sendUserSwitchBroadcastsLocked(oldUserId, userId);
14050 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014051 } finally {
14052 Binder.restoreCallingIdentity(ident);
14053 }
Amith Yamasani13593602012-03-22 16:16:17 -070014054
Amith Yamasani4b2e9342011-03-31 12:38:53 -070014055 return true;
14056 }
Amith Yamasani742a6712011-05-04 14:49:28 -070014057
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070014058 void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) {
14059 long ident = Binder.clearCallingIdentity();
14060 try {
14061 Intent intent;
14062 if (oldUserId >= 0) {
14063 intent = new Intent(Intent.ACTION_USER_BACKGROUND);
14064 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
14065 intent.putExtra(Intent.EXTRA_USER_HANDLE, oldUserId);
14066 broadcastIntentLocked(null, null, intent,
14067 null, null, 0, null, null, null,
14068 false, false, MY_PID, Process.SYSTEM_UID, oldUserId);
14069 }
14070 if (newUserId >= 0) {
14071 intent = new Intent(Intent.ACTION_USER_FOREGROUND);
14072 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
14073 intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId);
14074 broadcastIntentLocked(null, null, intent,
14075 null, null, 0, null, null, null,
14076 false, false, MY_PID, Process.SYSTEM_UID, newUserId);
14077 intent = new Intent(Intent.ACTION_USER_SWITCHED);
14078 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
14079 intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId);
14080 broadcastIntentLocked(null, null, intent,
14081 null, null, 0, null, null,
14082 android.Manifest.permission.MANAGE_USERS,
14083 false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
14084 }
14085 } finally {
14086 Binder.restoreCallingIdentity(ident);
14087 }
14088 }
14089
14090 void dispatchUserSwitch(final UserStartedState uss, final int oldUserId,
14091 final int newUserId) {
14092 final int N = mUserSwitchObservers.beginBroadcast();
14093 if (N > 0) {
14094 final IRemoteCallback callback = new IRemoteCallback.Stub() {
14095 int mCount = 0;
14096 @Override
14097 public void sendResult(Bundle data) throws RemoteException {
14098 synchronized (ActivityManagerService.this) {
14099 if (mCurUserSwitchCallback == this) {
14100 mCount++;
14101 if (mCount == N) {
14102 sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
14103 }
14104 }
14105 }
14106 }
14107 };
14108 synchronized (this) {
14109 mCurUserSwitchCallback = callback;
14110 }
14111 for (int i=0; i<N; i++) {
14112 try {
14113 mUserSwitchObservers.getBroadcastItem(i).onUserSwitching(
14114 newUserId, callback);
14115 } catch (RemoteException e) {
14116 }
14117 }
14118 } else {
14119 synchronized (this) {
14120 sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
14121 }
14122 }
14123 mUserSwitchObservers.finishBroadcast();
14124 }
14125
14126 void timeoutUserSwitch(UserStartedState uss, int oldUserId, int newUserId) {
14127 synchronized (this) {
14128 Slog.w(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId);
14129 sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
14130 }
14131 }
14132
14133 void sendContinueUserSwitchLocked(UserStartedState uss, int oldUserId, int newUserId) {
14134 mCurUserSwitchCallback = null;
14135 mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG);
14136 mHandler.sendMessage(mHandler.obtainMessage(CONTINUE_USER_SWITCH_MSG,
14137 oldUserId, newUserId, uss));
14138 }
14139
14140 void continueUserSwitch(UserStartedState uss, int oldUserId, int newUserId) {
14141 final int N = mUserSwitchObservers.beginBroadcast();
14142 for (int i=0; i<N; i++) {
14143 try {
14144 mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(newUserId);
14145 } catch (RemoteException e) {
14146 }
14147 }
14148 mUserSwitchObservers.finishBroadcast();
14149 synchronized (this) {
14150 mWindowManager.stopFreezingScreen();
14151 }
14152 }
14153
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014154 void finishUserSwitch(UserStartedState uss) {
14155 synchronized (this) {
14156 if (uss.mState == UserStartedState.STATE_BOOTING
14157 && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) {
14158 uss.mState = UserStartedState.STATE_RUNNING;
Dianne Hackborn41203752012-08-31 14:05:51 -070014159 final int userId = uss.mHandle.getIdentifier();
14160 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null);
14161 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
14162 broadcastIntentLocked(null, null, intent,
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014163 null, null, 0, null, null,
14164 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
Dianne Hackborn41203752012-08-31 14:05:51 -070014165 false, false, MY_PID, Process.SYSTEM_UID, userId);
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014166 }
Dianne Hackbornbb1aeff2012-09-18 15:48:21 -070014167 int num = mUserLru.size();
14168 int i = 0;
14169 while (num > MAX_RUNNING_USERS && i < mUserLru.size()) {
14170 Integer oldUserId = mUserLru.get(i);
14171 UserStartedState oldUss = mStartedUsers.get(oldUserId);
14172 if (oldUss == null) {
14173 // Shouldn't happen, but be sane if it does.
14174 mUserLru.remove(i);
14175 num--;
14176 continue;
14177 }
14178 if (oldUss.mState == UserStartedState.STATE_STOPPING) {
14179 // This user is already stopping, doesn't count.
14180 num--;
14181 i++;
14182 continue;
14183 }
14184 if (oldUserId == UserHandle.USER_OWNER || oldUserId == mCurrentUserId) {
14185 // Owner and current can't be stopped, but count as running.
14186 i++;
14187 continue;
14188 }
14189 // This is a user to be stopped.
14190 stopUserLocked(oldUserId, null);
14191 num--;
14192 i++;
14193 }
Amith Yamasani52f1d752012-03-28 18:19:29 -070014194 }
Amith Yamasani52f1d752012-03-28 18:19:29 -070014195 }
14196
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014197 @Override
14198 public int stopUser(final int userId, final IStopUserCallback callback) {
14199 if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
14200 != PackageManager.PERMISSION_GRANTED) {
14201 String msg = "Permission Denial: switchUser() from pid="
14202 + Binder.getCallingPid()
14203 + ", uid=" + Binder.getCallingUid()
14204 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
14205 Slog.w(TAG, msg);
14206 throw new SecurityException(msg);
14207 }
14208 if (userId <= 0) {
14209 throw new IllegalArgumentException("Can't stop primary user " + userId);
14210 }
Amith Yamasani13593602012-03-22 16:16:17 -070014211 synchronized (this) {
Dianne Hackbornbb1aeff2012-09-18 15:48:21 -070014212 return stopUserLocked(userId, callback);
14213 }
14214 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014215
Dianne Hackbornbb1aeff2012-09-18 15:48:21 -070014216 private int stopUserLocked(final int userId, final IStopUserCallback callback) {
14217 if (mCurrentUserId == userId) {
14218 return ActivityManager.USER_OP_IS_CURRENT;
14219 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014220
Dianne Hackbornbb1aeff2012-09-18 15:48:21 -070014221 final UserStartedState uss = mStartedUsers.get(userId);
14222 if (uss == null) {
14223 // User is not started, nothing to do... but we do need to
14224 // callback if requested.
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014225 if (callback != null) {
Dianne Hackbornbb1aeff2012-09-18 15:48:21 -070014226 mHandler.post(new Runnable() {
14227 @Override
14228 public void run() {
14229 try {
14230 callback.userStopped(userId);
14231 } catch (RemoteException e) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014232 }
Dianne Hackbornbb1aeff2012-09-18 15:48:21 -070014233 }
14234 });
14235 }
14236 return ActivityManager.USER_OP_SUCCESS;
14237 }
14238
14239 if (callback != null) {
14240 uss.mStopCallbacks.add(callback);
14241 }
14242
14243 if (uss.mState != UserStartedState.STATE_STOPPING) {
14244 uss.mState = UserStartedState.STATE_STOPPING;
14245
14246 long ident = Binder.clearCallingIdentity();
14247 try {
14248 // Inform of user switch
14249 Intent intent = new Intent(Intent.ACTION_SHUTDOWN);
14250 final IIntentReceiver resultReceiver = new IIntentReceiver.Stub() {
14251 @Override
14252 public void performReceive(Intent intent, int resultCode, String data,
14253 Bundle extras, boolean ordered, boolean sticky, int sendingUser) {
14254 finishUserStop(uss);
14255 }
14256 };
14257 broadcastIntentLocked(null, null, intent,
14258 null, resultReceiver, 0, null, null, null,
14259 true, false, MY_PID, Process.SYSTEM_UID, userId);
14260 } finally {
14261 Binder.restoreCallingIdentity(ident);
Amith Yamasani13593602012-03-22 16:16:17 -070014262 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014263 }
Amith Yamasani13593602012-03-22 16:16:17 -070014264
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014265 return ActivityManager.USER_OP_SUCCESS;
14266 }
14267
14268 void finishUserStop(UserStartedState uss) {
14269 final int userId = uss.mHandle.getIdentifier();
14270 boolean stopped;
14271 ArrayList<IStopUserCallback> callbacks;
14272 synchronized (this) {
14273 callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks);
14274 if (uss.mState != UserStartedState.STATE_STOPPING
14275 || mStartedUsers.get(userId) != uss) {
14276 stopped = false;
14277 } else {
14278 stopped = true;
14279 // User can no longer run.
14280 mStartedUsers.remove(userId);
Dianne Hackbornbb1aeff2012-09-18 15:48:21 -070014281 mUserLru.remove(Integer.valueOf(userId));
Dianne Hackbornc72fc672012-09-20 13:12:03 -070014282 updateStartedUserArrayLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014283
14284 // Clean up all state and processes associated with the user.
14285 // Kill all the processes for the user.
14286 forceStopUserLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -070014287 }
14288 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014289
14290 for (int i=0; i<callbacks.size(); i++) {
14291 try {
14292 if (stopped) callbacks.get(i).userStopped(userId);
14293 else callbacks.get(i).userStopAborted(userId);
14294 } catch (RemoteException e) {
14295 }
14296 }
14297 }
14298
14299 @Override
14300 public UserInfo getCurrentUser() {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070014301 if ((checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
14302 != PackageManager.PERMISSION_GRANTED) && (
14303 checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
14304 != PackageManager.PERMISSION_GRANTED)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014305 String msg = "Permission Denial: getCurrentUser() from pid="
14306 + Binder.getCallingPid()
14307 + ", uid=" + Binder.getCallingUid()
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070014308 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014309 Slog.w(TAG, msg);
14310 throw new SecurityException(msg);
14311 }
14312 synchronized (this) {
Dianne Hackborn1676c852012-09-10 14:52:30 -070014313 return getUserManagerLocked().getUserInfo(mCurrentUserId);
Dianne Hackborn80a4af22012-08-27 19:18:31 -070014314 }
Amith Yamasani13593602012-03-22 16:16:17 -070014315 }
14316
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -070014317 int getCurrentUserIdLocked() {
14318 return mCurrentUserId;
14319 }
14320
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070014321 @Override
14322 public boolean isUserRunning(int userId) {
14323 if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
14324 != PackageManager.PERMISSION_GRANTED) {
14325 String msg = "Permission Denial: isUserRunning() from pid="
14326 + Binder.getCallingPid()
14327 + ", uid=" + Binder.getCallingUid()
14328 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS;
14329 Slog.w(TAG, msg);
14330 throw new SecurityException(msg);
14331 }
14332 synchronized (this) {
Dianne Hackborn1676c852012-09-10 14:52:30 -070014333 return isUserRunningLocked(userId);
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -070014334 }
14335 }
14336
Dianne Hackborn1676c852012-09-10 14:52:30 -070014337 boolean isUserRunningLocked(int userId) {
14338 UserStartedState state = mStartedUsers.get(userId);
14339 return state != null && state.mState != UserStartedState.STATE_STOPPING;
Amith Yamasani258848d2012-08-10 17:06:33 -070014340 }
Amith Yamasani742a6712011-05-04 14:49:28 -070014341
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070014342 @Override
Dianne Hackbornc72fc672012-09-20 13:12:03 -070014343 public int[] getRunningUserIds() {
14344 if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
14345 != PackageManager.PERMISSION_GRANTED) {
14346 String msg = "Permission Denial: isUserRunning() from pid="
14347 + Binder.getCallingPid()
14348 + ", uid=" + Binder.getCallingUid()
14349 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS;
14350 Slog.w(TAG, msg);
14351 throw new SecurityException(msg);
14352 }
14353 synchronized (this) {
14354 return mStartedUserArray;
14355 }
14356 }
14357
14358 private void updateStartedUserArrayLocked() {
14359 mStartedUserArray = new int[mStartedUsers.size()];
14360 for (int i=0; i<mStartedUsers.size(); i++) {
14361 mStartedUserArray[i] = mStartedUsers.keyAt(i);
14362 }
14363 }
14364
14365 @Override
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070014366 public void registerUserSwitchObserver(IUserSwitchObserver observer) {
14367 if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
14368 != PackageManager.PERMISSION_GRANTED) {
14369 String msg = "Permission Denial: registerUserSwitchObserver() from pid="
14370 + Binder.getCallingPid()
14371 + ", uid=" + Binder.getCallingUid()
14372 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
14373 Slog.w(TAG, msg);
14374 throw new SecurityException(msg);
14375 }
14376
14377 mUserSwitchObservers.register(observer);
14378 }
14379
14380 @Override
14381 public void unregisterUserSwitchObserver(IUserSwitchObserver observer) {
14382 mUserSwitchObservers.unregister(observer);
14383 }
14384
Dianne Hackborn1676c852012-09-10 14:52:30 -070014385 private boolean userExists(int userId) {
14386 if (userId == 0) {
14387 return true;
14388 }
14389 UserManagerService ums = getUserManagerLocked();
14390 return ums != null ? (ums.getUserInfo(userId) != null) : false;
14391 }
14392
14393 int[] getUsersLocked() {
14394 UserManagerService ums = getUserManagerLocked();
14395 return ums != null ? ums.getUserIds() : new int[] { 0 };
14396 }
14397
14398 UserManagerService getUserManagerLocked() {
Amith Yamasani258848d2012-08-10 17:06:33 -070014399 if (mUserManager == null) {
Dianne Hackborn1676c852012-09-10 14:52:30 -070014400 IBinder b = ServiceManager.getService(Context.USER_SERVICE);
14401 mUserManager = (UserManagerService)IUserManager.Stub.asInterface(b);
Amith Yamasani258848d2012-08-10 17:06:33 -070014402 }
14403 return mUserManager;
Amith Yamasani742a6712011-05-04 14:49:28 -070014404 }
14405
Amith Yamasani37ce3a82012-02-06 12:04:42 -080014406 private void checkValidCaller(int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070014407 if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080014408
14409 throw new SecurityException("Caller uid=" + uid
14410 + " is not privileged to communicate with user=" + userId);
14411 }
Amith Yamasani742a6712011-05-04 14:49:28 -070014412
14413 private int applyUserId(int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070014414 return UserHandle.getUid(userId, uid);
Amith Yamasani742a6712011-05-04 14:49:28 -070014415 }
14416
Dianne Hackborn599db5c2012-08-03 19:28:48 -070014417 ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) {
Amith Yamasani2f6c9eb2012-02-06 15:31:35 -080014418 if (info == null) return null;
Amith Yamasani742a6712011-05-04 14:49:28 -070014419 ApplicationInfo newInfo = new ApplicationInfo(info);
14420 newInfo.uid = applyUserId(info.uid, userId);
Amith Yamasania4a54e22012-04-16 15:44:19 -070014421 newInfo.dataDir = USER_DATA_DIR + userId + "/"
14422 + info.packageName;
Amith Yamasani742a6712011-05-04 14:49:28 -070014423 return newInfo;
14424 }
14425
14426 ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) {
Amith Yamasania4a54e22012-04-16 15:44:19 -070014427 if (aInfo == null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070014428 || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) {
Amith Yamasani742a6712011-05-04 14:49:28 -070014429 return aInfo;
14430 }
14431
14432 ActivityInfo info = new ActivityInfo(aInfo);
14433 info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId);
14434 return info;
14435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014436}