blob: 5921d88ef1d194c151b4800a31ff8e4bdb0e06f3 [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 Hackborna924dc0d2011-02-17 14:22:17 -080030import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Dianne Hackborndd71fc82009-12-16 19:24:32 -080032import dalvik.system.Zygote;
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.app.Activity;
35import android.app.ActivityManager;
36import android.app.ActivityManagerNative;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -070037import android.app.ActivityOptions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.app.ActivityThread;
39import android.app.AlertDialog;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070040import android.app.AppGlobals;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020041import android.app.ApplicationErrorReport;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.Dialog;
Dianne Hackbornb06ea702009-07-13 13:07:51 -070043import android.app.IActivityController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.app.IApplicationThread;
45import android.app.IInstrumentationWatcher;
Dianne Hackborn860755f2010-06-03 18:47:52 -070046import android.app.INotificationManager;
Jeff Sharkeya4620792011-05-20 15:29:23 -070047import android.app.IProcessObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.app.IServiceConnection;
49import android.app.IThumbnailReceiver;
50import android.app.Instrumentation;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070051import android.app.Notification;
Dianne Hackborn860755f2010-06-03 18:47:52 -070052import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.app.PendingIntent;
Christopher Tate45281862010-03-05 15:46:30 -080054import android.app.backup.IBackupManager;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020055import android.content.ActivityNotFoundException;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080056import android.content.BroadcastReceiver;
Dianne Hackborn21c241e2012-03-08 13:57:23 -080057import android.content.ClipData;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070058import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.ComponentName;
Jeff Sharkey110a6b62012-03-12 11:12:41 -070060import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.ContentResolver;
62import android.content.Context;
Christian Mehlmauer7664e202010-07-20 08:46:17 +020063import android.content.DialogInterface;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070064import android.content.IContentProvider;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070065import android.content.IIntentReceiver;
66import android.content.IIntentSender;
Adam Powelldd8fab22012-03-22 17:47:27 -070067import android.content.Intent;
68import android.content.IntentFilter;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070069import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.pm.ActivityInfo;
71import android.content.pm.ApplicationInfo;
72import android.content.pm.ConfigurationInfo;
73import android.content.pm.IPackageDataObserver;
74import android.content.pm.IPackageManager;
75import android.content.pm.InstrumentationInfo;
Dan Egnor66c40e72010-01-26 16:23:11 -080076import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.content.pm.PackageManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070078import android.content.pm.UserInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070079import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070080import android.content.pm.PathPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.content.pm.ProviderInfo;
82import android.content.pm.ResolveInfo;
83import android.content.pm.ServiceInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -040084import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.content.res.Configuration;
86import android.graphics.Bitmap;
Robert Greenwalt434203a2010-10-11 16:00:27 -070087import android.net.Proxy;
88import android.net.ProxyProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.net.Uri;
90import android.os.Binder;
Dan Egnor60d87622009-12-16 16:32:58 -080091import android.os.Build;
Dan Egnor42471dd2010-01-07 17:25:22 -080092import android.os.Bundle;
Dianne Hackborn3025ef32009-08-31 21:31:47 -070093import android.os.Debug;
Dan Egnor60d87622009-12-16 16:32:58 -080094import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.os.Environment;
Dan Egnor42471dd2010-01-07 17:25:22 -080096import android.os.FileObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.os.FileUtils;
98import android.os.Handler;
99import android.os.IBinder;
100import android.os.IPermissionController;
101import android.os.Looper;
102import android.os.Message;
103import android.os.Parcel;
104import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.os.Process;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700106import android.os.RemoteCallbackList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.os.RemoteException;
rpcraigec7ed14c2012-07-25 13:10:37 -0400108import android.os.SELinux;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.os.ServiceManager;
Brad Fitzpatrick46d42382010-06-11 13:57:58 -0700110import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.os.SystemClock;
112import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700113import android.os.UserHandle;
Amith Yamasani258848d2012-08-10 17:06:33 -0700114import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.provider.Settings;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700116import android.text.format.Time;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.util.EventLog;
Joe Onorato5d3bea62010-03-01 13:44:29 -0800118import android.util.Log;
Adam Powelldd8fab22012-03-22 17:47:27 -0700119import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.util.PrintWriterPrinter;
Adam Powelldd8fab22012-03-22 17:47:27 -0700121import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import android.util.SparseArray;
Amith Yamasani742a6712011-05-04 14:49:28 -0700123import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700124import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import android.view.Gravity;
126import android.view.LayoutInflater;
127import android.view.View;
128import android.view.WindowManager;
129import android.view.WindowManagerPolicy;
130
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700131import java.io.BufferedInputStream;
132import java.io.BufferedOutputStream;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700133import java.io.BufferedReader;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700134import java.io.DataInputStream;
135import java.io.DataOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.io.File;
137import java.io.FileDescriptor;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700138import java.io.FileInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139import java.io.FileNotFoundException;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700140import java.io.FileOutputStream;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200141import java.io.IOException;
Dan Egnora455d192010-03-12 08:52:28 -0800142import java.io.InputStreamReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143import java.io.PrintWriter;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700144import java.io.StringWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.lang.ref.WeakReference;
146import java.util.ArrayList;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700147import java.util.Collections;
148import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149import java.util.HashMap;
150import java.util.HashSet;
151import java.util.Iterator;
152import java.util.List;
153import java.util.Locale;
154import java.util.Map;
Amith Yamasani13593602012-03-22 16:16:17 -0700155import java.util.Map.Entry;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -0700156import java.util.Set;
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700157import java.util.concurrent.atomic.AtomicBoolean;
158import java.util.concurrent.atomic.AtomicLong;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700160public final class ActivityManagerService extends ActivityManagerNative
161 implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
Amith Yamasani742a6712011-05-04 14:49:28 -0700162 private static final String USER_DATA_DIR = "/data/user/";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final String TAG = "ActivityManager";
Amith Yamasani742a6712011-05-04 14:49:28 -0700164 static final String TAG_MU = "ActivityManagerServiceMU";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 static final boolean DEBUG = false;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400166 static final boolean localLOGV = DEBUG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 static final boolean DEBUG_SWITCH = localLOGV || false;
168 static final boolean DEBUG_TASKS = localLOGV || false;
169 static final boolean DEBUG_PAUSE = localLOGV || false;
170 static final boolean DEBUG_OOM_ADJ = localLOGV || false;
171 static final boolean DEBUG_TRANSITION = localLOGV || false;
172 static final boolean DEBUG_BROADCAST = localLOGV || false;
Christopher Tatef46723b2012-01-26 14:19:24 -0800173 static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false;
Dianne Hackborn82f3f002009-06-16 18:49:05 -0700174 static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 static final boolean DEBUG_SERVICE = localLOGV || false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700176 static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 static final boolean DEBUG_VISBILITY = localLOGV || false;
178 static final boolean DEBUG_PROCESSES = localLOGV || false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700179 static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false;
Dianne Hackborna1e989b2009-09-01 19:54:29 -0700180 static final boolean DEBUG_PROVIDER = localLOGV || false;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800181 static final boolean DEBUG_URI_PERMISSION = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 static final boolean DEBUG_USER_LEAVING = localLOGV || false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700183 static final boolean DEBUG_RESULTS = localLOGV || false;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700184 static final boolean DEBUG_BACKUP = localLOGV || false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700185 static final boolean DEBUG_CONFIGURATION = localLOGV || false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700186 static final boolean DEBUG_POWER = localLOGV || false;
187 static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false;
Amith Yamasani742a6712011-05-04 14:49:28 -0700188 static final boolean DEBUG_MU = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 static final boolean VALIDATE_TOKENS = false;
190 static final boolean SHOW_ACTIVITY_START_TIME = true;
191
192 // Control over CPU and battery monitoring.
193 static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes.
194 static final boolean MONITOR_CPU_USAGE = true;
195 static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds.
196 static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample.
197 static final boolean MONITOR_THREAD_CPU_USAGE = false;
198
Dianne Hackborn1655be42009-05-08 14:29:01 -0700199 // The flags that are set for all calls we make to the package manager.
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700200 static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
Dianne Hackborn1655be42009-05-08 14:29:01 -0700201
Dianne Hackbornf02e57b2011-03-01 22:21:04 -0800202 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700204 static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 // Maximum number of recent tasks that we can remember.
207 static final int MAX_RECENT_TASKS = 20;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700208
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700209 // Amount of time after a call to stopAppSwitches() during which we will
210 // prevent further untrusted switches from happening.
211 static final long APP_SWITCH_DELAY_TIME = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
213 // How long we wait for a launched process to attach to the activity manager
214 // before we decide it's never going to come up for real.
215 static final int PROC_START_TIMEOUT = 10*1000;
216
Jeff Brown3f9dd282011-07-08 20:02:19 -0700217 // How long we wait for a launched process to attach to the activity manager
218 // before we decide it's never going to come up for real, when the process was
219 // started with a wrapper for instrumentation (such as Valgrind) because it
220 // could take much longer than usual.
221 static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000;
222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 // How long to wait after going idle before forcing apps to GC.
224 static final int GC_TIMEOUT = 5*1000;
225
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700226 // The minimum amount of time between successive GC requests for a process.
227 static final int GC_MIN_INTERVAL = 60*1000;
228
Dianne Hackborn287952c2010-09-22 22:34:31 -0700229 // The rate at which we check for apps using excessive power -- 15 mins.
230 static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000;
231
232 // The minimum sample duration we will allow before deciding we have
233 // enough data on wake locks to start killing things.
234 static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
235
236 // The minimum sample duration we will allow before deciding we have
237 // enough data on CPU usage to start killing things.
238 static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 // How long we allow a receiver to run before giving up on it.
Christopher Tatef46723b2012-01-26 14:19:24 -0800241 static final int BROADCAST_FG_TIMEOUT = 10*1000;
242 static final int BROADCAST_BG_TIMEOUT = 60*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 // How long we wait until we timeout on key dispatching.
245 static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 // How long we wait until we timeout on key dispatching during instrumentation.
248 static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;
249
Dan Egnor42471dd2010-01-07 17:25:22 -0800250 static final int MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
252 static final String[] EMPTY_STRING_ARRAY = new String[0];
253
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700254 public ActivityStack mMainStack;
Mike Lockwood3a74bd32011-08-12 13:55:22 -0700255
256 private final boolean mHeadless;
257
Joe Onorato54a4a412011-11-02 20:50:08 -0700258 // Whether we should show our dialogs (ANR, crash, etc) or just perform their
259 // default actuion automatically. Important for devices without direct input
260 // devices.
261 private boolean mShowDialogs = true;
262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700264 * Description of a request to start a new activity, which has been held
265 * due to app switches being disabled.
266 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267 static class PendingActivityLaunch {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700268 ActivityRecord r;
269 ActivityRecord sourceRecord;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700270 int startFlags;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700271 }
272
273 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
274 = new ArrayList<PendingActivityLaunch>();
275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800277 BroadcastQueue mFgBroadcastQueue;
278 BroadcastQueue mBgBroadcastQueue;
Christopher Tatef46723b2012-01-26 14:19:24 -0800279 // Convenient for easy iteration over the queues. Foreground is first
280 // so that dispatch of foreground broadcasts gets precedence.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800281 final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2];
Christopher Tatef46723b2012-01-26 14:19:24 -0800282
283 BroadcastQueue broadcastQueueForIntent(Intent intent) {
284 final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0;
285 if (DEBUG_BACKGROUND_BROADCAST) {
286 Slog.i(TAG, "Broadcast intent " + intent + " on "
287 + (isFg ? "foreground" : "background")
288 + " queue");
289 }
290 return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue;
291 }
292
293 BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) {
294 for (BroadcastQueue queue : mBroadcastQueues) {
295 BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver);
296 if (r != null) {
297 return r;
298 }
299 }
300 return null;
301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
303 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 * Activity we have told the window manager to have key focus.
305 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700306 ActivityRecord mFocusedActivity = null;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 * List of intents that were used to start the most recent tasks.
309 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700310 final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311
312 /**
Dianne Hackborn7d608422011-08-07 16:24:18 -0700313 * Process management.
314 */
315 final ProcessList mProcessList = new ProcessList();
316
317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * All of the applications we currently have running organized by name.
319 * The keys are strings of the application package name (as
320 * returned by the package manager), and the keys are ApplicationRecord
321 * objects.
322 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700323 final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324
325 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800326 * The currently running isolated processes.
327 */
328 final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>();
329
330 /**
331 * Counter for assigning isolated process uids, to avoid frequently reusing the
332 * same ones.
333 */
334 int mNextIsolatedProcessUid = 0;
335
336 /**
Dianne Hackborn860755f2010-06-03 18:47:52 -0700337 * The currently running heavy-weight process, if any.
338 */
339 ProcessRecord mHeavyWeightProcess = null;
340
341 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 * The last time that various processes have crashed.
343 */
344 final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>();
345
346 /**
347 * Set of applications that we consider to be bad, and will reject
348 * incoming broadcasts from (which the user has no control over).
349 * Processes are added to this set when they have crashed twice within
350 * a minimum amount of time; they are removed from it when they are
351 * later restarted (hopefully due to some user action). The value is the
352 * time it was added to the list.
353 */
354 final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>();
355
356 /**
357 * All of the processes we currently have running organized by pid.
358 * The keys are the pid running the application.
359 *
360 * <p>NOTE: This object is protected by its own lock, NOT the global
361 * activity manager lock!
362 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700363 final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364
365 /**
366 * All of the processes that have been forced to be foreground. The key
367 * is the pid of the caller who requested it (we hold a death
368 * link on it).
369 */
370 abstract class ForegroundToken implements IBinder.DeathRecipient {
371 int pid;
372 IBinder token;
373 }
374 final SparseArray<ForegroundToken> mForegroundProcesses
375 = new SparseArray<ForegroundToken>();
376
377 /**
378 * List of records for processes that someone had tried to start before the
379 * system was ready. We don't start them at that point, but ensure they
380 * are started by the time booting is complete.
381 */
382 final ArrayList<ProcessRecord> mProcessesOnHold
383 = new ArrayList<ProcessRecord>();
384
385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * List of persistent applications that are in the process
387 * of being started.
388 */
389 final ArrayList<ProcessRecord> mPersistentStartingProcesses
390 = new ArrayList<ProcessRecord>();
391
392 /**
393 * Processes that are being forcibly torn down.
394 */
395 final ArrayList<ProcessRecord> mRemovedProcesses
396 = new ArrayList<ProcessRecord>();
397
398 /**
399 * List of running applications, sorted by recent usage.
400 * The first entry in the list is the least recently used.
401 * It contains ApplicationRecord objects. This list does NOT include
402 * any persistent application records (since we never want to exit them).
403 */
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800404 final ArrayList<ProcessRecord> mLruProcesses
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 = new ArrayList<ProcessRecord>();
406
407 /**
408 * List of processes that should gc as soon as things are idle.
409 */
410 final ArrayList<ProcessRecord> mProcessesToGc
411 = new ArrayList<ProcessRecord>();
412
413 /**
The Android Open Source Project4df24232009-03-05 14:34:35 -0800414 * This is the process holding what we currently consider to be
415 * the "home" activity.
416 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700417 ProcessRecord mHomeProcess;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800418
419 /**
Dianne Hackbornf35fe232011-11-01 19:25:20 -0700420 * This is the process holding the activity the user last visited that
421 * is in a different process from the one they are currently in.
422 */
423 ProcessRecord mPreviousProcess;
Dianne Hackborn50685602011-12-01 12:23:37 -0800424
425 /**
426 * The time at which the previous process was last visible.
427 */
428 long mPreviousProcessVisibleTime;
429
Dianne Hackbornf35fe232011-11-01 19:25:20 -0700430 /**
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400431 * Packages that the user has asked to have run in screen size
432 * compatibility mode instead of filling the screen.
433 */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700434 final CompatModePackages mCompatModePackages;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400435
436 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 * Set of PendingResultRecord objects that are currently active.
438 */
439 final HashSet mPendingResultRecords = new HashSet();
440
441 /**
442 * Set of IntentSenderRecord objects that are currently active.
443 */
444 final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords
445 = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>();
446
447 /**
Brad Fitzpatrickf3d86be2010-11-23 10:31:52 -0800448 * Fingerprints (hashCode()) of stack traces that we've
Brad Fitzpatrick143666f2010-06-14 12:40:21 -0700449 * already logged DropBox entries for. Guarded by itself. If
450 * something (rogue user app) forces this over
451 * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared.
452 */
453 private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>();
454 private static final int MAX_DUP_SUPPRESSED_STACKS = 5000;
455
456 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -0700457 * Strict Mode background batched logging state.
458 *
459 * The string buffer is guarded by itself, and its lock is also
460 * used to determine if another batched write is already
461 * in-flight.
462 */
463 private final StringBuilder mStrictModeBuffer = new StringBuilder();
464
465 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 * Keeps track of all IIntentReceivers that have been registered for
467 * broadcasts. Hash keys are the receiver IBinder, hash value is
468 * a ReceiverList.
469 */
470 final HashMap mRegisteredReceivers = new HashMap();
471
472 /**
473 * Resolver for broadcast intents to registered receivers.
474 * Holds BroadcastFilter (subclass of IntentFilter).
475 */
476 final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver
477 = new IntentResolver<BroadcastFilter, BroadcastFilter>() {
478 @Override
479 protected boolean allowFilterResult(
480 BroadcastFilter filter, List<BroadcastFilter> dest) {
481 IBinder target = filter.receiverList.receiver.asBinder();
482 for (int i=dest.size()-1; i>=0; i--) {
483 if (dest.get(i).receiverList.receiver.asBinder() == target) {
484 return false;
485 }
486 }
487 return true;
488 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700489
490 @Override
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700491 protected BroadcastFilter[] newArray(int size) {
492 return new BroadcastFilter[size];
493 }
494
495 @Override
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700496 protected String packageForFilter(BroadcastFilter filter) {
497 return filter.packageName;
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 };
500
501 /**
502 * State of all active sticky broadcasts. Keys are the action of the
503 * sticky Intent, values are an ArrayList of all broadcasted intents with
504 * that action (which should usually be one).
505 */
506 final HashMap<String, ArrayList<Intent>> mStickyBroadcasts =
507 new HashMap<String, ArrayList<Intent>>();
508
Dianne Hackborn599db5c2012-08-03 19:28:48 -0700509 final ActiveServices mServices;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
511 /**
Christopher Tate181fafa2009-05-14 11:12:14 -0700512 * Backup/restore process management
513 */
514 String mBackupAppName = null;
515 BackupRecord mBackupTarget = null;
516
517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 * List of PendingThumbnailsRecord objects of clients who are still
519 * waiting to receive all of the thumbnails for a task.
520 */
521 final ArrayList mPendingThumbnails = new ArrayList();
522
523 /**
524 * List of HistoryRecord objects that have been finished and must
525 * still report back to a pending thumbnail receiver.
526 */
527 final ArrayList mCancelledThumbnails = new ArrayList();
528
Amith Yamasani742a6712011-05-04 14:49:28 -0700529 final ProviderMap mProviderMap = new ProviderMap();
530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 /**
532 * List of content providers who have clients waiting for them. The
533 * application is currently being launched and the provider will be
534 * removed from this list once it is published.
535 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700536 final ArrayList<ContentProviderRecord> mLaunchingProviders
537 = new ArrayList<ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538
539 /**
540 * Global set of specific Uri permissions that have been granted.
541 */
542 final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions
543 = new SparseArray<HashMap<Uri, UriPermission>>();
544
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800545 CoreSettingsObserver mCoreSettingsObserver;
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 /**
548 * Thread-local storage used to carry caller permissions over through
549 * indirect content-provider access.
550 * @see #ActivityManagerService.openContentUri()
551 */
552 private class Identity {
553 public int pid;
554 public int uid;
555
556 Identity(int _pid, int _uid) {
557 pid = _pid;
558 uid = _uid;
559 }
560 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>();
563
564 /**
565 * All information we have collected about the runtime performance of
566 * any user id that can impact battery performance.
567 */
568 final BatteryStatsService mBatteryStatsService;
569
570 /**
571 * information about component usage
572 */
573 final UsageStatsService mUsageStatsService;
574
575 /**
576 * Current configuration information. HistoryRecord objects are given
577 * a reference to this object to indicate which configuration they are
578 * currently running in, so this object must be kept immutable.
579 */
580 Configuration mConfiguration = new Configuration();
581
582 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800583 * Current sequencing integer of the configuration, for skipping old
584 * configurations.
585 */
586 int mConfigurationSeq = 0;
587
588 /**
Jack Palevichb90d28c2009-07-22 15:35:24 -0700589 * Hardware-reported OpenGLES version.
590 */
591 final int GL_ES_VERSION;
592
593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * List of initialization arguments to pass to all processes when binding applications to them.
595 * For example, references to the commonly used services.
596 */
597 HashMap<String, IBinder> mAppBindArgs;
598
599 /**
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700600 * Temporary to avoid allocations. Protected by main lock.
601 */
602 final StringBuilder mStringBuilder = new StringBuilder(256);
603
604 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 * Used to control how we initialize the service.
606 */
607 boolean mStartRunning = false;
608 ComponentName mTopComponent;
609 String mTopAction;
610 String mTopData;
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700611 boolean mProcessesReady = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 boolean mSystemReady = false;
613 boolean mBooting = false;
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700614 boolean mWaitingUpdate = false;
615 boolean mDidUpdate = false;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700616 boolean mOnBattery = false;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700617 boolean mLaunchWarningShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618
619 Context mContext;
620
621 int mFactoryTest;
622
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -0700623 boolean mCheckedForSetup;
624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700626 * The time at which we will allow normal application switches again,
627 * after a call to {@link #stopAppSwitches()}.
628 */
629 long mAppSwitchesAllowedTime;
630
631 /**
632 * This is set to true after the first switch after mAppSwitchesAllowedTime
633 * is set; any switches after that will clear the time.
634 */
635 boolean mDidAppSwitch;
636
637 /**
Dianne Hackborn287952c2010-09-22 22:34:31 -0700638 * Last time (in realtime) at which we checked for power usage.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700639 */
Dianne Hackborn287952c2010-09-22 22:34:31 -0700640 long mLastPowerCheckRealtime;
641
642 /**
643 * Last time (in uptime) at which we checked for power usage.
644 */
645 long mLastPowerCheckUptime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700646
647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * Set while we are wanting to sleep, to prevent any
649 * activities from being started/resumed.
650 */
651 boolean mSleeping = false;
652
653 /**
Dianne Hackbornff5b1582012-04-12 17:24:07 -0700654 * State of external calls telling us if the device is asleep.
655 */
656 boolean mWentToSleep = false;
657
658 /**
659 * State of external call telling us if the lock screen is shown.
660 */
661 boolean mLockScreenShown = false;
662
663 /**
Dianne Hackborn55280a92009-05-07 15:53:46 -0700664 * Set if we are shutting down the system, similar to sleeping.
665 */
666 boolean mShuttingDown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667
668 /**
669 * Task identifier that activities are currently being started
670 * in. Incremented each time a new task is created.
671 * todo: Replace this with a TokenSpace class that generates non-repeating
672 * integers that won't wrap.
673 */
674 int mCurTask = 1;
675
676 /**
677 * Current sequence id for oom_adj computation traversal.
678 */
679 int mAdjSeq = 0;
680
681 /**
Dianne Hackborn906497c2010-05-10 15:57:38 -0700682 * Current sequence id for process LRU updating.
683 */
684 int mLruSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685
686 /**
Dianne Hackbornee7621c2012-08-13 16:42:18 -0700687 * Keep track of the non-hidden/empty process we last found, to help
688 * determine how to distribute hidden/empty processes next time.
689 */
690 int mNumNonHiddenProcs = 0;
691
692 /**
693 * Keep track of the number of hidden procs, to balance oom adj
694 * distribution between those and empty procs.
695 */
696 int mNumHiddenProcs = 0;
697
698 /**
Dianne Hackborne02c88a2011-10-28 13:58:15 -0700699 * Keep track of the number of service processes we last found, to
700 * determine on the next iteration which should be B services.
701 */
702 int mNumServiceProcs = 0;
703 int mNewNumServiceProcs = 0;
704
705 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 * System monitoring: number of processes that died since the last
707 * N procs were started.
708 */
709 int[] mProcDeaths = new int[20];
710
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700711 /**
712 * This is set if we had to do a delayed dexopt of an app before launching
713 * it, to increasing the ANR timeouts in that case.
714 */
715 boolean mDidDexOpt;
716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 String mDebugApp = null;
718 boolean mWaitForDebugger = false;
719 boolean mDebugTransient = false;
720 String mOrigDebugApp = null;
721 boolean mOrigWaitForDebugger = false;
722 boolean mAlwaysFinishActivities = false;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700723 IActivityController mController = null;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700724 String mProfileApp = null;
725 ProcessRecord mProfileProc = null;
726 String mProfileFile;
727 ParcelFileDescriptor mProfileFd;
728 int mProfileType = 0;
729 boolean mAutoStopProfiler = false;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800730 String mOpenGlTraceApp = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700732 static class ProcessChangeItem {
733 static final int CHANGE_ACTIVITIES = 1<<0;
734 static final int CHANGE_IMPORTANCE= 1<<1;
735 int changes;
736 int uid;
737 int pid;
738 int importance;
739 boolean foregroundActivities;
740 }
741
Jeff Sharkeya4620792011-05-20 15:29:23 -0700742 final RemoteCallbackList<IProcessObserver> mProcessObservers
743 = new RemoteCallbackList<IProcessObserver>();
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700744 ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5];
745
746 final ArrayList<ProcessChangeItem> mPendingProcessChanges
747 = new ArrayList<ProcessChangeItem>();
748 final ArrayList<ProcessChangeItem> mAvailProcessChanges
749 = new ArrayList<ProcessChangeItem>();
Jeff Sharkeya4620792011-05-20 15:29:23 -0700750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 /**
752 * Callback of last caller to {@link #requestPss}.
753 */
754 Runnable mRequestPssCallback;
755
756 /**
757 * Remaining processes for which we are waiting results from the last
758 * call to {@link #requestPss}.
759 */
760 final ArrayList<ProcessRecord> mRequestPssList
761 = new ArrayList<ProcessRecord>();
762
763 /**
764 * Runtime statistics collection thread. This object's lock is used to
765 * protect all related state.
766 */
767 final Thread mProcessStatsThread;
768
769 /**
770 * Used to collect process stats when showing not responding dialog.
771 * Protected by mProcessStatsThread.
772 */
773 final ProcessStats mProcessStats = new ProcessStats(
774 MONITOR_THREAD_CPU_USAGE);
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700775 final AtomicLong mLastCpuTime = new AtomicLong(0);
776 final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true);
777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 long mLastWriteTime = 0;
779
780 /**
781 * Set to true after the system has finished booting.
782 */
783 boolean mBooted = false;
784
Dianne Hackborn7d608422011-08-07 16:24:18 -0700785 int mProcessLimit = ProcessList.MAX_HIDDEN_APPS;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700786 int mProcessLimitOverride = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787
788 WindowManagerService mWindowManager;
789
790 static ActivityManagerService mSelf;
791 static ActivityThread mSystemThread;
792
Amith Yamasani258848d2012-08-10 17:06:33 -0700793 private int mCurrentUserId;
794 private SparseIntArray mLoggedInUsers = new SparseIntArray(5);
795 private UserManager mUserManager;
796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 private final class AppDeathRecipient implements IBinder.DeathRecipient {
798 final ProcessRecord mApp;
799 final int mPid;
800 final IApplicationThread mAppThread;
801
802 AppDeathRecipient(ProcessRecord app, int pid,
803 IApplicationThread thread) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800804 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 TAG, "New death recipient " + this
806 + " for thread " + thread.asBinder());
807 mApp = app;
808 mPid = pid;
809 mAppThread = thread;
810 }
811
812 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800813 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 TAG, "Death received in " + this
815 + " for thread " + mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 synchronized(ActivityManagerService.this) {
817 appDiedLocked(mApp, mPid, mAppThread);
818 }
819 }
820 }
821
822 static final int SHOW_ERROR_MSG = 1;
823 static final int SHOW_NOT_RESPONDING_MSG = 2;
824 static final int SHOW_FACTORY_ERROR_MSG = 3;
825 static final int UPDATE_CONFIGURATION_MSG = 4;
826 static final int GC_BACKGROUND_PROCESSES_MSG = 5;
827 static final int WAIT_FOR_DEBUGGER_MSG = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 static final int SERVICE_TIMEOUT_MSG = 12;
829 static final int UPDATE_TIME_ZONE = 13;
830 static final int SHOW_UID_ERROR_MSG = 14;
831 static final int IM_FEELING_LUCKY_MSG = 15;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 static final int PROC_START_TIMEOUT_MSG = 20;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700833 static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -0700834 static final int KILL_APPLICATION_MSG = 22;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800835 static final int FINALIZE_PENDING_INTENT_MSG = 23;
Dianne Hackborn860755f2010-06-03 18:47:52 -0700836 static final int POST_HEAVY_NOTIFICATION_MSG = 24;
837 static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700838 static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700839 static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700840 static final int CLEAR_DNS_CACHE = 28;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700841 static final int UPDATE_HTTP_PROXY = 29;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700842 static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30;
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700843 static final int DISPATCH_PROCESSES_CHANGED = 31;
Dianne Hackborn36f80f32011-05-31 18:26:45 -0700844 static final int DISPATCH_PROCESS_DIED = 32;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700845 static final int REPORT_MEM_USAGE = 33;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800847 static final int FIRST_ACTIVITY_STACK_MSG = 100;
848 static final int FIRST_BROADCAST_QUEUE_MSG = 200;
849 static final int FIRST_COMPAT_MODE_MSG = 300;
850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 AlertDialog mUidAlert;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700852 CompatModeDialog mCompatModeDialog;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700853 long mLastMemUsageReportTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854
855 final Handler mHandler = new Handler() {
856 //public Handler() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800857 // if (localLOGV) Slog.v(TAG, "Handler started!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 //}
859
860 public void handleMessage(Message msg) {
861 switch (msg.what) {
862 case SHOW_ERROR_MSG: {
863 HashMap data = (HashMap) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 synchronized (ActivityManagerService.this) {
865 ProcessRecord proc = (ProcessRecord)data.get("app");
866 if (proc != null && proc.crashDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800867 Slog.e(TAG, "App already has crash dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 return;
869 }
870 AppErrorResult res = (AppErrorResult) data.get("result");
Joe Onorato54a4a412011-11-02 20:50:08 -0700871 if (mShowDialogs && !mSleeping && !mShuttingDown) {
Dan Egnorb7f03672009-12-09 16:22:32 -0800872 Dialog d = new AppErrorDialog(mContext, res, proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 d.show();
874 proc.crashDialog = d;
875 } else {
876 // The device is asleep, so just pretend that the user
877 // saw a crash dialog and hit "force quit".
878 res.set(0);
879 }
880 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700881
882 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 } break;
884 case SHOW_NOT_RESPONDING_MSG: {
885 synchronized (ActivityManagerService.this) {
886 HashMap data = (HashMap) msg.obj;
887 ProcessRecord proc = (ProcessRecord)data.get("app");
888 if (proc != null && proc.anrDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800889 Slog.e(TAG, "App already has anr dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 return;
891 }
The Android Open Source Project4df24232009-03-05 14:34:35 -0800892
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700893 Intent intent = new Intent("android.intent.action.ANR");
894 if (!mProcessesReady) {
Christopher Tatef46723b2012-01-26 14:19:24 -0800895 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
896 | Intent.FLAG_RECEIVER_FOREGROUND);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700897 }
898 broadcastIntentLocked(null, null, intent,
The Android Open Source Project4df24232009-03-05 14:34:35 -0800899 null, null, 0, null, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700900 false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800901
Justin Kohbc52ca22012-03-29 15:11:44 -0700902 if (mShowDialogs) {
903 Dialog d = new AppNotRespondingDialog(ActivityManagerService.this,
Mike Lockwood69ccdbd2012-04-03 11:53:47 -0700904 mContext, proc, (ActivityRecord)data.get("activity"));
Justin Kohbc52ca22012-03-29 15:11:44 -0700905 d.show();
906 proc.anrDialog = d;
907 } else {
908 // Just kill the app if there is no dialog to be shown.
909 killAppAtUsersRequest(proc, null);
910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700912
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700913 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 } break;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700915 case SHOW_STRICT_MODE_VIOLATION_MSG: {
916 HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
917 synchronized (ActivityManagerService.this) {
918 ProcessRecord proc = (ProcessRecord) data.get("app");
919 if (proc == null) {
920 Slog.e(TAG, "App not found when showing strict mode dialog.");
921 break;
922 }
923 if (proc.crashDialog != null) {
924 Slog.e(TAG, "App already has strict mode dialog: " + proc);
925 return;
926 }
927 AppErrorResult res = (AppErrorResult) data.get("result");
Joe Onorato54a4a412011-11-02 20:50:08 -0700928 if (mShowDialogs && !mSleeping && !mShuttingDown) {
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700929 Dialog d = new StrictModeViolationDialog(mContext, res, proc);
930 d.show();
931 proc.crashDialog = d;
932 } else {
933 // The device is asleep, so just pretend that the user
934 // saw a crash dialog and hit "force quit".
935 res.set(0);
936 }
937 }
938 ensureBootCompleted();
939 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 case SHOW_FACTORY_ERROR_MSG: {
941 Dialog d = new FactoryErrorDialog(
942 mContext, msg.getData().getCharSequence("msg"));
943 d.show();
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700944 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 } break;
946 case UPDATE_CONFIGURATION_MSG: {
947 final ContentResolver resolver = mContext.getContentResolver();
948 Settings.System.putConfiguration(resolver, (Configuration)msg.obj);
949 } break;
950 case GC_BACKGROUND_PROCESSES_MSG: {
951 synchronized (ActivityManagerService.this) {
952 performAppGcsIfAppropriateLocked();
953 }
954 } break;
955 case WAIT_FOR_DEBUGGER_MSG: {
956 synchronized (ActivityManagerService.this) {
957 ProcessRecord app = (ProcessRecord)msg.obj;
958 if (msg.arg1 != 0) {
959 if (!app.waitedForDebugger) {
960 Dialog d = new AppWaitingForDebuggerDialog(
961 ActivityManagerService.this,
962 mContext, app);
963 app.waitDialog = d;
964 app.waitedForDebugger = true;
965 d.show();
966 }
967 } else {
968 if (app.waitDialog != null) {
969 app.waitDialog.dismiss();
970 app.waitDialog = null;
971 }
972 }
973 }
974 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 case SERVICE_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700976 if (mDidDexOpt) {
977 mDidDexOpt = false;
978 Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
979 nmsg.obj = msg.obj;
Dianne Hackborn599db5c2012-08-03 19:28:48 -0700980 mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700981 return;
982 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -0700983 mServices.serviceTimeout((ProcessRecord)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 } break;
985 case UPDATE_TIME_ZONE: {
986 synchronized (ActivityManagerService.this) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800987 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
988 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 if (r.thread != null) {
990 try {
991 r.thread.updateTimeZone();
992 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800993 Slog.w(TAG, "Failed to update time zone for: " + r.info.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
995 }
996 }
997 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700998 } break;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700999 case CLEAR_DNS_CACHE: {
1000 synchronized (ActivityManagerService.this) {
1001 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1002 ProcessRecord r = mLruProcesses.get(i);
1003 if (r.thread != null) {
1004 try {
1005 r.thread.clearDnsCache();
1006 } catch (RemoteException ex) {
1007 Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName);
1008 }
1009 }
1010 }
1011 }
1012 } break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001013 case UPDATE_HTTP_PROXY: {
1014 ProxyProperties proxy = (ProxyProperties)msg.obj;
1015 String host = "";
1016 String port = "";
1017 String exclList = "";
1018 if (proxy != null) {
1019 host = proxy.getHost();
1020 port = Integer.toString(proxy.getPort());
1021 exclList = proxy.getExclusionList();
1022 }
1023 synchronized (ActivityManagerService.this) {
1024 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1025 ProcessRecord r = mLruProcesses.get(i);
1026 if (r.thread != null) {
1027 try {
1028 r.thread.setHttpProxy(host, port, exclList);
1029 } catch (RemoteException ex) {
1030 Slog.w(TAG, "Failed to update http proxy for: " +
1031 r.info.processName);
1032 }
1033 }
1034 }
1035 }
1036 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 case SHOW_UID_ERROR_MSG: {
Joe Onorato54a4a412011-11-02 20:50:08 -07001038 String title = "System UIDs Inconsistent";
1039 String text = "UIDs on the system are inconsistent, you need to wipe your"
1040 + " data partition or your device will be unstable.";
1041 Log.e(TAG, title + ": " + text);
1042 if (mShowDialogs) {
1043 // XXX This is a temporary dialog, no need to localize.
1044 AlertDialog d = new BaseErrorDialog(mContext);
1045 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
1046 d.setCancelable(false);
1047 d.setTitle(title);
1048 d.setMessage(text);
1049 d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky",
1050 mHandler.obtainMessage(IM_FEELING_LUCKY_MSG));
1051 mUidAlert = d;
1052 d.show();
1053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 } break;
1055 case IM_FEELING_LUCKY_MSG: {
1056 if (mUidAlert != null) {
1057 mUidAlert.dismiss();
1058 mUidAlert = null;
1059 }
1060 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 case PROC_START_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001062 if (mDidDexOpt) {
1063 mDidDexOpt = false;
1064 Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1065 nmsg.obj = msg.obj;
1066 mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT);
1067 return;
1068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 ProcessRecord app = (ProcessRecord)msg.obj;
1070 synchronized (ActivityManagerService.this) {
1071 processStartTimedOutLocked(app);
1072 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001073 } break;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001074 case DO_PENDING_ACTIVITY_LAUNCHES_MSG: {
1075 synchronized (ActivityManagerService.this) {
1076 doPendingActivityLaunchesLocked(true);
1077 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001078 } break;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001079 case KILL_APPLICATION_MSG: {
1080 synchronized (ActivityManagerService.this) {
1081 int uid = msg.arg1;
1082 boolean restart = (msg.arg2 == 1);
1083 String pkg = (String) msg.obj;
Amith Yamasani483f3b02012-03-13 16:08:00 -07001084 forceStopPackageLocked(pkg, uid, restart, false, true, false,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001085 UserHandle.getUserId(uid));
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001086 }
1087 } break;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001088 case FINALIZE_PENDING_INTENT_MSG: {
1089 ((PendingIntentRecord)msg.obj).completeFinalize();
1090 } break;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001091 case POST_HEAVY_NOTIFICATION_MSG: {
1092 INotificationManager inm = NotificationManager.getService();
1093 if (inm == null) {
1094 return;
1095 }
1096
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001097 ActivityRecord root = (ActivityRecord)msg.obj;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001098 ProcessRecord process = root.app;
1099 if (process == null) {
1100 return;
1101 }
1102
1103 try {
1104 Context context = mContext.createPackageContext(process.info.packageName, 0);
1105 String text = mContext.getString(R.string.heavy_weight_notification,
1106 context.getApplicationInfo().loadLabel(context.getPackageManager()));
1107 Notification notification = new Notification();
1108 notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon;
1109 notification.when = 0;
1110 notification.flags = Notification.FLAG_ONGOING_EVENT;
1111 notification.tickerText = text;
1112 notification.defaults = 0; // please be quiet
1113 notification.sound = null;
1114 notification.vibrate = null;
1115 notification.setLatestEventInfo(context, text,
1116 mContext.getText(R.string.heavy_weight_notification_detail),
1117 PendingIntent.getActivity(mContext, 0, root.intent,
1118 PendingIntent.FLAG_CANCEL_CURRENT));
1119
1120 try {
1121 int[] outId = new int[1];
1122 inm.enqueueNotification("android", R.string.heavy_weight_notification,
1123 notification, outId);
1124 } catch (RuntimeException e) {
1125 Slog.w(ActivityManagerService.TAG,
1126 "Error showing notification for heavy-weight app", e);
1127 } catch (RemoteException e) {
1128 }
1129 } catch (NameNotFoundException e) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07001130 Slog.w(TAG, "Unable to create context for heavy notification", e);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001131 }
1132 } break;
1133 case CANCEL_HEAVY_NOTIFICATION_MSG: {
1134 INotificationManager inm = NotificationManager.getService();
1135 if (inm == null) {
1136 return;
1137 }
1138 try {
1139 inm.cancelNotification("android",
1140 R.string.heavy_weight_notification);
1141 } catch (RuntimeException e) {
1142 Slog.w(ActivityManagerService.TAG,
1143 "Error canceling notification for service", e);
1144 } catch (RemoteException e) {
1145 }
1146 } break;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001147 case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: {
1148 synchronized (ActivityManagerService.this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001149 checkExcessivePowerUsageLocked(true);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07001150 removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07001151 Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
1152 sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001153 }
1154 } break;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001155 case SHOW_COMPAT_MODE_DIALOG_MSG: {
1156 synchronized (ActivityManagerService.this) {
1157 ActivityRecord ar = (ActivityRecord)msg.obj;
1158 if (mCompatModeDialog != null) {
1159 if (mCompatModeDialog.mAppInfo.packageName.equals(
1160 ar.info.applicationInfo.packageName)) {
1161 return;
1162 }
1163 mCompatModeDialog.dismiss();
1164 mCompatModeDialog = null;
1165 }
Dianne Hackborn29478262011-06-07 15:44:22 -07001166 if (ar != null && false) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001167 if (mCompatModePackages.getPackageAskCompatModeLocked(
1168 ar.packageName)) {
1169 int mode = mCompatModePackages.computeCompatModeLocked(
1170 ar.info.applicationInfo);
1171 if (mode == ActivityManager.COMPAT_MODE_DISABLED
1172 || mode == ActivityManager.COMPAT_MODE_ENABLED) {
1173 mCompatModeDialog = new CompatModeDialog(
1174 ActivityManagerService.this, mContext,
1175 ar.info.applicationInfo);
1176 mCompatModeDialog.show();
1177 }
1178 }
1179 }
1180 }
Dianne Hackborn36f80f32011-05-31 18:26:45 -07001181 break;
1182 }
Dianne Hackborna93c2c12012-05-31 15:29:36 -07001183 case DISPATCH_PROCESSES_CHANGED: {
1184 dispatchProcessesChanged();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001185 break;
1186 }
1187 case DISPATCH_PROCESS_DIED: {
Jeff Sharkey287bd832011-05-28 19:36:26 -07001188 final int pid = msg.arg1;
1189 final int uid = msg.arg2;
1190 dispatchProcessDied(pid, uid);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001191 break;
1192 }
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001193 case REPORT_MEM_USAGE: {
1194 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
1195 if (!isDebuggable) {
1196 return;
1197 }
1198 synchronized (ActivityManagerService.this) {
1199 long now = SystemClock.uptimeMillis();
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001200 if (now < (mLastMemUsageReportTime+5*60*1000)) {
1201 // Don't report more than every 5 minutes to somewhat
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001202 // avoid spamming.
1203 return;
1204 }
1205 mLastMemUsageReportTime = now;
1206 }
1207 Thread thread = new Thread() {
1208 @Override public void run() {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001209 StringBuilder dropBuilder = new StringBuilder(1024);
1210 StringBuilder logBuilder = new StringBuilder(1024);
Dianne Hackborn672342c2011-11-29 11:29:02 -08001211 StringWriter oomSw = new StringWriter();
1212 PrintWriter oomPw = new PrintWriter(oomSw);
1213 StringWriter catSw = new StringWriter();
1214 PrintWriter catPw = new PrintWriter(catSw);
1215 String[] emptyArgs = new String[] { };
1216 StringBuilder tag = new StringBuilder(128);
1217 StringBuilder stack = new StringBuilder(128);
1218 tag.append("Low on memory -- ");
1219 dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw,
1220 tag, stack);
1221 dropBuilder.append(stack);
1222 dropBuilder.append('\n');
1223 dropBuilder.append('\n');
1224 String oomString = oomSw.toString();
1225 dropBuilder.append(oomString);
1226 dropBuilder.append('\n');
1227 logBuilder.append(oomString);
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001228 try {
1229 java.lang.Process proc = Runtime.getRuntime().exec(new String[] {
1230 "procrank", });
1231 final InputStreamReader converter = new InputStreamReader(
1232 proc.getInputStream());
1233 BufferedReader in = new BufferedReader(converter);
1234 String line;
1235 while (true) {
1236 line = in.readLine();
1237 if (line == null) {
1238 break;
1239 }
1240 if (line.length() > 0) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001241 logBuilder.append(line);
1242 logBuilder.append('\n');
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001243 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001244 dropBuilder.append(line);
1245 dropBuilder.append('\n');
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001246 }
1247 converter.close();
1248 } catch (IOException e) {
1249 }
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001250 synchronized (ActivityManagerService.this) {
Dianne Hackborn672342c2011-11-29 11:29:02 -08001251 catPw.println();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001252 dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001253 catPw.println();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001254 mServices.dumpServicesLocked(null, catPw, emptyArgs, 0,
1255 false, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001256 catPw.println();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001257 dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001258 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001259 dropBuilder.append(catSw.toString());
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08001260 addErrorToDropBox("lowmem", null, "system_server", null,
1261 null, tag.toString(), dropBuilder.toString(), null, null);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001262 Slog.i(TAG, logBuilder.toString());
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001263 synchronized (ActivityManagerService.this) {
1264 long now = SystemClock.uptimeMillis();
1265 if (mLastMemUsageReportTime < now) {
1266 mLastMemUsageReportTime = now;
1267 }
1268 }
1269 }
1270 };
1271 thread.start();
1272 break;
1273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
1275 }
1276 };
1277
1278 public static void setSystemProcess() {
1279 try {
1280 ActivityManagerService m = mSelf;
1281
Dianne Hackborna573f6a2012-02-09 16:12:18 -08001282 ServiceManager.addService("activity", m, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 ServiceManager.addService("meminfo", new MemBinder(m));
Chet Haase9c1e23b2011-03-24 10:51:31 -07001284 ServiceManager.addService("gfxinfo", new GraphicsBinder(m));
Jeff Brown6754ba22011-12-14 20:20:01 -08001285 ServiceManager.addService("dbinfo", new DbBinder(m));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 if (MONITOR_CPU_USAGE) {
1287 ServiceManager.addService("cpuinfo", new CpuBinder(m));
1288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 ServiceManager.addService("permission", new PermissionController(m));
1290
1291 ApplicationInfo info =
1292 mSelf.mContext.getPackageManager().getApplicationInfo(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001293 "android", STOCK_PM_FLAGS);
Mike Cleron432b7132009-09-24 15:28:29 -07001294 mSystemThread.installSystemApplicationInfo(info);
1295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 synchronized (mSelf) {
1297 ProcessRecord app = mSelf.newProcessRecordLocked(
1298 mSystemThread.getApplicationThread(), info,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001299 info.processName, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 app.persistent = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08001301 app.pid = MY_PID;
Dianne Hackborn7d608422011-08-07 16:24:18 -07001302 app.maxAdj = ProcessList.SYSTEM_ADJ;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001303 mSelf.mProcessNames.put(app.processName, app.uid, app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 synchronized (mSelf.mPidsSelfLocked) {
1305 mSelf.mPidsSelfLocked.put(app.pid, app);
1306 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001307 mSelf.updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 }
1309 } catch (PackageManager.NameNotFoundException e) {
1310 throw new RuntimeException(
1311 "Unable to find android system package", e);
1312 }
1313 }
1314
1315 public void setWindowManager(WindowManagerService wm) {
1316 mWindowManager = wm;
1317 }
1318
1319 public static final Context main(int factoryTest) {
1320 AThread thr = new AThread();
1321 thr.start();
1322
1323 synchronized (thr) {
1324 while (thr.mService == null) {
1325 try {
1326 thr.wait();
1327 } catch (InterruptedException e) {
1328 }
1329 }
1330 }
1331
1332 ActivityManagerService m = thr.mService;
1333 mSelf = m;
1334 ActivityThread at = ActivityThread.systemMain();
1335 mSystemThread = at;
1336 Context context = at.getSystemContext();
Dianne Hackborn247fe742011-01-08 17:25:57 -08001337 context.setTheme(android.R.style.Theme_Holo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 m.mContext = context;
1339 m.mFactoryTest = factoryTest;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001340 m.mMainStack = new ActivityStack(m, context, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341
1342 m.mBatteryStatsService.publish(context);
1343 m.mUsageStatsService.publish(context);
1344
1345 synchronized (thr) {
1346 thr.mReady = true;
1347 thr.notifyAll();
1348 }
1349
1350 m.startRunning(null, null, null, null);
1351
1352 return context;
1353 }
1354
1355 public static ActivityManagerService self() {
1356 return mSelf;
1357 }
1358
1359 static class AThread extends Thread {
1360 ActivityManagerService mService;
1361 boolean mReady = false;
1362
1363 public AThread() {
1364 super("ActivityManager");
1365 }
1366
1367 public void run() {
1368 Looper.prepare();
1369
1370 android.os.Process.setThreadPriority(
1371 android.os.Process.THREAD_PRIORITY_FOREGROUND);
Christopher Tate160edb32010-06-30 17:46:30 -07001372 android.os.Process.setCanSelfBackground(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373
1374 ActivityManagerService m = new ActivityManagerService();
1375
1376 synchronized (this) {
1377 mService = m;
1378 notifyAll();
1379 }
1380
1381 synchronized (this) {
1382 while (!mReady) {
1383 try {
1384 wait();
1385 } catch (InterruptedException e) {
1386 }
1387 }
1388 }
1389
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001390 // For debug builds, log event loop stalls to dropbox for analysis.
1391 if (StrictMode.conditionallyEnableDebugLogging()) {
1392 Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper");
1393 }
1394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 Looper.loop();
1396 }
1397 }
1398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 static class MemBinder extends Binder {
1400 ActivityManagerService mActivityManagerService;
1401 MemBinder(ActivityManagerService activityManagerService) {
1402 mActivityManagerService = activityManagerService;
1403 }
1404
1405 @Override
1406 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001407 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1408 != PackageManager.PERMISSION_GRANTED) {
1409 pw.println("Permission Denial: can't dump meminfo from from pid="
1410 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1411 + " without permission " + android.Manifest.permission.DUMP);
1412 return;
1413 }
1414
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08001415 mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args,
Dianne Hackborn672342c2011-11-29 11:29:02 -08001416 false, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 }
1418 }
1419
Chet Haase9c1e23b2011-03-24 10:51:31 -07001420 static class GraphicsBinder extends Binder {
1421 ActivityManagerService mActivityManagerService;
1422 GraphicsBinder(ActivityManagerService activityManagerService) {
1423 mActivityManagerService = activityManagerService;
1424 }
1425
1426 @Override
1427 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001428 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1429 != PackageManager.PERMISSION_GRANTED) {
1430 pw.println("Permission Denial: can't dump gfxinfo from from pid="
1431 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1432 + " without permission " + android.Manifest.permission.DUMP);
1433 return;
1434 }
1435
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001436 mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args);
Chet Haase9c1e23b2011-03-24 10:51:31 -07001437 }
1438 }
1439
Jeff Brown6754ba22011-12-14 20:20:01 -08001440 static class DbBinder extends Binder {
1441 ActivityManagerService mActivityManagerService;
1442 DbBinder(ActivityManagerService activityManagerService) {
1443 mActivityManagerService = activityManagerService;
1444 }
1445
1446 @Override
1447 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1448 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1449 != PackageManager.PERMISSION_GRANTED) {
1450 pw.println("Permission Denial: can't dump dbinfo from from pid="
1451 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1452 + " without permission " + android.Manifest.permission.DUMP);
1453 return;
1454 }
1455
1456 mActivityManagerService.dumpDbInfo(fd, pw, args);
1457 }
1458 }
1459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 static class CpuBinder extends Binder {
1461 ActivityManagerService mActivityManagerService;
1462 CpuBinder(ActivityManagerService activityManagerService) {
1463 mActivityManagerService = activityManagerService;
1464 }
1465
1466 @Override
1467 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001468 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1469 != PackageManager.PERMISSION_GRANTED) {
1470 pw.println("Permission Denial: can't dump cpuinfo from from pid="
1471 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1472 + " without permission " + android.Manifest.permission.DUMP);
1473 return;
1474 }
1475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 synchronized (mActivityManagerService.mProcessStatsThread) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07001477 pw.print(mActivityManagerService.mProcessStats.printCurrentLoad());
1478 pw.print(mActivityManagerService.mProcessStats.printCurrentState(
1479 SystemClock.uptimeMillis()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481 }
1482 }
1483
1484 private ActivityManagerService() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001485 Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -07001486
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001487 mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT);
1488 mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT);
1489 mBroadcastQueues[0] = mFgBroadcastQueue;
1490 mBroadcastQueues[1] = mBgBroadcastQueue;
1491
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001492 mServices = new ActiveServices(this);
1493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 File dataDir = Environment.getDataDirectory();
1495 File systemDir = new File(dataDir, "system");
1496 systemDir.mkdirs();
1497 mBatteryStatsService = new BatteryStatsService(new File(
1498 systemDir, "batterystats.bin").toString());
1499 mBatteryStatsService.getActiveStatistics().readLocked();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001500 mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
Dianne Hackborn287952c2010-09-22 22:34:31 -07001501 mOnBattery = DEBUG_POWER ? true
1502 : mBatteryStatsService.getActiveStatistics().getIsOnBattery();
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001503 mBatteryStatsService.getActiveStatistics().setCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001505 mUsageStatsService = new UsageStatsService(new File(
Dianne Hackborn6447ca32009-04-07 19:50:08 -07001506 systemDir, "usagestats").toString());
Mike Lockwood3a74bd32011-08-12 13:55:22 -07001507 mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508
Jack Palevichb90d28c2009-07-22 15:35:24 -07001509 GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
1510 ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
1511
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001512 mConfiguration.setToDefaults();
1513 mConfiguration.locale = Locale.getDefault();
Dianne Hackborn813075a62011-11-14 17:45:19 -08001514 mConfigurationSeq = mConfiguration.seq = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 mProcessStats.init();
1516
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07001517 mCompatModePackages = new CompatModePackages(this, systemDir);
1518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 // Add ourself to the Watchdog monitors.
1520 Watchdog.getInstance().addMonitor(this);
1521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 mProcessStatsThread = new Thread("ProcessStats") {
1523 public void run() {
1524 while (true) {
1525 try {
1526 try {
1527 synchronized(this) {
1528 final long now = SystemClock.uptimeMillis();
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001529 long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001531 //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 // + ", write delay=" + nextWriteDelay);
1533 if (nextWriteDelay < nextCpuDelay) {
1534 nextCpuDelay = nextWriteDelay;
1535 }
1536 if (nextCpuDelay > 0) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001537 mProcessStatsMutexFree.set(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 this.wait(nextCpuDelay);
1539 }
1540 }
1541 } catch (InterruptedException e) {
1542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 updateCpuStatsNow();
1544 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001545 Slog.e(TAG, "Unexpected exception collecting process stats", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 }
1547 }
1548 }
1549 };
1550 mProcessStatsThread.start();
1551 }
1552
1553 @Override
1554 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1555 throws RemoteException {
Dianne Hackborna53de062012-05-08 18:53:51 -07001556 if (code == SYSPROPS_TRANSACTION) {
1557 // We need to tell all apps about the system property change.
1558 ArrayList<IBinder> procs = new ArrayList<IBinder>();
1559 synchronized(this) {
1560 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
1561 final int NA = apps.size();
1562 for (int ia=0; ia<NA; ia++) {
1563 ProcessRecord app = apps.valueAt(ia);
1564 if (app.thread != null) {
1565 procs.add(app.thread.asBinder());
1566 }
1567 }
1568 }
1569 }
1570
1571 int N = procs.size();
1572 for (int i=0; i<N; i++) {
1573 Parcel data2 = Parcel.obtain();
1574 try {
1575 procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0);
1576 } catch (RemoteException e) {
1577 }
1578 data2.recycle();
1579 }
1580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 try {
1582 return super.onTransact(code, data, reply, flags);
1583 } catch (RuntimeException e) {
1584 // The activity manager only throws security exceptions, so let's
1585 // log all others.
1586 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001587 Slog.e(TAG, "Activity Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
1589 throw e;
1590 }
1591 }
1592
1593 void updateCpuStats() {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001594 final long now = SystemClock.uptimeMillis();
1595 if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) {
1596 return;
1597 }
1598 if (mProcessStatsMutexFree.compareAndSet(true, false)) {
1599 synchronized (mProcessStatsThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 mProcessStatsThread.notify();
1601 }
1602 }
1603 }
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 void updateCpuStatsNow() {
1606 synchronized (mProcessStatsThread) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001607 mProcessStatsMutexFree.set(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 final long now = SystemClock.uptimeMillis();
1609 boolean haveNewCpuStats = false;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 if (MONITOR_CPU_USAGE &&
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001612 mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) {
1613 mLastCpuTime.set(now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 haveNewCpuStats = true;
1615 mProcessStats.update();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001616 //Slog.i(TAG, mProcessStats.printCurrentState());
1617 //Slog.i(TAG, "Total CPU usage: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 // + mProcessStats.getTotalCpuPercent() + "%");
1619
Joe Onorato8a9b2202010-02-26 18:56:32 -08001620 // Slog the cpu usage if the property is set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 if ("true".equals(SystemProperties.get("events.cpu"))) {
1622 int user = mProcessStats.getLastUserTime();
1623 int system = mProcessStats.getLastSystemTime();
1624 int iowait = mProcessStats.getLastIoWaitTime();
1625 int irq = mProcessStats.getLastIrqTime();
1626 int softIrq = mProcessStats.getLastSoftIrqTime();
1627 int idle = mProcessStats.getLastIdleTime();
1628
1629 int total = user + system + iowait + irq + softIrq + idle;
1630 if (total == 0) total = 1;
1631
Doug Zongker2bec3d42009-12-04 12:52:44 -08001632 EventLog.writeEvent(EventLogTags.CPU,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 ((user+system+iowait+irq+softIrq) * 100) / total,
1634 (user * 100) / total,
1635 (system * 100) / total,
1636 (iowait * 100) / total,
1637 (irq * 100) / total,
1638 (softIrq * 100) / total);
1639 }
1640 }
1641
Amith Yamasanie43530a2009-08-21 13:11:37 -07001642 long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes();
Amith Yamasani819f9282009-06-24 23:18:15 -07001643 final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001644 synchronized(bstats) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 synchronized(mPidsSelfLocked) {
1646 if (haveNewCpuStats) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001647 if (mOnBattery) {
1648 int perc = bstats.startAddingCpuLocked();
1649 int totalUTime = 0;
1650 int totalSTime = 0;
Dianne Hackborn287952c2010-09-22 22:34:31 -07001651 final int N = mProcessStats.countStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001653 ProcessStats.Stats st = mProcessStats.getStats(i);
1654 if (!st.working) {
1655 continue;
1656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 ProcessRecord pr = mPidsSelfLocked.get(st.pid);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001658 int otherUTime = (st.rel_utime*perc)/100;
1659 int otherSTime = (st.rel_stime*perc)/100;
1660 totalUTime += otherUTime;
1661 totalSTime += otherSTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 if (pr != null) {
1663 BatteryStatsImpl.Uid.Proc ps = pr.batteryStats;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001664 ps.addCpuTimeLocked(st.rel_utime-otherUTime,
1665 st.rel_stime-otherSTime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001666 ps.addSpeedStepTimes(cpuSpeedTimes);
Dianne Hackborn287952c2010-09-22 22:34:31 -07001667 pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10;
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001668 } else {
1669 BatteryStatsImpl.Uid.Proc ps =
Amith Yamasani819f9282009-06-24 23:18:15 -07001670 bstats.getProcessStatsLocked(st.name, st.pid);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001671 if (ps != null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001672 ps.addCpuTimeLocked(st.rel_utime-otherUTime,
1673 st.rel_stime-otherSTime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001674 ps.addSpeedStepTimes(cpuSpeedTimes);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
1677 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001678 bstats.finishAddingCpuLocked(perc, totalUTime,
1679 totalSTime, cpuSpeedTimes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681 }
1682 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (mLastWriteTime < (now-BATTERY_STATS_TIME)) {
1685 mLastWriteTime = now;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001686 mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
1688 }
1689 }
1690 }
1691
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001692 @Override
1693 public void batteryNeedsCpuUpdate() {
1694 updateCpuStatsNow();
1695 }
1696
1697 @Override
1698 public void batteryPowerChanged(boolean onBattery) {
1699 // When plugging in, update the CPU stats first before changing
1700 // the plug state.
1701 updateCpuStatsNow();
1702 synchronized (this) {
1703 synchronized(mPidsSelfLocked) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001704 mOnBattery = DEBUG_POWER ? true : onBattery;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001705 }
1706 }
1707 }
1708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 /**
1710 * Initialize the application bind args. These are passed to each
1711 * process when the bindApplication() IPC is sent to the process. They're
1712 * lazily setup to make sure the services are running when they're asked for.
1713 */
1714 private HashMap<String, IBinder> getCommonServicesLocked() {
1715 if (mAppBindArgs == null) {
1716 mAppBindArgs = new HashMap<String, IBinder>();
1717
1718 // Setup the application init args
1719 mAppBindArgs.put("package", ServiceManager.getService("package"));
1720 mAppBindArgs.put("window", ServiceManager.getService("window"));
1721 mAppBindArgs.put(Context.ALARM_SERVICE,
1722 ServiceManager.getService(Context.ALARM_SERVICE));
1723 }
1724 return mAppBindArgs;
1725 }
1726
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 final void setFocusedActivityLocked(ActivityRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 if (mFocusedActivity != r) {
1729 mFocusedActivity = r;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001730 if (r != null) {
1731 mWindowManager.setFocusedApp(r.appToken, true);
1732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 }
1734 }
1735
Dianne Hackborn906497c2010-05-10 15:57:38 -07001736 private final void updateLruProcessInternalLocked(ProcessRecord app,
1737 boolean oomAdj, boolean updateActivityTime, int bestPos) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 // put it on the LRU to keep track of when it should be exited.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001739 int lrui = mLruProcesses.indexOf(app);
1740 if (lrui >= 0) mLruProcesses.remove(lrui);
1741
1742 int i = mLruProcesses.size()-1;
1743 int skipTop = 0;
1744
Dianne Hackborn906497c2010-05-10 15:57:38 -07001745 app.lruSeq = mLruSeq;
1746
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001747 // compute the new weight for this process.
1748 if (updateActivityTime) {
1749 app.lastActivityTime = SystemClock.uptimeMillis();
1750 }
1751 if (app.activities.size() > 0) {
1752 // If this process has activities, we more strongly want to keep
1753 // it around.
1754 app.lruWeight = app.lastActivityTime;
1755 } else if (app.pubProviders.size() > 0) {
1756 // If this process contains content providers, we want to keep
1757 // it a little more strongly.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001758 app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001759 // Also don't let it kick out the first few "real" hidden processes.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001760 skipTop = ProcessList.MIN_HIDDEN_APPS;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001761 } else {
1762 // If this process doesn't have activities, we less strongly
1763 // want to keep it around, and generally want to avoid getting
1764 // in front of any very recently used activities.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001765 app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001766 // Also don't let it kick out the first few "real" hidden processes.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001767 skipTop = ProcessList.MIN_HIDDEN_APPS;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001768 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001769
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001770 while (i >= 0) {
1771 ProcessRecord p = mLruProcesses.get(i);
1772 // If this app shouldn't be in front of the first N background
1773 // apps, then skip over that many that are currently hidden.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001774 if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001775 skipTop--;
1776 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001777 if (p.lruWeight <= app.lruWeight || i < bestPos) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001778 mLruProcesses.add(i+1, app);
1779 break;
1780 }
1781 i--;
1782 }
1783 if (i < 0) {
1784 mLruProcesses.add(0, app);
1785 }
1786
Dianne Hackborn906497c2010-05-10 15:57:38 -07001787 // If the app is currently using a content provider or service,
1788 // bump those processes as well.
1789 if (app.connections.size() > 0) {
1790 for (ConnectionRecord cr : app.connections) {
1791 if (cr.binding != null && cr.binding.service != null
1792 && cr.binding.service.app != null
1793 && cr.binding.service.app.lruSeq != mLruSeq) {
Björn Davidsson90f9e312010-11-18 08:26:27 +01001794 updateLruProcessInternalLocked(cr.binding.service.app, false,
Dianne Hackborn906497c2010-05-10 15:57:38 -07001795 updateActivityTime, i+1);
1796 }
1797 }
1798 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001799 for (int j=app.conProviders.size()-1; j>=0; j--) {
1800 ContentProviderRecord cpr = app.conProviders.get(j).provider;
1801 if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) {
Jean-Baptiste Querub8c64052012-06-11 11:01:19 -07001802 updateLruProcessInternalLocked(cpr.proc, false,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001803 updateActivityTime, i+1);
Dianne Hackborn906497c2010-05-10 15:57:38 -07001804 }
1805 }
1806
Joe Onorato8a9b2202010-02-26 18:56:32 -08001807 //Slog.i(TAG, "Putting proc to front: " + app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 if (oomAdj) {
1809 updateOomAdjLocked();
1810 }
1811 }
1812
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001813 final void updateLruProcessLocked(ProcessRecord app,
Dianne Hackborn906497c2010-05-10 15:57:38 -07001814 boolean oomAdj, boolean updateActivityTime) {
1815 mLruSeq++;
1816 updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0);
1817 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001818
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001819 final ProcessRecord getProcessRecordLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 String processName, int uid) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07001821 if (uid == Process.SYSTEM_UID) {
Amith Yamasani0184ce92012-03-28 22:41:41 -07001822 // The system gets to run in any process. If there are multiple
1823 // processes with the same uid, just pick the first (this
1824 // should never happen).
1825 SparseArray<ProcessRecord> procs = mProcessNames.getMap().get(
1826 processName);
Amith Yamasania4a54e22012-04-16 15:44:19 -07001827 if (procs == null) return null;
1828 final int N = procs.size();
1829 for (int i = 0; i < N; i++) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001830 if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i);
Amith Yamasania4a54e22012-04-16 15:44:19 -07001831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833 ProcessRecord proc = mProcessNames.get(processName, uid);
1834 return proc;
1835 }
1836
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837 void ensurePackageDexOpt(String packageName) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001838 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001839 try {
1840 if (pm.performDexOpt(packageName)) {
1841 mDidDexOpt = true;
1842 }
1843 } catch (RemoteException e) {
1844 }
1845 }
1846
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001847 boolean isNextTransitionForward() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 int transit = mWindowManager.getPendingAppTransition();
1849 return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
1850 || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
1851 || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT;
1852 }
1853
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854 final ProcessRecord startProcessLocked(String processName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 ApplicationInfo info, boolean knownToBeDead, int intentFlags,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001856 String hostingType, ComponentName hostingName, boolean allowWhileBooting,
1857 boolean isolated) {
1858 ProcessRecord app;
1859 if (!isolated) {
1860 app = getProcessRecordLocked(processName, info.uid);
1861 } else {
1862 // If this is an isolated process, it can't re-use an existing process.
1863 app = null;
1864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 // We don't have to do anything more if:
1866 // (1) There is an existing application record; and
1867 // (2) The caller doesn't think it is dead, OR there is no thread
1868 // object attached to it so we know it couldn't have crashed; and
1869 // (3) There is a pid assigned to it, so it is either starting or
1870 // already running.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001871 if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 + " app=" + app + " knownToBeDead=" + knownToBeDead
1873 + " thread=" + (app != null ? app.thread : null)
1874 + " pid=" + (app != null ? app.pid : -1));
Magnus Edlund7bb25812010-02-24 15:45:06 +01001875 if (app != null && app.pid > 0) {
1876 if (!knownToBeDead || app.thread == null) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001877 // We already have the app running, or are waiting for it to
1878 // come up (we have a pid but not yet its thread), so keep it.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001879 if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001880 // If this is a new package in the process, add the package to the list
1881 app.addPackage(info.packageName);
Magnus Edlund7bb25812010-02-24 15:45:06 +01001882 return app;
1883 } else {
1884 // An application record is attached to a previous process,
1885 // clean it up now.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001886 if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app);
Dianne Hackborn130b0d22011-07-26 22:07:48 -07001887 handleAppDiedLocked(app, true, true);
Magnus Edlund7bb25812010-02-24 15:45:06 +01001888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01001890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 String hostingNameStr = hostingName != null
1892 ? hostingName.flattenToShortString() : null;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001893
1894 if (!isolated) {
1895 if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) {
1896 // If we are in the background, then check to see if this process
1897 // is bad. If so, we will just silently fail.
1898 if (mBadProcesses.get(info.processName, info.uid) != null) {
1899 if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid
1900 + "/" + info.processName);
1901 return null;
1902 }
1903 } else {
1904 // When the user is explicitly starting a process, then clear its
1905 // crash count so that we won't make it bad until they see at
1906 // least one crash dialog again, and make the process good again
1907 // if it had been bad.
1908 if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001909 + "/" + info.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001910 mProcessCrashTimes.remove(info.processName, info.uid);
1911 if (mBadProcesses.get(info.processName, info.uid) != null) {
1912 EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid,
1913 info.processName);
1914 mBadProcesses.remove(info.processName, info.uid);
1915 if (app != null) {
1916 app.bad = false;
1917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 }
1919 }
1920 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 if (app == null) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001923 app = newProcessRecordLocked(null, info, processName, isolated);
1924 if (app == null) {
1925 Slog.w(TAG, "Failed making new process record for "
1926 + processName + "/" + info.uid + " isolated=" + isolated);
1927 return null;
1928 }
1929 mProcessNames.put(processName, app.uid, app);
1930 if (isolated) {
1931 mIsolatedProcesses.put(app.uid, app);
1932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 } else {
1934 // If this is a new package in the process, add the package to the list
1935 app.addPackage(info.packageName);
1936 }
1937
1938 // If the system is not ready yet, then hold off on starting this
1939 // process until it is.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001940 if (!mProcessesReady
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001941 && !isAllowedWhileBooting(info)
1942 && !allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 if (!mProcessesOnHold.contains(app)) {
1944 mProcessesOnHold.add(app);
1945 }
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001946 if (DEBUG_PROCESSES) Slog.v(TAG, "System not ready, putting on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 return app;
1948 }
1949
1950 startProcessLocked(app, hostingType, hostingNameStr);
1951 return (app.pid != 0) ? app : null;
1952 }
1953
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001954 boolean isAllowedWhileBooting(ApplicationInfo ai) {
1955 return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0;
1956 }
1957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 private final void startProcessLocked(ProcessRecord app,
1959 String hostingType, String hostingNameStr) {
1960 if (app.pid > 0 && app.pid != MY_PID) {
1961 synchronized (mPidsSelfLocked) {
1962 mPidsSelfLocked.remove(app.pid);
1963 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
1964 }
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07001965 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
1967
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001968 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
1969 "startProcessLocked removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 mProcessesOnHold.remove(app);
1971
1972 updateCpuStats();
1973
1974 System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1);
1975 mProcDeaths[0] = 0;
1976
1977 try {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001978 int uid = app.uid;
Amith Yamasani742a6712011-05-04 14:49:28 -07001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 int[] gids = null;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07001981 int mountExternal = Zygote.MOUNT_EXTERNAL_NONE;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001982 if (!app.isolated) {
1983 try {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07001984 final PackageManager pm = mContext.getPackageManager();
1985 gids = pm.getPackageGids(app.info.packageName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001986 } catch (PackageManager.NameNotFoundException e) {
1987 Slog.w(TAG, "Unable to retrieve gids", e);
1988 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -07001989
1990 if (Environment.isExternalStorageEmulated()) {
1991 mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER;
1992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 }
1994 if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) {
1995 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
1996 && mTopComponent != null
1997 && app.processName.equals(mTopComponent.getPackageName())) {
1998 uid = 0;
1999 }
2000 if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL
2001 && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) {
2002 uid = 0;
2003 }
2004 }
2005 int debugFlags = 0;
2006 if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
2007 debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;
Elliott Hughesfa36aee2011-06-17 14:39:41 -07002008 // Also turn on CheckJNI for debuggable apps. It's quite
2009 // awkward to turn on otherwise.
2010 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
Ben Cheng6c0afff2010-02-14 16:18:56 -08002012 // Run the app in safe mode if its manifest requests so or the
2013 // system is booted in safe mode.
2014 if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||
2015 Zygote.systemInSafeMode == true) {
Ben Cheng23085b72010-02-08 16:06:32 -08002016 debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
2017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 if ("1".equals(SystemProperties.get("debug.checkjni"))) {
2019 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
2020 }
Elliott Hughesae07ecf2011-07-06 17:33:27 -07002021 if ("1".equals(SystemProperties.get("debug.jni.logging"))) {
2022 debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING;
2023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 if ("1".equals(SystemProperties.get("debug.assert"))) {
2025 debugFlags |= Zygote.DEBUG_ENABLE_ASSERT;
2026 }
Jeff Brown3f9dd282011-07-08 20:02:19 -07002027
2028 // Start the process. It will either succeed and return a result containing
2029 // the PID of the new process, or else throw a RuntimeException.
2030 Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread",
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07002031 app.processName, uid, uid, gids, debugFlags, mountExternal,
Stephen Smalley83d9eda2012-01-13 08:34:17 -05002032 app.info.targetSdkVersion, null, null);
Jeff Brown3f9dd282011-07-08 20:02:19 -07002033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 BatteryStatsImpl bs = app.batteryStats.getBatteryStats();
2035 synchronized (bs) {
2036 if (bs.isOnBattery()) {
2037 app.batteryStats.incStartsLocked();
2038 }
2039 }
2040
Jeff Brown3f9dd282011-07-08 20:02:19 -07002041 EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 app.processName, hostingType,
2043 hostingNameStr != null ? hostingNameStr : "");
2044
2045 if (app.persistent) {
Jeff Brown3f9dd282011-07-08 20:02:19 -07002046 Watchdog.getInstance().processStarted(app.processName, startResult.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002049 StringBuilder buf = mStringBuilder;
2050 buf.setLength(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 buf.append("Start proc ");
2052 buf.append(app.processName);
2053 buf.append(" for ");
2054 buf.append(hostingType);
2055 if (hostingNameStr != null) {
2056 buf.append(" ");
2057 buf.append(hostingNameStr);
2058 }
2059 buf.append(": pid=");
Jeff Brown3f9dd282011-07-08 20:02:19 -07002060 buf.append(startResult.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 buf.append(" uid=");
2062 buf.append(uid);
2063 buf.append(" gids={");
2064 if (gids != null) {
2065 for (int gi=0; gi<gids.length; gi++) {
2066 if (gi != 0) buf.append(", ");
2067 buf.append(gids[gi]);
2068
2069 }
2070 }
2071 buf.append("}");
Joe Onorato8a9b2202010-02-26 18:56:32 -08002072 Slog.i(TAG, buf.toString());
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07002073 app.setPid(startResult.pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07002074 app.usingWrapper = startResult.usingWrapper;
2075 app.removed = false;
2076 synchronized (mPidsSelfLocked) {
2077 this.mPidsSelfLocked.put(startResult.pid, app);
2078 Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
2079 msg.obj = app;
2080 mHandler.sendMessageDelayed(msg, startResult.usingWrapper
2081 ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083 } catch (RuntimeException e) {
2084 // XXX do better error recovery.
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07002085 app.setPid(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002086 Slog.e(TAG, "Failure starting process " + app.processName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
2088 }
2089
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 if (resumed) {
2092 mUsageStatsService.noteResumeComponent(resumedComponent.realActivity);
2093 } else {
2094 mUsageStatsService.notePauseComponent(resumedComponent.realActivity);
2095 }
2096 }
2097
Amith Yamasani742a6712011-05-04 14:49:28 -07002098 boolean startHomeActivityLocked(int userId) {
Mike Lockwooda8f767a2011-08-31 14:32:37 -04002099 if (mHeadless) {
2100 // Added because none of the other calls to ensureBootCompleted seem to fire
2101 // when running headless.
2102 ensureBootCompleted();
2103 return false;
2104 }
2105
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002106 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
2107 && mTopAction == null) {
2108 // We are running in factory test mode, but unable to find
2109 // the factory test app, so just sit around displaying the
2110 // error message and don't try to start anything.
2111 return false;
2112 }
2113 Intent intent = new Intent(
2114 mTopAction,
2115 mTopData != null ? Uri.parse(mTopData) : null);
2116 intent.setComponent(mTopComponent);
2117 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
2118 intent.addCategory(Intent.CATEGORY_HOME);
2119 }
2120 ActivityInfo aInfo =
2121 intent.resolveActivityInfo(mContext.getPackageManager(),
2122 STOCK_PM_FLAGS);
2123 if (aInfo != null) {
2124 intent.setComponent(new ComponentName(
2125 aInfo.applicationInfo.packageName, aInfo.name));
2126 // Don't do this if the home app is currently being
2127 // instrumented.
Amith Yamasani742a6712011-05-04 14:49:28 -07002128 aInfo = new ActivityInfo(aInfo);
2129 aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002130 ProcessRecord app = getProcessRecordLocked(aInfo.processName,
2131 aInfo.applicationInfo.uid);
2132 if (app == null || app.instrumentationClass == null) {
2133 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002134 mMainStack.startActivityLocked(null, intent, null, aInfo,
2135 null, null, 0, 0, 0, 0, null, false, null);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002136 }
2137 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002138
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002139 return true;
2140 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002141
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002142 /**
2143 * Starts the "new version setup screen" if appropriate.
2144 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145 void startSetupActivityLocked() {
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002146 // Only do this once per boot.
2147 if (mCheckedForSetup) {
2148 return;
2149 }
2150
2151 // We will show this screen if the current one is a different
2152 // version than the last one shown, and we are not running in
2153 // low-level factory test mode.
2154 final ContentResolver resolver = mContext.getContentResolver();
2155 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL &&
2156 Settings.Secure.getInt(resolver,
2157 Settings.Secure.DEVICE_PROVISIONED, 0) != 0) {
2158 mCheckedForSetup = true;
2159
2160 // See if we should be showing the platform update setup UI.
2161 Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP);
2162 List<ResolveInfo> ris = mSelf.mContext.getPackageManager()
2163 .queryIntentActivities(intent, PackageManager.GET_META_DATA);
2164
2165 // We don't allow third party apps to replace this.
2166 ResolveInfo ri = null;
2167 for (int i=0; ris != null && i<ris.size(); i++) {
2168 if ((ris.get(i).activityInfo.applicationInfo.flags
2169 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2170 ri = ris.get(i);
2171 break;
2172 }
2173 }
2174
2175 if (ri != null) {
2176 String vers = ri.activityInfo.metaData != null
2177 ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION)
2178 : null;
2179 if (vers == null && ri.activityInfo.applicationInfo.metaData != null) {
2180 vers = ri.activityInfo.applicationInfo.metaData.getString(
2181 Intent.METADATA_SETUP_VERSION);
2182 }
2183 String lastVers = Settings.Secure.getString(
2184 resolver, Settings.Secure.LAST_SETUP_SHOWN);
2185 if (vers != null && !vers.equals(lastVers)) {
2186 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2187 intent.setComponent(new ComponentName(
2188 ri.activityInfo.packageName, ri.activityInfo.name));
Dianne Hackborna4972e92012-03-14 10:38:05 -07002189 mMainStack.startActivityLocked(null, intent, null, ri.activityInfo,
2190 null, null, 0, 0, 0, 0, null, false, null);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002191 }
2192 }
2193 }
2194 }
2195
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002196 CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002197 return mCompatModePackages.compatibilityInfoForPackageLocked(ai);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002198 }
2199
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002200 void enforceNotIsolatedCaller(String caller) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002201 if (UserHandle.isIsolated(Binder.getCallingUid())) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002202 throw new SecurityException("Isolated process not allowed to call " + caller);
2203 }
2204 }
2205
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002206 public int getFrontActivityScreenCompatMode() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002207 enforceNotIsolatedCaller("getFrontActivityScreenCompatMode");
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002208 synchronized (this) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002209 return mCompatModePackages.getFrontActivityScreenCompatModeLocked();
2210 }
2211 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002212
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002213 public void setFrontActivityScreenCompatMode(int mode) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002214 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2215 "setFrontActivityScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002216 synchronized (this) {
2217 mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode);
2218 }
2219 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002220
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002221 public int getPackageScreenCompatMode(String packageName) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002222 enforceNotIsolatedCaller("getPackageScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002223 synchronized (this) {
2224 return mCompatModePackages.getPackageScreenCompatModeLocked(packageName);
2225 }
2226 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002227
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002228 public void setPackageScreenCompatMode(String packageName, int mode) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002229 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2230 "setPackageScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002231 synchronized (this) {
2232 mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002233 }
2234 }
2235
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002236 public boolean getPackageAskScreenCompat(String packageName) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002237 enforceNotIsolatedCaller("getPackageAskScreenCompat");
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002238 synchronized (this) {
2239 return mCompatModePackages.getPackageAskCompatModeLocked(packageName);
2240 }
2241 }
2242
2243 public void setPackageAskScreenCompat(String packageName, boolean ask) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002244 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2245 "setPackageAskScreenCompat");
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002246 synchronized (this) {
2247 mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask);
2248 }
2249 }
2250
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 void reportResumedActivityLocked(ActivityRecord r) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002252 //Slog.i(TAG, "**** REPORT RESUME: " + r);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002253 updateUsageStats(r, true);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255
Dianne Hackborna93c2c12012-05-31 15:29:36 -07002256 private void dispatchProcessesChanged() {
2257 int N;
2258 synchronized (this) {
2259 N = mPendingProcessChanges.size();
2260 if (mActiveProcessChanges.length < N) {
2261 mActiveProcessChanges = new ProcessChangeItem[N];
2262 }
2263 mPendingProcessChanges.toArray(mActiveProcessChanges);
2264 mAvailProcessChanges.addAll(mPendingProcessChanges);
2265 mPendingProcessChanges.clear();
2266 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes");
2267 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07002268 int i = mProcessObservers.beginBroadcast();
2269 while (i > 0) {
2270 i--;
2271 final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
2272 if (observer != null) {
2273 try {
Dianne Hackborna93c2c12012-05-31 15:29:36 -07002274 for (int j=0; j<N; j++) {
2275 ProcessChangeItem item = mActiveProcessChanges[j];
2276 if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) {
2277 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid="
2278 + item.pid + " uid=" + item.uid + ": "
2279 + item.foregroundActivities);
2280 observer.onForegroundActivitiesChanged(item.pid, item.uid,
2281 item.foregroundActivities);
2282 }
2283 if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) {
2284 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid="
2285 + item.pid + " uid=" + item.uid + ": " + item.importance);
2286 observer.onImportanceChanged(item.pid, item.uid,
2287 item.importance);
2288 }
2289 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07002290 } catch (RemoteException e) {
2291 }
2292 }
2293 }
2294 mProcessObservers.finishBroadcast();
2295 }
2296
2297 private void dispatchProcessDied(int pid, int uid) {
2298 int i = mProcessObservers.beginBroadcast();
2299 while (i > 0) {
2300 i--;
2301 final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
2302 if (observer != null) {
2303 try {
2304 observer.onProcessDied(pid, uid);
2305 } catch (RemoteException e) {
2306 }
2307 }
2308 }
2309 mProcessObservers.finishBroadcast();
2310 }
2311
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002312 final void doPendingActivityLaunchesLocked(boolean doResume) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002313 final int N = mPendingActivityLaunches.size();
2314 if (N <= 0) {
2315 return;
2316 }
2317 for (int i=0; i<N; i++) {
2318 PendingActivityLaunch pal = mPendingActivityLaunches.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002320 pal.startFlags, doResume && i == (N-1), null);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002321 }
2322 mPendingActivityLaunches.clear();
2323 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002324
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002325 public final int startActivity(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002326 Intent intent, String resolvedType, IBinder resultTo,
2327 String resultWho, int requestCode, int startFlags,
2328 String profileFile, ParcelFileDescriptor profileFd, Bundle options) {
Amith Yamasani82644082012-08-03 13:09:11 -07002329 return startActivityAsUser(caller, intent, resolvedType, resultTo, resultWho, requestCode,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002330 startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId());
Amith Yamasani82644082012-08-03 13:09:11 -07002331 }
2332
2333 public final int startActivityAsUser(IApplicationThread caller,
2334 Intent intent, String resolvedType, IBinder resultTo,
2335 String resultWho, int requestCode, int startFlags,
2336 String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002337 enforceNotIsolatedCaller("startActivity");
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002338 if (userId != UserHandle.getCallingUserId()) {
Amith Yamasani82644082012-08-03 13:09:11 -07002339 // Requesting a different user, make sure that they have the permission
2340 if (checkComponentPermission(
2341 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2342 Binder.getCallingPid(), Binder.getCallingUid(), -1, true)
2343 == PackageManager.PERMISSION_GRANTED) {
2344 // Translate to the current user id, if caller wasn't aware
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002345 if (userId == UserHandle.USER_CURRENT) {
Amith Yamasani82644082012-08-03 13:09:11 -07002346 userId = mCurrentUserId;
2347 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002348 } else {
Amith Yamasani82644082012-08-03 13:09:11 -07002349 String msg = "Permission Denial: "
2350 + "Request to startActivity as user " + userId
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002351 + " but is calling from user " + UserHandle.getCallingUserId()
Amith Yamasani82644082012-08-03 13:09:11 -07002352 + "; this requires "
2353 + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
2354 Slog.w(TAG, msg);
2355 throw new SecurityException(msg);
2356 }
2357 } else {
2358 if (intent.getCategories() != null
2359 && intent.getCategories().contains(Intent.CATEGORY_HOME)) {
2360 // Requesting home, set the identity to the current user
2361 // HACK!
2362 userId = mCurrentUserId;
2363 } else {
2364 // TODO: Fix this in a better way - calls coming from SystemUI should probably carry
2365 // the current user's userId
2366 if (Binder.getCallingUid() < Process.FIRST_APPLICATION_UID) {
2367 userId = 0;
2368 } else {
2369 userId = Binder.getOrigCallingUser();
2370 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002371 }
2372 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002373 return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002374 resultTo, resultWho, requestCode, startFlags, profileFile, profileFd,
2375 null, null, options, userId);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002376 }
2377
2378 public final WaitResult startActivityAndWait(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002379 Intent intent, String resolvedType, IBinder resultTo,
2380 String resultWho, int requestCode, int startFlags, String profileFile,
2381 ParcelFileDescriptor profileFd, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002382 enforceNotIsolatedCaller("startActivityAndWait");
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002383 WaitResult res = new WaitResult();
Amith Yamasani742a6712011-05-04 14:49:28 -07002384 int userId = Binder.getOrigCallingUser();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002385 mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002386 resultTo, resultWho, requestCode, startFlags, profileFile, profileFd,
2387 res, null, options, userId);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002388 return res;
2389 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08002390
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002391 public final int startActivityWithConfig(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002392 Intent intent, String resolvedType, IBinder resultTo,
2393 String resultWho, int requestCode, int startFlags, Configuration config,
2394 Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002395 enforceNotIsolatedCaller("startActivityWithConfig");
Amith Yamasani742a6712011-05-04 14:49:28 -07002396 int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002397 resultTo, resultWho, requestCode, startFlags,
2398 null, null, null, config, options, Binder.getOrigCallingUser());
Amith Yamasani742a6712011-05-04 14:49:28 -07002399 return ret;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002400 }
2401
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002402 public int startActivityIntentSender(IApplicationThread caller,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002403 IntentSender intent, Intent fillInIntent, String resolvedType,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002404 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002405 int flagsMask, int flagsValues, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002406 enforceNotIsolatedCaller("startActivityIntentSender");
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002407 // Refuse possible leaked file descriptors
2408 if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
2409 throw new IllegalArgumentException("File descriptors passed in Intent");
2410 }
2411
2412 IIntentSender sender = intent.getTarget();
2413 if (!(sender instanceof PendingIntentRecord)) {
2414 throw new IllegalArgumentException("Bad PendingIntent object");
2415 }
2416
2417 PendingIntentRecord pir = (PendingIntentRecord)sender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002418
2419 synchronized (this) {
2420 // If this is coming from the currently resumed activity, it is
2421 // effectively saying that app switches are allowed at this point.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002422 if (mMainStack.mResumedActivity != null
2423 && mMainStack.mResumedActivity.info.applicationInfo.uid ==
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002424 Binder.getCallingUid()) {
2425 mAppSwitchesAllowedTime = 0;
2426 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002427 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002428 int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null,
2429 resultTo, resultWho, requestCode, flagsMask, flagsValues, options);
Amith Yamasani742a6712011-05-04 14:49:28 -07002430 return ret;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002431 }
2432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 public boolean startNextMatchingActivity(IBinder callingActivity,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002434 Intent intent, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 // Refuse possible leaked file descriptors
2436 if (intent != null && intent.hasFileDescriptors() == true) {
2437 throw new IllegalArgumentException("File descriptors passed in Intent");
2438 }
2439
2440 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002441 ActivityRecord r = mMainStack.isInStackLocked(callingActivity);
2442 if (r == null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002443 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 return false;
2445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 if (r.app == null || r.app.thread == null) {
2447 // The caller is not running... d'oh!
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002448 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 return false;
2450 }
2451 intent = new Intent(intent);
2452 // The caller is not allowed to change the data.
2453 intent.setDataAndType(r.intent.getData(), r.intent.getType());
2454 // And we are resetting to find the next component...
2455 intent.setComponent(null);
2456
2457 ActivityInfo aInfo = null;
2458 try {
2459 List<ResolveInfo> resolves =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002460 AppGlobals.getPackageManager().queryIntentActivities(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 intent, r.resolvedType,
Amith Yamasani483f3b02012-03-13 16:08:00 -07002462 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002463 UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464
2465 // Look for the original activity in the list...
2466 final int N = resolves != null ? resolves.size() : 0;
2467 for (int i=0; i<N; i++) {
2468 ResolveInfo rInfo = resolves.get(i);
2469 if (rInfo.activityInfo.packageName.equals(r.packageName)
2470 && rInfo.activityInfo.name.equals(r.info.name)) {
2471 // We found the current one... the next matching is
2472 // after it.
2473 i++;
2474 if (i<N) {
2475 aInfo = resolves.get(i).activityInfo;
2476 }
2477 break;
2478 }
2479 }
2480 } catch (RemoteException e) {
2481 }
2482
2483 if (aInfo == null) {
2484 // Nobody who is next!
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002485 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 return false;
2487 }
2488
2489 intent.setComponent(new ComponentName(
2490 aInfo.applicationInfo.packageName, aInfo.name));
2491 intent.setFlags(intent.getFlags()&~(
2492 Intent.FLAG_ACTIVITY_FORWARD_RESULT|
2493 Intent.FLAG_ACTIVITY_CLEAR_TOP|
2494 Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
2495 Intent.FLAG_ACTIVITY_NEW_TASK));
2496
2497 // Okay now we need to start the new activity, replacing the
2498 // currently running activity. This is a little tricky because
2499 // we want to start the new one as if the current one is finished,
2500 // but not finish the current one first so that there is no flicker.
2501 // And thus...
2502 final boolean wasFinishing = r.finishing;
2503 r.finishing = true;
2504
2505 // Propagate reply information over to the new activity.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002506 final ActivityRecord resultTo = r.resultTo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 final String resultWho = r.resultWho;
2508 final int requestCode = r.requestCode;
2509 r.resultTo = null;
2510 if (resultTo != null) {
2511 resultTo.removeResultsLocked(r, resultWho, requestCode);
2512 }
2513
2514 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002515 int res = mMainStack.startActivityLocked(r.app.thread, intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002516 r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null,
2517 resultWho, requestCode, -1, r.launchedFromUid, 0,
2518 options, false, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 Binder.restoreCallingIdentity(origId);
2520
2521 r.finishing = wasFinishing;
Dianne Hackborna4972e92012-03-14 10:38:05 -07002522 if (res != ActivityManager.START_SUCCESS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 return false;
2524 }
2525 return true;
2526 }
2527 }
2528
Dianne Hackborn2d91af02009-07-16 13:34:33 -07002529 public final int startActivityInPackage(int uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 Intent intent, String resolvedType, IBinder resultTo,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002531 String resultWho, int requestCode, int startFlags, Bundle options) {
Dianne Hackborn2d91af02009-07-16 13:34:33 -07002532
2533 // This is so super not safe, that only the system (or okay root)
2534 // can do it.
2535 final int callingUid = Binder.getCallingUid();
2536 if (callingUid != 0 && callingUid != Process.myUid()) {
2537 throw new SecurityException(
2538 "startActivityInPackage only available to the system");
2539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540
Amith Yamasani742a6712011-05-04 14:49:28 -07002541 int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002542 resultTo, resultWho, requestCode, startFlags,
Amith Yamasani7300dbe2012-08-17 14:17:57 -07002543 null, null, null, null, options, UserHandle.getUserId(uid));
Amith Yamasani742a6712011-05-04 14:49:28 -07002544 return ret;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002545 }
2546
2547 public final int startActivities(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002548 Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002549 enforceNotIsolatedCaller("startActivities");
Amith Yamasani742a6712011-05-04 14:49:28 -07002550 int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002551 options, Binder.getOrigCallingUser());
Amith Yamasani742a6712011-05-04 14:49:28 -07002552 return ret;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002553 }
2554
2555 public final int startActivitiesInPackage(int uid,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002556 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
2557 Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002558
2559 // This is so super not safe, that only the system (or okay root)
2560 // can do it.
2561 final int callingUid = Binder.getCallingUid();
2562 if (callingUid != 0 && callingUid != Process.myUid()) {
2563 throw new SecurityException(
2564 "startActivityInPackage only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002566 int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002567 options, UserHandle.getUserId(uid));
Amith Yamasani742a6712011-05-04 14:49:28 -07002568 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
2570
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002571 final void addRecentTaskLocked(TaskRecord task) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 int N = mRecentTasks.size();
Dianne Hackborn7c0e75e2010-12-21 19:15:40 -08002573 // Quick case: check if the top-most recent task is the same.
2574 if (N > 0 && mRecentTasks.get(0) == task) {
2575 return;
2576 }
2577 // Remove any existing entries that are the same kind of task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 for (int i=0; i<N; i++) {
2579 TaskRecord tr = mRecentTasks.get(i);
Amith Yamasani742a6712011-05-04 14:49:28 -07002580 if (task.userId == tr.userId
2581 && ((task.affinity != null && task.affinity.equals(tr.affinity))
2582 || (task.intent != null && task.intent.filterEquals(tr.intent)))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 mRecentTasks.remove(i);
2584 i--;
2585 N--;
2586 if (task.intent == null) {
2587 // If the new recent task we are adding is not fully
2588 // specified, then replace it with the existing recent task.
2589 task = tr;
2590 }
2591 }
2592 }
2593 if (N >= MAX_RECENT_TASKS) {
2594 mRecentTasks.remove(N-1);
2595 }
2596 mRecentTasks.add(0, task);
2597 }
2598
2599 public void setRequestedOrientation(IBinder token,
2600 int requestedOrientation) {
2601 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002602 ActivityRecord r = mMainStack.isInStackLocked(token);
2603 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 return;
2605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbe707852011-11-11 14:32:10 -08002607 mWindowManager.setAppOrientation(r.appToken, requestedOrientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 Configuration config = mWindowManager.updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002609 mConfiguration,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002610 r.mayFreezeScreenLocked(r.app) ? r.appToken : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 if (config != null) {
2612 r.frozenBeforeDestroy = true;
Dianne Hackborn813075a62011-11-14 17:45:19 -08002613 if (!updateConfigurationLocked(config, r, false, false)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 }
2616 }
2617 Binder.restoreCallingIdentity(origId);
2618 }
2619 }
2620
2621 public int getRequestedOrientation(IBinder token) {
2622 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002623 ActivityRecord r = mMainStack.isInStackLocked(token);
2624 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2626 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002627 return mWindowManager.getAppOrientation(r.appToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 }
2629 }
2630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 /**
2632 * This is the internal entry point for handling Activity.finish().
2633 *
2634 * @param token The Binder token referencing the Activity we want to finish.
2635 * @param resultCode Result code, if any, from this Activity.
2636 * @param resultData Result data (Intent), if any, from this Activity.
2637 *
Alexey Tarasov83bad3d2009-08-12 15:05:43 +11002638 * @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 -08002639 */
2640 public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) {
2641 // Refuse possible leaked file descriptors
2642 if (resultData != null && resultData.hasFileDescriptors() == true) {
2643 throw new IllegalArgumentException("File descriptors passed in Intent");
2644 }
2645
2646 synchronized(this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002647 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 // Find the first activity that is not finishing.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002649 ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 if (next != null) {
2651 // ask watcher if this is allowed
2652 boolean resumeOK = true;
2653 try {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002654 resumeOK = mController.activityResuming(next.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002656 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 }
2658
2659 if (!resumeOK) {
2660 return false;
2661 }
2662 }
2663 }
2664 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002665 boolean res = mMainStack.requestFinishActivityLocked(token, resultCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 resultData, "app-request");
2667 Binder.restoreCallingIdentity(origId);
2668 return res;
2669 }
2670 }
2671
Dianne Hackborn860755f2010-06-03 18:47:52 -07002672 public final void finishHeavyWeightApp() {
2673 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2674 != PackageManager.PERMISSION_GRANTED) {
2675 String msg = "Permission Denial: finishHeavyWeightApp() from pid="
2676 + Binder.getCallingPid()
2677 + ", uid=" + Binder.getCallingUid()
2678 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2679 Slog.w(TAG, msg);
2680 throw new SecurityException(msg);
2681 }
2682
2683 synchronized(this) {
2684 if (mHeavyWeightProcess == null) {
2685 return;
2686 }
2687
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002688 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(
Dianne Hackborn860755f2010-06-03 18:47:52 -07002689 mHeavyWeightProcess.activities);
2690 for (int i=0; i<activities.size(); i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002691 ActivityRecord r = activities.get(i);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002692 if (!r.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002693 int index = mMainStack.indexOfTokenLocked(r.appToken);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002694 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED,
Dianne Hackborn860755f2010-06-03 18:47:52 -07002696 null, "finish-heavy");
2697 }
2698 }
2699 }
2700
2701 mHeavyWeightProcess = null;
2702 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
2703 }
2704 }
2705
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002706 public void crashApplication(int uid, int initialPid, String packageName,
2707 String message) {
2708 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2709 != PackageManager.PERMISSION_GRANTED) {
2710 String msg = "Permission Denial: crashApplication() from pid="
2711 + Binder.getCallingPid()
2712 + ", uid=" + Binder.getCallingUid()
2713 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2714 Slog.w(TAG, msg);
2715 throw new SecurityException(msg);
2716 }
2717
2718 synchronized(this) {
2719 ProcessRecord proc = null;
2720
2721 // Figure out which process to kill. We don't trust that initialPid
2722 // still has any relation to current pids, so must scan through the
2723 // list.
2724 synchronized (mPidsSelfLocked) {
2725 for (int i=0; i<mPidsSelfLocked.size(); i++) {
2726 ProcessRecord p = mPidsSelfLocked.valueAt(i);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002727 if (p.uid != uid) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002728 continue;
2729 }
2730 if (p.pid == initialPid) {
2731 proc = p;
2732 break;
2733 }
2734 for (String str : p.pkgList) {
2735 if (str.equals(packageName)) {
2736 proc = p;
2737 }
2738 }
2739 }
2740 }
2741
2742 if (proc == null) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07002743 Slog.w(TAG, "crashApplication: nothing for uid=" + uid
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002744 + " initialPid=" + initialPid
2745 + " packageName=" + packageName);
2746 return;
2747 }
2748
2749 if (proc.thread != null) {
Dianne Hackborn9f531192010-08-04 17:48:03 -07002750 if (proc.pid == Process.myPid()) {
2751 Log.w(TAG, "crashApplication: trying to crash self!");
2752 return;
2753 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002754 long ident = Binder.clearCallingIdentity();
2755 try {
2756 proc.thread.scheduleCrash(message);
2757 } catch (RemoteException e) {
2758 }
2759 Binder.restoreCallingIdentity(ident);
2760 }
2761 }
2762 }
2763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 public final void finishSubActivity(IBinder token, String resultWho,
2765 int requestCode) {
2766 synchronized(this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002768 mMainStack.finishSubActivityLocked(token, resultWho, requestCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 Binder.restoreCallingIdentity(origId);
2770 }
2771 }
2772
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002773 public boolean finishActivityAffinity(IBinder token) {
2774 synchronized(this) {
2775 final long origId = Binder.clearCallingIdentity();
2776 boolean res = mMainStack.finishActivityAffinityLocked(token);
2777 Binder.restoreCallingIdentity(origId);
2778 return res;
2779 }
2780 }
2781
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002782 public boolean willActivityBeVisible(IBinder token) {
2783 synchronized(this) {
2784 int i;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002785 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
2786 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002787 if (r.appToken == token) {
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002788 return true;
2789 }
2790 if (r.fullscreen && !r.finishing) {
2791 return false;
2792 }
2793 }
2794 return true;
2795 }
2796 }
2797
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002798 public void overridePendingTransition(IBinder token, String packageName,
2799 int enterAnim, int exitAnim) {
2800 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002801 ActivityRecord self = mMainStack.isInStackLocked(token);
2802 if (self == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002803 return;
2804 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002805
2806 final long origId = Binder.clearCallingIdentity();
2807
2808 if (self.state == ActivityState.RESUMED
2809 || self.state == ActivityState.PAUSING) {
2810 mWindowManager.overridePendingAppTransition(packageName,
Dianne Hackborn84375872012-06-01 19:03:50 -07002811 enterAnim, exitAnim, null);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002812 }
2813
2814 Binder.restoreCallingIdentity(origId);
2815 }
2816 }
2817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 * Main function for removing an existing process from the activity manager
2820 * as a result of that process going away. Clears out all connections
2821 * to the process.
2822 */
2823 private final void handleAppDiedLocked(ProcessRecord app,
Dianne Hackborn130b0d22011-07-26 22:07:48 -07002824 boolean restarting, boolean allowRestart) {
2825 cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 if (!restarting) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002827 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 }
2829
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002830 if (mProfileProc == app) {
2831 clearProfilerLocked();
2832 }
2833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 // Just in case...
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002835 if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) {
2836 if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity);
2837 mMainStack.mPausingActivity = null;
2838 }
2839 if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) {
2840 mMainStack.mLastPausedActivity = null;
2841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
2843 // Remove this application's activities from active lists.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002844 mMainStack.removeHistoryRecordsForAppLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845
2846 boolean atTop = true;
2847 boolean hasVisibleActivities = false;
2848
2849 // Clean out the history list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002850 int i = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002851 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 TAG, "Removing app " + app + " from history with " + i + " entries");
2853 while (i > 0) {
2854 i--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002855 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002856 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2858 if (r.app == app) {
2859 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002860 if (ActivityStack.DEBUG_ADD_REMOVE) {
2861 RuntimeException here = new RuntimeException("here");
2862 here.fillInStackTrace();
2863 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2864 + ": haveState=" + r.haveState
2865 + " stateNotNeeded=" + r.stateNotNeeded
2866 + " finishing=" + r.finishing
2867 + " state=" + r.state, here);
2868 }
2869 if (!r.finishing) {
2870 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
Dianne Hackborn8bf0aa92011-11-29 13:54:43 -08002871 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2872 System.identityHashCode(r),
2873 r.task.taskId, r.shortComponentName,
2874 "proc died without state saved");
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002875 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002876 mMainStack.removeActivityFromHistoryLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877
2878 } else {
2879 // We have the current state for this activity, so
2880 // it can be restarted later when needed.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002881 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 TAG, "Keeping entry, setting app to null");
2883 if (r.visible) {
2884 hasVisibleActivities = true;
2885 }
2886 r.app = null;
2887 r.nowVisible = false;
2888 if (!r.haveState) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002889 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
2890 "App died, clearing saved state of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 r.icicle = null;
2892 }
2893 }
2894
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002895 r.stack.cleanUpActivityLocked(r, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 }
2897 atTop = false;
2898 }
2899
2900 app.activities.clear();
2901
2902 if (app.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002903 Slog.w(TAG, "Crash of app " + app.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 + " running instrumentation " + app.instrumentationClass);
2905 Bundle info = new Bundle();
2906 info.putString("shortMsg", "Process crashed.");
2907 finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info);
2908 }
2909
2910 if (!restarting) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 if (!mMainStack.resumeTopActivityLocked(null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 // If there was nothing to resume, and we are not already
2913 // restarting this process, but there is a visible activity that
2914 // is hosted by the process... then make sure all visible
2915 // activities are running, taking care of restarting this
2916 // process.
2917 if (hasVisibleActivities) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002918 mMainStack.ensureActivitiesVisibleLocked(null, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 }
2920 }
2921 }
2922 }
2923
2924 private final int getLRURecordIndexForAppLocked(IApplicationThread thread) {
2925 IBinder threadBinder = thread.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 // Find the application record.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002927 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2928 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 if (rec.thread != null && rec.thread.asBinder() == threadBinder) {
2930 return i;
2931 }
2932 }
2933 return -1;
2934 }
2935
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936 final ProcessRecord getRecordForAppLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 IApplicationThread thread) {
2938 if (thread == null) {
2939 return null;
2940 }
2941
2942 int appIndex = getLRURecordIndexForAppLocked(thread);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002943 return appIndex >= 0 ? mLruProcesses.get(appIndex) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 }
2945
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002946 final void appDiedLocked(ProcessRecord app, int pid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 IApplicationThread thread) {
2948
2949 mProcDeaths[0]++;
2950
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002951 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
2952 synchronized (stats) {
2953 stats.noteProcessDiedLocked(app.info.uid, pid);
2954 }
2955
Magnus Edlund7bb25812010-02-24 15:45:06 +01002956 // Clean up already done if the process has been re-started.
2957 if (app.pid == pid && app.thread != null &&
2958 app.thread.asBinder() == thread.asBinder()) {
Dianne Hackborn906497c2010-05-10 15:57:38 -07002959 if (!app.killedBackground) {
2960 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
2961 + ") has died.");
2962 }
Doug Zongker2bec3d42009-12-04 12:52:44 -08002963 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002964 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 TAG, "Dying app: " + app + ", pid: " + pid
2966 + ", thread: " + thread.asBinder());
2967 boolean doLowMem = app.instrumentationClass == null;
Dianne Hackborn130b0d22011-07-26 22:07:48 -07002968 handleAppDiedLocked(app, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969
2970 if (doLowMem) {
2971 // If there are no longer any background processes running,
2972 // and the app that died was not running instrumentation,
2973 // then tell everyone we are now low on memory.
2974 boolean haveBg = false;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002975 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2976 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn7d608422011-08-07 16:24:18 -07002977 if (rec.thread != null && rec.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 haveBg = true;
2979 break;
2980 }
2981 }
2982
2983 if (!haveBg) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002984 EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size());
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002985 long now = SystemClock.uptimeMillis();
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002986 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2987 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn36124872009-10-08 16:22:03 -07002988 if (rec != app && rec.thread != null &&
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002989 (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) {
2990 // The low memory report is overriding any current
2991 // state for a GC request. Make sure to do
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002992 // heavy/important/visible/foreground processes first.
Dianne Hackborn7d608422011-08-07 16:24:18 -07002993 if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002994 rec.lastRequestedGc = 0;
2995 } else {
2996 rec.lastRequestedGc = rec.lastLowMemory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002998 rec.reportLowMemory = true;
2999 rec.lastLowMemory = now;
3000 mProcessesToGc.remove(rec);
3001 addProcessToGcListLocked(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 }
3003 }
Dianne Hackborn04d6db32011-11-04 20:07:24 -07003004 mHandler.sendEmptyMessage(REPORT_MEM_USAGE);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003005 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 }
3007 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01003008 } else if (app.pid != pid) {
3009 // A new process has already been started.
Joe Onorato8a9b2202010-02-26 18:56:32 -08003010 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
Magnus Edlund7bb25812010-02-24 15:45:06 +01003011 + ") has died and restarted (pid " + app.pid + ").");
Dianne Hackborn28a8c2b2010-03-01 11:30:02 -08003012 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003013 } else if (DEBUG_PROCESSES) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003014 Slog.d(TAG, "Received spurious death notification for thread "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 + thread.asBinder());
3016 }
3017 }
3018
Dan Egnor42471dd2010-01-07 17:25:22 -08003019 /**
3020 * If a stack trace dump file is configured, dump process stack traces.
Christopher Tate6ee412d2010-05-28 12:01:56 -07003021 * @param clearTraces causes the dump file to be erased prior to the new
3022 * traces being written, if true; when false, the new traces will be
3023 * appended to any existing file content.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003024 * @param firstPids of dalvik VM processes to dump stack traces for first
3025 * @param lastPids of dalvik VM processes to dump stack traces for last
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003026 * @param nativeProcs optional list of native process names to dump stack crawls
Dan Egnor42471dd2010-01-07 17:25:22 -08003027 * @return file containing stack traces, or null if no dump file is configured
3028 */
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003029 public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids,
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003030 ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003031 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
3032 if (tracesPath == null || tracesPath.length() == 0) {
3033 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003035
3036 File tracesFile = new File(tracesPath);
3037 try {
3038 File tracesDir = tracesFile.getParentFile();
rpcraigec7ed14c2012-07-25 13:10:37 -04003039 if (!tracesDir.exists()) {
3040 tracesFile.mkdirs();
3041 if (!SELinux.restorecon(tracesDir)) {
3042 return null;
3043 }
3044 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003045 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
3046
Christopher Tate6ee412d2010-05-28 12:01:56 -07003047 if (clearTraces && tracesFile.exists()) tracesFile.delete();
Dan Egnor42471dd2010-01-07 17:25:22 -08003048 tracesFile.createNewFile();
3049 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
3050 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003051 Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
Dan Egnor42471dd2010-01-07 17:25:22 -08003052 return null;
3053 }
3054
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003055 dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003056 return tracesFile;
3057 }
3058
3059 private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids,
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003060 ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003061 // Use a FileObserver to detect when traces finish writing.
3062 // The order of traces is considered important to maintain for legibility.
3063 FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {
3064 public synchronized void onEvent(int event, String path) { notify(); }
3065 };
3066
3067 try {
3068 observer.startWatching();
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003069
3070 // First collect all of the stacks of the most important pids.
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003071 if (firstPids != null) {
3072 try {
3073 int num = firstPids.size();
3074 for (int i = 0; i < num; i++) {
3075 synchronized (observer) {
3076 Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
3077 observer.wait(200); // Wait for write-close, give up after 200msec
3078 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003079 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003080 } catch (InterruptedException e) {
3081 Log.wtf(TAG, e);
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003082 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003083 }
3084
3085 // Next measure CPU usage.
3086 if (processStats != null) {
3087 processStats.init();
3088 System.gc();
3089 processStats.update();
3090 try {
3091 synchronized (processStats) {
3092 processStats.wait(500); // measure over 1/2 second.
3093 }
3094 } catch (InterruptedException e) {
3095 }
3096 processStats.update();
3097
3098 // We'll take the stack crawls of just the top apps using CPU.
3099 final int N = processStats.countWorkingStats();
3100 int numProcs = 0;
3101 for (int i=0; i<N && numProcs<5; i++) {
3102 ProcessStats.Stats stats = processStats.getWorkingStats(i);
3103 if (lastPids.indexOfKey(stats.pid) >= 0) {
3104 numProcs++;
3105 try {
3106 synchronized (observer) {
3107 Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
3108 observer.wait(200); // Wait for write-close, give up after 200msec
3109 }
3110 } catch (InterruptedException e) {
3111 Log.wtf(TAG, e);
3112 }
3113
3114 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003115 }
3116 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003117
Dan Egnor42471dd2010-01-07 17:25:22 -08003118 } finally {
3119 observer.stopWatching();
3120 }
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003121
3122 if (nativeProcs != null) {
3123 int[] pids = Process.getPidsForCommands(nativeProcs);
3124 if (pids != null) {
3125 for (int pid : pids) {
3126 Debug.dumpNativeBacktraceToFile(pid, tracesPath);
3127 }
3128 }
3129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 }
3131
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003132 final void logAppTooSlow(ProcessRecord app, long startTime, String msg) {
Dianne Hackborn69dc66e2012-03-26 10:50:54 -07003133 if (true || IS_USER_BUILD) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003134 return;
3135 }
3136 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
3137 if (tracesPath == null || tracesPath.length() == 0) {
3138 return;
3139 }
3140
3141 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
3142 StrictMode.allowThreadDiskWrites();
3143 try {
3144 final File tracesFile = new File(tracesPath);
3145 final File tracesDir = tracesFile.getParentFile();
3146 final File tracesTmp = new File(tracesDir, "__tmp__");
3147 try {
rpcraigec7ed14c2012-07-25 13:10:37 -04003148 if (!tracesDir.exists()) {
3149 tracesFile.mkdirs();
3150 if (!SELinux.restorecon(tracesDir.getPath())) {
3151 return;
3152 }
3153 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003154 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
3155
3156 if (tracesFile.exists()) {
3157 tracesTmp.delete();
3158 tracesFile.renameTo(tracesTmp);
3159 }
3160 StringBuilder sb = new StringBuilder();
3161 Time tobj = new Time();
3162 tobj.set(System.currentTimeMillis());
3163 sb.append(tobj.format("%Y-%m-%d %H:%M:%S"));
3164 sb.append(": ");
3165 TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb);
3166 sb.append(" since ");
3167 sb.append(msg);
3168 FileOutputStream fos = new FileOutputStream(tracesFile);
3169 fos.write(sb.toString().getBytes());
3170 if (app == null) {
3171 fos.write("\n*** No application process!".getBytes());
3172 }
3173 fos.close();
3174 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
3175 } catch (IOException e) {
3176 Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e);
3177 return;
3178 }
3179
3180 if (app != null) {
3181 ArrayList<Integer> firstPids = new ArrayList<Integer>();
3182 firstPids.add(app.pid);
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003183 dumpStackTraces(tracesPath, firstPids, null, null, null);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003184 }
3185
3186 File lastTracesFile = null;
3187 File curTracesFile = null;
3188 for (int i=9; i>=0; i--) {
3189 String name = String.format("slow%02d.txt", i);
3190 curTracesFile = new File(tracesDir, name);
3191 if (curTracesFile.exists()) {
3192 if (lastTracesFile != null) {
3193 curTracesFile.renameTo(lastTracesFile);
3194 } else {
3195 curTracesFile.delete();
3196 }
3197 }
3198 lastTracesFile = curTracesFile;
3199 }
3200 tracesFile.renameTo(curTracesFile);
3201 if (tracesTmp.exists()) {
3202 tracesTmp.renameTo(tracesFile);
3203 }
3204 } finally {
3205 StrictMode.setThreadPolicy(oldPolicy);
3206 }
3207 }
3208
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003209 final void appNotResponding(ProcessRecord app, ActivityRecord activity,
3210 ActivityRecord parent, final String annotation) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003211 ArrayList<Integer> firstPids = new ArrayList<Integer>(5);
3212 SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20);
3213
Dianne Hackborn287952c2010-09-22 22:34:31 -07003214 if (mController != null) {
3215 try {
3216 // 0 == continue, -1 = kill process immediately
3217 int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation);
3218 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
3219 } catch (RemoteException e) {
3220 mController = null;
3221 }
3222 }
3223
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003224 long anrTime = SystemClock.uptimeMillis();
3225 if (MONITOR_CPU_USAGE) {
3226 updateCpuStatsNow();
3227 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003228
3229 synchronized (this) {
3230 // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
3231 if (mShuttingDown) {
3232 Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
3233 return;
3234 } else if (app.notResponding) {
3235 Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
3236 return;
3237 } else if (app.crashing) {
3238 Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
3239 return;
3240 }
3241
3242 // In case we come through here for the same app before completing
3243 // this one, mark as anring now so we will bail out.
3244 app.notResponding = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08003245
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003246 // Log the ANR to the event log.
3247 EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags,
3248 annotation);
Dan Egnor42471dd2010-01-07 17:25:22 -08003249
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003250 // Dump thread traces as quickly as we can, starting with "interesting" processes.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003251 firstPids.add(app.pid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003252
3253 int parentPid = app.pid;
3254 if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid;
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003255 if (parentPid != app.pid) firstPids.add(parentPid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003256
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003257 if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID);
Dan Egnor42471dd2010-01-07 17:25:22 -08003258
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003259 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
3260 ProcessRecord r = mLruProcesses.get(i);
3261 if (r != null && r.thread != null) {
3262 int pid = r.pid;
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003263 if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) {
3264 if (r.persistent) {
3265 firstPids.add(pid);
3266 } else {
3267 lastPids.put(pid, Boolean.TRUE);
3268 }
3269 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 }
3272 }
3273
Dan Egnor42471dd2010-01-07 17:25:22 -08003274 // Log the ANR to the main log.
Jeff Browndeb6ed82012-04-10 14:26:26 -07003275 StringBuilder info = new StringBuilder();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003276 info.setLength(0);
Dan Egnor42471dd2010-01-07 17:25:22 -08003277 info.append("ANR in ").append(app.processName);
3278 if (activity != null && activity.shortComponentName != null) {
3279 info.append(" (").append(activity.shortComponentName).append(")");
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07003280 }
Eric Rowe6f4f6192010-02-17 18:29:04 -08003281 info.append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 if (annotation != null) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08003283 info.append("Reason: ").append(annotation).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003285 if (parent != null && parent != activity) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08003286 info.append("Parent: ").append(parent.shortComponentName).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288
Dianne Hackborn287952c2010-09-22 22:34:31 -07003289 final ProcessStats processStats = new ProcessStats(true);
3290
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003291 File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003292
Dan Egnor42471dd2010-01-07 17:25:22 -08003293 String cpuInfo = null;
3294 if (MONITOR_CPU_USAGE) {
3295 updateCpuStatsNow();
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003296 synchronized (mProcessStatsThread) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003297 cpuInfo = mProcessStats.printCurrentState(anrTime);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003298 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003299 info.append(processStats.printCurrentLoad());
Dan Egnor42471dd2010-01-07 17:25:22 -08003300 info.append(cpuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 }
3302
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003303 info.append(processStats.printCurrentState(anrTime));
3304
Joe Onorato8a9b2202010-02-26 18:56:32 -08003305 Slog.e(TAG, info.toString());
Dan Egnor42471dd2010-01-07 17:25:22 -08003306 if (tracesFile == null) {
3307 // There is no trace file, so dump (only) the alleged culprit's threads to the log
3308 Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
3309 }
3310
Jeff Sharkeya353d262011-10-28 11:12:06 -07003311 addErrorToDropBox("anr", app, app.processName, activity, parent, annotation,
3312 cpuInfo, tracesFile, null);
Dan Egnor42471dd2010-01-07 17:25:22 -08003313
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003314 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 try {
Dan Egnor42471dd2010-01-07 17:25:22 -08003316 // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
3317 int res = mController.appNotResponding(app.processName, app.pid, info.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 if (res != 0) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003319 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
3320 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 }
3322 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003323 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 }
3325 }
3326
Dan Egnor42471dd2010-01-07 17:25:22 -08003327 // Unless configured otherwise, swallow ANRs in background processes & kill the process.
3328 boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
3329 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003330
3331 synchronized (this) {
3332 if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003333 Slog.w(TAG, "Killing " + app + ": background ANR");
3334 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
3335 app.processName, app.setAdj, "background ANR");
3336 Process.killProcessQuiet(app.pid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003337 return;
3338 }
3339
3340 // Set the app's notResponding state, and look up the errorReportReceiver
3341 makeAppNotRespondingLocked(app,
3342 activity != null ? activity.shortComponentName : null,
3343 annotation != null ? "ANR " + annotation : "ANR",
3344 info.toString());
3345
3346 // Bring up the infamous App Not Responding dialog
3347 Message msg = Message.obtain();
3348 HashMap map = new HashMap();
3349 msg.what = SHOW_NOT_RESPONDING_MSG;
3350 msg.obj = map;
3351 map.put("app", app);
3352 if (activity != null) {
3353 map.put("activity", activity);
3354 }
3355
3356 mHandler.sendMessage(msg);
Dan Egnor42471dd2010-01-07 17:25:22 -08003357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
3359
Dianne Hackborn0dad3642010-09-09 21:25:35 -07003360 final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) {
3361 if (!mLaunchWarningShown) {
3362 mLaunchWarningShown = true;
3363 mHandler.post(new Runnable() {
3364 @Override
3365 public void run() {
3366 synchronized (ActivityManagerService.this) {
3367 final Dialog d = new LaunchWarningWindow(mContext, cur, next);
3368 d.show();
3369 mHandler.postDelayed(new Runnable() {
3370 @Override
3371 public void run() {
3372 synchronized (ActivityManagerService.this) {
3373 d.dismiss();
3374 mLaunchWarningShown = false;
3375 }
3376 }
3377 }, 4000);
3378 }
3379 }
3380 });
3381 }
3382 }
3383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 public boolean clearApplicationUserData(final String packageName,
Amith Yamasani742a6712011-05-04 14:49:28 -07003385 final IPackageDataObserver observer, final int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003386 enforceNotIsolatedCaller("clearApplicationUserData");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 int uid = Binder.getCallingUid();
3388 int pid = Binder.getCallingPid();
3389 long callingId = Binder.clearCallingIdentity();
3390 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003391 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 int pkgUid = -1;
3393 synchronized(this) {
3394 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003395 pkgUid = pm.getPackageUid(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 } catch (RemoteException e) {
3397 }
3398 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003399 Slog.w(TAG, "Invalid packageName:" + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 return false;
3401 }
3402 if (uid == pkgUid || checkComponentPermission(
3403 android.Manifest.permission.CLEAR_APP_USER_DATA,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08003404 pid, uid, -1, true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003406 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 } else {
3408 throw new SecurityException(pid+" does not have permission:"+
3409 android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" +
3410 "for process:"+packageName);
3411 }
3412 }
3413
3414 try {
3415 //clear application user data
Amith Yamasani483f3b02012-03-13 16:08:00 -07003416 pm.clearApplicationUserData(packageName, observer, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
3418 Uri.fromParts("package", packageName, null));
3419 intent.putExtra(Intent.EXTRA_UID, pkgUid);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003420 broadcastIntentInPackage("android", Process.SYSTEM_UID, intent,
Amith Yamasani742a6712011-05-04 14:49:28 -07003421 null, null, 0, null, null, null, false, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 } catch (RemoteException e) {
3423 }
3424 } finally {
3425 Binder.restoreCallingIdentity(callingId);
3426 }
3427 return true;
3428 }
3429
Dianne Hackborn03abb812010-01-04 18:43:19 -08003430 public void killBackgroundProcesses(final String packageName) {
3431 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
3432 != PackageManager.PERMISSION_GRANTED &&
3433 checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES)
3434 != PackageManager.PERMISSION_GRANTED) {
3435 String msg = "Permission Denial: killBackgroundProcesses() from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 + Binder.getCallingPid()
3437 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn03abb812010-01-04 18:43:19 -08003438 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003439 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 throw new SecurityException(msg);
3441 }
3442
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003443 int userId = UserHandle.getCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 long callingId = Binder.clearCallingIdentity();
3445 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003446 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 int pkgUid = -1;
3448 synchronized(this) {
3449 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003450 pkgUid = pm.getPackageUid(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 } catch (RemoteException e) {
3452 }
3453 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003454 Slog.w(TAG, "Invalid packageName: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 return;
3456 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003457 killPackageProcessesLocked(packageName, pkgUid,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003458 ProcessList.SERVICE_ADJ, false, true, true, false, "kill background");
3459 }
3460 } finally {
3461 Binder.restoreCallingIdentity(callingId);
3462 }
3463 }
3464
3465 public void killAllBackgroundProcesses() {
3466 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
3467 != PackageManager.PERMISSION_GRANTED) {
3468 String msg = "Permission Denial: killAllBackgroundProcesses() from pid="
3469 + Binder.getCallingPid()
3470 + ", uid=" + Binder.getCallingUid()
3471 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
3472 Slog.w(TAG, msg);
3473 throw new SecurityException(msg);
3474 }
3475
3476 long callingId = Binder.clearCallingIdentity();
3477 try {
3478 synchronized(this) {
3479 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
3480 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
3481 final int NA = apps.size();
3482 for (int ia=0; ia<NA; ia++) {
3483 ProcessRecord app = apps.valueAt(ia);
3484 if (app.persistent) {
3485 // we don't kill persistent processes
3486 continue;
3487 }
3488 if (app.removed) {
3489 procs.add(app);
3490 } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
3491 app.removed = true;
3492 procs.add(app);
3493 }
3494 }
3495 }
3496
3497 int N = procs.size();
3498 for (int i=0; i<N; i++) {
3499 removeProcessLocked(procs.get(i), false, true, "kill all background");
3500 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003501 }
3502 } finally {
3503 Binder.restoreCallingIdentity(callingId);
3504 }
3505 }
3506
3507 public void forceStopPackage(final String packageName) {
3508 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
3509 != PackageManager.PERMISSION_GRANTED) {
3510 String msg = "Permission Denial: forceStopPackage() from pid="
3511 + Binder.getCallingPid()
3512 + ", uid=" + Binder.getCallingUid()
3513 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003514 Slog.w(TAG, msg);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003515 throw new SecurityException(msg);
3516 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003517 final int userId = UserHandle.getCallingUserId();
Dianne Hackborn03abb812010-01-04 18:43:19 -08003518 long callingId = Binder.clearCallingIdentity();
3519 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003520 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn03abb812010-01-04 18:43:19 -08003521 int pkgUid = -1;
3522 synchronized(this) {
3523 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003524 pkgUid = pm.getPackageUid(packageName, userId);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003525 } catch (RemoteException e) {
3526 }
3527 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003528 Slog.w(TAG, "Invalid packageName: " + packageName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003529 return;
3530 }
3531 forceStopPackageLocked(packageName, pkgUid);
Dianne Hackborne7f97212011-02-24 14:40:20 -08003532 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003533 pm.setPackageStoppedState(packageName, true, userId);
Dianne Hackborne7f97212011-02-24 14:40:20 -08003534 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08003535 } catch (IllegalArgumentException e) {
3536 Slog.w(TAG, "Failed trying to unstop package "
3537 + packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08003538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 }
3540 } finally {
3541 Binder.restoreCallingIdentity(callingId);
3542 }
3543 }
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003544
3545 /*
3546 * The pkg name and uid have to be specified.
3547 * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int)
3548 */
3549 public void killApplicationWithUid(String pkg, int uid) {
3550 if (pkg == null) {
3551 return;
3552 }
3553 // Make sure the uid is valid.
3554 if (uid < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003555 Slog.w(TAG, "Invalid uid specified for pkg : " + pkg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003556 return;
3557 }
3558 int callerUid = Binder.getCallingUid();
3559 // Only the system server can kill an application
3560 if (callerUid == Process.SYSTEM_UID) {
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07003561 // Post an aysnc message to kill the application
3562 Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
3563 msg.arg1 = uid;
3564 msg.arg2 = 0;
3565 msg.obj = pkg;
Suchi Amalapurapud50066f2009-08-18 16:57:41 -07003566 mHandler.sendMessage(msg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003567 } else {
3568 throw new SecurityException(callerUid + " cannot kill pkg: " +
3569 pkg);
3570 }
3571 }
3572
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003573 public void closeSystemDialogs(String reason) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003574 enforceNotIsolatedCaller("closeSystemDialogs");
Dianne Hackborne302a162012-05-15 14:58:32 -07003575
3576 final int uid = Binder.getCallingUid();
3577 final long origId = Binder.clearCallingIdentity();
3578 synchronized (this) {
3579 closeSystemDialogsLocked(uid, reason);
3580 }
3581 Binder.restoreCallingIdentity(origId);
3582 }
3583
3584 void closeSystemDialogsLocked(int callingUid, String reason) {
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003585 Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003586 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003587 if (reason != null) {
3588 intent.putExtra("reason", reason);
3589 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003590 mWindowManager.closeSystemDialogs(reason);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003591
Dianne Hackborne302a162012-05-15 14:58:32 -07003592 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
3593 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackborn1927ae82012-06-22 15:21:36 -07003594 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Dianne Hackborne302a162012-05-15 14:58:32 -07003595 r.stack.finishActivityLocked(r, i,
3596 Activity.RESULT_CANCELED, null, "close-sys");
Dianne Hackbornffa42482009-09-23 22:20:11 -07003597 }
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003598 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003599
3600 broadcastIntentLocked(null, null, intent, null,
3601 null, 0, null, null, null, false, false, -1,
3602 callingUid, 0 /* TODO: Verify */);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003603 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003604
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003605 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids)
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003606 throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003607 enforceNotIsolatedCaller("getProcessMemoryInfo");
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003608 Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length];
3609 for (int i=pids.length-1; i>=0; i--) {
3610 infos[i] = new Debug.MemoryInfo();
3611 Debug.getMemoryInfo(pids[i], infos[i]);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003612 }
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003613 return infos;
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003614 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07003615
Dianne Hackbornb437e092011-08-05 17:50:29 -07003616 public long[] getProcessPss(int[] pids) throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003617 enforceNotIsolatedCaller("getProcessPss");
Dianne Hackbornb437e092011-08-05 17:50:29 -07003618 long[] pss = new long[pids.length];
3619 for (int i=pids.length-1; i>=0; i--) {
3620 pss[i] = Debug.getPss(pids[i]);
3621 }
3622 return pss;
3623 }
3624
Christopher Tate5e1ab332009-09-01 20:32:49 -07003625 public void killApplicationProcess(String processName, int uid) {
3626 if (processName == null) {
3627 return;
3628 }
3629
3630 int callerUid = Binder.getCallingUid();
3631 // Only the system server can kill an application
3632 if (callerUid == Process.SYSTEM_UID) {
3633 synchronized (this) {
3634 ProcessRecord app = getProcessRecordLocked(processName, uid);
Christopher Tate4a627c72011-04-01 14:43:32 -07003635 if (app != null && app.thread != null) {
Christopher Tate5e1ab332009-09-01 20:32:49 -07003636 try {
3637 app.thread.scheduleSuicide();
3638 } catch (RemoteException e) {
3639 // If the other end already died, then our work here is done.
3640 }
3641 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003642 Slog.w(TAG, "Process/uid not found attempting kill of "
Christopher Tate5e1ab332009-09-01 20:32:49 -07003643 + processName + " / " + uid);
3644 }
3645 }
3646 } else {
3647 throw new SecurityException(callerUid + " cannot kill app process: " +
3648 processName);
3649 }
3650 }
3651
Dianne Hackborn03abb812010-01-04 18:43:19 -08003652 private void forceStopPackageLocked(final String packageName, int uid) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003653 forceStopPackageLocked(packageName, uid, false, false, true, false, UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
3655 Uri.fromParts("package", packageName, null));
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003656 if (!mProcessesReady) {
3657 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
3658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 intent.putExtra(Intent.EXTRA_UID, uid);
3660 broadcastIntentLocked(null, null, intent,
3661 null, null, 0, null, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07003662 false, false,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003663 MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 }
3665
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003666 private final boolean killPackageProcessesLocked(String packageName, int uid,
Christopher Tate3dacd842011-08-19 14:56:15 -07003667 int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003668 boolean evenPersistent, String reason) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003669 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670
Dianne Hackborn03abb812010-01-04 18:43:19 -08003671 // Remove all processes this package may have touched: all with the
3672 // same UID (except for the system or root user), and all whose name
3673 // matches the package name.
3674 final String procNamePrefix = packageName + ":";
3675 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
3676 final int NA = apps.size();
3677 for (int ia=0; ia<NA; ia++) {
3678 ProcessRecord app = apps.valueAt(ia);
Christopher Tate3dacd842011-08-19 14:56:15 -07003679 if (app.persistent && !evenPersistent) {
Christopher Tate064d8422011-07-26 15:38:07 -07003680 // we don't kill persistent processes
3681 continue;
3682 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003683 if (app.removed) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003684 if (doit) {
3685 procs.add(app);
3686 }
Amith Yamasani13593602012-03-22 16:16:17 -07003687 // If uid is specified and the uid and process name match
3688 // Or, the uid is not specified and the process name matches
3689 } else if (((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid)
Amith Yamasani34db3d62012-04-02 16:35:19 -07003690 || ((app.processName.equals(packageName)
3691 || app.processName.startsWith(procNamePrefix))
3692 && uid < 0))) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003693 if (app.setAdj >= minOomAdj) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003694 if (!doit) {
3695 return true;
3696 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003697 app.removed = true;
3698 procs.add(app);
3699 }
3700 }
3701 }
3702 }
3703
3704 int N = procs.size();
3705 for (int i=0; i<N; i++) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003706 removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003707 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003708 return N > 0;
Dianne Hackborn03abb812010-01-04 18:43:19 -08003709 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003710
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003711 private final boolean forceStopPackageLocked(String name, int uid,
Christopher Tate3dacd842011-08-19 14:56:15 -07003712 boolean callerWillRestart, boolean purgeCache, boolean doit,
Amith Yamasani483f3b02012-03-13 16:08:00 -07003713 boolean evenPersistent, int userId) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07003714 int i;
3715 int N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 if (uid < 0) {
3718 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003719 uid = AppGlobals.getPackageManager().getPackageUid(name, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 } catch (RemoteException e) {
3721 }
3722 }
3723
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003724 if (doit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003725 Slog.i(TAG, "Force stopping package " + name + " uid=" + uid);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003726
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003727 Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator();
3728 while (badApps.hasNext()) {
3729 SparseArray<Long> ba = badApps.next();
3730 if (ba.get(uid) != null) {
3731 badApps.remove();
3732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 }
3734 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003735
3736 boolean didSomething = killPackageProcessesLocked(name, uid, -100,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003737 callerWillRestart, false, doit, evenPersistent, "force stop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003739 TaskRecord lastTask = null;
3740 for (i=0; i<mMainStack.mHistory.size(); i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003741 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003742 final boolean samePackage = r.packageName.equals(name);
Amith Yamasani13593602012-03-22 16:16:17 -07003743 if (r.userId == userId
3744 && (samePackage || r.task == lastTask)
Christopher Tate3dacd842011-08-19 14:56:15 -07003745 && (r.app == null || evenPersistent || !r.app.persistent)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003746 if (!doit) {
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003747 if (r.finishing) {
3748 // If this activity is just finishing, then it is not
3749 // interesting as far as something to stop.
3750 continue;
3751 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003752 return true;
3753 }
3754 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003755 Slog.i(TAG, " Force finishing activity " + r);
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003756 if (samePackage) {
3757 if (r.app != null) {
3758 r.app.removed = true;
3759 }
3760 r.app = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 }
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003762 lastTask = r.task;
3763 if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED,
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003764 null, "force-stop", true)) {
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003765 i--;
3766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 }
3768 }
3769
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003770 if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) {
3771 if (!doit) {
3772 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003774 didSomething = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003776
3777 ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>();
Amith Yamasani13593602012-03-22 16:16:17 -07003778 for (ContentProviderRecord provider : mProviderMap.getProvidersByClass(userId).values()) {
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003779 if (provider.info.packageName.equals(name)
3780 && (provider.proc == null || evenPersistent || !provider.proc.persistent)) {
3781 if (!doit) {
3782 return true;
3783 }
3784 didSomething = true;
3785 providers.add(provider);
3786 }
3787 }
3788
3789 N = providers.size();
3790 for (i=0; i<N; i++) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003791 removeDyingProviderLocked(null, providers.get(i), true);
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003792 }
3793
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003794 if (doit) {
3795 if (purgeCache) {
3796 AttributeCache ac = AttributeCache.instance();
3797 if (ac != null) {
3798 ac.removePackage(name);
3799 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003800 }
Dianne Hackborn38cc8962011-10-13 11:33:55 -07003801 if (mBooted) {
3802 mMainStack.resumeTopActivityLocked(null);
3803 mMainStack.scheduleIdleLocked();
3804 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003805 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003806
3807 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 }
3809
Dianne Hackborn130b0d22011-07-26 22:07:48 -07003810 private final boolean removeProcessLocked(ProcessRecord app,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003811 boolean callerWillRestart, boolean allowRestart, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 final String name = app.processName;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003813 final int uid = app.uid;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003814 if (DEBUG_PROCESSES) Slog.d(
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003815 TAG, "Force removing proc " + app.toShortString() + " (" + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 + "/" + uid + ")");
3817
3818 mProcessNames.remove(name, uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003819 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003820 if (mHeavyWeightProcess == app) {
3821 mHeavyWeightProcess = null;
3822 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
3823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 boolean needRestart = false;
3825 if (app.pid > 0 && app.pid != MY_PID) {
3826 int pid = app.pid;
3827 synchronized (mPidsSelfLocked) {
3828 mPidsSelfLocked.remove(pid);
3829 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
3830 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003831 Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason);
Dianne Hackborn130b0d22011-07-26 22:07:48 -07003832 handleAppDiedLocked(app, true, allowRestart);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003833 mLruProcesses.remove(app);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003834 Process.killProcessQuiet(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003836 if (app.persistent && !app.isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 if (!callerWillRestart) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003838 addAppLocked(app.info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 } else {
3840 needRestart = true;
3841 }
3842 }
3843 } else {
3844 mRemovedProcesses.add(app);
3845 }
3846
3847 return needRestart;
3848 }
3849
3850 private final void processStartTimedOutLocked(ProcessRecord app) {
3851 final int pid = app.pid;
3852 boolean gone = false;
3853 synchronized (mPidsSelfLocked) {
3854 ProcessRecord knownApp = mPidsSelfLocked.get(pid);
3855 if (knownApp != null && knownApp.thread == null) {
3856 mPidsSelfLocked.remove(pid);
3857 gone = true;
3858 }
3859 }
3860
3861 if (gone) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003862 Slog.w(TAG, "Process " + app + " failed to attach");
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003863 EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.uid,
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003864 app.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003865 mProcessNames.remove(app.processName, app.uid);
3866 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003867 if (mHeavyWeightProcess == app) {
3868 mHeavyWeightProcess = null;
3869 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
3870 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003871 // Take care of any launching providers waiting for this process.
3872 checkAppInLaunchingProvidersLocked(app, true);
3873 // Take care of any services that are waiting for the process.
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003874 mServices.processStartTimedOutLocked(app);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003875 EventLog.writeEvent(EventLogTags.AM_KILL, pid,
3876 app.processName, app.setAdj, "start timeout");
3877 Process.killProcessQuiet(pid);
Christopher Tate181fafa2009-05-14 11:12:14 -07003878 if (mBackupTarget != null && mBackupTarget.app.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003879 Slog.w(TAG, "Unattached app died before backup, skipping");
Christopher Tate181fafa2009-05-14 11:12:14 -07003880 try {
3881 IBackupManager bm = IBackupManager.Stub.asInterface(
3882 ServiceManager.getService(Context.BACKUP_SERVICE));
3883 bm.agentDisconnected(app.info.packageName);
3884 } catch (RemoteException e) {
3885 // Can't happen; the backup manager is local
3886 }
3887 }
Christopher Tatef46723b2012-01-26 14:19:24 -08003888 if (isPendingBroadcastProcessLocked(pid)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003889 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
Christopher Tatef46723b2012-01-26 14:19:24 -08003890 skipPendingBroadcastLocked(pid);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003893 Slog.w(TAG, "Spurious process start timeout - pid not known for " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
3895 }
3896
3897 private final boolean attachApplicationLocked(IApplicationThread thread,
3898 int pid) {
3899
3900 // Find the application record that is being attached... either via
3901 // the pid if we are running in multiple processes, or just pull the
3902 // next app record if we are emulating process with anonymous threads.
3903 ProcessRecord app;
3904 if (pid != MY_PID && pid >= 0) {
3905 synchronized (mPidsSelfLocked) {
3906 app = mPidsSelfLocked.get(pid);
3907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 } else {
3909 app = null;
3910 }
3911
3912 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003913 Slog.w(TAG, "No pending application record for pid " + pid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 + " (IApplicationThread " + thread + "); dropping process");
Doug Zongker2bec3d42009-12-04 12:52:44 -08003915 EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 if (pid > 0 && pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003917 Process.killProcessQuiet(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 } else {
3919 try {
3920 thread.scheduleExit();
3921 } catch (Exception e) {
3922 // Ignore exceptions.
3923 }
3924 }
3925 return false;
3926 }
3927
3928 // If this application record is still attached to a previous
3929 // process, clean it up now.
3930 if (app.thread != null) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -07003931 handleAppDiedLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 }
3933
3934 // Tell the process all about itself.
3935
Joe Onorato8a9b2202010-02-26 18:56:32 -08003936 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 TAG, "Binding process pid " + pid + " to record " + app);
3938
3939 String processName = app.processName;
3940 try {
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -07003941 AppDeathRecipient adr = new AppDeathRecipient(
3942 app, pid, thread);
3943 thread.asBinder().linkToDeath(adr, 0);
3944 app.deathRecipient = adr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 } catch (RemoteException e) {
3946 app.resetPackageList();
3947 startProcessLocked(app, "link fail", processName);
3948 return false;
3949 }
3950
Doug Zongker2bec3d42009-12-04 12:52:44 -08003951 EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952
3953 app.thread = thread;
3954 app.curAdj = app.setAdj = -100;
Dianne Hackborn09c916b2009-12-08 14:50:51 -08003955 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
3956 app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 app.forcingToForeground = null;
3958 app.foregroundServices = false;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07003959 app.hasShownUi = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 app.debugging = false;
3961
3962 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
3963
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003964 boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003965 List providers = normalMode ? generateApplicationProvidersLocked(app) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003967 if (!normalMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003968 Slog.i(TAG, "Launching preboot mode app: " + app);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003969 }
3970
Joe Onorato8a9b2202010-02-26 18:56:32 -08003971 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 TAG, "New app record " + app
3973 + " thread=" + thread.asBinder() + " pid=" + pid);
3974 try {
3975 int testMode = IApplicationThread.DEBUG_OFF;
3976 if (mDebugApp != null && mDebugApp.equals(processName)) {
3977 testMode = mWaitForDebugger
3978 ? IApplicationThread.DEBUG_WAIT
3979 : IApplicationThread.DEBUG_ON;
3980 app.debugging = true;
3981 if (mDebugTransient) {
3982 mDebugApp = mOrigDebugApp;
3983 mWaitForDebugger = mOrigWaitForDebugger;
3984 }
3985 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003986 String profileFile = app.instrumentationProfileFile;
3987 ParcelFileDescriptor profileFd = null;
3988 boolean profileAutoStop = false;
3989 if (mProfileApp != null && mProfileApp.equals(processName)) {
3990 mProfileProc = app;
3991 profileFile = mProfileFile;
3992 profileFd = mProfileFd;
3993 profileAutoStop = mAutoStopProfiler;
3994 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08003995 boolean enableOpenGlTrace = false;
3996 if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) {
3997 enableOpenGlTrace = true;
3998 mOpenGlTraceApp = null;
3999 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004000
Christopher Tate181fafa2009-05-14 11:12:14 -07004001 // If the app is being launched for restore or full backup, set it up specially
4002 boolean isRestrictedBackupMode = false;
4003 if (mBackupTarget != null && mBackupAppName.equals(processName)) {
4004 isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE)
Christopher Tate75a99702011-05-18 16:28:19 -07004005 || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL)
Christopher Tate181fafa2009-05-14 11:12:14 -07004006 || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL);
4007 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004008
Dianne Hackbornd7f6daa2009-06-22 17:06:35 -07004009 ensurePackageDexOpt(app.instrumentationInfo != null
4010 ? app.instrumentationInfo.packageName
4011 : app.info.packageName);
4012 if (app.instrumentationClass != null) {
4013 ensurePackageDexOpt(app.instrumentationClass.getPackageName());
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07004014 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004015 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004016 + processName + " with config " + mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004017 ApplicationInfo appInfo = app.instrumentationInfo != null
4018 ? app.instrumentationInfo : app.info;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -07004019 app.compat = compatibilityInfoForPackageLocked(appInfo);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004020 if (profileFd != null) {
4021 profileFd = profileFd.dup();
4022 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004023 thread.bindApplication(processName, appInfo, providers,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004024 app.instrumentationClass, profileFile, profileFd, profileAutoStop,
Siva Velusamy92a8b222012-03-09 16:24:04 -08004025 app.instrumentationArguments, app.instrumentationWatcher, testMode,
4026 enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent,
Dianne Hackborn813075a62011-11-14 17:45:19 -08004027 new Configuration(mConfiguration), app.compat, getCommonServicesLocked(),
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004028 mCoreSettingsObserver.getCoreSettingsLocked());
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004029 updateLruProcessLocked(app, false, true);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07004030 app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 } catch (Exception e) {
4032 // todo: Yikes! What should we do? For now we will try to
4033 // start another process, but that could easily get us in
4034 // an infinite loop of restarting processes...
Joe Onorato8a9b2202010-02-26 18:56:32 -08004035 Slog.w(TAG, "Exception thrown during bind!", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036
4037 app.resetPackageList();
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -07004038 app.unlinkDeathRecipient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 startProcessLocked(app, "bind fail", processName);
4040 return false;
4041 }
4042
4043 // Remove this record from the list of starting applications.
4044 mPersistentStartingProcesses.remove(app);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07004045 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
4046 "Attach application locked removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 mProcessesOnHold.remove(app);
4048
4049 boolean badApp = false;
4050 boolean didSomething = false;
4051
4052 // See if the top visible activity is waiting to run in this process...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004053 ActivityRecord hr = mMainStack.topRunningActivityLocked(null);
Christopher Tate04c0af82010-06-07 18:35:20 -07004054 if (hr != null && normalMode) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004055 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 && processName.equals(hr.processName)) {
4057 try {
Mike Lockwood3a74bd32011-08-12 13:55:22 -07004058 if (mHeadless) {
4059 Slog.e(TAG, "Starting activities not supported on headless device: " + hr);
4060 } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 didSomething = true;
4062 }
4063 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004064 Slog.w(TAG, "Exception in new application when starting activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 + hr.intent.getComponent().flattenToShortString(), e);
4066 badApp = true;
4067 }
4068 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004069 mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 }
4071 }
4072
4073 // Find any services that should be running in this process...
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004074 if (!badApp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 try {
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004076 didSomething |= mServices.attachApplicationLocked(app, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 } catch (Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 badApp = true;
4079 }
4080 }
4081
Christopher Tatef46723b2012-01-26 14:19:24 -08004082 // Check if a next-broadcast receiver is in this process...
4083 if (!badApp && isPendingBroadcastProcessLocked(pid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 try {
Christopher Tatef46723b2012-01-26 14:19:24 -08004085 didSomething = sendPendingBroadcastsLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 } catch (Exception e) {
Christopher Tatef46723b2012-01-26 14:19:24 -08004087 // If the app died trying to launch the receiver we declare it 'bad'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 badApp = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 }
4090 }
4091
Christopher Tate181fafa2009-05-14 11:12:14 -07004092 // Check whether the next backup agent is in this process...
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004093 if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004094 if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07004095 ensurePackageDexOpt(mBackupTarget.appInfo.packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -07004096 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004097 thread.scheduleCreateBackupAgent(mBackupTarget.appInfo,
4098 compatibilityInfoForPackageLocked(mBackupTarget.appInfo),
4099 mBackupTarget.backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -07004100 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004101 Slog.w(TAG, "Exception scheduling backup agent creation: ");
Christopher Tate181fafa2009-05-14 11:12:14 -07004102 e.printStackTrace();
4103 }
4104 }
4105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 if (badApp) {
4107 // todo: Also need to kill application to deal with all
4108 // kinds of exceptions.
Dianne Hackborn130b0d22011-07-26 22:07:48 -07004109 handleAppDiedLocked(app, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 return false;
4111 }
4112
4113 if (!didSomething) {
4114 updateOomAdjLocked();
4115 }
4116
4117 return true;
4118 }
4119
4120 public final void attachApplication(IApplicationThread thread) {
4121 synchronized (this) {
4122 int callingPid = Binder.getCallingPid();
4123 final long origId = Binder.clearCallingIdentity();
4124 attachApplicationLocked(thread, callingPid);
4125 Binder.restoreCallingIdentity(origId);
4126 }
4127 }
4128
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004129 public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004131 ActivityRecord r = mMainStack.activityIdleInternal(token, false, config);
4132 if (stopProfiling) {
4133 synchronized (this) {
4134 if (mProfileProc == r.app) {
4135 if (mProfileFd != null) {
4136 try {
4137 mProfileFd.close();
4138 } catch (IOException e) {
4139 }
4140 clearProfilerLocked();
4141 }
4142 }
4143 }
4144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 Binder.restoreCallingIdentity(origId);
4146 }
4147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 void enableScreenAfterBoot() {
Doug Zongker2bec3d42009-12-04 12:52:44 -08004149 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004150 SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 mWindowManager.enableScreenAfterBoot();
Jeff Brownc042ee22012-05-08 13:03:42 -07004152
4153 synchronized (this) {
4154 updateEventDispatchingLocked();
4155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
4157
Dianne Hackborn661cd522011-08-22 00:26:20 -07004158 public void showBootMessage(final CharSequence msg, final boolean always) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004159 enforceNotIsolatedCaller("showBootMessage");
Dianne Hackborn661cd522011-08-22 00:26:20 -07004160 mWindowManager.showBootMessage(msg, always);
4161 }
4162
Dianne Hackborn90c52de2011-09-23 12:57:44 -07004163 public void dismissKeyguardOnNextActivity() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004164 enforceNotIsolatedCaller("dismissKeyguardOnNextActivity");
Jeff Sharkey7ffaa982012-04-30 16:59:05 -07004165 final long token = Binder.clearCallingIdentity();
4166 try {
4167 synchronized (this) {
4168 if (mLockScreenShown) {
4169 mLockScreenShown = false;
4170 comeOutOfSleepIfNeededLocked();
4171 }
4172 mMainStack.dismissKeyguardOnNextActivityLocked();
Dianne Hackborn1e88e982012-04-24 18:35:55 -07004173 }
Jeff Sharkey7ffaa982012-04-30 16:59:05 -07004174 } finally {
4175 Binder.restoreCallingIdentity(token);
Dianne Hackborn90c52de2011-09-23 12:57:44 -07004176 }
4177 }
4178
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004179 final void finishBooting() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004180 IntentFilter pkgFilter = new IntentFilter();
4181 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
4182 pkgFilter.addDataScheme("package");
4183 mContext.registerReceiver(new BroadcastReceiver() {
4184 @Override
4185 public void onReceive(Context context, Intent intent) {
4186 String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
4187 if (pkgs != null) {
4188 for (String pkg : pkgs) {
Vairavan Srinivasan61f07652010-07-22 13:36:40 -07004189 synchronized (ActivityManagerService.this) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07004190 if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) {
4191 setResultCode(Activity.RESULT_OK);
4192 return;
4193 }
4194 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004195 }
4196 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004197 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004198 }, pkgFilter);
Amith Yamasani13593602012-03-22 16:16:17 -07004199
4200 IntentFilter userFilter = new IntentFilter();
4201 userFilter.addAction(Intent.ACTION_USER_REMOVED);
4202 mContext.registerReceiver(new BroadcastReceiver() {
4203 @Override
4204 public void onReceive(Context context, Intent intent) {
4205 onUserRemoved(intent);
4206 }
4207 }, userFilter);
4208
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004209 synchronized (this) {
4210 // Ensure that any processes we had put on hold are now started
4211 // up.
4212 final int NP = mProcessesOnHold.size();
4213 if (NP > 0) {
4214 ArrayList<ProcessRecord> procs =
4215 new ArrayList<ProcessRecord>(mProcessesOnHold);
4216 for (int ip=0; ip<NP; ip++) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07004217 if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: "
4218 + procs.get(ip));
4219 startProcessLocked(procs.get(ip), "on-hold", null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004220 }
4221 }
4222
4223 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004224 // Start looking for apps that are abusing wake locks.
4225 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07004226 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004227 // Tell anyone interested that we are done booting!
Dianne Hackbornf4c454b2010-08-11 12:47:41 -07004228 SystemProperties.set("sys.boot_completed", "1");
Guang Zhu191713a2012-01-12 12:02:22 -08004229 SystemProperties.set("dev.bootcomplete", "1");
Amith Yamasani258848d2012-08-10 17:06:33 -07004230 List<UserInfo> users = getUserManager().getUsers();
4231 for (UserInfo user : users) {
4232 broadcastIntentLocked(null, null,
4233 new Intent(Intent.ACTION_BOOT_COMPLETED, null),
4234 null, null, 0, null, null,
4235 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
4236 false, false, MY_PID, Process.SYSTEM_UID, user.id);
Amith Yamasani4860cfc2012-08-08 19:14:18 -07004237 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004238 }
4239 }
4240 }
4241
4242 final void ensureBootCompleted() {
4243 boolean booting;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004244 boolean enableScreen;
4245 synchronized (this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004246 booting = mBooting;
4247 mBooting = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004248 enableScreen = !mBooted;
4249 mBooted = true;
4250 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004251
4252 if (booting) {
4253 finishBooting();
4254 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004255
4256 if (enableScreen) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004257 enableScreenAfterBoot();
4258 }
4259 }
4260
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004261 public final void activityPaused(IBinder token) {
4262 final long origId = Binder.clearCallingIdentity();
4263 mMainStack.activityPaused(token, false);
4264 Binder.restoreCallingIdentity(origId);
4265 }
4266
4267 public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail,
4268 CharSequence description) {
4269 if (localLOGV) Slog.v(
4270 TAG, "Activity stopped: token=" + token);
4271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 // Refuse possible leaked file descriptors
4273 if (icicle != null && icicle.hasFileDescriptors()) {
4274 throw new IllegalArgumentException("File descriptors passed in Bundle");
4275 }
4276
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004277 ActivityRecord r = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278
4279 final long origId = Binder.clearCallingIdentity();
4280
4281 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004282 r = mMainStack.isInStackLocked(token);
4283 if (r != null) {
4284 r.stack.activityStoppedLocked(r, icicle, thumbnail, description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
4286 }
4287
4288 if (r != null) {
4289 sendPendingThumbnail(r, null, null, null, false);
4290 }
4291
4292 trimApplications();
4293
4294 Binder.restoreCallingIdentity(origId);
4295 }
4296
4297 public final void activityDestroyed(IBinder token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004298 if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299 mMainStack.activityDestroyed(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300 }
4301
4302 public String getCallingPackage(IBinder token) {
4303 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004304 ActivityRecord r = getCallingRecordLocked(token);
Dianne Hackborn9bbcb912009-10-20 15:42:38 -07004305 return r != null && r.app != null ? r.info.packageName : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 }
4307 }
4308
4309 public ComponentName getCallingActivity(IBinder token) {
4310 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004311 ActivityRecord r = getCallingRecordLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 return r != null ? r.intent.getComponent() : null;
4313 }
4314 }
4315
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004316 private ActivityRecord getCallingRecordLocked(IBinder token) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004317 ActivityRecord r = mMainStack.isInStackLocked(token);
4318 if (r == null) {
4319 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004321 return r.resultTo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 }
4323
4324 public ComponentName getActivityClassForToken(IBinder token) {
4325 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004326 ActivityRecord r = mMainStack.isInStackLocked(token);
4327 if (r == null) {
4328 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004330 return r.intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 }
4332 }
4333
4334 public String getPackageForToken(IBinder token) {
4335 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004336 ActivityRecord r = mMainStack.isInStackLocked(token);
4337 if (r == null) {
4338 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004340 return r.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 }
4342 }
4343
4344 public IIntentSender getIntentSender(int type,
4345 String packageName, IBinder token, String resultWho,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004346 int requestCode, Intent[] intents, String[] resolvedTypes,
4347 int flags, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004348 enforceNotIsolatedCaller("getIntentSender");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 // Refuse possible leaked file descriptors
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004350 if (intents != null) {
4351 if (intents.length < 1) {
4352 throw new IllegalArgumentException("Intents array length must be >= 1");
4353 }
4354 for (int i=0; i<intents.length; i++) {
4355 Intent intent = intents[i];
Dianne Hackborn52b0ce02011-04-14 13:09:32 -07004356 if (intent != null) {
4357 if (intent.hasFileDescriptors()) {
4358 throw new IllegalArgumentException("File descriptors passed in Intent");
4359 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004360 if (type == ActivityManager.INTENT_SENDER_BROADCAST &&
Dianne Hackborn52b0ce02011-04-14 13:09:32 -07004361 (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
4362 throw new IllegalArgumentException(
4363 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
4364 }
4365 intents[i] = new Intent(intent);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004366 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004367 }
4368 if (resolvedTypes != null && resolvedTypes.length != intents.length) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004369 throw new IllegalArgumentException(
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004370 "Intent array length does not match resolvedTypes length");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004371 }
4372 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004373 if (options != null) {
4374 if (options.hasFileDescriptors()) {
4375 throw new IllegalArgumentException("File descriptors passed in options");
4376 }
4377 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 synchronized(this) {
4380 int callingUid = Binder.getCallingUid();
4381 try {
Jeff Brown10e89712011-07-08 18:52:57 -07004382 if (callingUid != 0 && callingUid != Process.SYSTEM_UID) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004383 int uid = AppGlobals.getPackageManager()
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004384 .getPackageUid(packageName, UserHandle.getUserId(callingUid));
4385 if (!UserHandle.isSameApp(callingUid, uid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 String msg = "Permission Denial: getIntentSender() from pid="
4387 + Binder.getCallingPid()
4388 + ", uid=" + Binder.getCallingUid()
4389 + ", (need uid=" + uid + ")"
4390 + " is not allowed to send as package " + packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004391 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 throw new SecurityException(msg);
4393 }
4394 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004395
Amith Yamasani742a6712011-05-04 14:49:28 -07004396 if (DEBUG_MU)
4397 Slog.i(TAG_MU, "Getting intent sender for origCallingUid="
4398 + Binder.getOrigCallingUid());
4399 return getIntentSenderLocked(type, packageName, Binder.getOrigCallingUid(),
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004400 token, resultWho, requestCode, intents, resolvedTypes, flags, options);
Dianne Hackborn860755f2010-06-03 18:47:52 -07004401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 } catch (RemoteException e) {
4403 throw new SecurityException(e);
4404 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004405 }
4406 }
4407
4408 IIntentSender getIntentSenderLocked(int type,
4409 String packageName, int callingUid, IBinder token, String resultWho,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004410 int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
4411 Bundle options) {
Amith Yamasani742a6712011-05-04 14:49:28 -07004412 if (DEBUG_MU)
4413 Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004414 ActivityRecord activity = null;
Dianne Hackborna4972e92012-03-14 10:38:05 -07004415 if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004416 activity = mMainStack.isInStackLocked(token);
4417 if (activity == null) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07004418 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004420 if (activity.finishing) {
4421 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004423 }
4424
4425 final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
4426 final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
4427 final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
4428 flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
4429 |PendingIntent.FLAG_UPDATE_CURRENT);
4430
4431 PendingIntentRecord.Key key = new PendingIntentRecord.Key(
4432 type, packageName, activity, resultWho,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004433 requestCode, intents, resolvedTypes, flags, options);
Dianne Hackborn860755f2010-06-03 18:47:52 -07004434 WeakReference<PendingIntentRecord> ref;
4435 ref = mIntentSenderRecords.get(key);
4436 PendingIntentRecord rec = ref != null ? ref.get() : null;
4437 if (rec != null) {
4438 if (!cancelCurrent) {
4439 if (updateCurrent) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004440 if (rec.key.requestIntent != null) {
Adam Powell501d4a52012-04-30 15:03:57 -07004441 rec.key.requestIntent.replaceExtras(intents != null ?
4442 intents[intents.length - 1] : null);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004443 }
4444 if (intents != null) {
4445 intents[intents.length-1] = rec.key.requestIntent;
4446 rec.key.allIntents = intents;
4447 rec.key.allResolvedTypes = resolvedTypes;
4448 } else {
4449 rec.key.allIntents = null;
4450 rec.key.allResolvedTypes = null;
4451 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 return rec;
4454 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004455 rec.canceled = true;
4456 mIntentSenderRecords.remove(key);
4457 }
4458 if (noCreate) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 return rec;
4460 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004461 rec = new PendingIntentRecord(this, key, callingUid);
4462 mIntentSenderRecords.put(key, rec.ref);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004463 if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07004464 if (activity.pendingResults == null) {
4465 activity.pendingResults
4466 = new HashSet<WeakReference<PendingIntentRecord>>();
4467 }
4468 activity.pendingResults.add(rec.ref);
4469 }
4470 return rec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 }
4472
4473 public void cancelIntentSender(IIntentSender sender) {
4474 if (!(sender instanceof PendingIntentRecord)) {
4475 return;
4476 }
4477 synchronized(this) {
4478 PendingIntentRecord rec = (PendingIntentRecord)sender;
4479 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004480 int uid = AppGlobals.getPackageManager()
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004481 .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId());
4482 if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 String msg = "Permission Denial: cancelIntentSender() from pid="
4484 + Binder.getCallingPid()
4485 + ", uid=" + Binder.getCallingUid()
4486 + " is not allowed to cancel packges "
4487 + rec.key.packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004488 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 throw new SecurityException(msg);
4490 }
4491 } catch (RemoteException e) {
4492 throw new SecurityException(e);
4493 }
4494 cancelIntentSenderLocked(rec, true);
4495 }
4496 }
4497
4498 void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) {
4499 rec.canceled = true;
4500 mIntentSenderRecords.remove(rec.key);
4501 if (cleanActivity && rec.key.activity != null) {
4502 rec.key.activity.pendingResults.remove(rec.ref);
4503 }
4504 }
4505
4506 public String getPackageForIntentSender(IIntentSender pendingResult) {
4507 if (!(pendingResult instanceof PendingIntentRecord)) {
4508 return null;
4509 }
Brad Fitzpatrickb213d102010-04-19 11:58:52 -07004510 try {
4511 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4512 return res.key.packageName;
4513 } catch (ClassCastException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 }
4515 return null;
4516 }
4517
Christopher Tatec4a07d12012-04-06 14:19:13 -07004518 public int getUidForIntentSender(IIntentSender sender) {
4519 if (sender instanceof PendingIntentRecord) {
4520 try {
4521 PendingIntentRecord res = (PendingIntentRecord)sender;
4522 return res.uid;
4523 } catch (ClassCastException e) {
4524 }
4525 }
4526 return -1;
4527 }
4528
Dianne Hackborn6c418d52011-06-29 14:05:33 -07004529 public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) {
4530 if (!(pendingResult instanceof PendingIntentRecord)) {
4531 return false;
4532 }
4533 try {
4534 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4535 if (res.key.allIntents == null) {
4536 return false;
4537 }
4538 for (int i=0; i<res.key.allIntents.length; i++) {
4539 Intent intent = res.key.allIntents[i];
4540 if (intent.getPackage() != null && intent.getComponent() != null) {
4541 return false;
4542 }
4543 }
4544 return true;
4545 } catch (ClassCastException e) {
4546 }
4547 return false;
4548 }
4549
Dianne Hackborn1927ae82012-06-22 15:21:36 -07004550 public boolean isIntentSenderAnActivity(IIntentSender pendingResult) {
4551 if (!(pendingResult instanceof PendingIntentRecord)) {
4552 return false;
4553 }
4554 try {
4555 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4556 if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) {
4557 return true;
4558 }
4559 return false;
4560 } catch (ClassCastException e) {
4561 }
4562 return false;
4563 }
4564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 public void setProcessLimit(int max) {
4566 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
4567 "setProcessLimit()");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004568 synchronized (this) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07004569 mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004570 mProcessLimitOverride = max;
4571 }
4572 trimApplications();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573 }
4574
4575 public int getProcessLimit() {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004576 synchronized (this) {
4577 return mProcessLimitOverride;
4578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 }
4580
4581 void foregroundTokenDied(ForegroundToken token) {
4582 synchronized (ActivityManagerService.this) {
4583 synchronized (mPidsSelfLocked) {
4584 ForegroundToken cur
4585 = mForegroundProcesses.get(token.pid);
4586 if (cur != token) {
4587 return;
4588 }
4589 mForegroundProcesses.remove(token.pid);
4590 ProcessRecord pr = mPidsSelfLocked.get(token.pid);
4591 if (pr == null) {
4592 return;
4593 }
4594 pr.forcingToForeground = null;
4595 pr.foregroundServices = false;
4596 }
4597 updateOomAdjLocked();
4598 }
4599 }
4600
4601 public void setProcessForeground(IBinder token, int pid, boolean isForeground) {
4602 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
4603 "setProcessForeground()");
4604 synchronized(this) {
4605 boolean changed = false;
4606
4607 synchronized (mPidsSelfLocked) {
4608 ProcessRecord pr = mPidsSelfLocked.get(pid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004609 if (pr == null && isForeground) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004610 Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 return;
4612 }
4613 ForegroundToken oldToken = mForegroundProcesses.get(pid);
4614 if (oldToken != null) {
4615 oldToken.token.unlinkToDeath(oldToken, 0);
4616 mForegroundProcesses.remove(pid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004617 if (pr != null) {
4618 pr.forcingToForeground = null;
4619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 changed = true;
4621 }
4622 if (isForeground && token != null) {
4623 ForegroundToken newToken = new ForegroundToken() {
4624 public void binderDied() {
4625 foregroundTokenDied(this);
4626 }
4627 };
4628 newToken.pid = pid;
4629 newToken.token = token;
4630 try {
4631 token.linkToDeath(newToken, 0);
4632 mForegroundProcesses.put(pid, newToken);
4633 pr.forcingToForeground = token;
4634 changed = true;
4635 } catch (RemoteException e) {
4636 // If the process died while doing this, we will later
4637 // do the cleanup with the process death link.
4638 }
4639 }
4640 }
4641
4642 if (changed) {
4643 updateOomAdjLocked();
4644 }
4645 }
4646 }
4647
4648 // =========================================================
4649 // PERMISSIONS
4650 // =========================================================
4651
4652 static class PermissionController extends IPermissionController.Stub {
4653 ActivityManagerService mActivityManagerService;
4654 PermissionController(ActivityManagerService activityManagerService) {
4655 mActivityManagerService = activityManagerService;
4656 }
4657
4658 public boolean checkPermission(String permission, int pid, int uid) {
4659 return mActivityManagerService.checkPermission(permission, pid,
4660 uid) == PackageManager.PERMISSION_GRANTED;
4661 }
4662 }
4663
4664 /**
4665 * This can be called with or without the global lock held.
4666 */
4667 int checkComponentPermission(String permission, int pid, int uid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08004668 int owningUid, boolean exported) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 // We might be performing an operation on behalf of an indirect binder
4670 // invocation, e.g. via {@link #openContentUri}. Check and adjust the
4671 // client identity accordingly before proceeding.
4672 Identity tlsIdentity = sCallerIdentity.get();
4673 if (tlsIdentity != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004674 Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 + tlsIdentity.pid + "," + tlsIdentity.uid + "}");
4676 uid = tlsIdentity.uid;
4677 pid = tlsIdentity.pid;
4678 }
4679
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004680 if (pid == MY_PID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 return PackageManager.PERMISSION_GRANTED;
4682 }
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004683
4684 return ActivityManager.checkComponentPermission(permission, uid,
4685 owningUid, exported);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 }
4687
4688 /**
4689 * As the only public entry point for permissions checking, this method
4690 * can enforce the semantic that requesting a check on a null global
4691 * permission is automatically denied. (Internally a null permission
4692 * string is used when calling {@link #checkComponentPermission} in cases
4693 * when only uid-based security is needed.)
4694 *
4695 * This can be called with or without the global lock held.
4696 */
4697 public int checkPermission(String permission, int pid, int uid) {
4698 if (permission == null) {
4699 return PackageManager.PERMISSION_DENIED;
4700 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004701 return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004702 }
4703
4704 /**
4705 * Binder IPC calls go through the public entry point.
4706 * This can be called with or without the global lock held.
4707 */
4708 int checkCallingPermission(String permission) {
4709 return checkPermission(permission,
4710 Binder.getCallingPid(),
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004711 UserHandle.getAppId(Binder.getCallingUid()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004712 }
4713
4714 /**
4715 * This can be called with or without the global lock held.
4716 */
4717 void enforceCallingPermission(String permission, String func) {
4718 if (checkCallingPermission(permission)
4719 == PackageManager.PERMISSION_GRANTED) {
4720 return;
4721 }
4722
4723 String msg = "Permission Denial: " + func + " from pid="
4724 + Binder.getCallingPid()
4725 + ", uid=" + Binder.getCallingUid()
4726 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004727 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 throw new SecurityException(msg);
4729 }
4730
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004731 /**
4732 * Determine if UID is holding permissions required to access {@link Uri} in
4733 * the given {@link ProviderInfo}. Final permission checking is always done
4734 * in {@link ContentProvider}.
4735 */
4736 private final boolean checkHoldingPermissionsLocked(
4737 IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004738 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4739 "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid);
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004740
4741 if (pi.applicationInfo.uid == uid) {
4742 return true;
4743 } else if (!pi.exported) {
4744 return false;
4745 }
4746
4747 boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0;
4748 boolean writeMet = (modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004749 try {
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004750 // check if target holds top-level <provider> permissions
4751 if (!readMet && pi.readPermission != null
4752 && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) {
4753 readMet = true;
4754 }
4755 if (!writeMet && pi.writePermission != null
4756 && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) {
4757 writeMet = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004759
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004760 // track if unprotected read/write is allowed; any denied
4761 // <path-permission> below removes this ability
4762 boolean allowDefaultRead = pi.readPermission == null;
4763 boolean allowDefaultWrite = pi.writePermission == null;
Dianne Hackborn48058e82010-09-27 16:53:23 -07004764
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004765 // check if target holds any <path-permission> that match uri
4766 final PathPermission[] pps = pi.pathPermissions;
4767 if (pps != null) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004768 final String path = uri.getPath();
4769 int i = pps.length;
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004770 while (i > 0 && (!readMet || !writeMet)) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004771 i--;
4772 PathPermission pp = pps[i];
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004773 if (pp.match(path)) {
4774 if (!readMet) {
4775 final String pprperm = pp.getReadPermission();
4776 if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for "
4777 + pprperm + " for " + pp.getPath()
4778 + ": match=" + pp.match(path)
4779 + " check=" + pm.checkUidPermission(pprperm, uid));
4780 if (pprperm != null) {
4781 if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) {
4782 readMet = true;
4783 } else {
4784 allowDefaultRead = false;
4785 }
4786 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004787 }
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004788 if (!writeMet) {
4789 final String ppwperm = pp.getWritePermission();
4790 if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm "
4791 + ppwperm + " for " + pp.getPath()
4792 + ": match=" + pp.match(path)
4793 + " check=" + pm.checkUidPermission(ppwperm, uid));
4794 if (ppwperm != null) {
4795 if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) {
4796 writeMet = true;
4797 } else {
4798 allowDefaultWrite = false;
4799 }
4800 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004801 }
4802 }
4803 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07004804 }
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004805
4806 // grant unprotected <provider> read/write, if not blocked by
4807 // <path-permission> above
4808 if (allowDefaultRead) readMet = true;
4809 if (allowDefaultWrite) writeMet = true;
4810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004811 } catch (RemoteException e) {
4812 return false;
4813 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004814
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004815 return readMet && writeMet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 }
4817
4818 private final boolean checkUriPermissionLocked(Uri uri, int uid,
4819 int modeFlags) {
4820 // Root gets to do everything.
Jeff Brown10e89712011-07-08 18:52:57 -07004821 if (uid == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 return true;
4823 }
4824 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid);
4825 if (perms == null) return false;
4826 UriPermission perm = perms.get(uri);
4827 if (perm == null) return false;
4828 return (modeFlags&perm.modeFlags) == modeFlags;
4829 }
4830
4831 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004832 enforceNotIsolatedCaller("checkUriPermission");
4833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 // Another redirected-binder-call permissions check as in
4835 // {@link checkComponentPermission}.
4836 Identity tlsIdentity = sCallerIdentity.get();
4837 if (tlsIdentity != null) {
4838 uid = tlsIdentity.uid;
4839 pid = tlsIdentity.pid;
4840 }
4841
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004842 uid = UserHandle.getAppId(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843 // Our own process gets to do everything.
4844 if (pid == MY_PID) {
4845 return PackageManager.PERMISSION_GRANTED;
4846 }
4847 synchronized(this) {
4848 return checkUriPermissionLocked(uri, uid, modeFlags)
4849 ? PackageManager.PERMISSION_GRANTED
4850 : PackageManager.PERMISSION_DENIED;
4851 }
4852 }
4853
Dianne Hackborn39792d22010-08-19 18:01:52 -07004854 /**
4855 * Check if the targetPkg can be granted permission to access uri by
4856 * the callingUid using the given modeFlags. Throws a security exception
4857 * if callingUid is not allowed to do this. Returns the uid of the target
4858 * if the URI permission grant should be performed; returns -1 if it is not
4859 * needed (for example targetPkg already has permission to access the URI).
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004860 * If you already know the uid of the target, you can supply it in
4861 * lastTargetUid else set that to -1.
Dianne Hackborn39792d22010-08-19 18:01:52 -07004862 */
4863 int checkGrantUriPermissionLocked(int callingUid, String targetPkg,
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004864 Uri uri, int modeFlags, int lastTargetUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4866 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4867 if (modeFlags == 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004868 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 }
4870
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004871 if (targetPkg != null) {
4872 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4873 "Checking grant " + targetPkg + " permission to " + uri);
4874 }
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004875
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004876 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877
4878 // If this is not a content: uri, we can't do anything with it.
4879 if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004880 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004881 "Can't grant URI permission for non-content URI: " + uri);
Dianne Hackborn39792d22010-08-19 18:01:52 -07004882 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 }
4884
4885 String name = uri.getAuthority();
4886 ProviderInfo pi = null;
Amith Yamasani742a6712011-05-04 14:49:28 -07004887 ContentProviderRecord cpr = mProviderMap.getProviderByName(name,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004888 UserHandle.getUserId(callingUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 if (cpr != null) {
4890 pi = cpr.info;
4891 } else {
4892 try {
4893 pi = pm.resolveContentProvider(name,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004894 PackageManager.GET_URI_PERMISSION_PATTERNS, UserHandle.getUserId(callingUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 } catch (RemoteException ex) {
4896 }
4897 }
4898 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07004899 Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString());
Dianne Hackborn39792d22010-08-19 18:01:52 -07004900 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 }
4902
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004903 int targetUid = lastTargetUid;
4904 if (targetUid < 0 && targetPkg != null) {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004905 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004906 targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid));
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004907 if (targetUid < 0) {
4908 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4909 "Can't grant URI permission no uid for: " + targetPkg);
4910 return -1;
4911 }
4912 } catch (RemoteException ex) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004913 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 }
4916
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004917 if (targetUid >= 0) {
4918 // First... does the target actually need this permission?
4919 if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) {
4920 // No need to grant the target this permission.
4921 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4922 "Target " + targetPkg + " already has full permission to " + uri);
4923 return -1;
4924 }
4925 } else {
4926 // First... there is no target package, so can anyone access it?
4927 boolean allowed = pi.exported;
4928 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
4929 if (pi.readPermission != null) {
4930 allowed = false;
4931 }
4932 }
4933 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
4934 if (pi.writePermission != null) {
4935 allowed = false;
4936 }
4937 }
4938 if (allowed) {
4939 return -1;
4940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 }
4942
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004943 // Second... is the provider allowing granting of URI permissions?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 if (!pi.grantUriPermissions) {
4945 throw new SecurityException("Provider " + pi.packageName
4946 + "/" + pi.name
4947 + " does not allow granting of Uri permissions (uri "
4948 + uri + ")");
4949 }
4950 if (pi.uriPermissionPatterns != null) {
4951 final int N = pi.uriPermissionPatterns.length;
4952 boolean allowed = false;
4953 for (int i=0; i<N; i++) {
4954 if (pi.uriPermissionPatterns[i] != null
4955 && pi.uriPermissionPatterns[i].match(uri.getPath())) {
4956 allowed = true;
4957 break;
4958 }
4959 }
4960 if (!allowed) {
4961 throw new SecurityException("Provider " + pi.packageName
4962 + "/" + pi.name
4963 + " does not allow granting of permission to path of Uri "
4964 + uri);
4965 }
4966 }
4967
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004968 // Third... does the caller itself have permission to access
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 // this uri?
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004970 if (callingUid != Process.myUid()) {
4971 if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) {
4972 if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
4973 throw new SecurityException("Uid " + callingUid
4974 + " does not have permission to uri " + uri);
4975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 }
4977 }
4978
Dianne Hackborn39792d22010-08-19 18:01:52 -07004979 return targetUid;
4980 }
4981
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004982 public int checkGrantUriPermission(int callingUid, String targetPkg,
4983 Uri uri, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004984 enforceNotIsolatedCaller("checkGrantUriPermission");
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004985 synchronized(this) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004986 return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1);
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004987 }
4988 }
4989
Dianne Hackborn39792d22010-08-19 18:01:52 -07004990 void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg,
4991 Uri uri, int modeFlags, UriPermissionOwner owner) {
4992 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4993 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4994 if (modeFlags == 0) {
4995 return;
4996 }
4997
4998 // So here we are: the caller has the assumed permission
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 // to the uri, and the target doesn't. Let's now give this to
5000 // the target.
5001
Joe Onorato8a9b2202010-02-26 18:56:32 -08005002 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn39792d22010-08-19 18:01:52 -07005003 "Granting " + targetPkg + "/" + targetUid + " permission to " + uri);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 HashMap<Uri, UriPermission> targetUris
5006 = mGrantedUriPermissions.get(targetUid);
5007 if (targetUris == null) {
5008 targetUris = new HashMap<Uri, UriPermission>();
5009 mGrantedUriPermissions.put(targetUid, targetUris);
5010 }
5011
5012 UriPermission perm = targetUris.get(uri);
5013 if (perm == null) {
5014 perm = new UriPermission(targetUid, uri);
5015 targetUris.put(uri, perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07005017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 perm.modeFlags |= modeFlags;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005019 if (owner == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 perm.globalModeFlags |= modeFlags;
Vairavan Srinivasan91c12c22011-01-21 18:26:06 -08005021 } else {
5022 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
5023 perm.readOwners.add(owner);
5024 owner.addReadPermission(perm);
5025 }
5026 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
5027 perm.writeOwners.add(owner);
5028 owner.addWritePermission(perm);
5029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 }
5031 }
5032
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005033 void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri,
5034 int modeFlags, UriPermissionOwner owner) {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005035 if (targetPkg == null) {
5036 throw new NullPointerException("targetPkg");
5037 }
5038
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005039 int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1);
Dianne Hackborn39792d22010-08-19 18:01:52 -07005040 if (targetUid < 0) {
5041 return;
5042 }
5043
5044 grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner);
5045 }
5046
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005047 static class NeededUriGrants extends ArrayList<Uri> {
5048 final String targetPkg;
5049 final int targetUid;
5050 final int flags;
5051
5052 NeededUriGrants(String _targetPkg, int _targetUid, int _flags) {
5053 targetPkg = _targetPkg;
5054 targetUid = _targetUid;
5055 flags = _flags;
5056 }
5057 }
5058
Dianne Hackborn39792d22010-08-19 18:01:52 -07005059 /**
5060 * Like checkGrantUriPermissionLocked, but takes an Intent.
5061 */
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005062 NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid,
5063 String targetPkg, Intent intent, int mode, NeededUriGrants needed) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07005064 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005065 "Checking URI perm to data=" + (intent != null ? intent.getData() : null)
5066 + " clip=" + (intent != null ? intent.getClipData() : null)
Dianne Hackbornb424b632010-08-18 15:59:05 -07005067 + " from " + intent + "; flags=0x"
5068 + Integer.toHexString(intent != null ? intent.getFlags() : 0));
5069
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005070 if (targetPkg == null) {
5071 throw new NullPointerException("targetPkg");
5072 }
5073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 if (intent == null) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005075 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 }
5077 Uri data = intent.getData();
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005078 ClipData clip = intent.getClipData();
5079 if (data == null && clip == null) {
5080 return null;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005081 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005082 if (data != null) {
5083 int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data,
5084 mode, needed != null ? needed.targetUid : -1);
5085 if (target > 0) {
5086 if (needed == null) {
5087 needed = new NeededUriGrants(targetPkg, target, mode);
5088 }
5089 needed.add(data);
5090 }
5091 }
5092 if (clip != null) {
5093 for (int i=0; i<clip.getItemCount(); i++) {
5094 Uri uri = clip.getItemAt(i).getUri();
5095 if (uri != null) {
5096 int target = -1;
5097 target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri,
5098 mode, needed != null ? needed.targetUid : -1);
5099 if (target > 0) {
5100 if (needed == null) {
5101 needed = new NeededUriGrants(targetPkg, target, mode);
5102 }
5103 needed.add(uri);
5104 }
5105 } else {
5106 Intent clipIntent = clip.getItemAt(i).getIntent();
5107 if (clipIntent != null) {
5108 NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked(
5109 callingUid, targetPkg, clipIntent, mode, needed);
5110 if (newNeeded != null) {
5111 needed = newNeeded;
5112 }
5113 }
5114 }
5115 }
5116 }
5117
5118 return needed;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005119 }
5120
5121 /**
5122 * Like grantUriPermissionUncheckedLocked, but takes an Intent.
5123 */
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005124 void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed,
5125 UriPermissionOwner owner) {
5126 if (needed != null) {
5127 for (int i=0; i<needed.size(); i++) {
5128 grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg,
5129 needed.get(i), needed.flags, owner);
5130 }
5131 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07005132 }
5133
5134 void grantUriPermissionFromIntentLocked(int callingUid,
5135 String targetPkg, Intent intent, UriPermissionOwner owner) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005136 NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg,
Dianne Hackbornd9781fe2012-03-08 18:04:18 -08005137 intent, intent != null ? intent.getFlags() : 0, null);
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005138 if (needed == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 return;
5140 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07005141
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005142 grantUriPermissionUncheckedFromIntentLocked(needed, owner);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 }
5144
5145 public void grantUriPermission(IApplicationThread caller, String targetPkg,
5146 Uri uri, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005147 enforceNotIsolatedCaller("grantUriPermission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 synchronized(this) {
5149 final ProcessRecord r = getRecordForAppLocked(caller);
5150 if (r == null) {
5151 throw new SecurityException("Unable to find app for caller "
5152 + caller
5153 + " when granting permission to uri " + uri);
5154 }
5155 if (targetPkg == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -07005156 throw new IllegalArgumentException("null target");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 }
5158 if (uri == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -07005159 throw new IllegalArgumentException("null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 }
5161
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005162 grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005163 null);
5164 }
5165 }
5166
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005167 void removeUriPermissionIfNeededLocked(UriPermission perm) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION
5169 |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) {
5170 HashMap<Uri, UriPermission> perms
5171 = mGrantedUriPermissions.get(perm.uid);
5172 if (perms != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005173 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005174 "Removing " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 perms.remove(perm.uri);
5176 if (perms.size() == 0) {
5177 mGrantedUriPermissions.remove(perm.uid);
5178 }
5179 }
5180 }
5181 }
5182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 private void revokeUriPermissionLocked(int callingUid, Uri uri,
5184 int modeFlags) {
5185 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5186 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5187 if (modeFlags == 0) {
5188 return;
5189 }
5190
Joe Onorato8a9b2202010-02-26 18:56:32 -08005191 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005192 "Revoking all granted permissions to " + uri);
5193
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005194 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195
5196 final String authority = uri.getAuthority();
5197 ProviderInfo pi = null;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005198 int userId = UserHandle.getUserId(callingUid);
Amith Yamasani483f3b02012-03-13 16:08:00 -07005199 ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 if (cpr != null) {
5201 pi = cpr.info;
5202 } else {
5203 try {
5204 pi = pm.resolveContentProvider(authority,
Amith Yamasani483f3b02012-03-13 16:08:00 -07005205 PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 } catch (RemoteException ex) {
5207 }
5208 }
5209 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07005210 Slog.w(TAG, "No content provider found for permission revoke: " + uri.toSafeString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 return;
5212 }
5213
5214 // Does the caller have this permission on the URI?
Dianne Hackborn48058e82010-09-27 16:53:23 -07005215 if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 // Right now, if you are not the original owner of the permission,
5217 // you are not allowed to revoke it.
5218 //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
5219 throw new SecurityException("Uid " + callingUid
5220 + " does not have permission to uri " + uri);
5221 //}
5222 }
5223
5224 // Go through all of the permissions and remove any that match.
5225 final List<String> SEGMENTS = uri.getPathSegments();
5226 if (SEGMENTS != null) {
5227 final int NS = SEGMENTS.size();
5228 int N = mGrantedUriPermissions.size();
5229 for (int i=0; i<N; i++) {
5230 HashMap<Uri, UriPermission> perms
5231 = mGrantedUriPermissions.valueAt(i);
5232 Iterator<UriPermission> it = perms.values().iterator();
5233 toploop:
5234 while (it.hasNext()) {
5235 UriPermission perm = it.next();
5236 Uri targetUri = perm.uri;
5237 if (!authority.equals(targetUri.getAuthority())) {
5238 continue;
5239 }
5240 List<String> targetSegments = targetUri.getPathSegments();
5241 if (targetSegments == null) {
5242 continue;
5243 }
5244 if (targetSegments.size() < NS) {
5245 continue;
5246 }
5247 for (int j=0; j<NS; j++) {
5248 if (!SEGMENTS.get(j).equals(targetSegments.get(j))) {
5249 continue toploop;
5250 }
5251 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005252 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005253 "Revoking " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 perm.clearModes(modeFlags);
5255 if (perm.modeFlags == 0) {
5256 it.remove();
5257 }
5258 }
5259 if (perms.size() == 0) {
5260 mGrantedUriPermissions.remove(
5261 mGrantedUriPermissions.keyAt(i));
5262 N--;
5263 i--;
5264 }
5265 }
5266 }
5267 }
5268
5269 public void revokeUriPermission(IApplicationThread caller, Uri uri,
5270 int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005271 enforceNotIsolatedCaller("revokeUriPermission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 synchronized(this) {
5273 final ProcessRecord r = getRecordForAppLocked(caller);
5274 if (r == null) {
5275 throw new SecurityException("Unable to find app for caller "
5276 + caller
5277 + " when revoking permission to uri " + uri);
5278 }
5279 if (uri == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005280 Slog.w(TAG, "revokeUriPermission: null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 return;
5282 }
5283
5284 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5285 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5286 if (modeFlags == 0) {
5287 return;
5288 }
5289
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005290 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291
5292 final String authority = uri.getAuthority();
5293 ProviderInfo pi = null;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005294 ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 if (cpr != null) {
5296 pi = cpr.info;
5297 } else {
5298 try {
5299 pi = pm.resolveContentProvider(authority,
Amith Yamasani483f3b02012-03-13 16:08:00 -07005300 PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 } catch (RemoteException ex) {
5302 }
5303 }
5304 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07005305 Slog.w(TAG, "No content provider found for permission revoke: "
5306 + uri.toSafeString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 return;
5308 }
5309
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005310 revokeUriPermissionLocked(r.uid, uri, modeFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 }
5312 }
5313
Dianne Hackborn7e269642010-08-25 19:50:20 -07005314 @Override
5315 public IBinder newUriPermissionOwner(String name) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005316 enforceNotIsolatedCaller("newUriPermissionOwner");
Dianne Hackborn7e269642010-08-25 19:50:20 -07005317 synchronized(this) {
5318 UriPermissionOwner owner = new UriPermissionOwner(this, name);
5319 return owner.getExternalTokenLocked();
5320 }
5321 }
5322
5323 @Override
5324 public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg,
5325 Uri uri, int modeFlags) {
5326 synchronized(this) {
5327 UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
5328 if (owner == null) {
5329 throw new IllegalArgumentException("Unknown owner: " + token);
5330 }
5331 if (fromUid != Binder.getCallingUid()) {
5332 if (Binder.getCallingUid() != Process.myUid()) {
5333 // Only system code can grant URI permissions on behalf
5334 // of other users.
5335 throw new SecurityException("nice try");
5336 }
5337 }
5338 if (targetPkg == null) {
5339 throw new IllegalArgumentException("null target");
5340 }
5341 if (uri == null) {
5342 throw new IllegalArgumentException("null uri");
5343 }
5344
5345 grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner);
5346 }
5347 }
5348
5349 @Override
5350 public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) {
5351 synchronized(this) {
5352 UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
5353 if (owner == null) {
5354 throw new IllegalArgumentException("Unknown owner: " + token);
5355 }
5356
5357 if (uri == null) {
5358 owner.removeUriPermissionsLocked(mode);
5359 } else {
5360 owner.removeUriPermissionLocked(uri, mode);
5361 }
5362 }
5363 }
5364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 public void showWaitingForDebugger(IApplicationThread who, boolean waiting) {
5366 synchronized (this) {
5367 ProcessRecord app =
5368 who != null ? getRecordForAppLocked(who) : null;
5369 if (app == null) return;
5370
5371 Message msg = Message.obtain();
5372 msg.what = WAIT_FOR_DEBUGGER_MSG;
5373 msg.obj = app;
5374 msg.arg1 = waiting ? 1 : 0;
5375 mHandler.sendMessage(msg);
5376 }
5377 }
5378
5379 public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07005380 final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ);
5381 final long hiddenAppMem = mProcessList.getMemLevel(ProcessList.HIDDEN_APP_MIN_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 outInfo.availMem = Process.getFreeMemory();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07005383 outInfo.totalMem = Process.getTotalMemory();
Dianne Hackborn7d608422011-08-07 16:24:18 -07005384 outInfo.threshold = homeAppMem;
5385 outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2));
5386 outInfo.hiddenAppThreshold = hiddenAppMem;
5387 outInfo.secondaryServerThreshold = mProcessList.getMemLevel(
Dianne Hackborne02c88a2011-10-28 13:58:15 -07005388 ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07005389 outInfo.visibleAppThreshold = mProcessList.getMemLevel(
5390 ProcessList.VISIBLE_APP_ADJ);
5391 outInfo.foregroundAppThreshold = mProcessList.getMemLevel(
5392 ProcessList.FOREGROUND_APP_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 }
5394
5395 // =========================================================
5396 // TASK MANAGEMENT
5397 // =========================================================
5398
5399 public List getTasks(int maxNum, int flags,
5400 IThumbnailReceiver receiver) {
5401 ArrayList list = new ArrayList();
5402
5403 PendingThumbnailsRecord pending = null;
5404 IApplicationThread topThumbnail = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005405 ActivityRecord topRecord = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406
5407 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005408 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 TAG, "getTasks: max=" + maxNum + ", flags=" + flags
5410 + ", receiver=" + receiver);
5411
5412 if (checkCallingPermission(android.Manifest.permission.GET_TASKS)
5413 != PackageManager.PERMISSION_GRANTED) {
5414 if (receiver != null) {
5415 // If the caller wants to wait for pending thumbnails,
5416 // it ain't gonna get them.
5417 try {
5418 receiver.finished();
5419 } catch (RemoteException ex) {
5420 }
5421 }
5422 String msg = "Permission Denial: getTasks() from pid="
5423 + Binder.getCallingPid()
5424 + ", uid=" + Binder.getCallingUid()
5425 + " requires " + android.Manifest.permission.GET_TASKS;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005426 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 throw new SecurityException(msg);
5428 }
5429
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005430 int pos = mMainStack.mHistory.size()-1;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005431 ActivityRecord next =
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005432 pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005433 ActivityRecord top = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 TaskRecord curTask = null;
5435 int numActivities = 0;
5436 int numRunning = 0;
5437 while (pos >= 0 && maxNum > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005438 final ActivityRecord r = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 pos--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005440 next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441
5442 // Initialize state for next task if needed.
5443 if (top == null ||
5444 (top.state == ActivityState.INITIALIZING
5445 && top.task == r.task)) {
5446 top = r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 curTask = r.task;
5448 numActivities = numRunning = 0;
5449 }
5450
5451 // Add 'r' into the current task.
5452 numActivities++;
5453 if (r.app != null && r.app.thread != null) {
5454 numRunning++;
5455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456
Joe Onorato8a9b2202010-02-26 18:56:32 -08005457 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 TAG, r.intent.getComponent().flattenToShortString()
5459 + ": task=" + r.task);
5460
5461 // If the next one is a different task, generate a new
5462 // TaskInfo entry for what we have.
5463 if (next == null || next.task != curTask) {
5464 ActivityManager.RunningTaskInfo ci
5465 = new ActivityManager.RunningTaskInfo();
5466 ci.id = curTask.taskId;
5467 ci.baseActivity = r.intent.getComponent();
5468 ci.topActivity = top.intent.getComponent();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07005469 if (top.thumbHolder != null) {
5470 ci.description = top.thumbHolder.lastDescription;
5471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 ci.numActivities = numActivities;
5473 ci.numRunning = numRunning;
5474 //System.out.println(
5475 // "#" + maxNum + ": " + " descr=" + ci.description);
5476 if (ci.thumbnail == null && receiver != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005477 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 TAG, "State=" + top.state + "Idle=" + top.idle
5479 + " app=" + top.app
5480 + " thr=" + (top.app != null ? top.app.thread : null));
5481 if (top.state == ActivityState.RESUMED
5482 || top.state == ActivityState.PAUSING) {
5483 if (top.idle && top.app != null
5484 && top.app.thread != null) {
5485 topRecord = top;
5486 topThumbnail = top.app.thread;
5487 } else {
5488 top.thumbnailNeeded = true;
5489 }
5490 }
5491 if (pending == null) {
5492 pending = new PendingThumbnailsRecord(receiver);
5493 }
5494 pending.pendingRecords.add(top);
5495 }
5496 list.add(ci);
5497 maxNum--;
5498 top = null;
5499 }
5500 }
5501
5502 if (pending != null) {
5503 mPendingThumbnails.add(pending);
5504 }
5505 }
5506
Joe Onorato8a9b2202010-02-26 18:56:32 -08005507 if (localLOGV) Slog.v(TAG, "We have pending thumbnails: " + pending);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508
5509 if (topThumbnail != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005510 if (localLOGV) Slog.v(TAG, "Requesting top thumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005511 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08005512 topThumbnail.requestThumbnail(topRecord.appToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005513 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005514 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08005515 sendPendingThumbnail(null, topRecord.appToken, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005516 }
5517 }
5518
5519 if (pending == null && receiver != null) {
5520 // In this case all thumbnails were available and the client
5521 // is being asked to be told when the remaining ones come in...
5522 // which is unusually, since the top-most currently running
5523 // activity should never have a canned thumbnail! Oh well.
5524 try {
5525 receiver.finished();
5526 } catch (RemoteException ex) {
5527 }
5528 }
5529
5530 return list;
5531 }
5532
5533 public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
Amith Yamasani82644082012-08-03 13:09:11 -07005534 int flags, int userId) {
Amith Yamasani742a6712011-05-04 14:49:28 -07005535 final int callingUid = Binder.getCallingUid();
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005536 if (userId != UserHandle.getCallingUserId()) {
Amith Yamasani82644082012-08-03 13:09:11 -07005537 // Check if the caller is holding permissions for cross-user requests.
5538 if (checkComponentPermission(
5539 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
5540 Binder.getCallingPid(), callingUid, -1, true)
5541 != PackageManager.PERMISSION_GRANTED) {
5542 String msg = "Permission Denial: "
5543 + "Request to get recent tasks for user " + userId
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005544 + " but is calling from user " + UserHandle.getUserId(callingUid)
Amith Yamasani82644082012-08-03 13:09:11 -07005545 + "; this requires "
5546 + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
5547 Slog.w(TAG, msg);
5548 throw new SecurityException(msg);
5549 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005550 if (userId == UserHandle.USER_CURRENT) {
Amith Yamasani82644082012-08-03 13:09:11 -07005551 userId = mCurrentUserId;
5552 }
5553 }
5554 }
5555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 synchronized (this) {
5557 enforceCallingPermission(android.Manifest.permission.GET_TASKS,
5558 "getRecentTasks()");
Dianne Hackborn8238e712012-04-24 11:15:40 -07005559 final boolean detailed = checkCallingPermission(
5560 android.Manifest.permission.GET_DETAILED_TASKS)
5561 == PackageManager.PERMISSION_GRANTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005563 IPackageManager pm = AppGlobals.getPackageManager();
Amith Yamasani82644082012-08-03 13:09:11 -07005564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 final int N = mRecentTasks.size();
5566 ArrayList<ActivityManager.RecentTaskInfo> res
5567 = new ArrayList<ActivityManager.RecentTaskInfo>(
5568 maxNum < N ? maxNum : N);
5569 for (int i=0; i<N && maxNum > 0; i++) {
5570 TaskRecord tr = mRecentTasks.get(i);
Amith Yamasani742a6712011-05-04 14:49:28 -07005571 // Only add calling user's recent tasks
Amith Yamasani82644082012-08-03 13:09:11 -07005572 if (tr.userId != userId) continue;
Dianne Hackborn905577f2011-09-07 18:31:28 -07005573 // Return the entry if desired by the caller. We always return
5574 // the first entry, because callers always expect this to be the
Amith Yamasani742a6712011-05-04 14:49:28 -07005575 // foreground app. We may filter others if the caller has
Dianne Hackborn905577f2011-09-07 18:31:28 -07005576 // not supplied RECENT_WITH_EXCLUDED and there is some reason
5577 // we should exclude the entry.
Amith Yamasani742a6712011-05-04 14:49:28 -07005578
Dianne Hackborn905577f2011-09-07 18:31:28 -07005579 if (i == 0
5580 || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 || (tr.intent == null)
5582 || ((tr.intent.getFlags()
5583 &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) {
5584 ActivityManager.RecentTaskInfo rti
5585 = new ActivityManager.RecentTaskInfo();
5586 rti.id = tr.numActivities > 0 ? tr.taskId : -1;
Dianne Hackbornd94df452011-02-16 18:53:31 -08005587 rti.persistentId = tr.taskId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 rti.baseIntent = new Intent(
5589 tr.intent != null ? tr.intent : tr.affinityIntent);
Dianne Hackborn8238e712012-04-24 11:15:40 -07005590 if (!detailed) {
5591 rti.baseIntent.replaceExtras((Bundle)null);
5592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 rti.origActivity = tr.origActivity;
Dianne Hackbornd2835932010-12-13 16:28:46 -08005594 rti.description = tr.lastDescription;
5595
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005596 if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) {
5597 // Check whether this activity is currently available.
5598 try {
5599 if (rti.origActivity != null) {
Amith Yamasani82644082012-08-03 13:09:11 -07005600 if (pm.getActivityInfo(rti.origActivity, 0, userId)
Amith Yamasani483f3b02012-03-13 16:08:00 -07005601 == null) {
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005602 continue;
5603 }
5604 } else if (rti.baseIntent != null) {
5605 if (pm.queryIntentActivities(rti.baseIntent,
Amith Yamasani82644082012-08-03 13:09:11 -07005606 null, 0, userId) == null) {
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005607 continue;
5608 }
5609 }
5610 } catch (RemoteException e) {
5611 // Will never happen.
5612 }
5613 }
5614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 res.add(rti);
5616 maxNum--;
5617 }
5618 }
5619 return res;
5620 }
5621 }
5622
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005623 private TaskRecord taskForIdLocked(int id) {
5624 final int N = mRecentTasks.size();
5625 for (int i=0; i<N; i++) {
5626 TaskRecord tr = mRecentTasks.get(i);
5627 if (tr.taskId == id) {
5628 return tr;
Dianne Hackbornd94df452011-02-16 18:53:31 -08005629 }
5630 }
5631 return null;
5632 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005633
5634 public ActivityManager.TaskThumbnails getTaskThumbnails(int id) {
5635 synchronized (this) {
5636 enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER,
5637 "getTaskThumbnails()");
5638 TaskRecord tr = taskForIdLocked(id);
5639 if (tr != null) {
5640 return mMainStack.getTaskThumbnailsLocked(tr);
5641 }
5642 }
5643 return null;
5644 }
5645
5646 public boolean removeSubTask(int taskId, int subTaskIndex) {
5647 synchronized (this) {
5648 enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS,
5649 "removeSubTask()");
5650 long ident = Binder.clearCallingIdentity();
5651 try {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005652 return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex,
5653 true) != null;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005654 } finally {
5655 Binder.restoreCallingIdentity(ident);
5656 }
5657 }
5658 }
5659
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005660 private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) {
5661 final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005662 Intent baseIntent = new Intent(
5663 tr.intent != null ? tr.intent : tr.affinityIntent);
5664 ComponentName component = baseIntent.getComponent();
5665 if (component == null) {
5666 Slog.w(TAG, "Now component for base intent of task: " + tr);
5667 return;
5668 }
5669
5670 // Find any running services associated with this app.
Dianne Hackborn599db5c2012-08-03 19:28:48 -07005671 mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005672
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005673 if (killProcesses) {
5674 // Find any running processes associated with this app.
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005675 final String pkg = component.getPackageName();
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005676 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005677 HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap();
5678 for (SparseArray<ProcessRecord> uids : pmap.values()) {
5679 for (int i=0; i<uids.size(); i++) {
5680 ProcessRecord proc = uids.valueAt(i);
5681 if (proc.userId != tr.userId) {
5682 continue;
5683 }
5684 if (!proc.pkgList.contains(pkg)) {
5685 continue;
5686 }
5687 procs.add(proc);
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005688 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005689 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005690
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005691 // Kill the running processes.
5692 for (int i=0; i<procs.size(); i++) {
5693 ProcessRecord pr = procs.get(i);
5694 if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
5695 Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task");
5696 EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid,
5697 pr.processName, pr.setAdj, "remove task");
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005698 pr.killedBackground = true;
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005699 Process.killProcessQuiet(pr.pid);
5700 } else {
5701 pr.waitingToKill = "remove task";
5702 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005703 }
5704 }
5705 }
5706
5707 public boolean removeTask(int taskId, int flags) {
5708 synchronized (this) {
5709 enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS,
5710 "removeTask()");
5711 long ident = Binder.clearCallingIdentity();
5712 try {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005713 ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1,
5714 false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005715 if (r != null) {
5716 mRecentTasks.remove(r.task);
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005717 cleanUpRemovedTaskLocked(r.task, flags);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005718 return true;
Dianne Hackborneeb1dca2011-09-08 13:30:11 -07005719 } else {
5720 TaskRecord tr = null;
5721 int i=0;
5722 while (i < mRecentTasks.size()) {
5723 TaskRecord t = mRecentTasks.get(i);
5724 if (t.taskId == taskId) {
5725 tr = t;
5726 break;
5727 }
5728 i++;
5729 }
5730 if (tr != null) {
5731 if (tr.numActivities <= 0) {
5732 // Caller is just removing a recent task that is
5733 // not actively running. That is easy!
5734 mRecentTasks.remove(i);
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005735 cleanUpRemovedTaskLocked(tr, flags);
5736 return true;
Dianne Hackborneeb1dca2011-09-08 13:30:11 -07005737 } else {
5738 Slog.w(TAG, "removeTask: task " + taskId
5739 + " does not have activities to remove, "
5740 + " but numActivities=" + tr.numActivities
5741 + ": " + tr);
5742 }
5743 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005744 }
5745 } finally {
5746 Binder.restoreCallingIdentity(ident);
5747 }
5748 }
5749 return false;
5750 }
Dianne Hackbornd94df452011-02-16 18:53:31 -08005751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 private final int findAffinityTaskTopLocked(int startIndex, String affinity) {
5753 int j;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005754 TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 TaskRecord jt = startTask;
5756
5757 // First look backwards
5758 for (j=startIndex-1; j>=0; j--) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005759 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 if (r.task != jt) {
5761 jt = r.task;
5762 if (affinity.equals(jt.affinity)) {
5763 return j;
5764 }
5765 }
5766 }
5767
5768 // Now look forwards
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005769 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 jt = startTask;
5771 for (j=startIndex+1; j<N; j++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005772 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 if (r.task != jt) {
5774 if (affinity.equals(jt.affinity)) {
5775 return j;
5776 }
5777 jt = r.task;
5778 }
5779 }
5780
5781 // Might it be at the top?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005782 if (affinity.equals(((ActivityRecord)mMainStack.mHistory.get(N-1)).task.affinity)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 return N-1;
5784 }
5785
5786 return -1;
5787 }
5788
5789 /**
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005790 * TODO: Add mController hook
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005791 */
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005792 public void moveTaskToFront(int task, int flags, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5794 "moveTaskToFront()");
5795
5796 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005797 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5798 Binder.getCallingUid(), "Task to front")) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005799 ActivityOptions.abort(options);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005800 return;
5801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 final long origId = Binder.clearCallingIdentity();
5803 try {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005804 TaskRecord tr = taskForIdLocked(task);
5805 if (tr != null) {
5806 if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
5807 mMainStack.mUserLeaving = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005809 if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
5810 // Caller wants the home activity moved with it. To accomplish this,
5811 // we'll just move the home task to the top first.
5812 mMainStack.moveHomeToFrontLocked();
5813 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005814 mMainStack.moveTaskToFrontLocked(tr, null, options);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005815 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005817 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
5818 ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005819 if (hr.task.taskId == task) {
Dianne Hackbornd94df452011-02-16 18:53:31 -08005820 if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
5821 mMainStack.mUserLeaving = true;
5822 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005823 if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
5824 // Caller wants the home activity moved with it. To accomplish this,
5825 // we'll just move the home task to the top first.
5826 mMainStack.moveHomeToFrontLocked();
5827 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005828 mMainStack.moveTaskToFrontLocked(hr.task, null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 return;
5830 }
5831 }
5832 } finally {
5833 Binder.restoreCallingIdentity(origId);
5834 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005835 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 }
5837 }
5838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 public void moveTaskToBack(int task) {
5840 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5841 "moveTaskToBack()");
5842
5843 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005844 if (mMainStack.mResumedActivity != null
5845 && mMainStack.mResumedActivity.task.taskId == task) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005846 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5847 Binder.getCallingUid(), "Task to back")) {
5848 return;
5849 }
5850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005851 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005852 mMainStack.moveTaskToBackLocked(task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 Binder.restoreCallingIdentity(origId);
5854 }
5855 }
5856
5857 /**
5858 * Moves an activity, and all of the other activities within the same task, to the bottom
5859 * of the history stack. The activity's order within the task is unchanged.
5860 *
5861 * @param token A reference to the activity we wish to move
5862 * @param nonRoot If false then this only works if the activity is the root
5863 * of a task; if true it will work for any activity in a task.
5864 * @return Returns true if the move completed, false if not.
5865 */
5866 public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005867 enforceNotIsolatedCaller("moveActivityTaskToBack");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005868 synchronized(this) {
5869 final long origId = Binder.clearCallingIdentity();
5870 int taskId = getTaskForActivityLocked(token, !nonRoot);
5871 if (taskId >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005872 return mMainStack.moveTaskToBackLocked(taskId, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 }
5874 Binder.restoreCallingIdentity(origId);
5875 }
5876 return false;
5877 }
5878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 public void moveTaskBackwards(int task) {
5880 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5881 "moveTaskBackwards()");
5882
5883 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005884 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5885 Binder.getCallingUid(), "Task backwards")) {
5886 return;
5887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 final long origId = Binder.clearCallingIdentity();
5889 moveTaskBackwardsLocked(task);
5890 Binder.restoreCallingIdentity(origId);
5891 }
5892 }
5893
5894 private final void moveTaskBackwardsLocked(int task) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005895 Slog.e(TAG, "moveTaskBackwards not yet implemented!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896 }
5897
5898 public int getTaskForActivity(IBinder token, boolean onlyRoot) {
5899 synchronized(this) {
5900 return getTaskForActivityLocked(token, onlyRoot);
5901 }
5902 }
5903
5904 int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005905 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 TaskRecord lastTask = null;
5907 for (int i=0; i<N; i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005908 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackbornbe707852011-11-11 14:32:10 -08005909 if (r.appToken == token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 if (!onlyRoot || lastTask != r.task) {
5911 return r.task.taskId;
5912 }
5913 return -1;
5914 }
5915 lastTask = r.task;
5916 }
5917
5918 return -1;
5919 }
5920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 // =========================================================
5922 // THUMBNAILS
5923 // =========================================================
5924
5925 public void reportThumbnail(IBinder token,
5926 Bitmap thumbnail, CharSequence description) {
5927 //System.out.println("Report thumbnail for " + token + ": " + thumbnail);
5928 final long origId = Binder.clearCallingIdentity();
5929 sendPendingThumbnail(null, token, thumbnail, description, true);
5930 Binder.restoreCallingIdentity(origId);
5931 }
5932
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005933 final void sendPendingThumbnail(ActivityRecord r, IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 Bitmap thumbnail, CharSequence description, boolean always) {
5935 TaskRecord task = null;
5936 ArrayList receivers = null;
5937
5938 //System.out.println("Send pending thumbnail: " + r);
5939
5940 synchronized(this) {
5941 if (r == null) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005942 r = mMainStack.isInStackLocked(token);
5943 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 return;
5945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07005947 if (thumbnail == null && r.thumbHolder != null) {
5948 thumbnail = r.thumbHolder.lastThumbnail;
5949 description = r.thumbHolder.lastDescription;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 }
5951 if (thumbnail == null && !always) {
5952 // If there is no thumbnail, and this entry is not actually
5953 // going away, then abort for now and pick up the next
5954 // thumbnail we get.
5955 return;
5956 }
5957 task = r.task;
5958
5959 int N = mPendingThumbnails.size();
5960 int i=0;
5961 while (i<N) {
5962 PendingThumbnailsRecord pr =
5963 (PendingThumbnailsRecord)mPendingThumbnails.get(i);
5964 //System.out.println("Looking in " + pr.pendingRecords);
5965 if (pr.pendingRecords.remove(r)) {
5966 if (receivers == null) {
5967 receivers = new ArrayList();
5968 }
5969 receivers.add(pr);
5970 if (pr.pendingRecords.size() == 0) {
5971 pr.finished = true;
5972 mPendingThumbnails.remove(i);
5973 N--;
5974 continue;
5975 }
5976 }
5977 i++;
5978 }
5979 }
5980
5981 if (receivers != null) {
5982 final int N = receivers.size();
5983 for (int i=0; i<N; i++) {
5984 try {
5985 PendingThumbnailsRecord pr =
5986 (PendingThumbnailsRecord)receivers.get(i);
5987 pr.receiver.newThumbnail(
5988 task != null ? task.taskId : -1, thumbnail, description);
5989 if (pr.finished) {
5990 pr.receiver.finished();
5991 }
5992 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005993 Slog.w(TAG, "Exception thrown when sending thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 }
5995 }
5996 }
5997 }
5998
5999 // =========================================================
6000 // CONTENT PROVIDERS
6001 // =========================================================
6002
Jeff Brown10e89712011-07-08 18:52:57 -07006003 private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) {
6004 List<ProviderInfo> providers = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006006 providers = AppGlobals.getPackageManager().
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006007 queryContentProviders(app.processName, app.uid,
Dianne Hackborn1655be42009-05-08 14:29:01 -07006008 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 } catch (RemoteException ex) {
6010 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006011 if (DEBUG_MU)
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006012 Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid);
6013 int userId = app.userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006014 if (providers != null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006015 int N = providers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 for (int i=0; i<N; i++) {
6017 ProviderInfo cpi =
6018 (ProviderInfo)providers.get(i);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006019 boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo,
6020 cpi.name, cpi.flags);
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006021 if (singleton && UserHandle.getUserId(app.uid) != 0) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006022 // This is a singleton provider, but a user besides the
6023 // default user is asking to initialize a process it runs
6024 // in... well, no, it doesn't actually run in this process,
6025 // it runs in the process of the default user. Get rid of it.
6026 providers.remove(i);
6027 N--;
6028 continue;
6029 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006030
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006031 ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006032 ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 if (cpr == null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006034 cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton);
Amith Yamasani742a6712011-05-04 14:49:28 -07006035 mProviderMap.putProviderByClass(comp, cpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006037 if (DEBUG_MU)
6038 Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 app.pubProviders.put(cpi.name, cpr);
6040 app.addPackage(cpi.applicationInfo.packageName);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07006041 ensurePackageDexOpt(cpi.applicationInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 }
6043 }
6044 return providers;
6045 }
6046
Jeff Sharkey110a6b62012-03-12 11:12:41 -07006047 /**
6048 * Check if {@link ProcessRecord} has a possible chance at accessing the
6049 * given {@link ProviderInfo}. Final permission checking is always done
6050 * in {@link ContentProvider}.
6051 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 private final String checkContentProviderPermissionLocked(
Dianne Hackbornb424b632010-08-18 15:59:05 -07006053 ProviderInfo cpi, ProcessRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 final int callingPid = (r != null) ? r.pid : Binder.getCallingPid();
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006055 final int callingUid = (r != null) ? r.uid : Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 if (checkComponentPermission(cpi.readPermission, callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006057 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackbornb424b632010-08-18 15:59:05 -07006058 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 return null;
6060 }
6061 if (checkComponentPermission(cpi.writePermission, callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006062 cpi.applicationInfo.uid, cpi.exported)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 == PackageManager.PERMISSION_GRANTED) {
6064 return null;
6065 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006066
6067 PathPermission[] pps = cpi.pathPermissions;
6068 if (pps != null) {
6069 int i = pps.length;
6070 while (i > 0) {
6071 i--;
6072 PathPermission pp = pps[i];
6073 if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006074 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackbornb424b632010-08-18 15:59:05 -07006075 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006076 return null;
6077 }
6078 if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006079 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006080 == PackageManager.PERMISSION_GRANTED) {
6081 return null;
6082 }
6083 }
6084 }
6085
Dianne Hackbornb424b632010-08-18 15:59:05 -07006086 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid);
6087 if (perms != null) {
6088 for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) {
6089 if (uri.getKey().getAuthority().equals(cpi.authority)) {
6090 return null;
6091 }
6092 }
6093 }
6094
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006095 String msg;
6096 if (!cpi.exported) {
6097 msg = "Permission Denial: opening provider " + cpi.name
6098 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
6099 + ", uid=" + callingUid + ") that is not exported from uid "
6100 + cpi.applicationInfo.uid;
6101 } else {
6102 msg = "Permission Denial: opening provider " + cpi.name
6103 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
6104 + ", uid=" + callingUid + ") requires "
6105 + cpi.readPermission + " or " + cpi.writePermission;
6106 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08006107 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 return msg;
6109 }
6110
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006111 ContentProviderConnection incProviderCountLocked(ProcessRecord r,
6112 final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006113 if (r != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006114 for (int i=0; i<r.conProviders.size(); i++) {
6115 ContentProviderConnection conn = r.conProviders.get(i);
6116 if (conn.provider == cpr) {
6117 if (DEBUG_PROVIDER) Slog.v(TAG,
6118 "Adding provider requested by "
6119 + r.processName + " from process "
6120 + cpr.info.processName + ": " + cpr.name.flattenToShortString()
6121 + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
6122 if (stable) {
6123 conn.stableCount++;
6124 conn.numStableIncs++;
6125 } else {
6126 conn.unstableCount++;
6127 conn.numUnstableIncs++;
6128 }
6129 return conn;
6130 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006131 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006132 ContentProviderConnection conn = new ContentProviderConnection(cpr, r);
6133 if (stable) {
6134 conn.stableCount = 1;
6135 conn.numStableIncs = 1;
6136 } else {
6137 conn.unstableCount = 1;
6138 conn.numUnstableIncs = 1;
6139 }
6140 cpr.connections.add(conn);
6141 r.conProviders.add(conn);
6142 return conn;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006143 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006144 cpr.addExternalProcessHandleLocked(externalProcessToken);
6145 return null;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006146 }
6147
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006148 boolean decProviderCountLocked(ContentProviderConnection conn,
6149 ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
6150 if (conn != null) {
6151 cpr = conn.provider;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006152 if (DEBUG_PROVIDER) Slog.v(TAG,
6153 "Removing provider requested by "
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006154 + conn.client.processName + " from process "
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006155 + cpr.info.processName + ": " + cpr.name.flattenToShortString()
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006156 + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
6157 if (stable) {
6158 conn.stableCount--;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006159 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006160 conn.unstableCount--;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006161 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006162 if (conn.stableCount == 0 && conn.unstableCount == 0) {
6163 cpr.connections.remove(conn);
6164 conn.client.conProviders.remove(conn);
6165 return true;
6166 }
6167 return false;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006168 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006169 cpr.removeExternalProcessHandleLocked(externalProcessToken);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006170 return false;
6171 }
6172
Amith Yamasani742a6712011-05-04 14:49:28 -07006173 private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006174 String name, IBinder token, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 ContentProviderRecord cpr;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006176 ContentProviderConnection conn = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 ProviderInfo cpi = null;
6178
6179 synchronized(this) {
6180 ProcessRecord r = null;
6181 if (caller != null) {
6182 r = getRecordForAppLocked(caller);
6183 if (r == null) {
6184 throw new SecurityException(
6185 "Unable to find app for caller " + caller
6186 + " (pid=" + Binder.getCallingPid()
6187 + ") when getting content provider " + name);
6188 }
6189 }
6190
6191 // First check if this content provider has been published...
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006192 int userId = UserHandle.getUserId(r != null ? r.uid : Binder.getCallingUid());
Amith Yamasani742a6712011-05-04 14:49:28 -07006193 cpr = mProviderMap.getProviderByName(name, userId);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006194 boolean providerRunning = cpr != null;
6195 if (providerRunning) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 cpi = cpr.info;
Dianne Hackbornb424b632010-08-18 15:59:05 -07006197 String msg;
6198 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
6199 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 }
6201
6202 if (r != null && cpr.canRunHere(r)) {
6203 // This provider has been published or is in the process
6204 // of being published... but it is also allowed to run
6205 // in the caller's process, so don't make a connection
6206 // and just let the caller instantiate its own instance.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006207 ContentProviderHolder holder = cpr.newHolder(null);
6208 // don't give caller the provider object, it needs
6209 // to make its own.
6210 holder.provider = null;
6211 return holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006212 }
6213
6214 final long origId = Binder.clearCallingIdentity();
6215
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006216 // In this case the provider instance already exists, so we can
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 // return it right away.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006218 conn = incProviderCountLocked(r, cpr, token, stable);
6219 if (conn != null && (conn.stableCount+conn.unstableCount) == 1) {
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006220 if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006221 // If this is a perceptible app accessing the provider,
Dianne Hackborn906497c2010-05-10 15:57:38 -07006222 // make sure to count it as being accessed and thus
6223 // back up on the LRU list. This is good because
6224 // content providers are often expensive to start.
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006225 updateLruProcessLocked(cpr.proc, false, true);
Dianne Hackborn906497c2010-05-10 15:57:38 -07006226 }
Dianne Hackborn6f86c0e2010-05-11 14:20:52 -07006227 }
6228
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006229 if (cpr.proc != null) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006230 if (false) {
6231 if (cpr.name.flattenToShortString().equals(
6232 "com.android.providers.calendar/.CalendarProvider2")) {
6233 Slog.v(TAG, "****************** KILLING "
6234 + cpr.name.flattenToShortString());
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006235 Process.killProcess(cpr.proc.pid);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006236 }
6237 }
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006238 boolean success = updateOomAdjLocked(cpr.proc);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006239 if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success);
6240 // NOTE: there is still a race here where a signal could be
6241 // pending on the process even though we managed to update its
6242 // adj level. Not sure what to do about this, but at least
6243 // the race is now smaller.
6244 if (!success) {
6245 // Uh oh... it looks like the provider's process
6246 // has been killed on us. We need to wait for a new
6247 // process to be started, and make sure its death
6248 // doesn't kill our process.
6249 Slog.i(TAG,
6250 "Existing provider " + cpr.name.flattenToShortString()
6251 + " is crashing; detaching " + r);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006252 boolean lastRef = decProviderCountLocked(conn, cpr, token, stable);
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006253 appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006254 if (!lastRef) {
6255 // This wasn't the last ref our process had on
6256 // the provider... we have now been killed, bail.
6257 return null;
6258 }
6259 providerRunning = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006260 conn = null;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 }
6263
6264 Binder.restoreCallingIdentity(origId);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006267 boolean singleton;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006268 if (!providerRunning) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006270 cpi = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07006271 resolveContentProvider(name,
Amith Yamasani483f3b02012-03-13 16:08:00 -07006272 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 } catch (RemoteException ex) {
6274 }
6275 if (cpi == null) {
6276 return null;
6277 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006278 singleton = isSingleton(cpi.processName, cpi.applicationInfo,
6279 cpi.name, cpi.flags);
6280 if (singleton) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07006281 userId = 0;
6282 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07006283 cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07006284
Dianne Hackbornb424b632010-08-18 15:59:05 -07006285 String msg;
6286 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
6287 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 }
6289
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07006290 if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08006291 && !cpi.processName.equals("system")) {
6292 // If this content provider does not run in the system
6293 // process, and the system is not yet ready to run other
6294 // processes, then fail fast instead of hanging.
6295 throw new IllegalArgumentException(
6296 "Attempt to launch content provider before system ready");
6297 }
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006298
6299 ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
Amith Yamasani483f3b02012-03-13 16:08:00 -07006300 cpr = mProviderMap.getProviderByClass(comp, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 final boolean firstClass = cpr == null;
6302 if (firstClass) {
6303 try {
6304 ApplicationInfo ai =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006305 AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 getApplicationInfo(
6307 cpi.applicationInfo.packageName,
Amith Yamasani483f3b02012-03-13 16:08:00 -07006308 STOCK_PM_FLAGS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 if (ai == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006310 Slog.w(TAG, "No package info for content provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006311 + cpi.name);
6312 return null;
6313 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07006314 ai = getAppInfoForUser(ai, userId);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006315 cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 } catch (RemoteException ex) {
6317 // pm is in same process, this will never happen.
6318 }
6319 }
6320
6321 if (r != null && cpr.canRunHere(r)) {
6322 // If this is a multiprocess provider, then just return its
6323 // info and allow the caller to instantiate it. Only do
6324 // this if the provider is the same user as the caller's
6325 // process, or can run as root (so can be in any process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006326 return cpr.newHolder(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 }
6328
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006329 if (DEBUG_PROVIDER) {
6330 RuntimeException e = new RuntimeException("here");
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006331 Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006332 + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 }
6334
6335 // This is single process, and our app is now connecting to it.
6336 // See if we are already in the process of launching this
6337 // provider.
6338 final int N = mLaunchingProviders.size();
6339 int i;
6340 for (i=0; i<N; i++) {
6341 if (mLaunchingProviders.get(i) == cpr) {
6342 break;
6343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 }
6345
6346 // If the provider is not already being launched, then get it
6347 // started.
6348 if (i >= N) {
6349 final long origId = Binder.clearCallingIdentity();
Dianne Hackborne7f97212011-02-24 14:40:20 -08006350
6351 try {
6352 // Content provider is now in use, its package can't be stopped.
6353 try {
6354 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07006355 cpr.appInfo.packageName, false, userId);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006356 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08006357 } catch (IllegalArgumentException e) {
6358 Slog.w(TAG, "Failed trying to unstop package "
6359 + cpr.appInfo.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006360 }
6361
6362 ProcessRecord proc = startProcessLocked(cpi.processName,
6363 cpr.appInfo, false, 0, "content provider",
6364 new ComponentName(cpi.applicationInfo.packageName,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006365 cpi.name), false, false);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006366 if (proc == null) {
6367 Slog.w(TAG, "Unable to launch app "
6368 + cpi.applicationInfo.packageName + "/"
6369 + cpi.applicationInfo.uid + " for provider "
6370 + name + ": process is bad");
6371 return null;
6372 }
6373 cpr.launchingApp = proc;
6374 mLaunchingProviders.add(cpr);
6375 } finally {
6376 Binder.restoreCallingIdentity(origId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 }
6379
6380 // Make sure the provider is published (the same provider class
6381 // may be published under multiple names).
6382 if (firstClass) {
Amith Yamasani742a6712011-05-04 14:49:28 -07006383 mProviderMap.putProviderByClass(comp, cpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 }
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006385
Amith Yamasani742a6712011-05-04 14:49:28 -07006386 mProviderMap.putProviderByName(name, cpr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006387 conn = incProviderCountLocked(r, cpr, token, stable);
6388 if (conn != null) {
6389 conn.waiting = true;
6390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 }
6392 }
6393
6394 // Wait for the provider to be published...
6395 synchronized (cpr) {
6396 while (cpr.provider == null) {
6397 if (cpr.launchingApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006398 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 + cpi.applicationInfo.packageName + "/"
6400 + cpi.applicationInfo.uid + " for provider "
6401 + name + ": launching app became null");
Doug Zongker2bec3d42009-12-04 12:52:44 -08006402 EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 cpi.applicationInfo.packageName,
6404 cpi.applicationInfo.uid, name);
6405 return null;
6406 }
6407 try {
Amith Yamasani742a6712011-05-04 14:49:28 -07006408 if (DEBUG_MU) {
6409 Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp="
6410 + cpr.launchingApp);
6411 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006412 if (conn != null) {
6413 conn.waiting = true;
6414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 cpr.wait();
6416 } catch (InterruptedException ex) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006417 } finally {
6418 if (conn != null) {
6419 conn.waiting = false;
6420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 }
6422 }
6423 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006424 return cpr != null ? cpr.newHolder(conn) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 }
6426
6427 public final ContentProviderHolder getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006428 IApplicationThread caller, String name, boolean stable) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006429 enforceNotIsolatedCaller("getContentProvider");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 if (caller == null) {
6431 String msg = "null IApplicationThread when getting content provider "
6432 + name;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006433 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006434 throw new SecurityException(msg);
6435 }
6436
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006437 return getContentProviderImpl(caller, name, null, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 }
6439
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006440 public ContentProviderHolder getContentProviderExternal(String name, IBinder token) {
6441 enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
6442 "Do not have permission in call getContentProviderExternal()");
6443 return getContentProviderExternalUnchecked(name, token);
6444 }
6445
6446 private ContentProviderHolder getContentProviderExternalUnchecked(String name,IBinder token) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006447 return getContentProviderImpl(null, name, token, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 }
6449
6450 /**
6451 * Drop a content provider from a ProcessRecord's bookkeeping
6452 * @param cpr
6453 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006454 public void removeContentProvider(IBinder connection, boolean stable) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006455 enforceNotIsolatedCaller("removeContentProvider");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006456 synchronized (this) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006457 ContentProviderConnection conn;
6458 try {
6459 conn = (ContentProviderConnection)connection;
6460 } catch (ClassCastException e) {
6461 String msg ="removeContentProvider: " + connection
6462 + " not a ContentProviderConnection";
6463 Slog.w(TAG, msg);
6464 throw new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006466 if (conn == null) {
6467 throw new NullPointerException("connection is null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006468 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006469 if (decProviderCountLocked(conn, null, null, stable)) {
6470 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 }
6473 }
6474
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006475 public void removeContentProviderExternal(String name, IBinder token) {
6476 enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
6477 "Do not have permission in call removeContentProviderExternal()");
6478 removeContentProviderExternalUnchecked(name, token);
6479 }
6480
6481 private void removeContentProviderExternalUnchecked(String name, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 synchronized (this) {
Amith Yamasani742a6712011-05-04 14:49:28 -07006483 ContentProviderRecord cpr = mProviderMap.getProviderByName(name,
6484 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006485 if(cpr == null) {
6486 //remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08006487 if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488 return;
6489 }
6490
6491 //update content provider record entry info
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006492 ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006493 ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp,
6494 Binder.getOrigCallingUser());
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006495 if (localCpr.hasExternalProcessHandles()) {
6496 if (localCpr.removeExternalProcessHandleLocked(token)) {
6497 updateOomAdjLocked();
6498 } else {
6499 Slog.e(TAG, "Attmpt to remove content provider " + localCpr
6500 + " with no external reference for token: "
6501 + token + ".");
6502 }
6503 } else {
6504 Slog.e(TAG, "Attmpt to remove content provider: " + localCpr
6505 + " with no external references.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 }
6508 }
6509
6510 public final void publishContentProviders(IApplicationThread caller,
6511 List<ContentProviderHolder> providers) {
6512 if (providers == null) {
6513 return;
6514 }
6515
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006516 enforceNotIsolatedCaller("publishContentProviders");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006517 synchronized (this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006518 final ProcessRecord r = getRecordForAppLocked(caller);
Amith Yamasani742a6712011-05-04 14:49:28 -07006519 if (DEBUG_MU)
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006520 Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 if (r == null) {
6522 throw new SecurityException(
6523 "Unable to find app for caller " + caller
6524 + " (pid=" + Binder.getCallingPid()
6525 + ") when publishing content providers");
6526 }
6527
6528 final long origId = Binder.clearCallingIdentity();
6529
6530 final int N = providers.size();
6531 for (int i=0; i<N; i++) {
6532 ContentProviderHolder src = providers.get(i);
6533 if (src == null || src.info == null || src.provider == null) {
6534 continue;
6535 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07006536 ContentProviderRecord dst = r.pubProviders.get(src.info.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006537 if (DEBUG_MU)
6538 Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 if (dst != null) {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006540 ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006541 mProviderMap.putProviderByClass(comp, dst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542 String names[] = dst.info.authority.split(";");
6543 for (int j = 0; j < names.length; j++) {
Amith Yamasani742a6712011-05-04 14:49:28 -07006544 mProviderMap.putProviderByName(names[j], dst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 }
6546
6547 int NL = mLaunchingProviders.size();
6548 int j;
6549 for (j=0; j<NL; j++) {
6550 if (mLaunchingProviders.get(j) == dst) {
6551 mLaunchingProviders.remove(j);
6552 j--;
6553 NL--;
6554 }
6555 }
6556 synchronized (dst) {
6557 dst.provider = src.provider;
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006558 dst.proc = r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 dst.notifyAll();
6560 }
6561 updateOomAdjLocked(r);
6562 }
6563 }
6564
6565 Binder.restoreCallingIdentity(origId);
6566 }
6567 }
6568
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006569 public boolean refContentProvider(IBinder connection, int stable, int unstable) {
6570 ContentProviderConnection conn;
6571 try {
6572 conn = (ContentProviderConnection)connection;
6573 } catch (ClassCastException e) {
6574 String msg ="refContentProvider: " + connection
6575 + " not a ContentProviderConnection";
6576 Slog.w(TAG, msg);
6577 throw new IllegalArgumentException(msg);
6578 }
6579 if (conn == null) {
6580 throw new NullPointerException("connection is null");
6581 }
6582
6583 synchronized (this) {
6584 if (stable > 0) {
6585 conn.numStableIncs += stable;
6586 }
6587 stable = conn.stableCount + stable;
6588 if (stable < 0) {
6589 throw new IllegalStateException("stableCount < 0: " + stable);
6590 }
6591
6592 if (unstable > 0) {
6593 conn.numUnstableIncs += unstable;
6594 }
6595 unstable = conn.unstableCount + unstable;
6596 if (unstable < 0) {
6597 throw new IllegalStateException("unstableCount < 0: " + unstable);
6598 }
6599
6600 if ((stable+unstable) <= 0) {
6601 throw new IllegalStateException("ref counts can't go to zero here: stable="
6602 + stable + " unstable=" + unstable);
6603 }
6604 conn.stableCount = stable;
6605 conn.unstableCount = unstable;
6606 return !conn.dead;
6607 }
6608 }
6609
6610 public void unstableProviderDied(IBinder connection) {
6611 ContentProviderConnection conn;
6612 try {
6613 conn = (ContentProviderConnection)connection;
6614 } catch (ClassCastException e) {
6615 String msg ="refContentProvider: " + connection
6616 + " not a ContentProviderConnection";
6617 Slog.w(TAG, msg);
6618 throw new IllegalArgumentException(msg);
6619 }
6620 if (conn == null) {
6621 throw new NullPointerException("connection is null");
6622 }
6623
6624 // Safely retrieve the content provider associated with the connection.
6625 IContentProvider provider;
6626 synchronized (this) {
6627 provider = conn.provider.provider;
6628 }
6629
6630 if (provider == null) {
6631 // Um, yeah, we're way ahead of you.
6632 return;
6633 }
6634
6635 // Make sure the caller is being honest with us.
6636 if (provider.asBinder().pingBinder()) {
6637 // Er, no, still looks good to us.
6638 synchronized (this) {
6639 Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid()
6640 + " says " + conn + " died, but we don't agree");
6641 return;
6642 }
6643 }
6644
6645 // Well look at that! It's dead!
6646 synchronized (this) {
6647 if (conn.provider.provider != provider) {
6648 // But something changed... good enough.
6649 return;
6650 }
6651
6652 ProcessRecord proc = conn.provider.proc;
6653 if (proc == null || proc.thread == null) {
6654 // Seems like the process is already cleaned up.
6655 return;
6656 }
6657
6658 // As far as we're concerned, this is just like receiving a
6659 // death notification... just a bit prematurely.
6660 Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid
6661 + ") early provider death");
Dianne Hackbornbd145db2012-06-05 16:20:46 -07006662 final long ident = Binder.clearCallingIdentity();
6663 try {
6664 appDiedLocked(proc, proc.pid, proc.thread);
6665 } finally {
6666 Binder.restoreCallingIdentity(ident);
6667 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006668 }
6669 }
6670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 public static final void installSystemProviders() {
Jeff Brown10e89712011-07-08 18:52:57 -07006672 List<ProviderInfo> providers;
Josh Bartel2ecce342010-02-25 10:55:48 -06006673 synchronized (mSelf) {
6674 ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID);
6675 providers = mSelf.generateApplicationProvidersLocked(app);
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08006676 if (providers != null) {
6677 for (int i=providers.size()-1; i>=0; i--) {
6678 ProviderInfo pi = (ProviderInfo)providers.get(i);
6679 if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
6680 Slog.w(TAG, "Not installing system proc provider " + pi.name
6681 + ": not system .apk");
6682 providers.remove(i);
6683 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08006684 }
6685 }
6686 }
Josh Bartel2ecce342010-02-25 10:55:48 -06006687 if (providers != null) {
6688 mSystemThread.installSystemProviders(providers);
6689 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006690
6691 mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf);
Mark Brophyc6350272011-08-05 16:16:39 +01006692
6693 mSelf.mUsageStatsService.monitorPackages();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 }
6695
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006696 /**
6697 * Allows app to retrieve the MIME type of a URI without having permission
6698 * to access its content provider.
6699 *
6700 * CTS tests for this functionality can be run with "runtest cts-appsecurity".
6701 *
6702 * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/
6703 * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java
6704 */
6705 public String getProviderMimeType(Uri uri) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006706 enforceNotIsolatedCaller("getProviderMimeType");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006707 final String name = uri.getAuthority();
6708 final long ident = Binder.clearCallingIdentity();
6709 ContentProviderHolder holder = null;
6710
6711 try {
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006712 holder = getContentProviderExternalUnchecked(name, null);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006713 if (holder != null) {
6714 return holder.provider.getType(uri);
6715 }
6716 } catch (RemoteException e) {
6717 Log.w(TAG, "Content provider dead retrieving " + uri, e);
6718 return null;
6719 } finally {
6720 if (holder != null) {
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006721 removeContentProviderExternalUnchecked(name, null);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006722 }
6723 Binder.restoreCallingIdentity(ident);
6724 }
6725
6726 return null;
6727 }
6728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 // =========================================================
6730 // GLOBAL MANAGEMENT
6731 // =========================================================
6732
6733 final ProcessRecord newProcessRecordLocked(IApplicationThread thread,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006734 ApplicationInfo info, String customProcess, boolean isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006735 String proc = customProcess != null ? customProcess : info.processName;
6736 BatteryStatsImpl.Uid.Proc ps = null;
6737 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006738 int uid = info.uid;
6739 if (isolated) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006740 int userId = UserHandle.getUserId(uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006741 int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1;
6742 uid = 0;
6743 while (true) {
6744 if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID
6745 || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) {
6746 mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID;
6747 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006748 uid = UserHandle.getUid(userId, mNextIsolatedProcessUid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006749 mNextIsolatedProcessUid++;
6750 if (mIsolatedProcesses.indexOfKey(uid) < 0) {
6751 // No process for this uid, use it.
6752 break;
6753 }
6754 stepsLeft--;
6755 if (stepsLeft <= 0) {
6756 return null;
6757 }
6758 }
6759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760 synchronized (stats) {
6761 ps = stats.getProcessStatsLocked(info.uid, proc);
6762 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006763 return new ProcessRecord(ps, thread, info, proc, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 }
6765
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006766 final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) {
6767 ProcessRecord app;
6768 if (!isolated) {
6769 app = getProcessRecordLocked(info.processName, info.uid);
6770 } else {
6771 app = null;
6772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773
6774 if (app == null) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006775 app = newProcessRecordLocked(null, info, null, isolated);
6776 mProcessNames.put(info.processName, app.uid, app);
6777 if (isolated) {
6778 mIsolatedProcesses.put(app.uid, app);
6779 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08006780 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 }
6782
Dianne Hackborne7f97212011-02-24 14:40:20 -08006783 // This package really, really can not be stopped.
6784 try {
6785 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006786 info.packageName, false, UserHandle.getUserId(app.uid));
Dianne Hackborne7f97212011-02-24 14:40:20 -08006787 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08006788 } catch (IllegalArgumentException e) {
6789 Slog.w(TAG, "Failed trying to unstop package "
6790 + info.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006791 }
6792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
6794 == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
6795 app.persistent = true;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07006796 app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 }
6798 if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
6799 mPersistentStartingProcesses.add(app);
6800 startProcessLocked(app, "added application", app.processName);
6801 }
6802
6803 return app;
6804 }
6805
6806 public void unhandledBack() {
6807 enforceCallingPermission(android.Manifest.permission.FORCE_BACK,
6808 "unhandledBack()");
6809
6810 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006811 int count = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006812 if (DEBUG_SWITCH) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813 TAG, "Performing unhandledBack(): stack size = " + count);
6814 if (count > 1) {
6815 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006816 mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 count-1, Activity.RESULT_CANCELED, null, "unhandled-back");
6818 Binder.restoreCallingIdentity(origId);
6819 }
6820 }
6821 }
6822
6823 public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006824 enforceNotIsolatedCaller("openContentUri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006825 String name = uri.getAuthority();
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006826 ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 ParcelFileDescriptor pfd = null;
6828 if (cph != null) {
6829 // We record the binder invoker's uid in thread-local storage before
6830 // going to the content provider to open the file. Later, in the code
6831 // that handles all permissions checks, we look for this uid and use
6832 // that rather than the Activity Manager's own uid. The effect is that
6833 // we do the check against the caller's permissions even though it looks
6834 // to the content provider like the Activity Manager itself is making
6835 // the request.
6836 sCallerIdentity.set(new Identity(
6837 Binder.getCallingPid(), Binder.getCallingUid()));
6838 try {
6839 pfd = cph.provider.openFile(uri, "r");
6840 } catch (FileNotFoundException e) {
6841 // do nothing; pfd will be returned null
6842 } finally {
6843 // Ensure that whatever happens, we clean up the identity state
6844 sCallerIdentity.remove();
6845 }
6846
6847 // We've got the fd now, so we're done with the provider.
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006848 removeContentProviderExternalUnchecked(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006850 Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 }
6852 return pfd;
6853 }
6854
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006855 // Actually is sleeping or shutting down or whatever else in the future
6856 // is an inactive state.
6857 public boolean isSleeping() {
6858 return mSleeping || mShuttingDown;
6859 }
6860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 public void goingToSleep() {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006862 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
6863 != PackageManager.PERMISSION_GRANTED) {
6864 throw new SecurityException("Requires permission "
6865 + android.Manifest.permission.DEVICE_POWER);
6866 }
6867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 synchronized(this) {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006869 mWentToSleep = true;
Jeff Brownc042ee22012-05-08 13:03:42 -07006870 updateEventDispatchingLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006872 if (!mSleeping) {
6873 mSleeping = true;
6874 mMainStack.stopIfSleepingLocked();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006875
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006876 // Initialize the wake times of all processes.
6877 checkExcessivePowerUsageLocked(false);
6878 mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
6879 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
6880 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
6881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882 }
6883 }
6884
Dianne Hackborn55280a92009-05-07 15:53:46 -07006885 public boolean shutdown(int timeout) {
6886 if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
6887 != PackageManager.PERMISSION_GRANTED) {
6888 throw new SecurityException("Requires permission "
6889 + android.Manifest.permission.SHUTDOWN);
6890 }
6891
6892 boolean timedout = false;
6893
6894 synchronized(this) {
6895 mShuttingDown = true;
Jeff Brownc042ee22012-05-08 13:03:42 -07006896 updateEventDispatchingLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -07006897
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006898 if (mMainStack.mResumedActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006899 mMainStack.stopIfSleepingLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -07006900 final long endTime = System.currentTimeMillis() + timeout;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006901 while (mMainStack.mResumedActivity != null
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08006902 || mMainStack.mPausingActivity != null) {
Dianne Hackborn55280a92009-05-07 15:53:46 -07006903 long delay = endTime - System.currentTimeMillis();
6904 if (delay <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006905 Slog.w(TAG, "Activity manager shutdown timed out");
Dianne Hackborn55280a92009-05-07 15:53:46 -07006906 timedout = true;
6907 break;
6908 }
6909 try {
6910 this.wait();
6911 } catch (InterruptedException e) {
6912 }
6913 }
6914 }
6915 }
6916
6917 mUsageStatsService.shutdown();
6918 mBatteryStatsService.shutdown();
6919
6920 return timedout;
6921 }
6922
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006923 public final void activitySlept(IBinder token) {
6924 if (localLOGV) Slog.v(
6925 TAG, "Activity slept: token=" + token);
6926
6927 ActivityRecord r = null;
6928
6929 final long origId = Binder.clearCallingIdentity();
6930
6931 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07006932 r = mMainStack.isInStackLocked(token);
6933 if (r != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006934 mMainStack.activitySleptLocked(r);
6935 }
6936 }
6937
6938 Binder.restoreCallingIdentity(origId);
6939 }
6940
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006941 private void comeOutOfSleepIfNeededLocked() {
6942 if (!mWentToSleep && !mLockScreenShown) {
6943 if (mSleeping) {
6944 mSleeping = false;
6945 mMainStack.awakeFromSleepingLocked();
6946 mMainStack.resumeTopActivityLocked(null);
6947 }
6948 }
6949 }
6950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006951 public void wakingUp() {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006952 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
6953 != PackageManager.PERMISSION_GRANTED) {
6954 throw new SecurityException("Requires permission "
6955 + android.Manifest.permission.DEVICE_POWER);
6956 }
6957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 synchronized(this) {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006959 mWentToSleep = false;
Jeff Brownc042ee22012-05-08 13:03:42 -07006960 updateEventDispatchingLocked();
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006961 comeOutOfSleepIfNeededLocked();
6962 }
6963 }
6964
Jeff Brownc042ee22012-05-08 13:03:42 -07006965 private void updateEventDispatchingLocked() {
6966 mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown);
6967 }
6968
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006969 public void setLockScreenShown(boolean shown) {
6970 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
6971 != PackageManager.PERMISSION_GRANTED) {
6972 throw new SecurityException("Requires permission "
6973 + android.Manifest.permission.DEVICE_POWER);
6974 }
6975
6976 synchronized(this) {
6977 mLockScreenShown = shown;
6978 comeOutOfSleepIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 }
6980 }
6981
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07006982 public void stopAppSwitches() {
6983 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
6984 != PackageManager.PERMISSION_GRANTED) {
6985 throw new SecurityException("Requires permission "
6986 + android.Manifest.permission.STOP_APP_SWITCHES);
6987 }
6988
6989 synchronized(this) {
6990 mAppSwitchesAllowedTime = SystemClock.uptimeMillis()
6991 + APP_SWITCH_DELAY_TIME;
6992 mDidAppSwitch = false;
6993 mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
6994 Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
6995 mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME);
6996 }
6997 }
6998
6999 public void resumeAppSwitches() {
7000 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
7001 != PackageManager.PERMISSION_GRANTED) {
7002 throw new SecurityException("Requires permission "
7003 + android.Manifest.permission.STOP_APP_SWITCHES);
7004 }
7005
7006 synchronized(this) {
7007 // Note that we don't execute any pending app switches... we will
7008 // let those wait until either the timeout, or the next start
7009 // activity request.
7010 mAppSwitchesAllowedTime = 0;
7011 }
7012 }
7013
7014 boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid,
7015 String name) {
7016 if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
7017 return true;
7018 }
7019
7020 final int perm = checkComponentPermission(
7021 android.Manifest.permission.STOP_APP_SWITCHES, callingPid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08007022 callingUid, -1, true);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007023 if (perm == PackageManager.PERMISSION_GRANTED) {
7024 return true;
7025 }
7026
Joe Onorato8a9b2202010-02-26 18:56:32 -08007027 Slog.w(TAG, name + " request from " + callingUid + " stopped");
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007028 return false;
7029 }
7030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 public void setDebugApp(String packageName, boolean waitForDebugger,
7032 boolean persistent) {
7033 enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
7034 "setDebugApp()");
7035
7036 // Note that this is not really thread safe if there are multiple
7037 // callers into it at the same time, but that's not a situation we
7038 // care about.
7039 if (persistent) {
7040 final ContentResolver resolver = mContext.getContentResolver();
7041 Settings.System.putString(
7042 resolver, Settings.System.DEBUG_APP,
7043 packageName);
7044 Settings.System.putInt(
7045 resolver, Settings.System.WAIT_FOR_DEBUGGER,
7046 waitForDebugger ? 1 : 0);
7047 }
7048
7049 synchronized (this) {
7050 if (!persistent) {
7051 mOrigDebugApp = mDebugApp;
7052 mOrigWaitForDebugger = mWaitForDebugger;
7053 }
7054 mDebugApp = packageName;
7055 mWaitForDebugger = waitForDebugger;
7056 mDebugTransient = !persistent;
7057 if (packageName != null) {
7058 final long origId = Binder.clearCallingIdentity();
Amith Yamasani483f3b02012-03-13 16:08:00 -07007059 forceStopPackageLocked(packageName, -1, false, false, true, true, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 Binder.restoreCallingIdentity(origId);
7061 }
7062 }
7063 }
7064
Siva Velusamy92a8b222012-03-09 16:24:04 -08007065 void setOpenGlTraceApp(ApplicationInfo app, String processName) {
7066 synchronized (this) {
7067 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
7068 if (!isDebuggable) {
7069 if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
7070 throw new SecurityException("Process not debuggable: " + app.packageName);
7071 }
7072 }
7073
7074 mOpenGlTraceApp = processName;
7075 }
7076 }
7077
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07007078 void setProfileApp(ApplicationInfo app, String processName, String profileFile,
7079 ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
7080 synchronized (this) {
7081 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
7082 if (!isDebuggable) {
7083 if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
7084 throw new SecurityException("Process not debuggable: " + app.packageName);
7085 }
7086 }
7087 mProfileApp = processName;
7088 mProfileFile = profileFile;
7089 if (mProfileFd != null) {
7090 try {
7091 mProfileFd.close();
7092 } catch (IOException e) {
7093 }
7094 mProfileFd = null;
7095 }
7096 mProfileFd = profileFd;
7097 mProfileType = 0;
7098 mAutoStopProfiler = autoStopProfiler;
7099 }
7100 }
7101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 public void setAlwaysFinish(boolean enabled) {
7103 enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH,
7104 "setAlwaysFinish()");
7105
7106 Settings.System.putInt(
7107 mContext.getContentResolver(),
7108 Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0);
7109
7110 synchronized (this) {
7111 mAlwaysFinishActivities = enabled;
7112 }
7113 }
7114
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007115 public void setActivityController(IActivityController controller) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007116 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007117 "setActivityController()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007119 mController = controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 }
7121 }
7122
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08007123 public boolean isUserAMonkey() {
7124 // For now the fact that there is a controller implies
7125 // we have a monkey.
7126 synchronized (this) {
7127 return mController != null;
7128 }
7129 }
7130
Jeff Sharkeya4620792011-05-20 15:29:23 -07007131 public void registerProcessObserver(IProcessObserver observer) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08007132 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
7133 "registerProcessObserver()");
7134 synchronized (this) {
7135 mProcessObservers.register(observer);
7136 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07007137 }
7138
7139 public void unregisterProcessObserver(IProcessObserver observer) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08007140 synchronized (this) {
7141 mProcessObservers.unregister(observer);
7142 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07007143 }
7144
Daniel Sandler69a48172010-06-23 16:29:36 -04007145 public void setImmersive(IBinder token, boolean immersive) {
7146 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07007147 ActivityRecord r = mMainStack.isInStackLocked(token);
7148 if (r == null) {
Daniel Sandler69a48172010-06-23 16:29:36 -04007149 throw new IllegalArgumentException();
7150 }
Daniel Sandler69a48172010-06-23 16:29:36 -04007151 r.immersive = immersive;
7152 }
7153 }
7154
7155 public boolean isImmersive(IBinder token) {
7156 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07007157 ActivityRecord r = mMainStack.isInStackLocked(token);
7158 if (r == null) {
Daniel Sandler69a48172010-06-23 16:29:36 -04007159 throw new IllegalArgumentException();
7160 }
Daniel Sandler69a48172010-06-23 16:29:36 -04007161 return r.immersive;
7162 }
7163 }
7164
7165 public boolean isTopActivityImmersive() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08007166 enforceNotIsolatedCaller("startActivity");
Daniel Sandler69a48172010-06-23 16:29:36 -04007167 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007168 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Daniel Sandler69a48172010-06-23 16:29:36 -04007169 return (r != null) ? r.immersive : false;
7170 }
7171 }
7172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007173 public final void enterSafeMode() {
7174 synchronized(this) {
7175 // It only makes sense to do this before the system is ready
7176 // and started launching other packages.
7177 if (!mSystemReady) {
7178 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007179 AppGlobals.getPackageManager().enterSafeMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 } catch (RemoteException e) {
7181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 }
7183 }
7184 }
7185
Jeff Brownb09abc12011-01-13 21:08:27 -08007186 public final void showSafeModeOverlay() {
7187 View v = LayoutInflater.from(mContext).inflate(
7188 com.android.internal.R.layout.safe_mode, null);
7189 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
7190 lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
7191 lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
7192 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
Fabrice Di Meglioaac0d4e2012-07-19 19:21:26 -07007193 lp.gravity = Gravity.BOTTOM | Gravity.START;
Jeff Brownb09abc12011-01-13 21:08:27 -08007194 lp.format = v.getBackground().getOpacity();
7195 lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
7196 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
7197 ((WindowManager)mContext.getSystemService(
7198 Context.WINDOW_SERVICE)).addView(v, lp);
7199 }
7200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007201 public void noteWakeupAlarm(IIntentSender sender) {
7202 if (!(sender instanceof PendingIntentRecord)) {
7203 return;
7204 }
7205 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
7206 synchronized (stats) {
7207 if (mBatteryStatsService.isOnBattery()) {
7208 mBatteryStatsService.enforceCallingPermission();
7209 PendingIntentRecord rec = (PendingIntentRecord)sender;
7210 int MY_UID = Binder.getCallingUid();
7211 int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
7212 BatteryStatsImpl.Uid.Pkg pkg =
7213 stats.getPackageStatsLocked(uid, rec.key.packageName);
7214 pkg.incWakeupsLocked();
7215 }
7216 }
7217 }
7218
Dianne Hackborn64825172011-03-02 21:32:58 -08007219 public boolean killPids(int[] pids, String pReason, boolean secure) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007221 throw new SecurityException("killPids only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007223 String reason = (pReason == null) ? "Unknown" : pReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 // XXX Note: don't acquire main activity lock here, because the window
7225 // manager calls in with its locks held.
7226
7227 boolean killed = false;
7228 synchronized (mPidsSelfLocked) {
7229 int[] types = new int[pids.length];
7230 int worstType = 0;
7231 for (int i=0; i<pids.length; i++) {
7232 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
7233 if (proc != null) {
7234 int type = proc.setAdj;
7235 types[i] = type;
7236 if (type > worstType) {
7237 worstType = type;
7238 }
7239 }
7240 }
7241
Dianne Hackborn64825172011-03-02 21:32:58 -08007242 // If the worst oom_adj is somewhere in the hidden proc LRU range,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 // then constrain it so we will kill all hidden procs.
Dianne Hackborne02c88a2011-10-28 13:58:15 -07007244 if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ
7245 && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07007246 worstType = ProcessList.HIDDEN_APP_MIN_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 }
Dianne Hackborn64825172011-03-02 21:32:58 -08007248
7249 // If this is not a secure call, don't let it kill processes that
7250 // are important.
Dianne Hackborne02c88a2011-10-28 13:58:15 -07007251 if (!secure && worstType < ProcessList.SERVICE_ADJ) {
7252 worstType = ProcessList.SERVICE_ADJ;
Dianne Hackborn64825172011-03-02 21:32:58 -08007253 }
7254
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007255 Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256 for (int i=0; i<pids.length; i++) {
7257 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
7258 if (proc == null) {
7259 continue;
7260 }
7261 int adj = proc.setAdj;
Dianne Hackborn906497c2010-05-10 15:57:38 -07007262 if (adj >= worstType && !proc.killedBackground) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07007263 Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007264 EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid,
7265 proc.processName, adj, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007266 killed = true;
Dianne Hackborn906497c2010-05-10 15:57:38 -07007267 proc.killedBackground = true;
7268 Process.killProcessQuiet(pids[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007269 }
7270 }
7271 }
7272 return killed;
7273 }
Jeff Sharkeyb9a07012012-03-22 17:00:04 -07007274
7275 @Override
7276 public boolean killProcessesBelowForeground(String reason) {
7277 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
7278 throw new SecurityException("killProcessesBelowForeground() only available to system");
7279 }
7280
7281 return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason);
7282 }
7283
7284 private boolean killProcessesBelowAdj(int belowAdj, String reason) {
7285 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
7286 throw new SecurityException("killProcessesBelowAdj() only available to system");
7287 }
7288
7289 boolean killed = false;
7290 synchronized (mPidsSelfLocked) {
7291 final int size = mPidsSelfLocked.size();
7292 for (int i = 0; i < size; i++) {
7293 final int pid = mPidsSelfLocked.keyAt(i);
7294 final ProcessRecord proc = mPidsSelfLocked.valueAt(i);
7295 if (proc == null) continue;
7296
7297 final int adj = proc.setAdj;
7298 if (adj > belowAdj && !proc.killedBackground) {
7299 Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
7300 EventLog.writeEvent(
7301 EventLogTags.AM_KILL, proc.pid, proc.processName, adj, reason);
7302 killed = true;
7303 proc.killedBackground = true;
7304 Process.killProcessQuiet(pid);
7305 }
7306 }
7307 }
7308 return killed;
7309 }
7310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 public final void startRunning(String pkg, String cls, String action,
7312 String data) {
7313 synchronized(this) {
7314 if (mStartRunning) {
7315 return;
7316 }
7317 mStartRunning = true;
7318 mTopComponent = pkg != null && cls != null
7319 ? new ComponentName(pkg, cls) : null;
7320 mTopAction = action != null ? action : Intent.ACTION_MAIN;
7321 mTopData = data;
7322 if (!mSystemReady) {
7323 return;
7324 }
7325 }
7326
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007327 systemReady(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328 }
7329
7330 private void retrieveSettings() {
7331 final ContentResolver resolver = mContext.getContentResolver();
7332 String debugApp = Settings.System.getString(
7333 resolver, Settings.System.DEBUG_APP);
7334 boolean waitForDebugger = Settings.System.getInt(
7335 resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0;
7336 boolean alwaysFinishActivities = Settings.System.getInt(
7337 resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0;
7338
7339 Configuration configuration = new Configuration();
7340 Settings.System.getConfiguration(resolver, configuration);
7341
7342 synchronized (this) {
7343 mDebugApp = mOrigDebugApp = debugApp;
7344 mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger;
7345 mAlwaysFinishActivities = alwaysFinishActivities;
7346 // This happens before any activities are started, so we can
7347 // change mConfiguration in-place.
Dianne Hackborn813075a62011-11-14 17:45:19 -08007348 updateConfigurationLocked(configuration, null, false, true);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007349 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007350 }
7351 }
7352
7353 public boolean testIsSystemReady() {
7354 // no need to synchronize(this) just to read & return the value
7355 return mSystemReady;
7356 }
7357
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007358 private static File getCalledPreBootReceiversFile() {
7359 File dataDir = Environment.getDataDirectory();
7360 File systemDir = new File(dataDir, "system");
7361 File fname = new File(systemDir, "called_pre_boots.dat");
7362 return fname;
7363 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07007364
7365 static final int LAST_DONE_VERSION = 10000;
7366
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007367 private static ArrayList<ComponentName> readLastDonePreBootReceivers() {
7368 ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>();
7369 File file = getCalledPreBootReceiversFile();
7370 FileInputStream fis = null;
7371 try {
7372 fis = new FileInputStream(file);
7373 DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048));
Dianne Hackborn661cd522011-08-22 00:26:20 -07007374 int fvers = dis.readInt();
7375 if (fvers == LAST_DONE_VERSION) {
7376 String vers = dis.readUTF();
7377 String codename = dis.readUTF();
7378 String build = dis.readUTF();
7379 if (android.os.Build.VERSION.RELEASE.equals(vers)
7380 && android.os.Build.VERSION.CODENAME.equals(codename)
7381 && android.os.Build.VERSION.INCREMENTAL.equals(build)) {
7382 int num = dis.readInt();
7383 while (num > 0) {
7384 num--;
7385 String pkg = dis.readUTF();
7386 String cls = dis.readUTF();
7387 lastDoneReceivers.add(new ComponentName(pkg, cls));
7388 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007389 }
7390 }
7391 } catch (FileNotFoundException e) {
7392 } catch (IOException e) {
7393 Slog.w(TAG, "Failure reading last done pre-boot receivers", e);
7394 } finally {
7395 if (fis != null) {
7396 try {
7397 fis.close();
7398 } catch (IOException e) {
7399 }
7400 }
7401 }
7402 return lastDoneReceivers;
7403 }
7404
7405 private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) {
7406 File file = getCalledPreBootReceiversFile();
7407 FileOutputStream fos = null;
7408 DataOutputStream dos = null;
7409 try {
7410 Slog.i(TAG, "Writing new set of last done pre-boot receivers...");
7411 fos = new FileOutputStream(file);
7412 dos = new DataOutputStream(new BufferedOutputStream(fos, 2048));
Dianne Hackborn661cd522011-08-22 00:26:20 -07007413 dos.writeInt(LAST_DONE_VERSION);
7414 dos.writeUTF(android.os.Build.VERSION.RELEASE);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007415 dos.writeUTF(android.os.Build.VERSION.CODENAME);
Dianne Hackborn661cd522011-08-22 00:26:20 -07007416 dos.writeUTF(android.os.Build.VERSION.INCREMENTAL);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007417 dos.writeInt(list.size());
7418 for (int i=0; i<list.size(); i++) {
7419 dos.writeUTF(list.get(i).getPackageName());
7420 dos.writeUTF(list.get(i).getClassName());
7421 }
7422 } catch (IOException e) {
7423 Slog.w(TAG, "Failure writing last done pre-boot receivers", e);
7424 file.delete();
7425 } finally {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07007426 FileUtils.sync(fos);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007427 if (dos != null) {
7428 try {
7429 dos.close();
7430 } catch (IOException e) {
7431 // TODO Auto-generated catch block
7432 e.printStackTrace();
7433 }
7434 }
7435 }
7436 }
7437
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007438 public void systemReady(final Runnable goingCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007439 synchronized(this) {
7440 if (mSystemReady) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007441 if (goingCallback != null) goingCallback.run();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007442 return;
7443 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007444
7445 // Check to see if there are any update receivers to run.
7446 if (!mDidUpdate) {
7447 if (mWaitingUpdate) {
7448 return;
7449 }
7450 Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
7451 List<ResolveInfo> ris = null;
7452 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007453 ris = AppGlobals.getPackageManager().queryIntentReceivers(
Amith Yamasani483f3b02012-03-13 16:08:00 -07007454 intent, null, 0, 0);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007455 } catch (RemoteException e) {
7456 }
7457 if (ris != null) {
7458 for (int i=ris.size()-1; i>=0; i--) {
7459 if ((ris.get(i).activityInfo.applicationInfo.flags
7460 &ApplicationInfo.FLAG_SYSTEM) == 0) {
7461 ris.remove(i);
7462 }
7463 }
7464 intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007465
7466 ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers();
7467
7468 final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>();
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007469 for (int i=0; i<ris.size(); i++) {
7470 ActivityInfo ai = ris.get(i).activityInfo;
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007471 ComponentName comp = new ComponentName(ai.packageName, ai.name);
7472 if (lastDoneReceivers.contains(comp)) {
7473 ris.remove(i);
7474 i--;
7475 }
7476 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07007477
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007478 for (int i=0; i<ris.size(); i++) {
7479 ActivityInfo ai = ris.get(i).activityInfo;
7480 ComponentName comp = new ComponentName(ai.packageName, ai.name);
7481 doneReceivers.add(comp);
7482 intent.setComponent(comp);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007483 IIntentReceiver finisher = null;
Dianne Hackbornd6847842010-01-12 18:14:19 -08007484 if (i == ris.size()-1) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007485 finisher = new IIntentReceiver.Stub() {
7486 public void performReceive(Intent intent, int resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -07007487 String data, Bundle extras, boolean ordered,
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07007488 boolean sticky) {
7489 // The raw IIntentReceiver interface is called
7490 // with the AM lock held, so redispatch to
7491 // execute our code without the lock.
7492 mHandler.post(new Runnable() {
7493 public void run() {
7494 synchronized (ActivityManagerService.this) {
7495 mDidUpdate = true;
7496 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007497 writeLastDonePreBootReceivers(doneReceivers);
Dianne Hackborn661cd522011-08-22 00:26:20 -07007498 showBootMessage(mContext.getText(
7499 R.string.android_upgrading_complete),
7500 false);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07007501 systemReady(goingCallback);
7502 }
7503 });
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007504 }
7505 };
7506 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007507 Slog.i(TAG, "Sending system update to: " + intent.getComponent());
Amith Yamasani742a6712011-05-04 14:49:28 -07007508 /* TODO: Send this to all users */
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007509 broadcastIntentLocked(null, null, intent, null, finisher,
Amith Yamasani742a6712011-05-04 14:49:28 -07007510 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID,
Amith Yamasanic600e21f2012-02-14 16:08:07 -08007511 0 /* UserId zero */);
Dianne Hackbornd6847842010-01-12 18:14:19 -08007512 if (finisher != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007513 mWaitingUpdate = true;
7514 }
7515 }
7516 }
7517 if (mWaitingUpdate) {
7518 return;
7519 }
7520 mDidUpdate = true;
7521 }
7522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 mSystemReady = true;
7524 if (!mStartRunning) {
7525 return;
7526 }
7527 }
7528
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007529 ArrayList<ProcessRecord> procsToKill = null;
7530 synchronized(mPidsSelfLocked) {
7531 for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
7532 ProcessRecord proc = mPidsSelfLocked.valueAt(i);
7533 if (!isAllowedWhileBooting(proc.info)){
7534 if (procsToKill == null) {
7535 procsToKill = new ArrayList<ProcessRecord>();
7536 }
7537 procsToKill.add(proc);
7538 }
7539 }
7540 }
7541
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007542 synchronized(this) {
7543 if (procsToKill != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007544 for (int i=procsToKill.size()-1; i>=0; i--) {
7545 ProcessRecord proc = procsToKill.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007546 Slog.i(TAG, "Removing system update proc: " + proc);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08007547 removeProcessLocked(proc, true, false, "system update done");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007548 }
7549 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007550
7551 // Now that we have cleaned up any update processes, we
7552 // are ready to start launching real processes and know that
7553 // we won't trample on them any more.
7554 mProcessesReady = true;
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007555 }
7556
Joe Onorato8a9b2202010-02-26 18:56:32 -08007557 Slog.i(TAG, "System now ready");
Doug Zongker2bec3d42009-12-04 12:52:44 -08007558 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007559 SystemClock.uptimeMillis());
7560
7561 synchronized(this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007562 // Make sure we have no pre-ready processes sitting around.
7563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
7565 ResolveInfo ri = mContext.getPackageManager()
7566 .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
Dianne Hackborn1655be42009-05-08 14:29:01 -07007567 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 CharSequence errorMsg = null;
7569 if (ri != null) {
7570 ActivityInfo ai = ri.activityInfo;
7571 ApplicationInfo app = ai.applicationInfo;
7572 if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
7573 mTopAction = Intent.ACTION_FACTORY_TEST;
7574 mTopData = null;
7575 mTopComponent = new ComponentName(app.packageName,
7576 ai.name);
7577 } else {
7578 errorMsg = mContext.getResources().getText(
7579 com.android.internal.R.string.factorytest_not_system);
7580 }
7581 } else {
7582 errorMsg = mContext.getResources().getText(
7583 com.android.internal.R.string.factorytest_no_action);
7584 }
7585 if (errorMsg != null) {
7586 mTopAction = null;
7587 mTopData = null;
7588 mTopComponent = null;
7589 Message msg = Message.obtain();
7590 msg.what = SHOW_FACTORY_ERROR_MSG;
7591 msg.getData().putCharSequence("msg", errorMsg);
7592 mHandler.sendMessage(msg);
7593 }
7594 }
7595 }
7596
7597 retrieveSettings();
7598
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007599 if (goingCallback != null) goingCallback.run();
7600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007601 synchronized (this) {
7602 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
7603 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007604 List apps = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07007605 getPersistentApplications(STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 if (apps != null) {
7607 int N = apps.size();
7608 int i;
7609 for (i=0; i<N; i++) {
7610 ApplicationInfo info
7611 = (ApplicationInfo)apps.get(i);
7612 if (info != null &&
7613 !info.packageName.equals("android")) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007614 addAppLocked(info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 }
7616 }
7617 }
7618 } catch (RemoteException ex) {
7619 // pm is in same process, this will never happen.
7620 }
7621 }
7622
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007623 // Start up initial activity.
7624 mBooting = true;
7625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007627 if (AppGlobals.getPackageManager().hasSystemUidErrors()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007628 Message msg = Message.obtain();
7629 msg.what = SHOW_UID_ERROR_MSG;
7630 mHandler.sendMessage(msg);
7631 }
7632 } catch (RemoteException e) {
7633 }
7634
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007635 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 }
7637 }
7638
Dan Egnorb7f03672009-12-09 16:22:32 -08007639 private boolean makeAppCrashingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007640 String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 app.crashing = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08007642 app.crashingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08007643 ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 startAppProblemLocked(app);
7645 app.stopFreezingAllLocked();
7646 return handleAppCrashLocked(app);
7647 }
7648
Dan Egnorb7f03672009-12-09 16:22:32 -08007649 private void makeAppNotRespondingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007650 String activity, String shortMsg, String longMsg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007651 app.notResponding = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08007652 app.notRespondingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08007653 ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
7654 activity, shortMsg, longMsg, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007655 startAppProblemLocked(app);
7656 app.stopFreezingAllLocked();
7657 }
7658
7659 /**
7660 * Generate a process error record, suitable for attachment to a ProcessRecord.
7661 *
7662 * @param app The ProcessRecord in which the error occurred.
7663 * @param condition Crashing, Application Not Responding, etc. Values are defined in
7664 * ActivityManager.AppErrorStateInfo
Dan Egnor60d87622009-12-16 16:32:58 -08007665 * @param activity The activity associated with the crash, if known.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 * @param shortMsg Short message describing the crash.
7667 * @param longMsg Long message describing the crash.
Dan Egnorb7f03672009-12-09 16:22:32 -08007668 * @param stackTrace Full crash stack trace, may be null.
7669 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 * @return Returns a fully-formed AppErrorStateInfo record.
7671 */
7672 private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007673 int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007674 ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
Dan Egnorb7f03672009-12-09 16:22:32 -08007675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 report.condition = condition;
7677 report.processName = app.processName;
7678 report.pid = app.pid;
7679 report.uid = app.info.uid;
Dan Egnor60d87622009-12-16 16:32:58 -08007680 report.tag = activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 report.shortMsg = shortMsg;
7682 report.longMsg = longMsg;
Dan Egnorb7f03672009-12-09 16:22:32 -08007683 report.stackTrace = stackTrace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684
7685 return report;
7686 }
7687
Dan Egnor42471dd2010-01-07 17:25:22 -08007688 void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007689 synchronized (this) {
7690 app.crashing = false;
7691 app.crashingReport = null;
7692 app.notResponding = false;
7693 app.notRespondingReport = null;
7694 if (app.anrDialog == fromDialog) {
7695 app.anrDialog = null;
7696 }
7697 if (app.waitDialog == fromDialog) {
7698 app.waitDialog = null;
7699 }
7700 if (app.pid > 0 && app.pid != MY_PID) {
Dan Egnor42471dd2010-01-07 17:25:22 -08007701 handleAppCrashLocked(app);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07007702 Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request");
Dianne Hackborn8633e682010-04-22 16:03:41 -07007703 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
7704 app.processName, app.setAdj, "user's request after error");
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07007705 Process.killProcessQuiet(app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 }
7708 }
Dan Egnor42471dd2010-01-07 17:25:22 -08007709
Dan Egnorb7f03672009-12-09 16:22:32 -08007710 private boolean handleAppCrashLocked(ProcessRecord app) {
Mike Lockwood86548c42011-09-13 17:21:46 -04007711 if (mHeadless) {
7712 Log.e(TAG, "handleAppCrashLocked: " + app.processName);
7713 return false;
7714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715 long now = SystemClock.uptimeMillis();
7716
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007717 Long crashTime;
7718 if (!app.isolated) {
7719 crashTime = mProcessCrashTimes.get(app.info.processName, app.uid);
7720 } else {
7721 crashTime = null;
7722 }
Dianne Hackborn7d608422011-08-07 16:24:18 -07007723 if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 // This process loses!
Joe Onorato8a9b2202010-02-26 18:56:32 -08007725 Slog.w(TAG, "Process " + app.info.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 + " has crashed too many times: killing!");
Doug Zongker2bec3d42009-12-04 12:52:44 -08007727 EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007728 app.info.processName, app.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007729 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
7730 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731 if (r.app == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007732 Slog.w(TAG, " Force finishing activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007733 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007734 r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "crashed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007735 }
7736 }
7737 if (!app.persistent) {
7738 // We don't want to start this process again until the user
7739 // explicitly does so... but for persistent process, we really
7740 // need to keep it running. If a persistent process is actually
7741 // repeatedly crashing, then badness for everyone.
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007742 EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007743 app.info.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007744 if (!app.isolated) {
7745 // XXX We don't have a way to mark isolated processes
7746 // as bad, since they don't have a peristent identity.
7747 mBadProcesses.put(app.info.processName, app.uid, now);
7748 mProcessCrashTimes.remove(app.info.processName, app.uid);
7749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750 app.bad = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 app.removed = true;
Dianne Hackborn130b0d22011-07-26 22:07:48 -07007752 // Don't let services in this process be restarted and potentially
7753 // annoy the user repeatedly. Unless it is persistent, since those
7754 // processes run critical code.
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08007755 removeProcessLocked(app, false, false, "crash");
Dianne Hackborncb44d962011-03-10 17:02:27 -08007756 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 return false;
7758 }
Dianne Hackborncb44d962011-03-10 17:02:27 -08007759 mMainStack.resumeTopActivityLocked(null);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007760 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007761 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Kevin Hester-Chowd87a9be2012-03-05 08:01:00 -08007762 if (r != null && r.app == app) {
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007763 // If the top running activity is from this crashing
7764 // process, then terminate it to avoid getting in a loop.
7765 Slog.w(TAG, " Force finishing activity "
7766 + r.intent.getComponent().flattenToShortString());
Dianne Hackbornbe707852011-11-11 14:32:10 -08007767 int index = mMainStack.indexOfActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007768 r.stack.finishActivityLocked(r, index,
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007769 Activity.RESULT_CANCELED, null, "crashed");
Dianne Hackborn070783f2010-12-29 16:46:28 -08007770 // Also terminate any activities below it that aren't yet
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007771 // stopped, to avoid a situation where one will get
7772 // re-start our crashing activity once it gets resumed again.
7773 index--;
7774 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007775 r = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007776 if (r.state == ActivityState.RESUMED
7777 || r.state == ActivityState.PAUSING
7778 || r.state == ActivityState.PAUSED) {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08007779 if (!r.isHomeActivity || mHomeProcess != r.app) {
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007780 Slog.w(TAG, " Force finishing activity "
7781 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007782 r.stack.finishActivityLocked(r, index,
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007783 Activity.RESULT_CANCELED, null, "crashed");
7784 }
7785 }
7786 }
7787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 }
7789
7790 // Bump up the crash count of any services currently running in the proc.
7791 if (app.services.size() != 0) {
7792 // Any services running in the application need to be placed
7793 // back in the pending list.
Dianne Hackborn860755f2010-06-03 18:47:52 -07007794 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007796 ServiceRecord sr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 sr.crashCount++;
7798 }
7799 }
Mattias Larssona4fd0072010-06-22 22:37:03 +02007800
7801 // If the crashing process is what we consider to be the "home process" and it has been
7802 // replaced by a third-party app, clear the package preferred activities from packages
7803 // with a home activity running in the process to prevent a repeatedly crashing app
7804 // from blocking the user to manually clear the list.
7805 if (app == mHomeProcess && mHomeProcess.activities.size() > 0
7806 && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
7807 Iterator it = mHomeProcess.activities.iterator();
7808 while (it.hasNext()) {
Jean-Baptiste Queru5ea89f72010-07-30 09:30:31 -07007809 ActivityRecord r = (ActivityRecord)it.next();
Mattias Larssona4fd0072010-06-22 22:37:03 +02007810 if (r.isHomeActivity) {
7811 Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
7812 try {
7813 ActivityThread.getPackageManager()
7814 .clearPackagePreferredActivities(r.packageName);
7815 } catch (RemoteException c) {
7816 // pm is in same process, this will never happen.
7817 }
7818 }
7819 }
7820 }
7821
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007822 if (!app.isolated) {
7823 // XXX Can't keep track of crash times for isolated processes,
7824 // because they don't have a perisistent identity.
7825 mProcessCrashTimes.put(app.info.processName, app.uid, now);
7826 }
7827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 return true;
7829 }
7830
7831 void startAppProblemLocked(ProcessRecord app) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08007832 app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
7833 mContext, app.info.packageName, app.info.flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 skipCurrentReceiverLocked(app);
7835 }
7836
7837 void skipCurrentReceiverLocked(ProcessRecord app) {
Christopher Tatef46723b2012-01-26 14:19:24 -08007838 for (BroadcastQueue queue : mBroadcastQueues) {
7839 queue.skipCurrentReceiverLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 }
7841 }
7842
Dan Egnor60d87622009-12-16 16:32:58 -08007843 /**
7844 * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes.
7845 * The application process will exit immediately after this call returns.
7846 * @param app object of the crashing app, null for the system server
7847 * @param crashInfo describing the exception
7848 */
7849 public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08007850 ProcessRecord r = findAppProcess(app, "Crash");
Jeff Sharkeya353d262011-10-28 11:12:06 -07007851 final String processName = app == null ? "system_server"
7852 : (r == null ? "unknown" : r.processName);
Dan Egnor60d87622009-12-16 16:32:58 -08007853
7854 EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(),
Jeff Sharkeya353d262011-10-28 11:12:06 -07007855 processName,
Dan Egnor2780e732010-01-22 14:47:35 -08007856 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08007857 crashInfo.exceptionClassName,
7858 crashInfo.exceptionMessage,
7859 crashInfo.throwFileName,
7860 crashInfo.throwLineNumber);
7861
Jeff Sharkeya353d262011-10-28 11:12:06 -07007862 addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08007863
7864 crashApplication(r, crashInfo);
7865 }
7866
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07007867 public void handleApplicationStrictModeViolation(
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007868 IBinder app,
7869 int violationMask,
7870 StrictMode.ViolationInfo info) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08007871 ProcessRecord r = findAppProcess(app, "StrictMode");
7872 if (r == null) {
7873 return;
7874 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07007875
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07007876 if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) {
Brad Fitzpatrickf3d86be2010-11-23 10:31:52 -08007877 Integer stackFingerprint = info.hashCode();
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07007878 boolean logIt = true;
7879 synchronized (mAlreadyLoggedViolatedStacks) {
7880 if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) {
7881 logIt = false;
7882 // TODO: sub-sample into EventLog for these, with
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007883 // the info.durationMillis? Then we'd get
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07007884 // the relative pain numbers, without logging all
7885 // the stack traces repeatedly. We'd want to do
7886 // likewise in the client code, which also does
7887 // dup suppression, before the Binder call.
7888 } else {
7889 if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) {
7890 mAlreadyLoggedViolatedStacks.clear();
7891 }
7892 mAlreadyLoggedViolatedStacks.add(stackFingerprint);
7893 }
7894 }
7895 if (logIt) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007896 logStrictModeViolationToDropBox(r, info);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07007897 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07007898 }
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07007899
7900 if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) {
7901 AppErrorResult result = new AppErrorResult();
7902 synchronized (this) {
7903 final long origId = Binder.clearCallingIdentity();
7904
7905 Message msg = Message.obtain();
7906 msg.what = SHOW_STRICT_MODE_VIOLATION_MSG;
7907 HashMap<String, Object> data = new HashMap<String, Object>();
7908 data.put("result", result);
7909 data.put("app", r);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07007910 data.put("violationMask", violationMask);
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007911 data.put("info", info);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07007912 msg.obj = data;
7913 mHandler.sendMessage(msg);
7914
7915 Binder.restoreCallingIdentity(origId);
7916 }
7917 int res = result.get();
Dianne Hackbornb424b632010-08-18 15:59:05 -07007918 Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07007919 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07007920 }
7921
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007922 // Depending on the policy in effect, there could be a bunch of
7923 // these in quick succession so we try to batch these together to
7924 // minimize disk writes, number of dropbox entries, and maximize
7925 // compression, by having more fewer, larger records.
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007926 private void logStrictModeViolationToDropBox(
7927 ProcessRecord process,
7928 StrictMode.ViolationInfo info) {
7929 if (info == null) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007930 return;
7931 }
7932 final boolean isSystemApp = process == null ||
7933 (process.info.flags & (ApplicationInfo.FLAG_SYSTEM |
7934 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
Jeff Sharkeya353d262011-10-28 11:12:06 -07007935 final String processName = process == null ? "unknown" : process.processName;
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007936 final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode";
7937 final DropBoxManager dbox = (DropBoxManager)
7938 mContext.getSystemService(Context.DROPBOX_SERVICE);
7939
7940 // Exit early if the dropbox isn't configured to accept this report type.
7941 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
7942
7943 boolean bufferWasEmpty;
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07007944 boolean needsFlush;
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007945 final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024);
7946 synchronized (sb) {
7947 bufferWasEmpty = sb.length() == 0;
Jeff Sharkeya353d262011-10-28 11:12:06 -07007948 appendDropBoxProcessHeaders(process, processName, sb);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007949 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
7950 sb.append("System-App: ").append(isSystemApp).append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007951 sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n");
7952 if (info.violationNumThisLoop != 0) {
7953 sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n");
7954 }
Brad Fitzpatrick599ca292010-10-22 14:47:03 -07007955 if (info.numAnimationsRunning != 0) {
7956 sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n");
7957 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07007958 if (info.broadcastIntentAction != null) {
7959 sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n");
7960 }
Brad Fitzpatricke7520d82010-11-10 18:08:36 -08007961 if (info.durationMillis != -1) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007962 sb.append("Duration-Millis: ").append(info.durationMillis).append("\n");
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007963 }
Brad Fitzpatrickbfbe5772011-01-19 00:10:58 -08007964 if (info.numInstances != -1) {
7965 sb.append("Instance-Count: ").append(info.numInstances).append("\n");
7966 }
Brad Fitzpatricke7520d82010-11-10 18:08:36 -08007967 if (info.tags != null) {
7968 for (String tag : info.tags) {
7969 sb.append("Span-Tag: ").append(tag).append("\n");
7970 }
7971 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007972 sb.append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007973 if (info.crashInfo != null && info.crashInfo.stackTrace != null) {
7974 sb.append(info.crashInfo.stackTrace);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007975 }
7976 sb.append("\n");
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07007977
7978 // Only buffer up to ~64k. Various logging bits truncate
7979 // things at 128k.
7980 needsFlush = (sb.length() > 64 * 1024);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007981 }
7982
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07007983 // Flush immediately if the buffer's grown too large, or this
7984 // is a non-system app. Non-system apps are isolated with a
7985 // different tag & policy and not batched.
7986 //
7987 // Batching is useful during internal testing with
7988 // StrictMode settings turned up high. Without batching,
7989 // thousands of separate files could be created on boot.
7990 if (!isSystemApp || needsFlush) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007991 new Thread("Error dump: " + dropboxTag) {
7992 @Override
7993 public void run() {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07007994 String report;
7995 synchronized (sb) {
7996 report = sb.toString();
7997 sb.delete(0, sb.length());
7998 sb.trimToSize();
7999 }
8000 if (report.length() != 0) {
8001 dbox.addText(dropboxTag, report);
8002 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008003 }
8004 }.start();
8005 return;
8006 }
8007
8008 // System app batching:
8009 if (!bufferWasEmpty) {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008010 // An existing dropbox-writing thread is outstanding, so
8011 // we don't need to start it up. The existing thread will
8012 // catch the buffer appends we just did.
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008013 return;
8014 }
8015
8016 // Worker thread to both batch writes and to avoid blocking the caller on I/O.
8017 // (After this point, we shouldn't access AMS internal data structures.)
8018 new Thread("Error dump: " + dropboxTag) {
8019 @Override
8020 public void run() {
8021 // 5 second sleep to let stacks arrive and be batched together
8022 try {
8023 Thread.sleep(5000); // 5 seconds
8024 } catch (InterruptedException e) {}
8025
8026 String errorReport;
8027 synchronized (mStrictModeBuffer) {
8028 errorReport = mStrictModeBuffer.toString();
8029 if (errorReport.length() == 0) {
8030 return;
8031 }
8032 mStrictModeBuffer.delete(0, mStrictModeBuffer.length());
8033 mStrictModeBuffer.trimToSize();
8034 }
8035 dbox.addText(dropboxTag, errorReport);
8036 }
8037 }.start();
8038 }
8039
Dan Egnor60d87622009-12-16 16:32:58 -08008040 /**
8041 * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors.
8042 * @param app object of the crashing app, null for the system server
8043 * @param tag reported by the caller
8044 * @param crashInfo describing the context of the error
8045 * @return true if the process should exit immediately (WTF is fatal)
8046 */
8047 public boolean handleApplicationWtf(IBinder app, String tag,
Dan Egnorb7f03672009-12-09 16:22:32 -08008048 ApplicationErrorReport.CrashInfo crashInfo) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08008049 ProcessRecord r = findAppProcess(app, "WTF");
Jeff Sharkeya353d262011-10-28 11:12:06 -07008050 final String processName = app == null ? "system_server"
8051 : (r == null ? "unknown" : r.processName);
Dan Egnor60d87622009-12-16 16:32:58 -08008052
8053 EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(),
Jeff Sharkeya353d262011-10-28 11:12:06 -07008054 processName,
Dan Egnor2780e732010-01-22 14:47:35 -08008055 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08008056 tag, crashInfo.exceptionMessage);
8057
Jeff Sharkeya353d262011-10-28 11:12:06 -07008058 addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08008059
Dianne Hackborn1ab43772011-03-15 14:38:02 -07008060 if (r != null && r.pid != Process.myPid() &&
8061 Settings.Secure.getInt(mContext.getContentResolver(),
8062 Settings.Secure.WTF_IS_FATAL, 0) != 0) {
Dan Egnor60d87622009-12-16 16:32:58 -08008063 crashApplication(r, crashInfo);
8064 return true;
8065 } else {
8066 return false;
8067 }
8068 }
8069
8070 /**
8071 * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit})
8072 * @return the corresponding {@link ProcessRecord} object, or null if none could be found
8073 */
Dianne Hackborncb44d962011-03-10 17:02:27 -08008074 private ProcessRecord findAppProcess(IBinder app, String reason) {
Dan Egnor60d87622009-12-16 16:32:58 -08008075 if (app == null) {
8076 return null;
8077 }
8078
8079 synchronized (this) {
8080 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
8081 final int NA = apps.size();
8082 for (int ia=0; ia<NA; ia++) {
8083 ProcessRecord p = apps.valueAt(ia);
8084 if (p.thread != null && p.thread.asBinder() == app) {
8085 return p;
8086 }
8087 }
8088 }
8089
Dianne Hackborncb44d962011-03-10 17:02:27 -08008090 Slog.w(TAG, "Can't find mystery application for " + reason
8091 + " from pid=" + Binder.getCallingPid()
8092 + " uid=" + Binder.getCallingUid() + ": " + app);
Dan Egnor60d87622009-12-16 16:32:58 -08008093 return null;
8094 }
8095 }
8096
8097 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008098 * Utility function for addErrorToDropBox and handleStrictModeViolation's logging
8099 * to append various headers to the dropbox log text.
Dan Egnor60d87622009-12-16 16:32:58 -08008100 */
Jeff Sharkeya353d262011-10-28 11:12:06 -07008101 private void appendDropBoxProcessHeaders(ProcessRecord process, String processName,
8102 StringBuilder sb) {
Vairavan Srinivasan68a4e0a2011-02-14 20:45:59 -08008103 // Watchdog thread ends up invoking this function (with
8104 // a null ProcessRecord) to add the stack file to dropbox.
8105 // Do not acquire a lock on this (am) in such cases, as it
8106 // could cause a potential deadlock, if and when watchdog
8107 // is invoked due to unavailability of lock on am and it
8108 // would prevent watchdog from killing system_server.
8109 if (process == null) {
Jeff Sharkeya353d262011-10-28 11:12:06 -07008110 sb.append("Process: ").append(processName).append("\n");
Vairavan Srinivasan68a4e0a2011-02-14 20:45:59 -08008111 return;
8112 }
Brad Fitzpatrick1e02d362010-09-10 09:19:50 -07008113 // Note: ProcessRecord 'process' is guarded by the service
8114 // instance. (notably process.pkgList, which could otherwise change
8115 // concurrently during execution of this method)
8116 synchronized (this) {
Jeff Sharkeya353d262011-10-28 11:12:06 -07008117 sb.append("Process: ").append(processName).append("\n");
Dan Egnora455d192010-03-12 08:52:28 -08008118 int flags = process.info.flags;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008119 IPackageManager pm = AppGlobals.getPackageManager();
Dan Egnora455d192010-03-12 08:52:28 -08008120 sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
8121 for (String pkg : process.pkgList) {
8122 sb.append("Package: ").append(pkg);
Dan Egnor42471dd2010-01-07 17:25:22 -08008123 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07008124 PackageInfo pi = pm.getPackageInfo(pkg, 0, 0);
Dan Egnora455d192010-03-12 08:52:28 -08008125 if (pi != null) {
8126 sb.append(" v").append(pi.versionCode);
8127 if (pi.versionName != null) {
8128 sb.append(" (").append(pi.versionName).append(")");
8129 }
8130 }
8131 } catch (RemoteException e) {
8132 Slog.e(TAG, "Error getting package info: " + pkg, e);
Dan Egnor60d87622009-12-16 16:32:58 -08008133 }
Dan Egnora455d192010-03-12 08:52:28 -08008134 sb.append("\n");
Dan Egnor60d87622009-12-16 16:32:58 -08008135 }
Dan Egnora455d192010-03-12 08:52:28 -08008136 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008137 }
8138
8139 private static String processClass(ProcessRecord process) {
8140 if (process == null || process.pid == MY_PID) {
8141 return "system_server";
8142 } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
8143 return "system_app";
8144 } else {
8145 return "data_app";
8146 }
8147 }
8148
8149 /**
8150 * Write a description of an error (crash, WTF, ANR) to the drop box.
8151 * @param eventType to include in the drop box tag ("crash", "wtf", etc.)
8152 * @param process which caused the error, null means the system server
8153 * @param activity which triggered the error, null if unknown
8154 * @param parent activity related to the error, null if unknown
8155 * @param subject line related to the error, null if absent
8156 * @param report in long form describing the error, null if absent
8157 * @param logFile to include in the report, null if none
8158 * @param crashInfo giving an application stack trace, null if absent
8159 */
8160 public void addErrorToDropBox(String eventType,
Jeff Sharkeya353d262011-10-28 11:12:06 -07008161 ProcessRecord process, String processName, ActivityRecord activity,
8162 ActivityRecord parent, String subject,
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008163 final String report, final File logFile,
8164 final ApplicationErrorReport.CrashInfo crashInfo) {
8165 // NOTE -- this must never acquire the ActivityManagerService lock,
8166 // otherwise the watchdog may be prevented from resetting the system.
8167
8168 final String dropboxTag = processClass(process) + "_" + eventType;
8169 final DropBoxManager dbox = (DropBoxManager)
8170 mContext.getSystemService(Context.DROPBOX_SERVICE);
8171
8172 // Exit early if the dropbox isn't configured to accept this report type.
8173 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
8174
8175 final StringBuilder sb = new StringBuilder(1024);
Jeff Sharkeya353d262011-10-28 11:12:06 -07008176 appendDropBoxProcessHeaders(process, processName, sb);
Dan Egnora455d192010-03-12 08:52:28 -08008177 if (activity != null) {
8178 sb.append("Activity: ").append(activity.shortComponentName).append("\n");
8179 }
8180 if (parent != null && parent.app != null && parent.app.pid != process.pid) {
8181 sb.append("Parent-Process: ").append(parent.app.processName).append("\n");
8182 }
8183 if (parent != null && parent != activity) {
8184 sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n");
8185 }
8186 if (subject != null) {
8187 sb.append("Subject: ").append(subject).append("\n");
8188 }
8189 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
Christian Lindeberg03d2ca62010-09-28 14:52:20 +02008190 if (Debug.isDebuggerConnected()) {
8191 sb.append("Debugger: Connected\n");
8192 }
Dan Egnora455d192010-03-12 08:52:28 -08008193 sb.append("\n");
8194
8195 // Do the rest in a worker thread to avoid blocking the caller on I/O
8196 // (After this point, we shouldn't access AMS internal data structures.)
8197 Thread worker = new Thread("Error dump: " + dropboxTag) {
8198 @Override
8199 public void run() {
8200 if (report != null) {
8201 sb.append(report);
8202 }
8203 if (logFile != null) {
8204 try {
8205 sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]"));
8206 } catch (IOException e) {
8207 Slog.e(TAG, "Error reading " + logFile, e);
8208 }
8209 }
8210 if (crashInfo != null && crashInfo.stackTrace != null) {
8211 sb.append(crashInfo.stackTrace);
8212 }
8213
8214 String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag;
8215 int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0);
8216 if (lines > 0) {
8217 sb.append("\n");
8218
8219 // Merge several logcat streams, and take the last N lines
8220 InputStreamReader input = null;
8221 try {
8222 java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat",
8223 "-v", "time", "-b", "events", "-b", "system", "-b", "main",
8224 "-t", String.valueOf(lines)).redirectErrorStream(true).start();
8225
8226 try { logcat.getOutputStream().close(); } catch (IOException e) {}
8227 try { logcat.getErrorStream().close(); } catch (IOException e) {}
8228 input = new InputStreamReader(logcat.getInputStream());
8229
8230 int num;
8231 char[] buf = new char[8192];
8232 while ((num = input.read(buf)) > 0) sb.append(buf, 0, num);
8233 } catch (IOException e) {
8234 Slog.e(TAG, "Error running logcat", e);
8235 } finally {
8236 if (input != null) try { input.close(); } catch (IOException e) {}
8237 }
8238 }
8239
8240 dbox.addText(dropboxTag, sb.toString());
Dan Egnor60d87622009-12-16 16:32:58 -08008241 }
Dan Egnora455d192010-03-12 08:52:28 -08008242 };
8243
Dianne Hackborn56385cc2012-04-30 15:07:47 -07008244 if (process == null) {
8245 // If process is null, we are being called from some internal code
8246 // and may be about to die -- run this synchronously.
8247 worker.run();
Dan Egnora455d192010-03-12 08:52:28 -08008248 } else {
8249 worker.start();
Dan Egnor60d87622009-12-16 16:32:58 -08008250 }
8251 }
8252
8253 /**
8254 * Bring up the "unexpected error" dialog box for a crashing app.
8255 * Deal with edge cases (intercepts from instrumented applications,
8256 * ActivityController, error intent receivers, that sort of thing).
8257 * @param r the application crashing
8258 * @param crashInfo describing the failure
8259 */
8260 private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnorb7f03672009-12-09 16:22:32 -08008261 long timeMillis = System.currentTimeMillis();
8262 String shortMsg = crashInfo.exceptionClassName;
8263 String longMsg = crashInfo.exceptionMessage;
8264 String stackTrace = crashInfo.stackTrace;
8265 if (shortMsg != null && longMsg != null) {
8266 longMsg = shortMsg + ": " + longMsg;
8267 } else if (shortMsg != null) {
8268 longMsg = shortMsg;
8269 }
8270
Dan Egnor60d87622009-12-16 16:32:58 -08008271 AppErrorResult result = new AppErrorResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008273 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 try {
8275 String name = r != null ? r.processName : null;
8276 int pid = r != null ? r.pid : Binder.getCallingPid();
Dan Egnor60d87622009-12-16 16:32:58 -08008277 if (!mController.appCrashed(name, pid,
Dan Egnorb7f03672009-12-09 16:22:32 -08008278 shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008279 Slog.w(TAG, "Force-killing crashed app " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 + " at watcher's request");
8281 Process.killProcess(pid);
Dan Egnorb7f03672009-12-09 16:22:32 -08008282 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008283 }
8284 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008285 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008286 }
8287 }
8288
8289 final long origId = Binder.clearCallingIdentity();
8290
8291 // If this process is running instrumentation, finish it.
8292 if (r != null && r.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008293 Slog.w(TAG, "Error in app " + r.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 + " running instrumentation " + r.instrumentationClass + ":");
Joe Onorato8a9b2202010-02-26 18:56:32 -08008295 if (shortMsg != null) Slog.w(TAG, " " + shortMsg);
8296 if (longMsg != null) Slog.w(TAG, " " + longMsg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 Bundle info = new Bundle();
8298 info.putString("shortMsg", shortMsg);
8299 info.putString("longMsg", longMsg);
8300 finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info);
8301 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08008302 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008303 }
8304
Dan Egnor60d87622009-12-16 16:32:58 -08008305 // If we can't identify the process or it's already exceeded its crash quota,
8306 // quit right away without showing a crash dialog.
8307 if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08008309 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008310 }
8311
8312 Message msg = Message.obtain();
8313 msg.what = SHOW_ERROR_MSG;
8314 HashMap data = new HashMap();
8315 data.put("result", result);
8316 data.put("app", r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 msg.obj = data;
8318 mHandler.sendMessage(msg);
8319
8320 Binder.restoreCallingIdentity(origId);
8321 }
8322
8323 int res = result.get();
8324
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008325 Intent appErrorIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008326 synchronized (this) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08008327 if (r != null && !r.isolated) {
8328 // XXX Can't keep track of crash time for isolated processes,
8329 // since they don't have a persistent identity.
8330 mProcessCrashTimes.put(r.info.processName, r.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 SystemClock.uptimeMillis());
8332 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008333 if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
Dan Egnorb7f03672009-12-09 16:22:32 -08008334 appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008335 }
8336 }
8337
8338 if (appErrorIntent != null) {
8339 try {
8340 mContext.startActivity(appErrorIntent);
8341 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008342 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 }
Dan Egnorb7f03672009-12-09 16:22:32 -08008346
8347 Intent createAppErrorIntentLocked(ProcessRecord r,
8348 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
8349 ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008350 if (report == null) {
8351 return null;
8352 }
8353 Intent result = new Intent(Intent.ACTION_APP_ERROR);
8354 result.setComponent(r.errorReportReceiver);
8355 result.putExtra(Intent.EXTRA_BUG_REPORT, report);
8356 result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
8357 return result;
8358 }
8359
Dan Egnorb7f03672009-12-09 16:22:32 -08008360 private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
8361 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008362 if (r.errorReportReceiver == null) {
8363 return null;
8364 }
8365
8366 if (!r.crashing && !r.notResponding) {
8367 return null;
8368 }
8369
Dan Egnorb7f03672009-12-09 16:22:32 -08008370 ApplicationErrorReport report = new ApplicationErrorReport();
8371 report.packageName = r.info.packageName;
8372 report.installerPackageName = r.errorReportReceiver.getPackageName();
8373 report.processName = r.processName;
8374 report.time = timeMillis;
Jacek Surazskie0ee6ef2010-01-07 16:23:03 +01008375 report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008376
Dan Egnorb7f03672009-12-09 16:22:32 -08008377 if (r.crashing) {
8378 report.type = ApplicationErrorReport.TYPE_CRASH;
8379 report.crashInfo = crashInfo;
8380 } else if (r.notResponding) {
8381 report.type = ApplicationErrorReport.TYPE_ANR;
8382 report.anrInfo = new ApplicationErrorReport.AnrInfo();
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008383
Dan Egnorb7f03672009-12-09 16:22:32 -08008384 report.anrInfo.activity = r.notRespondingReport.tag;
8385 report.anrInfo.cause = r.notRespondingReport.shortMsg;
8386 report.anrInfo.info = r.notRespondingReport.longMsg;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008387 }
8388
Dan Egnorb7f03672009-12-09 16:22:32 -08008389 return report;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008390 }
8391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008393 enforceNotIsolatedCaller("getProcessesInErrorState");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 // assume our apps are happy - lazy create the list
8395 List<ActivityManager.ProcessErrorStateInfo> errList = null;
8396
Dianne Hackborn0c380492012-08-20 17:23:30 -07008397 final boolean allUsers = ActivityManager.checkUidPermission(
8398 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
8399 Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED;
8400 int userId = UserHandle.getUserId(Binder.getCallingUid());
8401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008402 synchronized (this) {
8403
8404 // iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008405 for (int i=mLruProcesses.size()-1; i>=0; i--) {
8406 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn0c380492012-08-20 17:23:30 -07008407 if (!allUsers && app.userId != userId) {
8408 continue;
8409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008410 if ((app.thread != null) && (app.crashing || app.notResponding)) {
8411 // This one's in trouble, so we'll generate a report for it
8412 // crashes are higher priority (in case there's a crash *and* an anr)
8413 ActivityManager.ProcessErrorStateInfo report = null;
8414 if (app.crashing) {
8415 report = app.crashingReport;
8416 } else if (app.notResponding) {
8417 report = app.notRespondingReport;
8418 }
8419
8420 if (report != null) {
8421 if (errList == null) {
8422 errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1);
8423 }
8424 errList.add(report);
8425 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008426 Slog.w(TAG, "Missing app error report, app = " + app.processName +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 " crashing = " + app.crashing +
8428 " notResponding = " + app.notResponding);
8429 }
8430 }
8431 }
8432 }
8433
8434 return errList;
8435 }
Dianne Hackborn905577f2011-09-07 18:31:28 -07008436
8437 static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) {
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008438 if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn905577f2011-09-07 18:31:28 -07008439 if (currApp != null) {
8440 currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1;
8441 }
8442 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008443 } else if (adj >= ProcessList.SERVICE_B_ADJ) {
8444 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
Dianne Hackborn905577f2011-09-07 18:31:28 -07008445 } else if (adj >= ProcessList.HOME_APP_ADJ) {
8446 if (currApp != null) {
8447 currApp.lru = 0;
8448 }
8449 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008450 } else if (adj >= ProcessList.SERVICE_ADJ) {
Dianne Hackborn905577f2011-09-07 18:31:28 -07008451 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
8452 } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
8453 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
8454 } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
8455 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
8456 } else if (adj >= ProcessList.VISIBLE_APP_ADJ) {
8457 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
8458 } else {
8459 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
8460 }
8461 }
8462
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008463 private void fillInProcMemInfo(ProcessRecord app,
8464 ActivityManager.RunningAppProcessInfo outInfo) {
8465 outInfo.pid = app.pid;
8466 outInfo.uid = app.info.uid;
8467 if (mHeavyWeightProcess == app) {
8468 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE;
8469 }
8470 if (app.persistent) {
8471 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT;
8472 }
Dianne Hackborn0c380492012-08-20 17:23:30 -07008473 if (app.hasActivities) {
8474 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES;
8475 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008476 outInfo.lastTrimLevel = app.trimMemoryLevel;
8477 int adj = app.curAdj;
8478 outInfo.importance = oomAdjToImportance(adj, outInfo);
8479 outInfo.importanceReasonCode = app.adjTypeCode;
8480 }
8481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008482 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008483 enforceNotIsolatedCaller("getRunningAppProcesses");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 // Lazy instantiation of list
8485 List<ActivityManager.RunningAppProcessInfo> runList = null;
Dianne Hackborn0c380492012-08-20 17:23:30 -07008486 final boolean allUsers = ActivityManager.checkUidPermission(
8487 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
8488 Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED;
8489 int userId = UserHandle.getUserId(Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 synchronized (this) {
8491 // Iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008492 for (int i=mLruProcesses.size()-1; i>=0; i--) {
8493 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn0c380492012-08-20 17:23:30 -07008494 if (!allUsers && app.userId != userId) {
8495 continue;
8496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008497 if ((app.thread != null) && (!app.crashing && !app.notResponding)) {
8498 // Generate process state info for running application
8499 ActivityManager.RunningAppProcessInfo currApp =
8500 new ActivityManager.RunningAppProcessInfo(app.processName,
8501 app.pid, app.getPackageList());
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008502 fillInProcMemInfo(app, currApp);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008503 if (app.adjSource instanceof ProcessRecord) {
8504 currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid;
Dianne Hackborn905577f2011-09-07 18:31:28 -07008505 currApp.importanceReasonImportance = oomAdjToImportance(
8506 app.adjSourceOom, null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008507 } else if (app.adjSource instanceof ActivityRecord) {
8508 ActivityRecord r = (ActivityRecord)app.adjSource;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008509 if (r.app != null) currApp.importanceReasonPid = r.app.pid;
8510 }
8511 if (app.adjTarget instanceof ComponentName) {
8512 currApp.importanceReasonComponent = (ComponentName)app.adjTarget;
8513 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008514 //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 // + " lru=" + currApp.lru);
8516 if (runList == null) {
8517 runList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
8518 }
8519 runList.add(currApp);
8520 }
8521 }
8522 }
8523 return runList;
8524 }
8525
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008526 public List<ApplicationInfo> getRunningExternalApplications() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008527 enforceNotIsolatedCaller("getRunningExternalApplications");
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008528 List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses();
8529 List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>();
8530 if (runningApps != null && runningApps.size() > 0) {
8531 Set<String> extList = new HashSet<String>();
8532 for (ActivityManager.RunningAppProcessInfo app : runningApps) {
8533 if (app.pkgList != null) {
8534 for (String pkg : app.pkgList) {
8535 extList.add(pkg);
8536 }
8537 }
8538 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008539 IPackageManager pm = AppGlobals.getPackageManager();
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008540 for (String pkg : extList) {
8541 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07008542 ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId());
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008543 if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
8544 retList.add(info);
8545 }
8546 } catch (RemoteException e) {
8547 }
8548 }
8549 }
8550 return retList;
8551 }
8552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 @Override
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008554 public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) {
8555 enforceNotIsolatedCaller("getMyMemoryState");
8556 synchronized (this) {
8557 ProcessRecord proc;
8558 synchronized (mPidsSelfLocked) {
8559 proc = mPidsSelfLocked.get(Binder.getCallingPid());
8560 }
8561 fillInProcMemInfo(proc, outInfo);
8562 }
8563 }
8564
8565 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008567 if (checkCallingPermission(android.Manifest.permission.DUMP)
8568 != PackageManager.PERMISSION_GRANTED) {
8569 pw.println("Permission Denial: can't dump ActivityManager from from pid="
8570 + Binder.getCallingPid()
8571 + ", uid=" + Binder.getCallingUid()
8572 + " without permission "
8573 + android.Manifest.permission.DUMP);
8574 return;
8575 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008576
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008577 boolean dumpAll = false;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008578 boolean dumpClient = false;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008579 String dumpPackage = null;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008580
8581 int opti = 0;
8582 while (opti < args.length) {
8583 String opt = args[opti];
8584 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
8585 break;
8586 }
8587 opti++;
8588 if ("-a".equals(opt)) {
8589 dumpAll = true;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008590 } else if ("-c".equals(opt)) {
8591 dumpClient = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008592 } else if ("-h".equals(opt)) {
8593 pw.println("Activity manager dump options:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008594 pw.println(" [-a] [-c] [-h] [cmd] ...");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008595 pw.println(" cmd may be one of:");
Dianne Hackborn287952c2010-09-22 22:34:31 -07008596 pw.println(" a[ctivities]: activity stack state");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008597 pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state");
8598 pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state");
8599 pw.println(" p[rocesses] [PACKAGE_NAME]: process state");
Dianne Hackborn287952c2010-09-22 22:34:31 -07008600 pw.println(" o[om]: out of memory management");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008601 pw.println(" prov[iders] [COMP_SPEC ...]: content provider state");
Marco Nelissen18cb2872011-11-15 11:19:53 -08008602 pw.println(" provider [COMP_SPEC]: provider client-side state");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008603 pw.println(" s[ervices] [COMP_SPEC ...]: service state");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008604 pw.println(" service [COMP_SPEC]: service client-side state");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008605 pw.println(" package [PACKAGE_NAME]: all state related to given package");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008606 pw.println(" all: dump all activities");
8607 pw.println(" top: dump the top activity");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008608 pw.println(" cmd may also be a COMP_SPEC to dump activities.");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008609 pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),");
8610 pw.println(" a partial substring in a component name, a");
8611 pw.println(" hex object identifier.");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008612 pw.println(" -a: include all available server state.");
8613 pw.println(" -c: include client state.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 return;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008615 } else {
8616 pw.println("Unknown argument: " + opt + "; use -h for help");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008617 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008618 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008619
8620 long origId = Binder.clearCallingIdentity();
8621 boolean more = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008622 // Is the caller requesting to dump a particular piece of data?
8623 if (opti < args.length) {
8624 String cmd = args[opti];
8625 opti++;
8626 if ("activities".equals(cmd) || "a".equals(cmd)) {
8627 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008628 dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008629 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008630 } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008631 String[] newArgs;
8632 String name;
8633 if (opti >= args.length) {
8634 name = null;
8635 newArgs = EMPTY_STRING_ARRAY;
8636 } else {
8637 name = args[opti];
8638 opti++;
8639 newArgs = new String[args.length - opti];
8640 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8641 args.length - opti);
8642 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008643 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008644 dumpBroadcastsLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008645 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008646 } else if ("intents".equals(cmd) || "i".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008647 String[] newArgs;
8648 String name;
8649 if (opti >= args.length) {
8650 name = null;
8651 newArgs = EMPTY_STRING_ARRAY;
8652 } else {
8653 name = args[opti];
8654 opti++;
8655 newArgs = new String[args.length - opti];
8656 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8657 args.length - opti);
8658 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008659 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008660 dumpPendingIntentsLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008661 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008662 } else if ("processes".equals(cmd) || "p".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008663 String[] newArgs;
8664 String name;
8665 if (opti >= args.length) {
8666 name = null;
8667 newArgs = EMPTY_STRING_ARRAY;
8668 } else {
8669 name = args[opti];
8670 opti++;
8671 newArgs = new String[args.length - opti];
8672 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8673 args.length - opti);
8674 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008675 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008676 dumpProcessesLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008677 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07008678 } else if ("oom".equals(cmd) || "o".equals(cmd)) {
8679 synchronized (this) {
8680 dumpOomLocked(fd, pw, args, opti, true);
8681 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08008682 } else if ("provider".equals(cmd)) {
8683 String[] newArgs;
8684 String name;
8685 if (opti >= args.length) {
8686 name = null;
8687 newArgs = EMPTY_STRING_ARRAY;
8688 } else {
8689 name = args[opti];
8690 opti++;
8691 newArgs = new String[args.length - opti];
8692 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
8693 }
8694 if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) {
8695 pw.println("No providers match: " + name);
8696 pw.println("Use -h for help.");
8697 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008698 } else if ("providers".equals(cmd) || "prov".equals(cmd)) {
8699 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008700 dumpProvidersLocked(fd, pw, args, opti, true, null);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008701 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008702 } else if ("service".equals(cmd)) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008703 String[] newArgs;
8704 String name;
8705 if (opti >= args.length) {
8706 name = null;
8707 newArgs = EMPTY_STRING_ARRAY;
8708 } else {
8709 name = args[opti];
8710 opti++;
8711 newArgs = new String[args.length - opti];
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008712 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8713 args.length - opti);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008714 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07008715 if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008716 pw.println("No services match: " + name);
8717 pw.println("Use -h for help.");
8718 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008719 } else if ("package".equals(cmd)) {
8720 String[] newArgs;
8721 if (opti >= args.length) {
8722 pw.println("package: no package name specified");
8723 pw.println("Use -h for help.");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008724 } else {
8725 dumpPackage = args[opti];
8726 opti++;
8727 newArgs = new String[args.length - opti];
8728 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8729 args.length - opti);
8730 args = newArgs;
8731 opti = 0;
Amith Yamasani7463ada2012-04-11 15:02:39 -07008732 more = true;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008733 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008734 } else if ("services".equals(cmd) || "s".equals(cmd)) {
8735 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -07008736 mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008737 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07008738 } else {
8739 // Dumping a single activity?
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008740 if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) {
8741 pw.println("Bad activity command, or no activities match: " + cmd);
8742 pw.println("Use -h for help.");
Dianne Hackborn625ac272010-09-17 18:29:22 -07008743 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008744 }
8745 if (!more) {
8746 Binder.restoreCallingIdentity(origId);
Dianne Hackborn30d71892010-12-11 10:37:55 -08008747 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008749 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008750
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008751 // No piece of data specified, dump everything.
8752 synchronized (this) {
8753 boolean needSep;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008754 needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008755 if (needSep) {
8756 pw.println(" ");
8757 }
8758 if (dumpAll) {
8759 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008760 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008761 needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008762 if (needSep) {
8763 pw.println(" ");
8764 }
8765 if (dumpAll) {
8766 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008767 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008768 needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008769 if (needSep) {
8770 pw.println(" ");
8771 }
8772 if (dumpAll) {
8773 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008774 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07008775 needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008776 if (needSep) {
8777 pw.println(" ");
8778 }
8779 if (dumpAll) {
8780 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008781 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008782 needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008783 if (needSep) {
8784 pw.println(" ");
8785 }
8786 if (dumpAll) {
8787 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008788 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008789 dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008790 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008791 Binder.restoreCallingIdentity(origId);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008792 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008793
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008794 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008795 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008796 pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)");
8797 pw.println(" Main stack:");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008798 dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient,
8799 dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008800 pw.println(" ");
8801 pw.println(" Running activities (most recent first):");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008802 dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false,
8803 dumpPackage);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008804 if (mMainStack.mWaitingVisibleActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008805 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008806 pw.println(" Activities waiting for another to become visible:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008807 dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008808 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008809 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008810 if (mMainStack.mStoppingActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008812 pw.println(" Activities waiting to stop:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008813 dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008814 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008815 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08008816 if (mMainStack.mGoingToSleepActivities.size() > 0) {
8817 pw.println(" ");
8818 pw.println(" Activities waiting to sleep:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008819 dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008820 !dumpAll, false, dumpPackage);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08008821 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008822 if (mMainStack.mFinishingActivities.size() > 0) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008823 pw.println(" ");
8824 pw.println(" Activities waiting to finish:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008825 dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008826 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008829 pw.println(" ");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08008830 if (mMainStack.mPausingActivity != null) {
8831 pw.println(" mPausingActivity: " + mMainStack.mPausingActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008832 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008833 pw.println(" mResumedActivity: " + mMainStack.mResumedActivity);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008834 pw.println(" mFocusedActivity: " + mFocusedActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008835 if (dumpAll) {
8836 pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity);
8837 pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout);
Dianne Hackborn90c52de2011-09-23 12:57:44 -07008838 pw.println(" mDismissKeyguardOnNextActivity: "
8839 + mMainStack.mDismissKeyguardOnNextActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008841
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008842 if (mRecentTasks.size() > 0) {
8843 pw.println();
8844 pw.println(" Recent tasks:");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008845
8846 final int N = mRecentTasks.size();
8847 for (int i=0; i<N; i++) {
8848 TaskRecord tr = mRecentTasks.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008849 if (dumpPackage != null) {
8850 if (tr.realActivity == null ||
8851 !dumpPackage.equals(tr.realActivity)) {
8852 continue;
8853 }
8854 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008855 pw.print(" * Recent #"); pw.print(i); pw.print(": ");
8856 pw.println(tr);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008857 if (dumpAll) {
8858 mRecentTasks.get(i).dump(pw, " ");
8859 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008860 }
8861 }
8862
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008863 if (dumpAll) {
8864 pw.println(" ");
8865 pw.println(" mCurTask: " + mCurTask);
8866 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008867
8868 return true;
8869 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07008870
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008871 boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008872 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008873 boolean needSep = false;
8874 int numPers = 0;
8875
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008876 pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)");
8877
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008878 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008879 for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) {
8880 final int NA = procs.size();
8881 for (int ia=0; ia<NA; ia++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008882 ProcessRecord r = procs.valueAt(ia);
8883 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
8884 continue;
8885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008886 if (!needSep) {
8887 pw.println(" All known processes:");
8888 needSep = true;
8889 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008890 pw.print(r.persistent ? " *PERS*" : " *APP*");
8891 pw.print(" UID "); pw.print(procs.keyAt(ia));
8892 pw.print(" "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008893 r.dump(pw, " ");
8894 if (r.persistent) {
8895 numPers++;
8896 }
8897 }
8898 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008899 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08008900
8901 if (mIsolatedProcesses.size() > 0) {
8902 if (needSep) pw.println(" ");
8903 needSep = true;
8904 pw.println(" Isolated process list (sorted by uid):");
8905 for (int i=0; i<mIsolatedProcesses.size(); i++) {
8906 ProcessRecord r = mIsolatedProcesses.valueAt(i);
8907 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
8908 continue;
8909 }
8910 pw.println(String.format("%sIsolated #%2d: %s",
8911 " ", i, r.toString()));
8912 }
8913 }
8914
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008915 if (mLruProcesses.size() > 0) {
8916 if (needSep) pw.println(" ");
8917 needSep = true;
Dianne Hackborn905577f2011-09-07 18:31:28 -07008918 pw.println(" Process LRU list (sorted by oom_adj):");
Dianne Hackborn287952c2010-09-22 22:34:31 -07008919 dumpProcessOomList(pw, this, mLruProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008920 "Proc", "PERS", false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008921 needSep = true;
8922 }
8923
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008924 if (dumpAll) {
8925 synchronized (mPidsSelfLocked) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008926 boolean printed = false;
8927 for (int i=0; i<mPidsSelfLocked.size(); i++) {
8928 ProcessRecord r = mPidsSelfLocked.valueAt(i);
8929 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
8930 continue;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008931 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008932 if (!printed) {
8933 if (needSep) pw.println(" ");
8934 needSep = true;
8935 pw.println(" PID mappings:");
8936 printed = true;
8937 }
8938 pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i));
8939 pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008940 }
8941 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008942 }
8943
8944 if (mForegroundProcesses.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008945 synchronized (mPidsSelfLocked) {
8946 boolean printed = false;
8947 for (int i=0; i<mForegroundProcesses.size(); i++) {
8948 ProcessRecord r = mPidsSelfLocked.get(
8949 mForegroundProcesses.valueAt(i).pid);
8950 if (dumpPackage != null && (r == null
8951 || !dumpPackage.equals(r.info.packageName))) {
8952 continue;
8953 }
8954 if (!printed) {
8955 if (needSep) pw.println(" ");
8956 needSep = true;
8957 pw.println(" Foreground Processes:");
8958 printed = true;
8959 }
8960 pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i));
8961 pw.print(": "); pw.println(mForegroundProcesses.valueAt(i));
8962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008964 }
8965
8966 if (mPersistentStartingProcesses.size() > 0) {
8967 if (needSep) pw.println(" ");
8968 needSep = true;
8969 pw.println(" Persisent processes that are starting:");
8970 dumpProcessList(pw, this, mPersistentStartingProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008971 "Starting Norm", "Restarting PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008974 if (mRemovedProcesses.size() > 0) {
8975 if (needSep) pw.println(" ");
8976 needSep = true;
8977 pw.println(" Processes that are being removed:");
8978 dumpProcessList(pw, this, mRemovedProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008979 "Removed Norm", "Removed PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008980 }
8981
8982 if (mProcessesOnHold.size() > 0) {
8983 if (needSep) pw.println(" ");
8984 needSep = true;
8985 pw.println(" Processes that are on old until the system is ready:");
8986 dumpProcessList(pw, this, mProcessesOnHold, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008987 "OnHold Norm", "OnHold PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008990 needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008991
8992 if (mProcessCrashTimes.getMap().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008993 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008994 long now = SystemClock.uptimeMillis();
8995 for (Map.Entry<String, SparseArray<Long>> procs
8996 : mProcessCrashTimes.getMap().entrySet()) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008997 String pname = procs.getKey();
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008998 SparseArray<Long> uids = procs.getValue();
8999 final int N = uids.size();
9000 for (int i=0; i<N; i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009001 int puid = uids.keyAt(i);
9002 ProcessRecord r = mProcessNames.get(pname, puid);
9003 if (dumpPackage != null && (r == null
9004 || !dumpPackage.equals(r.info.packageName))) {
9005 continue;
9006 }
9007 if (!printed) {
9008 if (needSep) pw.println(" ");
9009 needSep = true;
9010 pw.println(" Time since processes crashed:");
9011 printed = true;
9012 }
9013 pw.print(" Process "); pw.print(pname);
9014 pw.print(" uid "); pw.print(puid);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009015 pw.print(": last crashed ");
Dianne Hackborn27ff9132012-03-06 14:57:58 -08009016 TimeUtils.formatDuration(now-uids.valueAt(i), pw);
9017 pw.println(" ago");
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009018 }
9019 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009022 if (mBadProcesses.getMap().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009023 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009024 for (Map.Entry<String, SparseArray<Long>> procs
9025 : mBadProcesses.getMap().entrySet()) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009026 String pname = procs.getKey();
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009027 SparseArray<Long> uids = procs.getValue();
9028 final int N = uids.size();
9029 for (int i=0; i<N; i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009030 int puid = uids.keyAt(i);
9031 ProcessRecord r = mProcessNames.get(pname, puid);
9032 if (dumpPackage != null && (r == null
9033 || !dumpPackage.equals(r.info.packageName))) {
9034 continue;
9035 }
9036 if (!printed) {
9037 if (needSep) pw.println(" ");
9038 needSep = true;
9039 pw.println(" Bad processes:");
9040 }
9041 pw.print(" Bad process "); pw.print(pname);
9042 pw.print(" uid "); pw.print(puid);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009043 pw.print(": crashed at time ");
9044 pw.println(uids.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009045 }
9046 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009048
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009049 pw.println();
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009050 pw.println(" mHomeProcess: " + mHomeProcess);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009051 pw.println(" mPreviousProcess: " + mPreviousProcess);
Dianne Hackborn50685602011-12-01 12:23:37 -08009052 if (dumpAll) {
9053 StringBuilder sb = new StringBuilder(128);
9054 sb.append(" mPreviousProcessVisibleTime: ");
9055 TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb);
9056 pw.println(sb);
9057 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07009058 if (mHeavyWeightProcess != null) {
9059 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
9060 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009061 pw.println(" mConfiguration: " + mConfiguration);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009062 if (dumpAll) {
9063 pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange);
Dianne Hackborn3d0724d2011-05-12 15:39:41 -07009064 if (mCompatModePackages.getPackages().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009065 boolean printed = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07009066 for (Map.Entry<String, Integer> entry
9067 : mCompatModePackages.getPackages().entrySet()) {
9068 String pkg = entry.getKey();
9069 int mode = entry.getValue();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009070 if (dumpPackage != null && !dumpPackage.equals(pkg)) {
9071 continue;
9072 }
9073 if (!printed) {
9074 pw.println(" mScreenCompatPackages:");
9075 printed = true;
9076 }
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07009077 pw.print(" "); pw.print(pkg); pw.print(": ");
9078 pw.print(mode); pw.println();
9079 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -07009080 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009081 }
Dianne Hackbornff5b1582012-04-12 17:24:07 -07009082 if (mSleeping || mWentToSleep || mLockScreenShown) {
9083 pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep
9084 + " mLockScreenShown " + mLockScreenShown);
9085 }
9086 if (mShuttingDown) {
9087 pw.println(" mShuttingDown=" + mShuttingDown);
9088 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009089 if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient
9090 || mOrigWaitForDebugger) {
9091 pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp
9092 + " mDebugTransient=" + mDebugTransient
9093 + " mOrigWaitForDebugger=" + mOrigWaitForDebugger);
9094 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08009095 if (mOpenGlTraceApp != null) {
9096 pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp);
9097 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07009098 if (mProfileApp != null || mProfileProc != null || mProfileFile != null
9099 || mProfileFd != null) {
9100 pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc);
9101 pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd);
9102 pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler="
9103 + mAutoStopProfiler);
9104 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009105 if (mAlwaysFinishActivities || mController != null) {
9106 pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities
9107 + " mController=" + mController);
9108 }
9109 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009110 pw.println(" Total persistent processes: " + numPers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 pw.println(" mStartRunning=" + mStartRunning
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07009112 + " mProcessesReady=" + mProcessesReady
9113 + " mSystemReady=" + mSystemReady);
9114 pw.println(" mBooting=" + mBooting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 + " mBooted=" + mBooted
9116 + " mFactoryTest=" + mFactoryTest);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009117 pw.print(" mLastPowerCheckRealtime=");
9118 TimeUtils.formatDuration(mLastPowerCheckRealtime, pw);
9119 pw.println("");
9120 pw.print(" mLastPowerCheckUptime=");
9121 TimeUtils.formatDuration(mLastPowerCheckUptime, pw);
9122 pw.println("");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009123 pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep);
9124 pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity);
Dianne Hackborn906497c2010-05-10 15:57:38 -07009125 pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq);
Dianne Hackbornee7621c2012-08-13 16:42:18 -07009126 pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs
9127 + " mNumHiddenProcs=" + mNumHiddenProcs
9128 + " mNumServiceProcs=" + mNumServiceProcs
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009129 + " mNewNumServiceProcs=" + mNewNumServiceProcs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009130 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009131
9132 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009133 }
9134
Dianne Hackborn287952c2010-09-22 22:34:31 -07009135 boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009136 int opti, boolean needSep, boolean dumpAll, String dumpPackage) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009137 if (mProcessesToGc.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009138 boolean printed = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009139 long now = SystemClock.uptimeMillis();
9140 for (int i=0; i<mProcessesToGc.size(); i++) {
9141 ProcessRecord proc = mProcessesToGc.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009142 if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) {
9143 continue;
9144 }
9145 if (!printed) {
9146 if (needSep) pw.println(" ");
9147 needSep = true;
9148 pw.println(" Processes that are waiting to GC:");
9149 printed = true;
9150 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07009151 pw.print(" Process "); pw.println(proc);
9152 pw.print(" lowMem="); pw.print(proc.reportLowMemory);
9153 pw.print(", last gced=");
9154 pw.print(now-proc.lastRequestedGc);
9155 pw.print(" ms ago, last lowMem=");
9156 pw.print(now-proc.lastLowMemory);
9157 pw.println(" ms ago");
9158
9159 }
9160 }
9161 return needSep;
9162 }
9163
9164 boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args,
9165 int opti, boolean dumpAll) {
9166 boolean needSep = false;
9167
9168 if (mLruProcesses.size() > 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009169 if (needSep) pw.println(" ");
9170 needSep = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07009171 pw.println(" OOM levels:");
Dianne Hackborn7d608422011-08-07 16:24:18 -07009172 pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009173 pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009174 pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ);
9175 pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ);
9176 pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ);
9177 pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ);
9178 pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009179 pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009180 pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009181 pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009182 pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009183 pw.print(" HIDDEN_APP_MIN_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MIN_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009184 pw.print(" HIDDEN_APP_MAX_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MAX_ADJ);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07009185
9186 if (needSep) pw.println(" ");
9187 needSep = true;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009188 pw.println(" Process OOM control:");
Dianne Hackborn905577f2011-09-07 18:31:28 -07009189 dumpProcessOomList(pw, this, mLruProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009190 "Proc", "PERS", true, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009191 needSep = true;
9192 }
9193
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009194 needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009195
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009196 pw.println();
Dianne Hackborn287952c2010-09-22 22:34:31 -07009197 pw.println(" mHomeProcess: " + mHomeProcess);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009198 pw.println(" mPreviousProcess: " + mPreviousProcess);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009199 if (mHeavyWeightProcess != null) {
9200 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
9201 }
9202
9203 return true;
9204 }
9205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 /**
9207 * There are three ways to call this:
Marco Nelissen18cb2872011-11-15 11:19:53 -08009208 * - no provider specified: dump all the providers
9209 * - a flattened component name that matched an existing provider was specified as the
9210 * first arg: dump that one provider
9211 * - the first arg isn't the flattened component name of an existing provider:
9212 * dump all providers whose component contains the first arg as a substring
9213 */
9214 protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args,
9215 int opti, boolean dumpAll) {
Marco Nelissende7408c2012-02-08 14:57:38 -08009216 return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll);
Marco Nelissen18cb2872011-11-15 11:19:53 -08009217 }
9218
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009219 static class ItemMatcher {
9220 ArrayList<ComponentName> components;
9221 ArrayList<String> strings;
9222 ArrayList<Integer> objects;
9223 boolean all;
9224
9225 ItemMatcher() {
9226 all = true;
9227 }
9228
9229 void build(String name) {
9230 ComponentName componentName = ComponentName.unflattenFromString(name);
9231 if (componentName != null) {
9232 if (components == null) {
9233 components = new ArrayList<ComponentName>();
9234 }
9235 components.add(componentName);
9236 all = false;
9237 } else {
9238 int objectId = 0;
9239 // Not a '/' separated full component name; maybe an object ID?
9240 try {
9241 objectId = Integer.parseInt(name, 16);
9242 if (objects == null) {
9243 objects = new ArrayList<Integer>();
9244 }
9245 objects.add(objectId);
9246 all = false;
9247 } catch (RuntimeException e) {
9248 // Not an integer; just do string match.
9249 if (strings == null) {
9250 strings = new ArrayList<String>();
9251 }
9252 strings.add(name);
9253 all = false;
9254 }
9255 }
9256 }
9257
9258 int build(String[] args, int opti) {
9259 for (; opti<args.length; opti++) {
9260 String name = args[opti];
9261 if ("--".equals(name)) {
9262 return opti+1;
9263 }
9264 build(name);
9265 }
9266 return opti;
9267 }
9268
9269 boolean match(Object object, ComponentName comp) {
9270 if (all) {
9271 return true;
9272 }
9273 if (components != null) {
9274 for (int i=0; i<components.size(); i++) {
9275 if (components.get(i).equals(comp)) {
9276 return true;
9277 }
9278 }
9279 }
9280 if (objects != null) {
9281 for (int i=0; i<objects.size(); i++) {
9282 if (System.identityHashCode(object) == objects.get(i)) {
9283 return true;
9284 }
9285 }
9286 }
9287 if (strings != null) {
9288 String flat = comp.flattenToString();
9289 for (int i=0; i<strings.size(); i++) {
9290 if (flat.contains(strings.get(i))) {
9291 return true;
9292 }
9293 }
9294 }
9295 return false;
9296 }
9297 }
9298
Dianne Hackborn625ac272010-09-17 18:29:22 -07009299 /**
9300 * There are three things that cmd can be:
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009301 * - a flattened component name that matches an existing activity
Dianne Hackborn625ac272010-09-17 18:29:22 -07009302 * - the cmd arg isn't the flattened component name of an existing activity:
9303 * dump all activity whose component contains the cmd as a substring
9304 * - A hex number of the ActivityRecord object instance.
9305 */
9306 protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args,
9307 int opti, boolean dumpAll) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07009308 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009309
9310 if ("all".equals(name)) {
9311 synchronized (this) {
9312 for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07009313 activities.add(r1);
9314 }
9315 }
Dianne Hackbornf9302322011-06-14 18:36:14 -07009316 } else if ("top".equals(name)) {
9317 synchronized (this) {
9318 final int N = mMainStack.mHistory.size();
9319 if (N > 0) {
9320 activities.add((ActivityRecord)mMainStack.mHistory.get(N-1));
9321 }
9322 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009323 } else {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009324 ItemMatcher matcher = new ItemMatcher();
9325 matcher.build(name);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009326
9327 synchronized (this) {
9328 for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009329 if (matcher.match(r1, r1.intent.getComponent())) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009330 activities.add(r1);
9331 }
9332 }
9333 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07009334 }
9335
9336 if (activities.size() <= 0) {
9337 return false;
9338 }
9339
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009340 String[] newArgs = new String[args.length - opti];
9341 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
9342
Dianne Hackborn30d71892010-12-11 10:37:55 -08009343 TaskRecord lastTask = null;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009344 boolean needSep = false;
Dianne Hackborn30d71892010-12-11 10:37:55 -08009345 for (int i=activities.size()-1; i>=0; i--) {
9346 ActivityRecord r = (ActivityRecord)activities.get(i);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009347 if (needSep) {
9348 pw.println();
9349 }
9350 needSep = true;
9351 synchronized (this) {
9352 if (lastTask != r.task) {
9353 lastTask = r.task;
9354 pw.print("TASK "); pw.print(lastTask.affinity);
9355 pw.print(" id="); pw.println(lastTask.taskId);
9356 if (dumpAll) {
9357 lastTask.dump(pw, " ");
9358 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08009359 }
9360 }
9361 dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009362 }
9363 return true;
9364 }
9365
9366 /**
9367 * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if
9368 * there is a thread associated with the activity.
9369 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08009370 private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw,
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009371 final ActivityRecord r, String[] args, boolean dumpAll) {
9372 String innerPrefix = prefix + " ";
Dianne Hackborn30d71892010-12-11 10:37:55 -08009373 synchronized (this) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009374 pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName);
9375 pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r)));
9376 pw.print(" pid=");
Dianne Hackborn30d71892010-12-11 10:37:55 -08009377 if (r.app != null) pw.println(r.app.pid);
9378 else pw.println("(not running)");
9379 if (dumpAll) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009380 r.dump(pw, innerPrefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009381 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07009382 }
9383 if (r.app != null && r.app.thread != null) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009384 // flush anything that is already in the PrintWriter since the thread is going
9385 // to write to the file descriptor directly
9386 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07009387 try {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009388 TransferPipe tp = new TransferPipe();
9389 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08009390 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
9391 r.appToken, innerPrefix, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009392 tp.go(fd);
9393 } finally {
9394 tp.kill();
9395 }
9396 } catch (IOException e) {
9397 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009398 } catch (RemoteException e) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009399 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
Dianne Hackborn625ac272010-09-17 18:29:22 -07009400 }
9401 }
9402 }
9403
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009404 boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009405 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009406 boolean needSep = false;
Dianne Hackborn786b4402012-08-27 15:14:02 -07009407 boolean onlyHistory = false;
9408
9409 if ("history".equals(dumpPackage)) {
9410 onlyHistory = true;
9411 dumpPackage = null;
9412 }
9413
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009414 pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)");
Dianne Hackborn786b4402012-08-27 15:14:02 -07009415 if (!onlyHistory && dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009416 if (mRegisteredReceivers.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009417 boolean printed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009418 Iterator it = mRegisteredReceivers.values().iterator();
9419 while (it.hasNext()) {
9420 ReceiverList r = (ReceiverList)it.next();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009421 if (dumpPackage != null && (r.app == null ||
9422 !dumpPackage.equals(r.app.info.packageName))) {
9423 continue;
9424 }
9425 if (!printed) {
9426 pw.println(" Registered Receivers:");
9427 needSep = true;
9428 printed = true;
9429 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009430 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 r.dump(pw, " ");
9432 }
9433 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009434
9435 if (mReceiverResolver.dump(pw, needSep ?
9436 "\n Receiver Resolver Table:" : " Receiver Resolver Table:",
9437 " ", dumpPackage, false)) {
9438 needSep = true;
9439 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009440 }
Christopher Tatef46723b2012-01-26 14:19:24 -08009441
9442 for (BroadcastQueue q : mBroadcastQueues) {
9443 needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009446 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009447
Dianne Hackborn786b4402012-08-27 15:14:02 -07009448 if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009449 if (needSep) {
9450 pw.println();
9451 }
9452 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009453 pw.println(" Sticky broadcasts:");
9454 StringBuilder sb = new StringBuilder(128);
9455 for (Map.Entry<String, ArrayList<Intent>> ent
9456 : mStickyBroadcasts.entrySet()) {
9457 pw.print(" * Sticky action "); pw.print(ent.getKey());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009458 if (dumpAll) {
9459 pw.println(":");
9460 ArrayList<Intent> intents = ent.getValue();
9461 final int N = intents.size();
9462 for (int i=0; i<N; i++) {
9463 sb.setLength(0);
9464 sb.append(" Intent: ");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08009465 intents.get(i).toShortString(sb, false, true, false, false);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009466 pw.println(sb.toString());
9467 Bundle bundle = intents.get(i).getExtras();
9468 if (bundle != null) {
9469 pw.print(" ");
9470 pw.println(bundle.toString());
9471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009472 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009473 } else {
9474 pw.println("");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009475 }
9476 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009477 needSep = true;
9478 }
9479
Dianne Hackborn786b4402012-08-27 15:14:02 -07009480 if (!onlyHistory && dumpAll) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009481 pw.println();
Christopher Tatef46723b2012-01-26 14:19:24 -08009482 for (BroadcastQueue queue : mBroadcastQueues) {
9483 pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]="
9484 + queue.mBroadcastsScheduled);
9485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009486 pw.println(" mHandler:");
9487 mHandler.dump(new PrintWriterPrinter(pw), " ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009488 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009489 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009490
9491 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 }
9493
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009494 boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009495 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07009496 boolean needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009497
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009498 ItemMatcher matcher = new ItemMatcher();
9499 matcher.build(args, opti);
9500
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009501 pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)");
Amith Yamasani742a6712011-05-04 14:49:28 -07009502
9503 mProviderMap.dumpProvidersLocked(pw, dumpAll);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009504
9505 if (mLaunchingProviders.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009506 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009507 for (int i=mLaunchingProviders.size()-1; i>=0; i--) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009508 ContentProviderRecord r = mLaunchingProviders.get(i);
9509 if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) {
9510 continue;
9511 }
9512 if (!printed) {
9513 if (needSep) pw.println(" ");
9514 needSep = true;
9515 pw.println(" Launching content providers:");
9516 printed = true;
9517 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009518 pw.print(" Launching #"); pw.print(i); pw.print(": ");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009519 pw.println(r);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009520 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009521 }
9522
9523 if (mGrantedUriPermissions.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009524 if (needSep) pw.println();
9525 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009526 pw.println("Granted Uri Permissions:");
9527 for (int i=0; i<mGrantedUriPermissions.size(); i++) {
9528 int uid = mGrantedUriPermissions.keyAt(i);
9529 HashMap<Uri, UriPermission> perms
9530 = mGrantedUriPermissions.valueAt(i);
9531 pw.print(" * UID "); pw.print(uid);
9532 pw.println(" holds:");
9533 for (UriPermission perm : perms.values()) {
9534 pw.print(" "); pw.println(perm);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009535 if (dumpAll) {
9536 perm.dump(pw, " ");
9537 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009538 }
9539 }
9540 needSep = true;
9541 }
9542
9543 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009544 }
9545
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009546 boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009547 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009548 boolean needSep = false;
9549
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009550 if (mIntentSenderRecords.size() > 0) {
9551 boolean printed = false;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009552 Iterator<WeakReference<PendingIntentRecord>> it
9553 = mIntentSenderRecords.values().iterator();
9554 while (it.hasNext()) {
9555 WeakReference<PendingIntentRecord> ref = it.next();
9556 PendingIntentRecord rec = ref != null ? ref.get(): null;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009557 if (dumpPackage != null && (rec == null
9558 || !dumpPackage.equals(rec.key.packageName))) {
9559 continue;
9560 }
9561 if (!printed) {
9562 pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)");
9563 printed = true;
9564 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009565 needSep = true;
9566 if (rec != null) {
9567 pw.print(" * "); pw.println(rec);
9568 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009569 rec.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009570 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009571 } else {
9572 pw.print(" * "); pw.println(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009573 }
9574 }
9575 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009576
9577 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 }
9579
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009580 private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009581 String prefix, String label, boolean complete, boolean brief, boolean client,
9582 String dumpPackage) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009583 TaskRecord lastTask = null;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009584 boolean needNL = false;
9585 final String innerPrefix = prefix + " ";
9586 final String[] args = new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009587 for (int i=list.size()-1; i>=0; i--) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009588 final ActivityRecord r = (ActivityRecord)list.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009589 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
9590 continue;
9591 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07009592 final boolean full = !brief && (complete || !r.isInHistory());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009593 if (needNL) {
9594 pw.println(" ");
9595 needNL = false;
9596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597 if (lastTask != r.task) {
9598 lastTask = r.task;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009599 pw.print(prefix);
9600 pw.print(full ? "* " : " ");
9601 pw.println(lastTask);
9602 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009603 lastTask.dump(pw, prefix + " ");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009604 } else if (complete) {
9605 // Complete + brief == give a summary. Isn't that obvious?!?
9606 if (lastTask.intent != null) {
Dianne Hackborn90c52de2011-09-23 12:57:44 -07009607 pw.print(prefix); pw.print(" ");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08009608 pw.println(lastTask.intent.toInsecureStringWithClip());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009609 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009611 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009612 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
9613 pw.print(" #"); pw.print(i); pw.print(": ");
9614 pw.println(r);
9615 if (full) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009616 r.dump(pw, innerPrefix);
9617 } else if (complete) {
9618 // Complete + brief == give a summary. Isn't that obvious?!?
Dianne Hackborn90c52de2011-09-23 12:57:44 -07009619 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009620 if (r.app != null) {
9621 pw.print(innerPrefix); pw.println(r.app);
9622 }
9623 }
9624 if (client && r.app != null && r.app.thread != null) {
9625 // flush anything that is already in the PrintWriter since the thread is going
9626 // to write to the file descriptor directly
9627 pw.flush();
9628 try {
9629 TransferPipe tp = new TransferPipe();
9630 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08009631 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
9632 r.appToken, innerPrefix, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009633 // Short timeout, since blocking here can
9634 // deadlock with the application.
9635 tp.go(fd, 2000);
9636 } finally {
9637 tp.kill();
9638 }
9639 } catch (IOException e) {
9640 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
9641 } catch (RemoteException e) {
9642 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
9643 }
9644 needNL = true;
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009646 }
9647 }
9648
Dianne Hackborn09c916b2009-12-08 14:50:51 -08009649 private static String buildOomTag(String prefix, String space, int val, int base) {
9650 if (val == base) {
9651 if (space == null) return prefix;
9652 return prefix + " ";
9653 }
9654 return prefix + "+" + Integer.toString(val-base);
9655 }
9656
9657 private static final int dumpProcessList(PrintWriter pw,
9658 ActivityManagerService service, List list,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009659 String prefix, String normalLabel, String persistentLabel,
9660 String dumpPackage) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009661 int numPers = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009662 final int N = list.size()-1;
9663 for (int i=N; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009664 ProcessRecord r = (ProcessRecord)list.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009665 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9666 continue;
9667 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07009668 pw.println(String.format("%s%s #%2d: %s",
9669 prefix, (r.persistent ? persistentLabel : normalLabel),
9670 i, r.toString()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009671 if (r.persistent) {
9672 numPers++;
9673 }
9674 }
9675 return numPers;
9676 }
9677
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009678 private static final boolean dumpProcessOomList(PrintWriter pw,
Dianne Hackborn905577f2011-09-07 18:31:28 -07009679 ActivityManagerService service, List<ProcessRecord> origList,
Dianne Hackborn287952c2010-09-22 22:34:31 -07009680 String prefix, String normalLabel, String persistentLabel,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009681 boolean inclDetails, String dumpPackage) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009682
Dianne Hackborn905577f2011-09-07 18:31:28 -07009683 ArrayList<Pair<ProcessRecord, Integer>> list
9684 = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size());
9685 for (int i=0; i<origList.size(); i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009686 ProcessRecord r = origList.get(i);
9687 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9688 continue;
9689 }
Dianne Hackborn905577f2011-09-07 18:31:28 -07009690 list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i));
9691 }
9692
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009693 if (list.size() <= 0) {
9694 return false;
9695 }
9696
Dianne Hackborn905577f2011-09-07 18:31:28 -07009697 Comparator<Pair<ProcessRecord, Integer>> comparator
9698 = new Comparator<Pair<ProcessRecord, Integer>>() {
9699 @Override
9700 public int compare(Pair<ProcessRecord, Integer> object1,
9701 Pair<ProcessRecord, Integer> object2) {
9702 if (object1.first.setAdj != object2.first.setAdj) {
9703 return object1.first.setAdj > object2.first.setAdj ? -1 : 1;
9704 }
9705 if (object1.second.intValue() != object2.second.intValue()) {
9706 return object1.second.intValue() > object2.second.intValue() ? -1 : 1;
9707 }
9708 return 0;
9709 }
9710 };
9711
9712 Collections.sort(list, comparator);
9713
Dianne Hackborn287952c2010-09-22 22:34:31 -07009714 final long curRealtime = SystemClock.elapsedRealtime();
9715 final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime;
9716 final long curUptime = SystemClock.uptimeMillis();
9717 final long uptimeSince = curUptime - service.mLastPowerCheckUptime;
9718
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009719 for (int i=list.size()-1; i>=0; i--) {
Dianne Hackborn905577f2011-09-07 18:31:28 -07009720 ProcessRecord r = list.get(i).first;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009721 String oomAdj;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009722 if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07009723 oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009724 } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) {
9725 oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009726 } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) {
9727 oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009728 } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) {
9729 oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009730 } else if (r.setAdj >= ProcessList.SERVICE_ADJ) {
9731 oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009732 } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) {
Dianne Hackborn672342c2011-11-29 11:29:02 -08009733 oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009734 } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
9735 oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ);
9736 } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
9737 oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ);
9738 } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) {
9739 oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ);
9740 } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) {
9741 oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009742 } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) {
9743 oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009744 } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) {
9745 oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009746 } else {
9747 oomAdj = Integer.toString(r.setAdj);
9748 }
9749 String schedGroup;
9750 switch (r.setSchedGroup) {
9751 case Process.THREAD_GROUP_BG_NONINTERACTIVE:
9752 schedGroup = "B";
9753 break;
9754 case Process.THREAD_GROUP_DEFAULT:
9755 schedGroup = "F";
9756 break;
9757 default:
9758 schedGroup = Integer.toString(r.setSchedGroup);
9759 break;
9760 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07009761 String foreground;
9762 if (r.foregroundActivities) {
9763 foreground = "A";
9764 } else if (r.foregroundServices) {
9765 foreground = "S";
9766 } else {
9767 foreground = " ";
9768 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07009769 pw.println(String.format("%s%s #%2d: adj=%s/%s%s trm=%2d %s (%s)",
Dianne Hackborn287952c2010-09-22 22:34:31 -07009770 prefix, (r.persistent ? persistentLabel : normalLabel),
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009771 (origList.size()-1)-list.get(i).second, oomAdj, schedGroup,
9772 foreground, r.trimMemoryLevel, r.toShortString(), r.adjType));
Dianne Hackborn287952c2010-09-22 22:34:31 -07009773 if (r.adjSource != null || r.adjTarget != null) {
9774 pw.print(prefix);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009775 pw.print(" ");
Dianne Hackborn287952c2010-09-22 22:34:31 -07009776 if (r.adjTarget instanceof ComponentName) {
9777 pw.print(((ComponentName)r.adjTarget).flattenToShortString());
9778 } else if (r.adjTarget != null) {
9779 pw.print(r.adjTarget.toString());
9780 } else {
9781 pw.print("{null}");
9782 }
9783 pw.print("<=");
9784 if (r.adjSource instanceof ProcessRecord) {
9785 pw.print("Proc{");
9786 pw.print(((ProcessRecord)r.adjSource).toShortString());
9787 pw.println("}");
9788 } else if (r.adjSource != null) {
9789 pw.println(r.adjSource.toString());
9790 } else {
9791 pw.println("{null}");
9792 }
9793 }
9794 if (inclDetails) {
9795 pw.print(prefix);
9796 pw.print(" ");
9797 pw.print("oom: max="); pw.print(r.maxAdj);
9798 pw.print(" hidden="); pw.print(r.hiddenAdj);
Dianne Hackbornee7621c2012-08-13 16:42:18 -07009799 pw.print(" empty="); pw.print(r.emptyAdj);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009800 pw.print(" curRaw="); pw.print(r.curRawAdj);
9801 pw.print(" setRaw="); pw.print(r.setRawAdj);
9802 pw.print(" cur="); pw.print(r.curAdj);
9803 pw.print(" set="); pw.println(r.setAdj);
9804 pw.print(prefix);
9805 pw.print(" ");
9806 pw.print("keeping="); pw.print(r.keeping);
9807 pw.print(" hidden="); pw.print(r.hidden);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07009808 pw.print(" empty="); pw.print(r.empty);
9809 pw.print(" hasAboveClient="); pw.println(r.hasAboveClient);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009810
9811 if (!r.keeping) {
9812 if (r.lastWakeTime != 0) {
9813 long wtime;
9814 BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics();
9815 synchronized (stats) {
9816 wtime = stats.getProcessWakeTime(r.info.uid,
9817 r.pid, curRealtime);
9818 }
9819 long timeUsed = wtime - r.lastWakeTime;
9820 pw.print(prefix);
9821 pw.print(" ");
9822 pw.print("keep awake over ");
9823 TimeUtils.formatDuration(realtimeSince, pw);
9824 pw.print(" used ");
9825 TimeUtils.formatDuration(timeUsed, pw);
9826 pw.print(" (");
9827 pw.print((timeUsed*100)/realtimeSince);
9828 pw.println("%)");
9829 }
9830 if (r.lastCpuTime != 0) {
9831 long timeUsed = r.curCpuTime - r.lastCpuTime;
9832 pw.print(prefix);
9833 pw.print(" ");
9834 pw.print("run cpu over ");
9835 TimeUtils.formatDuration(uptimeSince, pw);
9836 pw.print(" used ");
9837 TimeUtils.formatDuration(timeUsed, pw);
9838 pw.print(" (");
9839 pw.print((timeUsed*100)/uptimeSince);
9840 pw.println("%)");
9841 }
9842 }
9843 }
9844 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009845 return true;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009846 }
9847
Dianne Hackbornb437e092011-08-05 17:50:29 -07009848 ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009849 ArrayList<ProcessRecord> procs;
9850 synchronized (this) {
Dianne Hackbornb437e092011-08-05 17:50:29 -07009851 if (args != null && args.length > start
9852 && args[start].charAt(0) != '-') {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009853 procs = new ArrayList<ProcessRecord>();
9854 int pid = -1;
9855 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -07009856 pid = Integer.parseInt(args[start]);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009857 } catch (NumberFormatException e) {
9858
9859 }
9860 for (int i=mLruProcesses.size()-1; i>=0; i--) {
9861 ProcessRecord proc = mLruProcesses.get(i);
9862 if (proc.pid == pid) {
9863 procs.add(proc);
Dianne Hackbornb437e092011-08-05 17:50:29 -07009864 } else if (proc.processName.equals(args[start])) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009865 procs.add(proc);
9866 }
9867 }
9868 if (procs.size() <= 0) {
Dianne Hackbornb437e092011-08-05 17:50:29 -07009869 pw.println("No process found for: " + args[start]);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009870 return null;
9871 }
9872 } else {
9873 procs = new ArrayList<ProcessRecord>(mLruProcesses);
9874 }
9875 }
9876 return procs;
9877 }
9878
9879 final void dumpGraphicsHardwareUsage(FileDescriptor fd,
9880 PrintWriter pw, String[] args) {
Dianne Hackbornb437e092011-08-05 17:50:29 -07009881 ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009882 if (procs == null) {
9883 return;
9884 }
9885
9886 long uptime = SystemClock.uptimeMillis();
9887 long realtime = SystemClock.elapsedRealtime();
9888 pw.println("Applications Graphics Acceleration Info:");
9889 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
9890
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009891 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
9892 ProcessRecord r = procs.get(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -07009893 if (r.thread != null) {
9894 pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **");
9895 pw.flush();
9896 try {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009897 TransferPipe tp = new TransferPipe();
9898 try {
9899 r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args);
9900 tp.go(fd);
9901 } finally {
9902 tp.kill();
9903 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009904 } catch (IOException e) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009905 pw.println("Failure while dumping the app: " + r);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009906 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -07009907 } catch (RemoteException e) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009908 pw.println("Got a RemoteException while dumping the app " + r);
Chet Haase9c1e23b2011-03-24 10:51:31 -07009909 pw.flush();
9910 }
9911 }
9912 }
Chet Haase9c1e23b2011-03-24 10:51:31 -07009913 }
9914
Jeff Brown6754ba22011-12-14 20:20:01 -08009915 final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) {
9916 ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args);
9917 if (procs == null) {
9918 return;
9919 }
9920
9921 pw.println("Applications Database Info:");
9922
9923 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
9924 ProcessRecord r = procs.get(i);
9925 if (r.thread != null) {
9926 pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **");
9927 pw.flush();
9928 try {
9929 TransferPipe tp = new TransferPipe();
9930 try {
9931 r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args);
9932 tp.go(fd);
9933 } finally {
9934 tp.kill();
9935 }
9936 } catch (IOException e) {
9937 pw.println("Failure while dumping the app: " + r);
9938 pw.flush();
9939 } catch (RemoteException e) {
9940 pw.println("Got a RemoteException while dumping the app " + r);
9941 pw.flush();
9942 }
9943 }
9944 }
9945 }
9946
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009947 final static class MemItem {
9948 final String label;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009949 final String shortLabel;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009950 final long pss;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009951 final int id;
Dianne Hackborna4bacb82011-08-24 15:12:38 -07009952 ArrayList<MemItem> subitems;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009953
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009954 public MemItem(String _label, String _shortLabel, long _pss, int _id) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009955 label = _label;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009956 shortLabel = _shortLabel;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009957 pss = _pss;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009958 id = _id;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009959 }
9960 }
9961
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009962 static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items,
Dianne Hackbornb437e092011-08-05 17:50:29 -07009963 boolean sort) {
9964 if (sort) {
9965 Collections.sort(items, new Comparator<MemItem>() {
9966 @Override
9967 public int compare(MemItem lhs, MemItem rhs) {
9968 if (lhs.pss < rhs.pss) {
9969 return 1;
9970 } else if (lhs.pss > rhs.pss) {
9971 return -1;
9972 }
9973 return 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009974 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07009975 });
9976 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009977
9978 for (int i=0; i<items.size(); i++) {
9979 MemItem mi = items.get(i);
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009980 pw.print(prefix); pw.printf("%7d kB: ", mi.pss); pw.println(mi.label);
Dianne Hackborna4bacb82011-08-24 15:12:38 -07009981 if (mi.subitems != null) {
9982 dumpMemItems(pw, prefix + " ", mi.subitems, true);
9983 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009984 }
9985 }
9986
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009987 // These are in KB.
9988 static final long[] DUMP_MEM_BUCKETS = new long[] {
9989 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024,
9990 120*1024, 160*1024, 200*1024,
9991 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024,
9992 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024
9993 };
9994
Dianne Hackborn672342c2011-11-29 11:29:02 -08009995 static final void appendMemBucket(StringBuilder out, long memKB, String label,
9996 boolean stackLike) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08009997 int start = label.lastIndexOf('.');
9998 if (start >= 0) start++;
9999 else start = 0;
10000 int end = label.length();
10001 for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) {
10002 if (DUMP_MEM_BUCKETS[i] >= memKB) {
10003 long bucket = DUMP_MEM_BUCKETS[i]/1024;
10004 out.append(bucket);
Dianne Hackborn672342c2011-11-29 11:29:02 -080010005 out.append(stackLike ? "MB." : "MB ");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010006 out.append(label, start, end);
10007 return;
10008 }
10009 }
10010 out.append(memKB/1024);
Dianne Hackborn672342c2011-11-29 11:29:02 -080010011 out.append(stackLike ? "MB." : "MB ");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010012 out.append(label, start, end);
10013 }
10014
10015 static final int[] DUMP_MEM_OOM_ADJ = new int[] {
10016 ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ,
10017 ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ,
10018 ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ,
10019 ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ
10020 };
10021 static final String[] DUMP_MEM_OOM_LABEL = new String[] {
10022 "System", "Persistent", "Foreground",
10023 "Visible", "Perceptible", "Heavy Weight",
10024 "Backup", "A Services", "Home", "Previous",
10025 "B Services", "Background"
10026 };
10027
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010028 final void dumpApplicationMemoryUsage(FileDescriptor fd,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010029 PrintWriter pw, String prefix, String[] args, boolean brief,
Dianne Hackborn672342c2011-11-29 11:29:02 -080010030 PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010031 boolean dumpAll = false;
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010032 boolean oomOnly = false;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010033
10034 int opti = 0;
10035 while (opti < args.length) {
10036 String opt = args[opti];
10037 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
10038 break;
10039 }
10040 opti++;
10041 if ("-a".equals(opt)) {
10042 dumpAll = true;
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010043 } else if ("--oom".equals(opt)) {
10044 oomOnly = true;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010045 } else if ("-h".equals(opt)) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010046 pw.println("meminfo dump options: [-a] [--oom] [process]");
Dianne Hackbornb437e092011-08-05 17:50:29 -070010047 pw.println(" -a: include all available information for each process.");
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010048 pw.println(" --oom: only show processes organized by oom adj.");
Dianne Hackbornb437e092011-08-05 17:50:29 -070010049 pw.println("If [process] is specified it can be the name or ");
10050 pw.println("pid of a specific process to dump.");
10051 return;
10052 } else {
10053 pw.println("Unknown argument: " + opt + "; use -h for help");
10054 }
10055 }
10056
10057 ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010058 if (procs == null) {
10059 return;
10060 }
10061
Dianne Hackborn6447ca32009-04-07 19:50:08 -070010062 final boolean isCheckinRequest = scanArgs(args, "--checkin");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 long uptime = SystemClock.uptimeMillis();
10064 long realtime = SystemClock.elapsedRealtime();
Dianne Hackbornb437e092011-08-05 17:50:29 -070010065
10066 if (procs.size() == 1 || isCheckinRequest) {
10067 dumpAll = true;
10068 }
10069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010070 if (isCheckinRequest) {
10071 // short checkin version
10072 pw.println(uptime + "," + realtime);
10073 pw.flush();
10074 } else {
10075 pw.println("Applications Memory Usage (kB):");
10076 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
10077 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010078
Dianne Hackbornb437e092011-08-05 17:50:29 -070010079 String[] innerArgs = new String[args.length-opti];
10080 System.arraycopy(args, opti, innerArgs, 0, args.length-opti);
10081
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010082 ArrayList<MemItem> procMems = new ArrayList<MemItem>();
10083 long nativePss=0, dalvikPss=0, otherPss=0;
10084 long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS];
10085
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010086 long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length];
10087 ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[])
10088 new ArrayList[DUMP_MEM_OOM_LABEL.length];
Dianne Hackbornb437e092011-08-05 17:50:29 -070010089
10090 long totalPss = 0;
10091
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010092 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
10093 ProcessRecord r = procs.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094 if (r.thread != null) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010095 if (!isCheckinRequest && dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010096 pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **");
10097 pw.flush();
10098 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010099 Debug.MemoryInfo mi = null;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010100 if (dumpAll) {
10101 try {
10102 mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs);
10103 } catch (RemoteException e) {
10104 if (!isCheckinRequest) {
10105 pw.println("Got RemoteException!");
10106 pw.flush();
10107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010109 } else {
10110 mi = new Debug.MemoryInfo();
10111 Debug.getMemoryInfo(r.pid, mi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010112 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010113
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010114 if (!isCheckinRequest && mi != null) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010115 long myTotalPss = mi.getTotalPss();
10116 totalPss += myTotalPss;
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010117 MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")",
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010118 r.processName, myTotalPss, 0);
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010119 procMems.add(pssItem);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010120
10121 nativePss += mi.nativePss;
10122 dalvikPss += mi.dalvikPss;
10123 otherPss += mi.otherPss;
10124 for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
10125 long mem = mi.getOtherPss(j);
10126 miscPss[j] += mem;
10127 otherPss -= mem;
10128 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010129
10130 for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010131 if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex]
10132 || oomIndex == (oomPss.length-1)) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010133 oomPss[oomIndex] += myTotalPss;
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010134 if (oomProcs[oomIndex] == null) {
10135 oomProcs[oomIndex] = new ArrayList<MemItem>();
10136 }
10137 oomProcs[oomIndex].add(pssItem);
Dianne Hackbornb437e092011-08-05 17:50:29 -070010138 break;
10139 }
10140 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 }
10143 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010144
10145 if (!isCheckinRequest && procs.size() > 1) {
10146 ArrayList<MemItem> catMems = new ArrayList<MemItem>();
10147
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010148 catMems.add(new MemItem("Native", "Native", nativePss, -1));
10149 catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2));
10150 catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3));
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010151 for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010152 String label = Debug.MemoryInfo.getOtherLabel(j);
10153 catMems.add(new MemItem(label, label, miscPss[j], j));
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010154 }
10155
Dianne Hackbornb437e092011-08-05 17:50:29 -070010156 ArrayList<MemItem> oomMems = new ArrayList<MemItem>();
10157 for (int j=0; j<oomPss.length; j++) {
10158 if (oomPss[j] != 0) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010159 String label = DUMP_MEM_OOM_LABEL[j];
10160 MemItem item = new MemItem(label, label, oomPss[j],
10161 DUMP_MEM_OOM_ADJ[j]);
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010162 item.subitems = oomProcs[j];
10163 oomMems.add(item);
Dianne Hackbornb437e092011-08-05 17:50:29 -070010164 }
10165 }
10166
Dianne Hackborn672342c2011-11-29 11:29:02 -080010167 if (outTag != null || outStack != null) {
10168 if (outTag != null) {
10169 appendMemBucket(outTag, totalPss, "total", false);
10170 }
10171 if (outStack != null) {
10172 appendMemBucket(outStack, totalPss, "total", true);
10173 }
10174 boolean firstLine = true;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010175 for (int i=0; i<oomMems.size(); i++) {
10176 MemItem miCat = oomMems.get(i);
10177 if (miCat.subitems == null || miCat.subitems.size() < 1) {
10178 continue;
10179 }
10180 if (miCat.id < ProcessList.SERVICE_ADJ
10181 || miCat.id == ProcessList.HOME_APP_ADJ
10182 || miCat.id == ProcessList.PREVIOUS_APP_ADJ) {
Dianne Hackborn672342c2011-11-29 11:29:02 -080010183 if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) {
10184 outTag.append(" / ");
10185 }
10186 if (outStack != null) {
10187 if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) {
10188 if (firstLine) {
10189 outStack.append(":");
10190 firstLine = false;
10191 }
10192 outStack.append("\n\t at ");
10193 } else {
10194 outStack.append("$");
10195 }
10196 }
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010197 for (int j=0; j<miCat.subitems.size(); j++) {
10198 MemItem mi = miCat.subitems.get(j);
10199 if (j > 0) {
Dianne Hackborn672342c2011-11-29 11:29:02 -080010200 if (outTag != null) {
10201 outTag.append(" ");
10202 }
10203 if (outStack != null) {
10204 outStack.append("$");
10205 }
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010206 }
Dianne Hackborn672342c2011-11-29 11:29:02 -080010207 if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) {
10208 appendMemBucket(outTag, mi.pss, mi.shortLabel, false);
10209 }
10210 if (outStack != null) {
10211 appendMemBucket(outStack, mi.pss, mi.shortLabel, true);
10212 }
10213 }
10214 if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) {
10215 outStack.append("(");
10216 for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) {
10217 if (DUMP_MEM_OOM_ADJ[k] == miCat.id) {
10218 outStack.append(DUMP_MEM_OOM_LABEL[k]);
10219 outStack.append(":");
10220 outStack.append(DUMP_MEM_OOM_ADJ[k]);
10221 }
10222 }
10223 outStack.append(")");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010224 }
10225 }
10226 }
10227 }
10228
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010229 if (!brief && !oomOnly) {
Dianne Hackborn04d6db32011-11-04 20:07:24 -070010230 pw.println();
10231 pw.println("Total PSS by process:");
10232 dumpMemItems(pw, " ", procMems, true);
10233 pw.println();
10234 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010235 pw.println("Total PSS by OOM adjustment:");
10236 dumpMemItems(pw, " ", oomMems, false);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010237 if (!oomOnly) {
10238 PrintWriter out = categoryPw != null ? categoryPw : pw;
10239 out.println();
10240 out.println("Total PSS by category:");
10241 dumpMemItems(out, " ", catMems, true);
Dianne Hackborn04d6db32011-11-04 20:07:24 -070010242 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010243 pw.println();
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010244 pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB");
Dianne Hackbornd3e677b2012-04-05 14:58:18 -070010245 final int[] SINGLE_LONG_FORMAT = new int[] {
10246 Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG
10247 };
10248 long[] longOut = new long[1];
10249 Process.readProcFile("/sys/kernel/mm/ksm/pages_shared",
10250 SINGLE_LONG_FORMAT, null, longOut, null);
10251 long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10252 longOut[0] = 0;
10253 Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing",
10254 SINGLE_LONG_FORMAT, null, longOut, null);
10255 long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10256 longOut[0] = 0;
10257 Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared",
10258 SINGLE_LONG_FORMAT, null, longOut, null);
10259 long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10260 longOut[0] = 0;
10261 Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile",
10262 SINGLE_LONG_FORMAT, null, longOut, null);
10263 long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10264 pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared ");
10265 pw.print(shared); pw.println(" kB");
10266 pw.print(" "); pw.print(unshared); pw.print(" kB unshared; ");
10267 pw.print(voltile); pw.println(" kB volatile");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010269 }
10270
10271 /**
10272 * Searches array of arguments for the specified string
10273 * @param args array of argument strings
10274 * @param value value to search for
10275 * @return true if the value is contained in the array
10276 */
10277 private static boolean scanArgs(String[] args, String value) {
10278 if (args != null) {
10279 for (String arg : args) {
10280 if (value.equals(arg)) {
10281 return true;
10282 }
10283 }
10284 }
10285 return false;
10286 }
10287
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010288 private final boolean removeDyingProviderLocked(ProcessRecord proc,
10289 ContentProviderRecord cpr, boolean always) {
10290 final boolean inLaunching = mLaunchingProviders.contains(cpr);
10291
10292 if (!inLaunching || always) {
10293 synchronized (cpr) {
10294 cpr.launchingApp = null;
10295 cpr.notifyAll();
10296 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010297 mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010298 String names[] = cpr.info.authority.split(";");
10299 for (int j = 0; j < names.length; j++) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010300 mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 }
10302 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010303
10304 for (int i=0; i<cpr.connections.size(); i++) {
10305 ContentProviderConnection conn = cpr.connections.get(i);
10306 if (conn.waiting) {
10307 // If this connection is waiting for the provider, then we don't
10308 // need to mess with its process unless we are always removing
10309 // or for some reason the provider is not currently launching.
10310 if (inLaunching && !always) {
10311 continue;
10312 }
10313 }
10314 ProcessRecord capp = conn.client;
10315 conn.dead = true;
10316 if (conn.stableCount > 0) {
10317 if (!capp.persistent && capp.thread != null
10318 && capp.pid != 0
10319 && capp.pid != MY_PID) {
10320 Slog.i(TAG, "Kill " + capp.processName
10321 + " (pid " + capp.pid + "): provider " + cpr.info.name
10322 + " in dying process " + (proc != null ? proc.processName : "??"));
10323 EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid,
10324 capp.processName, capp.setAdj, "dying provider "
10325 + cpr.name.toShortString());
10326 Process.killProcessQuiet(capp.pid);
10327 }
10328 } else if (capp.thread != null && conn.provider.provider != null) {
10329 try {
10330 capp.thread.unstableProviderDied(conn.provider.provider.asBinder());
10331 } catch (RemoteException e) {
10332 }
10333 // In the protocol here, we don't expect the client to correctly
10334 // clean up this connection, we'll just remove it.
10335 cpr.connections.remove(i);
10336 conn.client.conProviders.remove(conn);
10337 }
10338 }
10339
10340 if (inLaunching && always) {
10341 mLaunchingProviders.remove(cpr);
10342 }
10343 return inLaunching;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 }
10345
10346 /**
10347 * Main code for cleaning up a process when it has gone away. This is
10348 * called both as a result of the process dying, or directly when stopping
10349 * a process when running in single process mode.
10350 */
10351 private final void cleanUpApplicationRecordLocked(ProcessRecord app,
Dianne Hackborn130b0d22011-07-26 22:07:48 -070010352 boolean restarting, boolean allowRestart, int index) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 if (index >= 0) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010354 mLruProcesses.remove(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 }
10356
Dianne Hackborn36124872009-10-08 16:22:03 -070010357 mProcessesToGc.remove(app);
10358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010359 // Dismiss any open dialogs.
10360 if (app.crashDialog != null) {
10361 app.crashDialog.dismiss();
10362 app.crashDialog = null;
10363 }
10364 if (app.anrDialog != null) {
10365 app.anrDialog.dismiss();
10366 app.anrDialog = null;
10367 }
10368 if (app.waitDialog != null) {
10369 app.waitDialog.dismiss();
10370 app.waitDialog = null;
10371 }
10372
10373 app.crashing = false;
10374 app.notResponding = false;
10375
10376 app.resetPackageList();
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -070010377 app.unlinkDeathRecipient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 app.thread = null;
10379 app.forcingToForeground = null;
10380 app.foregroundServices = false;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070010381 app.foregroundActivities = false;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070010382 app.hasShownUi = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070010383 app.hasAboveClient = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010384
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010385 mServices.killServicesLocked(app, allowRestart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386
10387 boolean restart = false;
10388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 // Remove published content providers.
10390 if (!app.pubProviders.isEmpty()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010391 Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010392 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010393 ContentProviderRecord cpr = it.next();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010394
10395 final boolean always = app.bad || !allowRestart;
10396 if (removeDyingProviderLocked(app, cpr, always) || always) {
10397 // We left the provider in the launching list, need to
10398 // restart it.
10399 restart = true;
10400 }
10401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 cpr.provider = null;
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -070010403 cpr.proc = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 }
10405 app.pubProviders.clear();
10406 }
10407
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010408 // Take care of any launching providers waiting for this process.
10409 if (checkAppInLaunchingProvidersLocked(app, false)) {
10410 restart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 // Unregister from connected content providers.
10414 if (!app.conProviders.isEmpty()) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010415 for (int i=0; i<app.conProviders.size(); i++) {
10416 ContentProviderConnection conn = app.conProviders.get(i);
10417 conn.provider.connections.remove(conn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010418 }
10419 app.conProviders.clear();
10420 }
10421
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010422 // At this point there may be remaining entries in mLaunchingProviders
10423 // where we were the only one waiting, so they are no longer of use.
10424 // Look for these and clean up if found.
10425 // XXX Commented out for now. Trying to figure out a way to reproduce
10426 // the actual situation to identify what is actually going on.
10427 if (false) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010428 for (int i=0; i<mLaunchingProviders.size(); i++) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010429 ContentProviderRecord cpr = (ContentProviderRecord)
10430 mLaunchingProviders.get(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010431 if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010432 synchronized (cpr) {
10433 cpr.launchingApp = null;
10434 cpr.notifyAll();
10435 }
10436 }
10437 }
10438 }
10439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440 skipCurrentReceiverLocked(app);
10441
10442 // Unregister any receivers.
10443 if (app.receivers.size() > 0) {
10444 Iterator<ReceiverList> it = app.receivers.iterator();
10445 while (it.hasNext()) {
10446 removeReceiverLocked(it.next());
10447 }
10448 app.receivers.clear();
10449 }
10450
Christopher Tate181fafa2009-05-14 11:12:14 -070010451 // If the app is undergoing backup, tell the backup manager about it
10452 if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010453 if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup");
Christopher Tate181fafa2009-05-14 11:12:14 -070010454 try {
10455 IBackupManager bm = IBackupManager.Stub.asInterface(
10456 ServiceManager.getService(Context.BACKUP_SERVICE));
10457 bm.agentDisconnected(app.info.packageName);
10458 } catch (RemoteException e) {
10459 // can't happen; backup manager is local
10460 }
10461 }
10462
Dianne Hackborna93c2c12012-05-31 15:29:36 -070010463 for (int i = mPendingProcessChanges.size()-1; i>=0; i--) {
10464 ProcessChangeItem item = mPendingProcessChanges.get(i);
10465 if (item.pid == app.pid) {
10466 mPendingProcessChanges.remove(i);
10467 mAvailProcessChanges.add(item);
10468 }
10469 }
Jeff Sharkey287bd832011-05-28 19:36:26 -070010470 mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070010471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010472 // If the caller is restarting this app, then leave it in its
10473 // current lists and let the caller take care of it.
10474 if (restarting) {
10475 return;
10476 }
10477
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010478 if (!app.persistent || app.isolated) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010479 if (DEBUG_PROCESSES) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 "Removing non-persistent process during cleanup: " + app);
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010481 mProcessNames.remove(app.processName, app.uid);
10482 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -070010483 if (mHeavyWeightProcess == app) {
10484 mHeavyWeightProcess = null;
10485 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
10486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010487 } else if (!app.removed) {
10488 // This app is persistent, so we need to keep its record around.
10489 // If it is not already on the pending app list, add it there
10490 // and start a new process for it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 if (mPersistentStartingProcesses.indexOf(app) < 0) {
10492 mPersistentStartingProcesses.add(app);
10493 restart = true;
10494 }
10495 }
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070010496 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
10497 "Clean-up removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 mProcessesOnHold.remove(app);
10499
The Android Open Source Project4df24232009-03-05 14:34:35 -080010500 if (app == mHomeProcess) {
10501 mHomeProcess = null;
10502 }
Dianne Hackbornf35fe232011-11-01 19:25:20 -070010503 if (app == mPreviousProcess) {
10504 mPreviousProcess = null;
10505 }
10506
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010507 if (restart && !app.isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010508 // We have components that still need to be running in the
10509 // process, so re-launch it.
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010510 mProcessNames.put(app.processName, app.uid, app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 startProcessLocked(app, "restart", app.processName);
10512 } else if (app.pid > 0 && app.pid != MY_PID) {
10513 // Goodbye!
10514 synchronized (mPidsSelfLocked) {
10515 mPidsSelfLocked.remove(app.pid);
10516 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
10517 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010518 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519 }
10520 }
10521
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010522 boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) {
10523 // Look through the content providers we are waiting to have launched,
10524 // and if any run in this process then either schedule a restart of
10525 // the process or kill the client waiting for it if this process has
10526 // gone bad.
10527 int NL = mLaunchingProviders.size();
10528 boolean restart = false;
10529 for (int i=0; i<NL; i++) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010530 ContentProviderRecord cpr = mLaunchingProviders.get(i);
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010531 if (cpr.launchingApp == app) {
10532 if (!alwaysBad && !app.bad) {
10533 restart = true;
10534 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010535 removeDyingProviderLocked(app, cpr, true);
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010536 NL = mLaunchingProviders.size();
10537 }
10538 }
10539 }
10540 return restart;
10541 }
10542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 // =========================================================
10544 // SERVICES
10545 // =========================================================
10546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 public List<ActivityManager.RunningServiceInfo> getServices(int maxNum,
10548 int flags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010549 enforceNotIsolatedCaller("getServices");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010551 return mServices.getRunningServiceInfoLocked(maxNum, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010552 }
10553 }
10554
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010555 public PendingIntent getRunningServiceControlPanel(ComponentName name) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010556 enforceNotIsolatedCaller("getRunningServiceControlPanel");
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010557 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010558 return mServices.getRunningServiceControlPanelLocked(name);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010559 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010560 }
10561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 public ComponentName startService(IApplicationThread caller, Intent service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010563 String resolvedType, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010564 enforceNotIsolatedCaller("startService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 // Refuse possible leaked file descriptors
10566 if (service != null && service.hasFileDescriptors() == true) {
10567 throw new IllegalArgumentException("File descriptors passed in Intent");
10568 }
10569
Amith Yamasani742a6712011-05-04 14:49:28 -070010570 if (DEBUG_SERVICE)
10571 Slog.v(TAG, "startService: " + service + " type=" + resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 synchronized(this) {
10573 final int callingPid = Binder.getCallingPid();
10574 final int callingUid = Binder.getCallingUid();
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010575 checkValidCaller(callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010576 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010577 ComponentName res = mServices.startServiceLocked(caller, service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010578 resolvedType, callingPid, callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 Binder.restoreCallingIdentity(origId);
10580 return res;
10581 }
10582 }
10583
10584 ComponentName startServiceInPackage(int uid,
10585 Intent service, String resolvedType) {
10586 synchronized(this) {
Amith Yamasani742a6712011-05-04 14:49:28 -070010587 if (DEBUG_SERVICE)
10588 Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010590 ComponentName res = mServices.startServiceLocked(null, service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010591 resolvedType, -1, uid, UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 Binder.restoreCallingIdentity(origId);
10593 return res;
10594 }
10595 }
10596
10597 public int stopService(IApplicationThread caller, Intent service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010598 String resolvedType, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010599 enforceNotIsolatedCaller("stopService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600 // Refuse possible leaked file descriptors
10601 if (service != null && service.hasFileDescriptors() == true) {
10602 throw new IllegalArgumentException("File descriptors passed in Intent");
10603 }
10604
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010605 checkValidCaller(Binder.getCallingUid(), userId);
10606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 synchronized(this) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010608 return mServices.stopServiceLocked(caller, service, resolvedType, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 }
10611
10612 public IBinder peekService(Intent service, String resolvedType) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010613 enforceNotIsolatedCaller("peekService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 // Refuse possible leaked file descriptors
10615 if (service != null && service.hasFileDescriptors() == true) {
10616 throw new IllegalArgumentException("File descriptors passed in Intent");
10617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010619 return mServices.peekServiceLocked(service, resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010621 }
10622
10623 public boolean stopServiceToken(ComponentName className, IBinder token,
10624 int startId) {
10625 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010626 return mServices.stopServiceTokenLocked(className, token, startId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010628 }
10629
10630 public void setServiceForeground(ComponentName className, IBinder token,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070010631 int id, Notification notification, boolean removeNotification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010633 mServices.setServiceForegroundLocked(className, token, id, notification,
10634 removeNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010635 }
10636 }
Amith Yamasania4a54e22012-04-16 15:44:19 -070010637
Dianne Hackborn7d19e022012-08-07 19:12:33 -070010638 boolean isSingleton(String componentProcessName, ApplicationInfo aInfo,
10639 String className, int flags) {
Amith Yamasania4a54e22012-04-16 15:44:19 -070010640 boolean result = false;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010641 if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -070010642 if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) {
10643 if (ActivityManager.checkUidPermission(
10644 android.Manifest.permission.INTERACT_ACROSS_USERS,
10645 aInfo.uid) != PackageManager.PERMISSION_GRANTED) {
10646 ComponentName comp = new ComponentName(aInfo.packageName, className);
10647 String msg = "Permission Denial: Component " + comp.flattenToShortString()
10648 + " requests FLAG_SINGLE_USER, but app does not hold "
10649 + android.Manifest.permission.INTERACT_ACROSS_USERS;
10650 Slog.w(TAG, msg);
10651 throw new SecurityException(msg);
10652 }
10653 result = true;
10654 }
Amith Yamasania4a54e22012-04-16 15:44:19 -070010655 } else if (componentProcessName == aInfo.packageName) {
10656 result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0;
10657 } else if ("system".equals(componentProcessName)) {
10658 result = true;
10659 }
10660 if (DEBUG_MU) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -070010661 Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo
10662 + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result);
Amith Yamasania4a54e22012-04-16 15:44:19 -070010663 }
10664 return result;
10665 }
10666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010667 public int bindService(IApplicationThread caller, IBinder token,
10668 Intent service, String resolvedType,
Amith Yamasani37ce3a82012-02-06 12:04:42 -080010669 IServiceConnection connection, int flags, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010670 enforceNotIsolatedCaller("bindService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010671 // Refuse possible leaked file descriptors
10672 if (service != null && service.hasFileDescriptors() == true) {
10673 throw new IllegalArgumentException("File descriptors passed in Intent");
10674 }
10675
Amith Yamasani37ce3a82012-02-06 12:04:42 -080010676 checkValidCaller(Binder.getCallingUid(), userId);
10677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010678 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010679 return mServices.bindServiceLocked(caller, token, service, resolvedType,
10680 connection, flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681 }
10682 }
10683
10684 public boolean unbindService(IServiceConnection connection) {
10685 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010686 return mServices.unbindServiceLocked(connection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 }
10689
10690 public void publishService(IBinder token, Intent intent, IBinder service) {
10691 // Refuse possible leaked file descriptors
10692 if (intent != null && intent.hasFileDescriptors() == true) {
10693 throw new IllegalArgumentException("File descriptors passed in Intent");
10694 }
10695
10696 synchronized(this) {
10697 if (!(token instanceof ServiceRecord)) {
10698 throw new IllegalArgumentException("Invalid service token");
10699 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010700 mServices.publishServiceLocked((ServiceRecord)token, intent, service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010701 }
10702 }
10703
10704 public void unbindFinished(IBinder token, Intent intent, boolean doRebind) {
10705 // Refuse possible leaked file descriptors
10706 if (intent != null && intent.hasFileDescriptors() == true) {
10707 throw new IllegalArgumentException("File descriptors passed in Intent");
10708 }
10709
10710 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010711 mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 }
10713 }
10714
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010715 public void serviceDoneExecuting(IBinder token, int type, int startId, int res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 synchronized(this) {
10717 if (!(token instanceof ServiceRecord)) {
10718 throw new IllegalArgumentException("Invalid service token");
10719 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010720 mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res);
Dianne Hackbornad5499d2010-03-29 18:08:45 -070010721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010722 }
10723
10724 // =========================================================
Christopher Tate181fafa2009-05-14 11:12:14 -070010725 // BACKUP AND RESTORE
10726 // =========================================================
10727
10728 // Cause the target app to be launched if necessary and its backup agent
10729 // instantiated. The backup agent will invoke backupAgentCreated() on the
10730 // activity manager to announce its creation.
10731 public boolean bindBackupAgent(ApplicationInfo app, int backupMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010732 if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -070010733 enforceCallingPermission("android.permission.BACKUP", "startBackupAgent");
10734
10735 synchronized(this) {
10736 // !!! TODO: currently no check here that we're already bound
10737 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
10738 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
10739 synchronized (stats) {
10740 ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name);
10741 }
10742
Dianne Hackborne7f97212011-02-24 14:40:20 -080010743 // Backup agent is now in use, its package can't be stopped.
10744 try {
10745 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010746 app.packageName, false, UserHandle.getUserId(app.uid));
Dianne Hackborne7f97212011-02-24 14:40:20 -080010747 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -080010748 } catch (IllegalArgumentException e) {
10749 Slog.w(TAG, "Failed trying to unstop package "
10750 + app.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -080010751 }
10752
Christopher Tate181fafa2009-05-14 11:12:14 -070010753 BackupRecord r = new BackupRecord(ss, app, backupMode);
Christopher Tate4a627c72011-04-01 14:43:32 -070010754 ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL)
10755 ? new ComponentName(app.packageName, app.backupAgentName)
10756 : new ComponentName("android", "FullBackupAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -070010757 // startProcessLocked() returns existing proc's record if it's already running
10758 ProcessRecord proc = startProcessLocked(app.processName, app,
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010759 false, 0, "backup", hostingName, false, false);
Christopher Tate181fafa2009-05-14 11:12:14 -070010760 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010761 Slog.e(TAG, "Unable to start backup agent process " + r);
Christopher Tate181fafa2009-05-14 11:12:14 -070010762 return false;
10763 }
10764
10765 r.app = proc;
10766 mBackupTarget = r;
10767 mBackupAppName = app.packageName;
10768
Christopher Tate6fa95972009-06-05 18:43:55 -070010769 // Try not to kill the process during backup
10770 updateOomAdjLocked(proc);
10771
Christopher Tate181fafa2009-05-14 11:12:14 -070010772 // If the process is already attached, schedule the creation of the backup agent now.
10773 // If it is not yet live, this will be done when it attaches to the framework.
10774 if (proc.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010775 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc);
Christopher Tate181fafa2009-05-14 11:12:14 -070010776 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -040010777 proc.thread.scheduleCreateBackupAgent(app,
10778 compatibilityInfoForPackageLocked(app), backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -070010779 } catch (RemoteException e) {
Christopher Tate436344a2009-09-30 16:17:37 -070010780 // Will time out on the backup manager side
Christopher Tate181fafa2009-05-14 11:12:14 -070010781 }
10782 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010783 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc not running, waiting for attach");
Christopher Tate181fafa2009-05-14 11:12:14 -070010784 }
10785 // Invariants: at this point, the target app process exists and the application
10786 // is either already running or in the process of coming up. mBackupTarget and
10787 // mBackupAppName describe the app, so that when it binds back to the AM we
10788 // know that it's scheduled for a backup-agent operation.
10789 }
10790
10791 return true;
10792 }
10793
10794 // A backup agent has just come up
10795 public void backupAgentCreated(String agentPackageName, IBinder agent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010796 if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName
Christopher Tate181fafa2009-05-14 11:12:14 -070010797 + " = " + agent);
10798
10799 synchronized(this) {
10800 if (!agentPackageName.equals(mBackupAppName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010801 Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!");
Christopher Tate181fafa2009-05-14 11:12:14 -070010802 return;
10803 }
Dianne Hackborn06740692010-09-22 22:46:21 -070010804 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010805
Dianne Hackborn06740692010-09-22 22:46:21 -070010806 long oldIdent = Binder.clearCallingIdentity();
10807 try {
10808 IBackupManager bm = IBackupManager.Stub.asInterface(
10809 ServiceManager.getService(Context.BACKUP_SERVICE));
10810 bm.agentConnected(agentPackageName, agent);
10811 } catch (RemoteException e) {
10812 // can't happen; the backup manager service is local
10813 } catch (Exception e) {
10814 Slog.w(TAG, "Exception trying to deliver BackupAgent binding: ");
10815 e.printStackTrace();
10816 } finally {
10817 Binder.restoreCallingIdentity(oldIdent);
Christopher Tate181fafa2009-05-14 11:12:14 -070010818 }
10819 }
10820
10821 // done with this agent
10822 public void unbindBackupAgent(ApplicationInfo appInfo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010823 if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo);
Christopher Tate8a27f922009-06-26 11:49:18 -070010824 if (appInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010825 Slog.w(TAG, "unbind backup agent for null app");
Christopher Tate8a27f922009-06-26 11:49:18 -070010826 return;
10827 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010828
10829 synchronized(this) {
Christopher Tate8a27f922009-06-26 11:49:18 -070010830 if (mBackupAppName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010831 Slog.w(TAG, "Unbinding backup agent with no active backup");
Christopher Tate8a27f922009-06-26 11:49:18 -070010832 return;
10833 }
10834
Christopher Tate181fafa2009-05-14 11:12:14 -070010835 if (!mBackupAppName.equals(appInfo.packageName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010836 Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target");
Christopher Tate181fafa2009-05-14 11:12:14 -070010837 return;
10838 }
10839
Christopher Tate6fa95972009-06-05 18:43:55 -070010840 ProcessRecord proc = mBackupTarget.app;
10841 mBackupTarget = null;
10842 mBackupAppName = null;
10843
10844 // Not backing this app up any more; reset its OOM adjustment
10845 updateOomAdjLocked(proc);
10846
Christopher Tatec7b31e32009-06-10 15:49:30 -070010847 // If the app crashed during backup, 'thread' will be null here
10848 if (proc.thread != null) {
10849 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -040010850 proc.thread.scheduleDestroyBackupAgent(appInfo,
10851 compatibilityInfoForPackageLocked(appInfo));
Christopher Tatec7b31e32009-06-10 15:49:30 -070010852 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010853 Slog.e(TAG, "Exception when unbinding backup agent:");
Christopher Tatec7b31e32009-06-10 15:49:30 -070010854 e.printStackTrace();
10855 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010856 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010857 }
10858 }
10859 // =========================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860 // BROADCASTS
10861 // =========================================================
10862
Josh Bartel7f208742010-02-25 11:01:44 -060010863 private final List getStickiesLocked(String action, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010864 List cur) {
10865 final ContentResolver resolver = mContext.getContentResolver();
10866 final ArrayList<Intent> list = mStickyBroadcasts.get(action);
10867 if (list == null) {
10868 return cur;
10869 }
10870 int N = list.size();
10871 for (int i=0; i<N; i++) {
10872 Intent intent = list.get(i);
10873 if (filter.match(resolver, intent, true, TAG) >= 0) {
10874 if (cur == null) {
10875 cur = new ArrayList<Intent>();
10876 }
10877 cur.add(intent);
10878 }
10879 }
10880 return cur;
10881 }
10882
Christopher Tatef46723b2012-01-26 14:19:24 -080010883 boolean isPendingBroadcastProcessLocked(int pid) {
10884 return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid)
10885 || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid);
10886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010887
Christopher Tatef46723b2012-01-26 14:19:24 -080010888 void skipPendingBroadcastLocked(int pid) {
10889 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
10890 for (BroadcastQueue queue : mBroadcastQueues) {
10891 queue.skipPendingBroadcastLocked(pid);
10892 }
10893 }
10894
10895 // The app just attached; send any pending broadcasts that it should receive
10896 boolean sendPendingBroadcastsLocked(ProcessRecord app) {
10897 boolean didSomething = false;
10898 for (BroadcastQueue queue : mBroadcastQueues) {
10899 didSomething |= queue.sendPendingBroadcastsLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 }
Christopher Tatef46723b2012-01-26 14:19:24 -080010901 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010902 }
10903
Dianne Hackborn6c418d52011-06-29 14:05:33 -070010904 public Intent registerReceiver(IApplicationThread caller, String callerPackage,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010905 IIntentReceiver receiver, IntentFilter filter, String permission) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010906 enforceNotIsolatedCaller("registerReceiver");
Dianne Hackbornb4163a62012-08-02 18:31:26 -070010907 int callingUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010908 synchronized(this) {
10909 ProcessRecord callerApp = null;
10910 if (caller != null) {
10911 callerApp = getRecordForAppLocked(caller);
10912 if (callerApp == null) {
10913 throw new SecurityException(
10914 "Unable to find app for caller " + caller
10915 + " (pid=" + Binder.getCallingPid()
10916 + ") when registering receiver " + receiver);
10917 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -070010918 if (callerApp.info.uid != Process.SYSTEM_UID &&
10919 !callerApp.pkgList.contains(callerPackage)) {
10920 throw new SecurityException("Given caller package " + callerPackage
10921 + " is not running in process " + callerApp);
10922 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070010923 callingUid = callerApp.info.uid;
Dianne Hackborn6c418d52011-06-29 14:05:33 -070010924 } else {
10925 callerPackage = null;
Dianne Hackbornb4163a62012-08-02 18:31:26 -070010926 callingUid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010927 }
10928
10929 List allSticky = null;
10930
10931 // Look for any matching sticky broadcasts...
10932 Iterator actions = filter.actionsIterator();
10933 if (actions != null) {
10934 while (actions.hasNext()) {
10935 String action = (String)actions.next();
Josh Bartel7f208742010-02-25 11:01:44 -060010936 allSticky = getStickiesLocked(action, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010937 }
10938 } else {
Josh Bartel7f208742010-02-25 11:01:44 -060010939 allSticky = getStickiesLocked(null, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010940 }
10941
10942 // The first sticky in the list is returned directly back to
10943 // the client.
10944 Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null;
10945
Joe Onorato8a9b2202010-02-26 18:56:32 -080010946 if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010947 + ": " + sticky);
10948
10949 if (receiver == null) {
10950 return sticky;
10951 }
10952
10953 ReceiverList rl
10954 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
10955 if (rl == null) {
10956 rl = new ReceiverList(this, callerApp,
10957 Binder.getCallingPid(),
10958 Binder.getCallingUid(), receiver);
10959 if (rl.app != null) {
10960 rl.app.receivers.add(rl);
10961 } else {
10962 try {
10963 receiver.asBinder().linkToDeath(rl, 0);
10964 } catch (RemoteException e) {
10965 return sticky;
10966 }
10967 rl.linkedToDeath = true;
10968 }
10969 mRegisteredReceivers.put(receiver.asBinder(), rl);
10970 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070010971 BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,
10972 permission, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010973 rl.add(bf);
10974 if (!bf.debugCheck()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010975 Slog.w(TAG, "==> For Dynamic broadast");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010976 }
10977 mReceiverResolver.addFilter(bf);
10978
10979 // Enqueue broadcasts for all existing stickies that match
10980 // this filter.
10981 if (allSticky != null) {
10982 ArrayList receivers = new ArrayList();
10983 receivers.add(bf);
10984
10985 int N = allSticky.size();
10986 for (int i=0; i<N; i++) {
10987 Intent intent = (Intent)allSticky.get(i);
Christopher Tatef46723b2012-01-26 14:19:24 -080010988 BroadcastQueue queue = broadcastQueueForIntent(intent);
10989 BroadcastRecord r = new BroadcastRecord(queue, intent, null,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010990 null, -1, -1, null, receivers, null, 0, null, null,
Dianne Hackborn786b4402012-08-27 15:14:02 -070010991 false, true, true, false, -1);
Christopher Tatef46723b2012-01-26 14:19:24 -080010992 queue.enqueueParallelBroadcastLocked(r);
10993 queue.scheduleBroadcastsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010994 }
10995 }
10996
10997 return sticky;
10998 }
10999 }
11000
11001 public void unregisterReceiver(IIntentReceiver receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011002 if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003
Christopher Tatef46723b2012-01-26 14:19:24 -080011004 final long origId = Binder.clearCallingIdentity();
11005 try {
11006 boolean doTrim = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007
Christopher Tatef46723b2012-01-26 14:19:24 -080011008 synchronized(this) {
11009 ReceiverList rl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011010 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
Christopher Tatef46723b2012-01-26 14:19:24 -080011011 if (rl != null) {
11012 if (rl.curBroadcast != null) {
11013 BroadcastRecord r = rl.curBroadcast;
11014 final boolean doNext = finishReceiverLocked(
11015 receiver.asBinder(), r.resultCode, r.resultData,
11016 r.resultExtras, r.resultAbort, true);
11017 if (doNext) {
11018 doTrim = true;
11019 r.queue.processNextBroadcast(false);
11020 }
11021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022
Christopher Tatef46723b2012-01-26 14:19:24 -080011023 if (rl.app != null) {
11024 rl.app.receivers.remove(rl);
11025 }
11026 removeReceiverLocked(rl);
11027 if (rl.linkedToDeath) {
11028 rl.linkedToDeath = false;
11029 rl.receiver.asBinder().unlinkToDeath(rl, 0);
11030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011031 }
11032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011033
Christopher Tatef46723b2012-01-26 14:19:24 -080011034 // If we actually concluded any broadcasts, we might now be able
11035 // to trim the recipients' apps from our working set
11036 if (doTrim) {
11037 trimApplications();
11038 return;
11039 }
11040
11041 } finally {
11042 Binder.restoreCallingIdentity(origId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011044 }
11045
11046 void removeReceiverLocked(ReceiverList rl) {
11047 mRegisteredReceivers.remove(rl.receiver.asBinder());
11048 int N = rl.size();
11049 for (int i=0; i<N; i++) {
11050 mReceiverResolver.removeFilter(rl.get(i));
11051 }
11052 }
11053
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011054 private final void sendPackageBroadcastLocked(int cmd, String[] packages) {
11055 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
11056 ProcessRecord r = mLruProcesses.get(i);
11057 if (r.thread != null) {
11058 try {
11059 r.thread.dispatchPackageBroadcast(cmd, packages);
11060 } catch (RemoteException ex) {
11061 }
11062 }
11063 }
11064 }
11065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 private final int broadcastIntentLocked(ProcessRecord callerApp,
11067 String callerPackage, Intent intent, String resolvedType,
11068 IIntentReceiver resultTo, int resultCode, String resultData,
11069 Bundle map, String requiredPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -070011070 boolean ordered, boolean sticky, int callingPid, int callingUid,
11071 int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011072 intent = new Intent(intent);
11073
Dianne Hackborne7f97212011-02-24 14:40:20 -080011074 // By default broadcasts do not go to stopped apps.
11075 intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
11076
Joe Onorato8a9b2202010-02-26 18:56:32 -080011077 if (DEBUG_BROADCAST_LIGHT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011078 TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent
Amith Yamasani13593602012-03-22 16:16:17 -070011079 + " ordered=" + ordered + " userid=" + userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011080 if ((resultTo != null) && !ordered) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011081 Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011082 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011083
11084 boolean onlySendToCaller = false;
11085
11086 // If the caller is trying to send this broadcast to a different
11087 // user, verify that is allowed.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011088 if (UserHandle.getUserId(callingUid) != userId) {
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011089 if (checkComponentPermission(
11090 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
11091 callingPid, callingUid, -1, true)
11092 != PackageManager.PERMISSION_GRANTED) {
11093 if (checkComponentPermission(
11094 android.Manifest.permission.INTERACT_ACROSS_USERS,
11095 callingPid, callingUid, -1, true)
11096 == PackageManager.PERMISSION_GRANTED) {
11097 onlySendToCaller = true;
11098 } else {
11099 String msg = "Permission Denial: " + intent.getAction()
11100 + " broadcast from " + callerPackage
11101 + " asks to send as user " + userId
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011102 + " but is calling from user " + UserHandle.getUserId(callingUid)
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011103 + "; this requires "
11104 + android.Manifest.permission.INTERACT_ACROSS_USERS;
11105 Slog.w(TAG, msg);
11106 throw new SecurityException(msg);
11107 }
11108 }
11109 }
11110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 // Handle special intents: if this broadcast is from the package
11112 // manager about a package being removed, we need to remove all of
11113 // its activities from the history stack.
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011114 final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 intent.getAction());
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011116 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
11117 || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080011118 || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011119 || uidRemoved) {
11120 if (checkComponentPermission(
11121 android.Manifest.permission.BROADCAST_PACKAGE_REMOVED,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -080011122 callingPid, callingUid, -1, true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011123 == PackageManager.PERMISSION_GRANTED) {
11124 if (uidRemoved) {
11125 final Bundle intentExtras = intent.getExtras();
11126 final int uid = intentExtras != null
11127 ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
11128 if (uid >= 0) {
11129 BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
11130 synchronized (bs) {
11131 bs.removeUidStatsLocked(uid);
11132 }
11133 }
11134 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011135 // If resources are unvailble just force stop all
11136 // those packages and flush the attribute cache as well.
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080011137 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011138 String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
11139 if (list != null && (list.length > 0)) {
11140 for (String pkg : list) {
Amith Yamasani483f3b02012-03-13 16:08:00 -070011141 forceStopPackageLocked(pkg, -1, false, true, true, false, userId);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011142 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011143 sendPackageBroadcastLocked(
11144 IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011145 }
11146 } else {
11147 Uri data = intent.getData();
11148 String ssp;
11149 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
11150 if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
11151 forceStopPackageLocked(ssp,
Amith Yamasani483f3b02012-03-13 16:08:00 -070011152 intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true,
11153 false, userId);
Dianne Hackbornde7faf62009-06-30 13:27:30 -070011154 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011155 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011156 sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,
11157 new String[] {ssp});
11158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159 }
11160 }
11161 }
11162 } else {
11163 String msg = "Permission Denial: " + intent.getAction()
11164 + " broadcast from " + callerPackage + " (pid=" + callingPid
11165 + ", uid=" + callingUid + ")"
11166 + " requires "
11167 + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011168 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011169 throw new SecurityException(msg);
11170 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011171
11172 // Special case for adding a package: by default turn on compatibility
11173 // mode.
11174 } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070011175 Uri data = intent.getData();
11176 String ssp;
11177 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
11178 mCompatModePackages.handlePackageAddedLocked(ssp,
11179 intent.getBooleanExtra(Intent.EXTRA_REPLACING, false));
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181 }
11182
11183 /*
11184 * If this is the time zone changed action, queue up a message that will reset the timezone
11185 * of all currently running processes. This message will get queued up before the broadcast
11186 * happens.
11187 */
11188 if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {
11189 mHandler.sendEmptyMessage(UPDATE_TIME_ZONE);
11190 }
11191
Robert Greenwalt03595d02010-11-02 14:08:23 -070011192 if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) {
11193 mHandler.sendEmptyMessage(CLEAR_DNS_CACHE);
11194 }
11195
Robert Greenwalt434203a2010-10-11 16:00:27 -070011196 if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) {
11197 ProxyProperties proxy = intent.getParcelableExtra("proxy");
11198 mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy));
11199 }
11200
Dianne Hackborn854060af2009-07-09 18:14:31 -070011201 /*
11202 * Prevent non-system code (defined here to be non-persistent
11203 * processes) from sending protected broadcasts.
11204 */
11205 if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID
fredc0f420372012-04-12 00:02:00 -070011206 || callingUid == Process.SHELL_UID || callingUid == Process.BLUETOOTH_UID ||
11207 callingUid == 0) {
Dianne Hackborn854060af2009-07-09 18:14:31 -070011208 // Always okay.
11209 } else if (callerApp == null || !callerApp.persistent) {
11210 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011211 if (AppGlobals.getPackageManager().isProtectedBroadcast(
Dianne Hackborn854060af2009-07-09 18:14:31 -070011212 intent.getAction())) {
11213 String msg = "Permission Denial: not allowed to send broadcast "
11214 + intent.getAction() + " from pid="
11215 + callingPid + ", uid=" + callingUid;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011216 Slog.w(TAG, msg);
Dianne Hackborn854060af2009-07-09 18:14:31 -070011217 throw new SecurityException(msg);
11218 }
11219 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011220 Slog.w(TAG, "Remote exception", e);
Dianne Hackborna4972e92012-03-14 10:38:05 -070011221 return ActivityManager.BROADCAST_SUCCESS;
Dianne Hackborn854060af2009-07-09 18:14:31 -070011222 }
11223 }
11224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 // Add to the sticky list if requested.
11226 if (sticky) {
11227 if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,
11228 callingPid, callingUid)
11229 != PackageManager.PERMISSION_GRANTED) {
11230 String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid="
11231 + callingPid + ", uid=" + callingUid
11232 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011233 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011234 throw new SecurityException(msg);
11235 }
11236 if (requiredPermission != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011237 Slog.w(TAG, "Can't broadcast sticky intent " + intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011238 + " and enforce permission " + requiredPermission);
Dianne Hackborna4972e92012-03-14 10:38:05 -070011239 return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011240 }
11241 if (intent.getComponent() != null) {
11242 throw new SecurityException(
11243 "Sticky broadcasts can't target a specific component");
11244 }
11245 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
11246 if (list == null) {
11247 list = new ArrayList<Intent>();
11248 mStickyBroadcasts.put(intent.getAction(), list);
11249 }
11250 int N = list.size();
11251 int i;
11252 for (i=0; i<N; i++) {
11253 if (intent.filterEquals(list.get(i))) {
11254 // This sticky already exists, replace it.
11255 list.set(i, new Intent(intent));
11256 break;
11257 }
11258 }
11259 if (i >= N) {
11260 list.add(new Intent(intent));
11261 }
11262 }
11263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011264 // Figure out who all will receive this broadcast.
11265 List receivers = null;
11266 List<BroadcastFilter> registeredReceivers = null;
11267 try {
Dianne Hackborn7d19e022012-08-07 19:12:33 -070011268 // Need to resolve the intent to interested receivers...
11269 if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)
11270 == 0) {
11271 receivers = AppGlobals.getPackageManager().queryIntentReceivers(
11272 intent, resolvedType, STOCK_PM_FLAGS, userId);
11273 }
11274 if (intent.getComponent() == null) {
11275 registeredReceivers = mReceiverResolver.queryIntent(intent,
11276 resolvedType, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011277 }
11278 } catch (RemoteException ex) {
11279 // pm is in same process, this will never happen.
11280 }
11281
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011282 final boolean replacePending =
11283 (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;
11284
Joe Onorato8a9b2202010-02-26 18:56:32 -080011285 if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction()
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011286 + " replacePending=" + replacePending);
11287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011288 int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
11289 if (!ordered && NR > 0) {
11290 // If we are not serializing this broadcast, then send the
11291 // registered receivers separately so they don't wait for the
11292 // components to be launched.
Christopher Tatef46723b2012-01-26 14:19:24 -080011293 final BroadcastQueue queue = broadcastQueueForIntent(intent);
11294 BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011295 callerPackage, callingPid, callingUid, requiredPermission,
11296 registeredReceivers, resultTo, resultCode, resultData, map,
Dianne Hackborn786b4402012-08-27 15:14:02 -070011297 ordered, sticky, false, onlySendToCaller, userId);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011298 if (DEBUG_BROADCAST) Slog.v(
Christopher Tatef46723b2012-01-26 14:19:24 -080011299 TAG, "Enqueueing parallel broadcast " + r);
11300 final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011301 if (!replaced) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011302 queue.enqueueParallelBroadcastLocked(r);
11303 queue.scheduleBroadcastsLocked();
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011305 registeredReceivers = null;
11306 NR = 0;
11307 }
11308
11309 // Merge into one list.
11310 int ir = 0;
11311 if (receivers != null) {
11312 // A special case for PACKAGE_ADDED: do not allow the package
11313 // being added to see this broadcast. This prevents them from
11314 // using this as a back door to get run as soon as they are
11315 // installed. Maybe in the future we want to have a special install
11316 // broadcast or such for apps, but we'd like to deliberately make
11317 // this decision.
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011318 String skipPackages[] = null;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011319 if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())
11320 || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())
11321 || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011322 Uri data = intent.getData();
11323 if (data != null) {
11324 String pkgName = data.getSchemeSpecificPart();
11325 if (pkgName != null) {
11326 skipPackages = new String[] { pkgName };
11327 }
11328 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011329 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011330 skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
The Android Open Source Project10592532009-03-18 17:39:46 -070011331 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011332 if (skipPackages != null && (skipPackages.length > 0)) {
11333 for (String skipPackage : skipPackages) {
11334 if (skipPackage != null) {
11335 int NT = receivers.size();
11336 for (int it=0; it<NT; it++) {
11337 ResolveInfo curt = (ResolveInfo)receivers.get(it);
11338 if (curt.activityInfo.packageName.equals(skipPackage)) {
11339 receivers.remove(it);
11340 it--;
11341 NT--;
11342 }
11343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011344 }
11345 }
11346 }
11347
11348 int NT = receivers != null ? receivers.size() : 0;
11349 int it = 0;
11350 ResolveInfo curt = null;
11351 BroadcastFilter curr = null;
11352 while (it < NT && ir < NR) {
11353 if (curt == null) {
11354 curt = (ResolveInfo)receivers.get(it);
11355 }
11356 if (curr == null) {
11357 curr = registeredReceivers.get(ir);
11358 }
11359 if (curr.getPriority() >= curt.priority) {
11360 // Insert this broadcast record into the final list.
11361 receivers.add(it, curr);
11362 ir++;
11363 curr = null;
11364 it++;
11365 NT++;
11366 } else {
11367 // Skip to the next ResolveInfo in the final list.
11368 it++;
11369 curt = null;
11370 }
11371 }
11372 }
11373 while (ir < NR) {
11374 if (receivers == null) {
11375 receivers = new ArrayList();
11376 }
11377 receivers.add(registeredReceivers.get(ir));
11378 ir++;
11379 }
11380
11381 if ((receivers != null && receivers.size() > 0)
11382 || resultTo != null) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011383 BroadcastQueue queue = broadcastQueueForIntent(intent);
11384 BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011385 callerPackage, callingPid, callingUid, requiredPermission,
Dianne Hackborn12527f92009-11-11 17:39:50 -080011386 receivers, resultTo, resultCode, resultData, map, ordered,
Dianne Hackborn786b4402012-08-27 15:14:02 -070011387 sticky, false, onlySendToCaller, userId);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011388 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011389 TAG, "Enqueueing ordered broadcast " + r
Christopher Tatef46723b2012-01-26 14:19:24 -080011390 + ": prev had " + queue.mOrderedBroadcasts.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011391 if (DEBUG_BROADCAST) {
11392 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011393 Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011394 }
Christopher Tatef46723b2012-01-26 14:19:24 -080011395 boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011396 if (!replaced) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011397 queue.enqueueOrderedBroadcastLocked(r);
11398 queue.scheduleBroadcastsLocked();
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011400 }
11401
Dianne Hackborna4972e92012-03-14 10:38:05 -070011402 return ActivityManager.BROADCAST_SUCCESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011403 }
11404
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011405 final Intent verifyBroadcastLocked(Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011406 // Refuse possible leaked file descriptors
11407 if (intent != null && intent.hasFileDescriptors() == true) {
11408 throw new IllegalArgumentException("File descriptors passed in Intent");
11409 }
11410
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011411 int flags = intent.getFlags();
11412
11413 if (!mProcessesReady) {
11414 // if the caller really truly claims to know what they're doing, go
11415 // ahead and allow the broadcast without launching any receivers
11416 if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) {
11417 intent = new Intent(intent);
11418 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
11419 } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
11420 Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent
11421 + " before boot completion");
11422 throw new IllegalStateException("Cannot broadcast before boot completed");
11423 }
11424 }
11425
11426 if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
11427 throw new IllegalArgumentException(
11428 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
11429 }
11430
11431 return intent;
11432 }
11433
11434 public final int broadcastIntent(IApplicationThread caller,
11435 Intent intent, String resolvedType, IIntentReceiver resultTo,
11436 int resultCode, String resultData, Bundle map,
Amith Yamasani742a6712011-05-04 14:49:28 -070011437 String requiredPermission, boolean serialized, boolean sticky, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011438 enforceNotIsolatedCaller("broadcastIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439 synchronized(this) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011440 intent = verifyBroadcastLocked(intent);
Dianne Hackborn9acc0302009-08-25 00:27:12 -070011441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011442 final ProcessRecord callerApp = getRecordForAppLocked(caller);
11443 final int callingPid = Binder.getCallingPid();
11444 final int callingUid = Binder.getCallingUid();
11445 final long origId = Binder.clearCallingIdentity();
11446 int res = broadcastIntentLocked(callerApp,
11447 callerApp != null ? callerApp.info.packageName : null,
11448 intent, resolvedType, resultTo,
Amith Yamasani742a6712011-05-04 14:49:28 -070011449 resultCode, resultData, map, requiredPermission, serialized, sticky,
11450 callingPid, callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011451 Binder.restoreCallingIdentity(origId);
11452 return res;
11453 }
11454 }
11455
11456 int broadcastIntentInPackage(String packageName, int uid,
11457 Intent intent, String resolvedType, IIntentReceiver resultTo,
11458 int resultCode, String resultData, Bundle map,
Amith Yamasani742a6712011-05-04 14:49:28 -070011459 String requiredPermission, boolean serialized, boolean sticky, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011460 synchronized(this) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011461 intent = verifyBroadcastLocked(intent);
11462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011463 final long origId = Binder.clearCallingIdentity();
11464 int res = broadcastIntentLocked(null, packageName, intent, resolvedType,
11465 resultTo, resultCode, resultData, map, requiredPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -070011466 serialized, sticky, -1, uid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011467 Binder.restoreCallingIdentity(origId);
11468 return res;
11469 }
11470 }
11471
Amith Yamasani742a6712011-05-04 14:49:28 -070011472 // TODO: Use the userId; maybe mStickyBroadcasts need to be tied to the user.
11473 public final void unbroadcastIntent(IApplicationThread caller, Intent intent, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011474 // Refuse possible leaked file descriptors
11475 if (intent != null && intent.hasFileDescriptors() == true) {
11476 throw new IllegalArgumentException("File descriptors passed in Intent");
11477 }
11478
11479 synchronized(this) {
11480 if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY)
11481 != PackageManager.PERMISSION_GRANTED) {
11482 String msg = "Permission Denial: unbroadcastIntent() from pid="
11483 + Binder.getCallingPid()
11484 + ", uid=" + Binder.getCallingUid()
11485 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011486 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011487 throw new SecurityException(msg);
11488 }
11489 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
11490 if (list != null) {
11491 int N = list.size();
11492 int i;
11493 for (i=0; i<N; i++) {
11494 if (intent.filterEquals(list.get(i))) {
11495 list.remove(i);
11496 break;
11497 }
11498 }
11499 }
11500 }
11501 }
11502
11503 private final boolean finishReceiverLocked(IBinder receiver, int resultCode,
11504 String resultData, Bundle resultExtras, boolean resultAbort,
11505 boolean explicit) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011506 final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver);
11507 if (r == null) {
11508 Slog.w(TAG, "finishReceiver called but not found on queue");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011509 return false;
11510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511
Christopher Tatef46723b2012-01-26 14:19:24 -080011512 return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort,
11513 explicit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011514 }
11515
11516 public void finishReceiver(IBinder who, int resultCode, String resultData,
11517 Bundle resultExtras, boolean resultAbort) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011518 if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011519
11520 // Refuse possible leaked file descriptors
11521 if (resultExtras != null && resultExtras.hasFileDescriptors()) {
11522 throw new IllegalArgumentException("File descriptors passed in Bundle");
11523 }
11524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011525 final long origId = Binder.clearCallingIdentity();
Christopher Tatef46723b2012-01-26 14:19:24 -080011526 try {
11527 boolean doNext = false;
11528 BroadcastRecord r = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011529
Christopher Tatef46723b2012-01-26 14:19:24 -080011530 synchronized(this) {
11531 r = broadcastRecordForReceiverLocked(who);
11532 if (r != null) {
11533 doNext = r.queue.finishReceiverLocked(r, resultCode,
11534 resultData, resultExtras, resultAbort, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011536 }
Jeff Brown4d94a762010-09-23 11:33:28 -070011537
Christopher Tatef46723b2012-01-26 14:19:24 -080011538 if (doNext) {
11539 r.queue.processNextBroadcast(false);
11540 }
11541 trimApplications();
11542 } finally {
11543 Binder.restoreCallingIdentity(origId);
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011547 // =========================================================
11548 // INSTRUMENTATION
11549 // =========================================================
11550
11551 public boolean startInstrumentation(ComponentName className,
11552 String profileFile, int flags, Bundle arguments,
11553 IInstrumentationWatcher watcher) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011554 enforceNotIsolatedCaller("startInstrumentation");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011555 // Refuse possible leaked file descriptors
11556 if (arguments != null && arguments.hasFileDescriptors()) {
11557 throw new IllegalArgumentException("File descriptors passed in Bundle");
11558 }
11559
11560 synchronized(this) {
11561 InstrumentationInfo ii = null;
11562 ApplicationInfo ai = null;
11563 try {
11564 ii = mContext.getPackageManager().getInstrumentationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070011565 className, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011566 ai = mContext.getPackageManager().getApplicationInfo(
Amith Yamasani483f3b02012-03-13 16:08:00 -070011567 ii.targetPackage, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011568 } catch (PackageManager.NameNotFoundException e) {
11569 }
11570 if (ii == null) {
11571 reportStartInstrumentationFailure(watcher, className,
11572 "Unable to find instrumentation info for: " + className);
11573 return false;
11574 }
11575 if (ai == null) {
11576 reportStartInstrumentationFailure(watcher, className,
11577 "Unable to find instrumentation target package: " + ii.targetPackage);
11578 return false;
11579 }
11580
11581 int match = mContext.getPackageManager().checkSignatures(
11582 ii.targetPackage, ii.packageName);
11583 if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) {
11584 String msg = "Permission Denial: starting instrumentation "
11585 + className + " from pid="
11586 + Binder.getCallingPid()
11587 + ", uid=" + Binder.getCallingPid()
11588 + " not allowed because package " + ii.packageName
11589 + " does not have a signature matching the target "
11590 + ii.targetPackage;
11591 reportStartInstrumentationFailure(watcher, className, msg);
11592 throw new SecurityException(msg);
11593 }
11594
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011595 int userId = UserHandle.getCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011596 final long origId = Binder.clearCallingIdentity();
Christopher Tate3dacd842011-08-19 14:56:15 -070011597 // Instrumentation can kill and relaunch even persistent processes
Amith Yamasani483f3b02012-03-13 16:08:00 -070011598 forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId);
Dianne Hackborna0c283e2012-02-09 10:47:01 -080011599 ProcessRecord app = addAppLocked(ai, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011600 app.instrumentationClass = className;
Dianne Hackborn1655be42009-05-08 14:29:01 -070011601 app.instrumentationInfo = ai;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602 app.instrumentationProfileFile = profileFile;
11603 app.instrumentationArguments = arguments;
11604 app.instrumentationWatcher = watcher;
11605 app.instrumentationResultClass = className;
11606 Binder.restoreCallingIdentity(origId);
11607 }
11608
11609 return true;
11610 }
11611
11612 /**
11613 * Report errors that occur while attempting to start Instrumentation. Always writes the
11614 * error to the logs, but if somebody is watching, send the report there too. This enables
11615 * the "am" command to report errors with more information.
11616 *
11617 * @param watcher The IInstrumentationWatcher. Null if there isn't one.
11618 * @param cn The component name of the instrumentation.
11619 * @param report The error report.
11620 */
11621 private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher,
11622 ComponentName cn, String report) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011623 Slog.w(TAG, report);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011624 try {
11625 if (watcher != null) {
11626 Bundle results = new Bundle();
11627 results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService");
11628 results.putString("Error", report);
11629 watcher.instrumentationStatus(cn, -1, results);
11630 }
11631 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011632 Slog.w(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011633 }
11634 }
11635
11636 void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) {
11637 if (app.instrumentationWatcher != null) {
11638 try {
11639 // NOTE: IInstrumentationWatcher *must* be oneway here
11640 app.instrumentationWatcher.instrumentationFinished(
11641 app.instrumentationClass,
11642 resultCode,
11643 results);
11644 } catch (RemoteException e) {
11645 }
11646 }
11647 app.instrumentationWatcher = null;
11648 app.instrumentationClass = null;
Dianne Hackborn1655be42009-05-08 14:29:01 -070011649 app.instrumentationInfo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011650 app.instrumentationProfileFile = null;
11651 app.instrumentationArguments = null;
11652
Amith Yamasani483f3b02012-03-13 16:08:00 -070011653 forceStopPackageLocked(app.processName, -1, false, false, true, true, app.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011654 }
11655
11656 public void finishInstrumentation(IApplicationThread target,
11657 int resultCode, Bundle results) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011658 int userId = UserHandle.getCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011659 // Refuse possible leaked file descriptors
11660 if (results != null && results.hasFileDescriptors()) {
11661 throw new IllegalArgumentException("File descriptors passed in Intent");
11662 }
11663
11664 synchronized(this) {
11665 ProcessRecord app = getRecordForAppLocked(target);
11666 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011667 Slog.w(TAG, "finishInstrumentation: no app for " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 return;
11669 }
11670 final long origId = Binder.clearCallingIdentity();
11671 finishInstrumentationLocked(app, resultCode, results);
11672 Binder.restoreCallingIdentity(origId);
11673 }
11674 }
11675
11676 // =========================================================
11677 // CONFIGURATION
11678 // =========================================================
11679
11680 public ConfigurationInfo getDeviceConfigurationInfo() {
11681 ConfigurationInfo config = new ConfigurationInfo();
11682 synchronized (this) {
11683 config.reqTouchScreen = mConfiguration.touchscreen;
11684 config.reqKeyboardType = mConfiguration.keyboard;
11685 config.reqNavigation = mConfiguration.navigation;
Dianne Hackbornfae76f52009-07-16 13:41:23 -070011686 if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD
11687 || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011688 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
11689 }
Dianne Hackbornfae76f52009-07-16 13:41:23 -070011690 if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED
11691 && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011692 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
11693 }
Jack Palevichb90d28c2009-07-22 15:35:24 -070011694 config.reqGlEsVersion = GL_ES_VERSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 }
11696 return config;
11697 }
11698
11699 public Configuration getConfiguration() {
11700 Configuration ci;
11701 synchronized(this) {
11702 ci = new Configuration(mConfiguration);
11703 }
11704 return ci;
11705 }
11706
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011707 public void updatePersistentConfiguration(Configuration values) {
11708 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
11709 "updateConfiguration()");
11710 enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS,
11711 "updateConfiguration()");
11712 if (values == null) {
11713 throw new NullPointerException("Configuration must not be null");
11714 }
11715
11716 synchronized(this) {
11717 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn813075a62011-11-14 17:45:19 -080011718 updateConfigurationLocked(values, null, true, false);
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011719 Binder.restoreCallingIdentity(origId);
11720 }
11721 }
11722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011723 public void updateConfiguration(Configuration values) {
11724 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
11725 "updateConfiguration()");
11726
11727 synchronized(this) {
11728 if (values == null && mWindowManager != null) {
11729 // sentinel: fetch the current configuration from the window manager
11730 values = mWindowManager.computeNewConfiguration();
11731 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070011732
11733 if (mWindowManager != null) {
11734 mProcessList.applyDisplaySize(mWindowManager);
11735 }
11736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011737 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011738 if (values != null) {
11739 Settings.System.clearConfiguration(values);
11740 }
Dianne Hackborn813075a62011-11-14 17:45:19 -080011741 updateConfigurationLocked(values, null, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742 Binder.restoreCallingIdentity(origId);
11743 }
11744 }
11745
11746 /**
11747 * Do either or both things: (1) change the current configuration, and (2)
11748 * make sure the given activity is running with the (now) current
11749 * configuration. Returns true if the activity has been left running, or
11750 * false if <var>starting</var> is being destroyed to match the new
11751 * configuration.
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011752 * @param persistent TODO
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011753 */
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011754 boolean updateConfigurationLocked(Configuration values,
Dianne Hackborn813075a62011-11-14 17:45:19 -080011755 ActivityRecord starting, boolean persistent, boolean initLocale) {
Mike Lockwood3a74bd32011-08-12 13:55:22 -070011756 // do nothing if we are headless
11757 if (mHeadless) return true;
11758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011759 int changes = 0;
11760
11761 boolean kept = true;
11762
11763 if (values != null) {
11764 Configuration newConfig = new Configuration(mConfiguration);
11765 changes = newConfig.updateFrom(values);
11766 if (changes != 0) {
Dianne Hackborndc6b6352009-09-30 14:20:09 -070011767 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011768 Slog.i(TAG, "Updating configuration to: " + values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011769 }
11770
Doug Zongker2bec3d42009-12-04 12:52:44 -080011771 EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772
Dianne Hackborn813075a62011-11-14 17:45:19 -080011773 if (values.locale != null && !initLocale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011774 saveLocaleLocked(values.locale,
11775 !values.locale.equals(mConfiguration.locale),
11776 values.userSetLocale);
11777 }
11778
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011779 mConfigurationSeq++;
11780 if (mConfigurationSeq <= 0) {
11781 mConfigurationSeq = 1;
11782 }
11783 newConfig.seq = mConfigurationSeq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011784 mConfiguration = newConfig;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011785 Slog.i(TAG, "Config changed: " + newConfig);
Dianne Hackborn813075a62011-11-14 17:45:19 -080011786
11787 final Configuration configCopy = new Configuration(mConfiguration);
Joe Onorato54a4a412011-11-02 20:50:08 -070011788
11789 // TODO: If our config changes, should we auto dismiss any currently
11790 // showing dialogs?
11791 mShowDialogs = shouldShowDialogs(newConfig);
Dianne Hackborn813075a62011-11-14 17:45:19 -080011792
Dianne Hackborn826d17c2009-11-12 12:55:51 -080011793 AttributeCache ac = AttributeCache.instance();
11794 if (ac != null) {
Dianne Hackborn813075a62011-11-14 17:45:19 -080011795 ac.updateConfiguration(configCopy);
Dianne Hackborn826d17c2009-11-12 12:55:51 -080011796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011797
Dianne Hackborn2f0b1752011-05-31 17:59:49 -070011798 // Make sure all resources in our process are updated
11799 // right now, so that anyone who is going to retrieve
11800 // resource values after we return will be sure to get
11801 // the new ones. This is especially important during
11802 // boot, where the first config change needs to guarantee
11803 // all resources have that config before following boot
11804 // code is executed.
Dianne Hackborn813075a62011-11-14 17:45:19 -080011805 mSystemThread.applyConfigurationToResources(configCopy);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -070011806
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011807 if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080011808 Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG);
Dianne Hackborn813075a62011-11-14 17:45:19 -080011809 msg.obj = new Configuration(configCopy);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080011810 mHandler.sendMessage(msg);
11811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011812
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011813 for (int i=mLruProcesses.size()-1; i>=0; i--) {
11814 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011815 try {
11816 if (app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011817 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -070011818 + app.processName + " new config " + mConfiguration);
Dianne Hackborn813075a62011-11-14 17:45:19 -080011819 app.thread.scheduleConfigurationChanged(configCopy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011820 }
11821 } catch (Exception e) {
11822 }
11823 }
11824 Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011825 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
11826 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011827 broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -070011828 null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080011829 if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
11830 broadcastIntentLocked(null, null,
11831 new Intent(Intent.ACTION_LOCALE_CHANGED),
11832 null, null, 0, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -070011833 null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080011834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011835 }
11836 }
11837
11838 if (changes != 0 && starting == null) {
11839 // If the configuration changed, and the caller is not already
11840 // in the process of starting an activity, then find the top
11841 // activity to check if its configuration needs to change.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011842 starting = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011843 }
11844
11845 if (starting != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011846 kept = mMainStack.ensureActivityConfigurationLocked(starting, changes);
Dianne Hackborn5f4d6432010-12-21 20:40:11 -080011847 // And we need to make sure at this point that all other activities
11848 // are made visible with the correct configuration.
11849 mMainStack.ensureActivitiesVisibleLocked(starting, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850 }
11851
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011852 if (values != null && mWindowManager != null) {
11853 mWindowManager.setNewConfiguration(mConfiguration);
11854 }
11855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 return kept;
11857 }
Joe Onorato54a4a412011-11-02 20:50:08 -070011858
11859 /**
11860 * Decide based on the configuration whether we should shouw the ANR,
11861 * crash, etc dialogs. The idea is that if there is no affordnace to
11862 * press the on-screen buttons, we shouldn't show the dialog.
11863 *
11864 * A thought: SystemUI might also want to get told about this, the Power
11865 * dialog / global actions also might want different behaviors.
11866 */
11867 private static final boolean shouldShowDialogs(Configuration config) {
11868 return !(config.keyboard == Configuration.KEYBOARD_NOKEYS
11869 && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH);
11870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011871
11872 /**
11873 * Save the locale. You must be inside a synchronized (this) block.
11874 */
11875 private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) {
11876 if(isDiff) {
11877 SystemProperties.set("user.language", l.getLanguage());
11878 SystemProperties.set("user.region", l.getCountry());
11879 }
11880
11881 if(isPersist) {
11882 SystemProperties.set("persist.sys.language", l.getLanguage());
11883 SystemProperties.set("persist.sys.country", l.getCountry());
11884 SystemProperties.set("persist.sys.localevar", l.getVariant());
11885 }
11886 }
11887
Adam Powelldd8fab22012-03-22 17:47:27 -070011888 @Override
11889 public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) {
11890 ActivityRecord srec = ActivityRecord.forToken(token);
Adam Powellb71a5bc2012-04-24 14:20:57 -070011891 return srec != null && srec.task.affinity != null &&
11892 srec.task.affinity.equals(destAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -070011893 }
11894
11895 public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode,
11896 Intent resultData) {
11897 ComponentName dest = destIntent.getComponent();
11898
11899 synchronized (this) {
11900 ActivityRecord srec = ActivityRecord.forToken(token);
Adam Powellb71a5bc2012-04-24 14:20:57 -070011901 if (srec == null) {
11902 return false;
11903 }
Adam Powelldd8fab22012-03-22 17:47:27 -070011904 ArrayList<ActivityRecord> history = srec.stack.mHistory;
11905 final int start = history.indexOf(srec);
11906 if (start < 0) {
11907 // Current activity is not in history stack; do nothing.
11908 return false;
11909 }
11910 int finishTo = start - 1;
11911 ActivityRecord parent = null;
11912 boolean foundParentInTask = false;
11913 if (dest != null) {
11914 TaskRecord tr = srec.task;
11915 for (int i = start - 1; i >= 0; i--) {
11916 ActivityRecord r = history.get(i);
11917 if (tr != r.task) {
11918 // Couldn't find parent in the same task; stop at the one above this.
11919 // (Root of current task; in-app "home" behavior)
11920 // Always at least finish the current activity.
11921 finishTo = Math.min(start - 1, i + 1);
11922 parent = history.get(finishTo);
11923 break;
11924 } else if (r.info.packageName.equals(dest.getPackageName()) &&
11925 r.info.name.equals(dest.getClassName())) {
11926 finishTo = i;
11927 parent = r;
11928 foundParentInTask = true;
11929 break;
11930 }
11931 }
11932 }
11933
11934 if (mController != null) {
11935 ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
11936 if (next != null) {
11937 // ask watcher if this is allowed
11938 boolean resumeOK = true;
11939 try {
11940 resumeOK = mController.activityResuming(next.packageName);
11941 } catch (RemoteException e) {
11942 mController = null;
11943 }
11944
11945 if (!resumeOK) {
11946 return false;
11947 }
11948 }
11949 }
11950 final long origId = Binder.clearCallingIdentity();
11951 for (int i = start; i > finishTo; i--) {
11952 ActivityRecord r = history.get(i);
11953 mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData,
11954 "navigate-up");
11955 // Only return the supplied result for the first activity finished
11956 resultCode = Activity.RESULT_CANCELED;
11957 resultData = null;
11958 }
11959
11960 if (parent != null && foundParentInTask) {
11961 final int parentLaunchMode = parent.info.launchMode;
11962 final int destIntentFlags = destIntent.getFlags();
11963 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
11964 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
11965 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
11966 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Adam Powell69de7e12012-05-07 18:42:24 -070011967 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
Adam Powelldd8fab22012-03-22 17:47:27 -070011968 } else {
11969 try {
11970 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011971 destIntent.getComponent(), 0, UserHandle.getCallingUserId());
Adam Powelldd8fab22012-03-22 17:47:27 -070011972 int res = mMainStack.startActivityLocked(srec.app.thread, destIntent,
11973 null, aInfo, parent.appToken, null,
11974 0, -1, parent.launchedFromUid, 0, null, true, null);
11975 foundParentInTask = res == ActivityManager.START_SUCCESS;
11976 } catch (RemoteException e) {
11977 foundParentInTask = false;
11978 }
11979 mMainStack.requestFinishActivityLocked(parent.appToken, resultCode,
11980 resultData, "navigate-up");
11981 }
11982 }
11983 Binder.restoreCallingIdentity(origId);
11984 return foundParentInTask;
11985 }
11986 }
11987
Dianne Hackborn5320eb82012-05-18 12:05:04 -070011988 public int getLaunchedFromUid(IBinder activityToken) {
11989 ActivityRecord srec = ActivityRecord.forToken(activityToken);
11990 if (srec == null) {
11991 return -1;
11992 }
11993 return srec.launchedFromUid;
11994 }
11995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 // =========================================================
11997 // LIFETIME MANAGEMENT
11998 // =========================================================
11999
Christopher Tatef46723b2012-01-26 14:19:24 -080012000 // Returns which broadcast queue the app is the current [or imminent] receiver
12001 // on, or 'null' if the app is not an active broadcast recipient.
12002 private BroadcastQueue isReceivingBroadcast(ProcessRecord app) {
12003 BroadcastRecord r = app.curReceiver;
12004 if (r != null) {
12005 return r.queue;
12006 }
12007
12008 // It's not the current receiver, but it might be starting up to become one
12009 synchronized (this) {
12010 for (BroadcastQueue queue : mBroadcastQueues) {
12011 r = queue.mPendingBroadcast;
12012 if (r != null && r.curApp == app) {
12013 // found it; report which queue it's in
12014 return queue;
12015 }
12016 }
12017 }
12018
12019 return null;
12020 }
12021
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012022 private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012023 int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 if (mAdjSeq == app.adjSeq) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012025 // This adjustment has already been computed. If we are calling
12026 // from the top, we may have already computed our adjustment with
12027 // an earlier hidden adjustment that isn't really for us... if
12028 // so, use the new hidden adjustment.
12029 if (!recursed && app.hidden) {
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012030 app.curAdj = app.curRawAdj = app.nonStoppingAdj =
12031 app.hasActivities ? hiddenAdj : emptyAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012032 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012033 return app.curRawAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012034 }
12035
12036 if (app.thread == null) {
12037 app.adjSeq = mAdjSeq;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012038 app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012039 return (app.curAdj=app.curRawAdj=ProcessList.HIDDEN_APP_MAX_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012040 }
12041
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012042 app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN;
12043 app.adjSource = null;
12044 app.adjTarget = null;
12045 app.empty = false;
12046 app.hidden = false;
12047
12048 final int activitiesSize = app.activities.size();
12049
Dianne Hackborn7d608422011-08-07 16:24:18 -070012050 if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012051 // The max adjustment doesn't allow this app to be anything
12052 // below foreground, so it is not worth doing work for it.
12053 app.adjType = "fixed";
12054 app.adjSeq = mAdjSeq;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012055 app.curRawAdj = app.nonStoppingAdj = app.maxAdj;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012056 app.hasActivities = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012057 app.foregroundActivities = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012058 app.keeping = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012059 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012060 // System process can do UI, and when they do we want to have
12061 // them trim their memory after the user leaves the UI. To
12062 // facilitate this, here we need to determine whether or not it
12063 // is currently showing UI.
12064 app.systemNoUi = true;
12065 if (app == TOP_APP) {
12066 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012067 app.hasActivities = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012068 } else if (activitiesSize > 0) {
12069 for (int j = 0; j < activitiesSize; j++) {
12070 final ActivityRecord r = app.activities.get(j);
12071 if (r.visible) {
12072 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012073 }
12074 if (r.app == app) {
12075 app.hasActivities = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012076 }
12077 }
12078 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012079 return (app.curAdj=app.maxAdj);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012080 }
12081
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012082 app.keeping = false;
12083 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012084 app.hasActivities = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012085
The Android Open Source Project4df24232009-03-05 14:34:35 -080012086 // Determine the importance of the process, starting with most
12087 // important to least, and assign an appropriate OOM adjustment.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012088 int adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012089 int schedGroup;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012090 boolean foregroundActivities = false;
12091 boolean interesting = false;
Christopher Tatef46723b2012-01-26 14:19:24 -080012092 BroadcastQueue queue;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012093 if (app == TOP_APP) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012094 // The last app on the list is the foreground app.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012095 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012096 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012097 app.adjType = "top-activity";
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012098 foregroundActivities = true;
12099 interesting = true;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012100 app.hasActivities = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012101 } else if (app.instrumentationClass != null) {
12102 // Don't want to kill running instrumentation.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012103 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012104 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012105 app.adjType = "instrumentation";
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012106 interesting = true;
Christopher Tatef46723b2012-01-26 14:19:24 -080012107 } else if ((queue = isReceivingBroadcast(app)) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012108 // An app that is currently receiving a broadcast also
Christopher Tatef46723b2012-01-26 14:19:24 -080012109 // counts as being in the foreground for OOM killer purposes.
12110 // It's placed in a sched group based on the nature of the
12111 // broadcast as reflected by which queue it's active in.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012112 adj = ProcessList.FOREGROUND_APP_ADJ;
Christopher Tatef46723b2012-01-26 14:19:24 -080012113 schedGroup = (queue == mFgBroadcastQueue)
12114 ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012115 app.adjType = "broadcast";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012116 } else if (app.executingServices.size() > 0) {
12117 // An app that is currently executing a service callback also
12118 // counts as being in the foreground.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012119 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012120 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012121 app.adjType = "exec-service";
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012122 } else {
12123 // Assume process is hidden (has activities); we will correct
12124 // later if this is not the case.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012125 adj = hiddenAdj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012126 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012127 app.hidden = true;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012128 app.adjType = "bg-activities";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012129 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012130
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012131 boolean hasStoppingActivities = false;
12132
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012133 // Examine all activities if not already foreground.
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012134 if (!foregroundActivities && activitiesSize > 0) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012135 for (int j = 0; j < activitiesSize; j++) {
12136 final ActivityRecord r = app.activities.get(j);
12137 if (r.visible) {
12138 // App has a visible activity; only upgrade adjustment.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012139 if (adj > ProcessList.VISIBLE_APP_ADJ) {
12140 adj = ProcessList.VISIBLE_APP_ADJ;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012141 app.adjType = "visible";
12142 }
12143 schedGroup = Process.THREAD_GROUP_DEFAULT;
12144 app.hidden = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012145 app.hasActivities = true;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012146 foregroundActivities = true;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012147 break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012148 } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012149 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12150 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012151 app.adjType = "pausing";
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012152 }
Dianne Hackborn8bf0aa92011-11-29 13:54:43 -080012153 app.hidden = false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012154 foregroundActivities = true;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012155 } else if (r.state == ActivityState.STOPPING) {
12156 // We will apply the actual adjustment later, because
12157 // we want to allow this process to immediately go through
12158 // any memory trimming that is in effect.
12159 app.hidden = false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012160 foregroundActivities = true;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012161 hasStoppingActivities = true;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012162 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012163 if (r.app == app) {
12164 app.hasActivities = true;
12165 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012166 }
12167 }
12168
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012169 if (adj == hiddenAdj && !app.hasActivities) {
12170 // Whoops, this process is completely empty as far as we know
12171 // at this point.
12172 adj = emptyAdj;
12173 app.empty = true;
12174 app.adjType = "bg-empty";
12175 }
12176
Dianne Hackborn7d608422011-08-07 16:24:18 -070012177 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012178 if (app.foregroundServices) {
12179 // The user is aware of this app, so make it visible.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012180 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012181 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012182 app.adjType = "foreground-service";
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012183 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012184 } else if (app.forcingToForeground != null) {
12185 // The user is aware of this app, so make it visible.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012186 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012187 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012188 app.adjType = "force-foreground";
12189 app.adjSource = app.forcingToForeground;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012190 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012191 }
12192 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012193
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012194 if (app.foregroundServices) {
12195 interesting = true;
12196 }
12197
Dianne Hackborn7d608422011-08-07 16:24:18 -070012198 if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012199 // We don't want to kill the current heavy-weight process.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012200 adj = ProcessList.HEAVY_WEIGHT_APP_ADJ;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012201 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012202 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012203 app.adjType = "heavy";
12204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012205
Dianne Hackborn7d608422011-08-07 16:24:18 -070012206 if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012207 // This process is hosting what we currently consider to be the
12208 // home app, so we don't want to let it go into the background.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012209 adj = ProcessList.HOME_APP_ADJ;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012210 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012211 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012212 app.adjType = "home";
12213 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012214
Dianne Hackbornf35fe232011-11-01 19:25:20 -070012215 if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess
12216 && app.activities.size() > 0) {
12217 // This was the previous process that showed UI to the user.
12218 // We want to try to keep it around more aggressively, to give
12219 // a good experience around switching between two apps.
12220 adj = ProcessList.PREVIOUS_APP_ADJ;
12221 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
12222 app.hidden = false;
12223 app.adjType = "previous";
12224 }
12225
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012226 if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj
12227 + " reason=" + app.adjType);
12228
The Android Open Source Project4df24232009-03-05 14:34:35 -080012229 // By default, we use the computed adjustment. It may be changed if
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012230 // there are applications dependent on our services or providers, but
12231 // this gives us a baseline and makes sure we don't get into an
12232 // infinite recursion.
12233 app.adjSeq = mAdjSeq;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012234 app.curRawAdj = app.nonStoppingAdj = adj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012235
Christopher Tate6fa95972009-06-05 18:43:55 -070012236 if (mBackupTarget != null && app == mBackupTarget.app) {
12237 // If possible we want to avoid killing apps while they're being backed up
Dianne Hackborn7d608422011-08-07 16:24:18 -070012238 if (adj > ProcessList.BACKUP_APP_ADJ) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012239 if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app);
Dianne Hackborn7d608422011-08-07 16:24:18 -070012240 adj = ProcessList.BACKUP_APP_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012241 app.adjType = "backup";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012242 app.hidden = false;
Christopher Tate6fa95972009-06-05 18:43:55 -070012243 }
12244 }
12245
Dianne Hackborn7d608422011-08-07 16:24:18 -070012246 if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012247 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012248 final long now = SystemClock.uptimeMillis();
12249 // This process is more important if the top activity is
12250 // bound to the service.
Dianne Hackborn860755f2010-06-03 18:47:52 -070012251 Iterator<ServiceRecord> jt = app.services.iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012252 while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012253 ServiceRecord s = jt.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012254 if (s.startRequested) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012255 if (app.hasShownUi && app != mHomeProcess) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012256 // If this process has shown some UI, let it immediately
12257 // go to the LRU list because it may be pretty heavy with
12258 // UI stuff. We'll tag it with a label just to help
12259 // debug and understand what is going on.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012260 if (adj > ProcessList.SERVICE_ADJ) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012261 app.adjType = "started-bg-ui-services";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012262 }
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012263 } else {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070012264 if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012265 // This service has seen some activity within
12266 // recent memory, so we will keep its process ahead
12267 // of the background processes.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012268 if (adj > ProcessList.SERVICE_ADJ) {
12269 adj = ProcessList.SERVICE_ADJ;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012270 app.adjType = "started-services";
12271 app.hidden = false;
12272 }
12273 }
12274 // If we have let the service slide into the background
12275 // state, still have some text describing what it is doing
12276 // even though the service no longer has an impact.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012277 if (adj > ProcessList.SERVICE_ADJ) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012278 app.adjType = "started-bg-services";
12279 }
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080012280 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012281 // Don't kill this process because it is doing work; it
12282 // has said it is doing work.
12283 app.keeping = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012284 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070012285 if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012286 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012287 Iterator<ArrayList<ConnectionRecord>> kt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012288 = s.connections.values().iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012289 while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012290 ArrayList<ConnectionRecord> clist = kt.next();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012291 for (int i=0; i<clist.size() && adj > ProcessList.FOREGROUND_APP_ADJ; i++) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012292 // XXX should compute this based on the max of
12293 // all connected clients.
12294 ConnectionRecord cr = clist.get(i);
12295 if (cr.binding.client == app) {
12296 // Binding to ourself is not interesting.
12297 continue;
12298 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012299 if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012300 ProcessRecord client = cr.binding.client;
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012301 int clientAdj = adj;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012302 int myHiddenAdj = hiddenAdj;
12303 if (myHiddenAdj > client.hiddenAdj) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012304 if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012305 myHiddenAdj = client.hiddenAdj;
12306 } else {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012307 myHiddenAdj = ProcessList.VISIBLE_APP_ADJ;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012308 }
12309 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012310 int myEmptyAdj = emptyAdj;
12311 if (myEmptyAdj > client.emptyAdj) {
12312 if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) {
12313 myEmptyAdj = client.emptyAdj;
12314 } else {
12315 myEmptyAdj = ProcessList.VISIBLE_APP_ADJ;
12316 }
12317 }
12318 clientAdj = computeOomAdjLocked(client, myHiddenAdj,
12319 myEmptyAdj, TOP_APP, true, doingAll);
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012320 String adjType = null;
12321 if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) {
12322 // Not doing bind OOM management, so treat
12323 // this guy more like a started service.
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012324 if (app.hasShownUi && app != mHomeProcess) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012325 // If this process has shown some UI, let it immediately
12326 // go to the LRU list because it may be pretty heavy with
12327 // UI stuff. We'll tag it with a label just to help
12328 // debug and understand what is going on.
12329 if (adj > clientAdj) {
12330 adjType = "bound-bg-ui-services";
12331 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012332 app.hidden = false;
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012333 clientAdj = adj;
12334 } else {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070012335 if (now >= (s.lastActivity
12336 + ActiveServices.MAX_SERVICE_INACTIVITY)) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012337 // This service has not seen activity within
12338 // recent memory, so allow it to drop to the
12339 // LRU list if there is no other reason to keep
12340 // it around. We'll also tag it with a label just
12341 // to help debug and undertand what is going on.
12342 if (adj > clientAdj) {
12343 adjType = "bound-bg-services";
12344 }
12345 clientAdj = adj;
12346 }
12347 }
12348 }
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012349 if (adj > clientAdj) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012350 // If this process has recently shown UI, and
12351 // the process that is binding to it is less
12352 // important than being visible, then we don't
12353 // care about the binding as much as we care
12354 // about letting this process get into the LRU
12355 // list to be killed and restarted if needed for
12356 // memory.
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012357 if (app.hasShownUi && app != mHomeProcess
12358 && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012359 adjType = "bound-bg-ui-services";
12360 } else {
12361 if ((cr.flags&(Context.BIND_ABOVE_CLIENT
12362 |Context.BIND_IMPORTANT)) != 0) {
12363 adj = clientAdj;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -070012364 } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0
12365 && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ
12366 && adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12367 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
12368 } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012369 adj = clientAdj;
12370 } else {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -070012371 app.pendingUiClean = true;
12372 if (adj > ProcessList.VISIBLE_APP_ADJ) {
12373 adj = ProcessList.VISIBLE_APP_ADJ;
12374 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012375 }
12376 if (!client.hidden) {
12377 app.hidden = false;
12378 }
12379 if (client.keeping) {
12380 app.keeping = true;
12381 }
12382 adjType = "service";
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012383 }
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012384 }
12385 if (adjType != null) {
12386 app.adjType = adjType;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012387 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12388 .REASON_SERVICE_IN_USE;
12389 app.adjSource = cr.binding.client;
Dianne Hackborn905577f2011-09-07 18:31:28 -070012390 app.adjSourceOom = clientAdj;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012391 app.adjTarget = s.name;
12392 }
12393 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
12394 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
12395 schedGroup = Process.THREAD_GROUP_DEFAULT;
12396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012397 }
12398 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012399 if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) {
12400 ActivityRecord a = cr.activity;
Dianne Hackborn7d608422011-08-07 16:24:18 -070012401 if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ &&
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012402 (a.visible || a.state == ActivityState.RESUMED
12403 || a.state == ActivityState.PAUSING)) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012404 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012405 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
12406 schedGroup = Process.THREAD_GROUP_DEFAULT;
12407 }
12408 app.hidden = false;
12409 app.adjType = "service";
12410 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12411 .REASON_SERVICE_IN_USE;
12412 app.adjSource = a;
Dianne Hackborn905577f2011-09-07 18:31:28 -070012413 app.adjSourceOom = adj;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012414 app.adjTarget = s.name;
12415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012417 }
12418 }
12419 }
12420 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012421
Dianne Hackborn287952c2010-09-22 22:34:31 -070012422 // Finally, if this process has active services running in it, we
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012423 // would like to avoid killing it unless it would prevent the current
12424 // application from running. By default we put the process in
12425 // with the rest of the background processes; as we scan through
12426 // its services we may bump it up from there.
12427 if (adj > hiddenAdj) {
12428 adj = hiddenAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012429 app.hidden = false;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012430 app.adjType = "bg-services";
12431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012432 }
12433
Dianne Hackborn7d608422011-08-07 16:24:18 -070012434 if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012435 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012436 Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012437 while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012438 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012439 ContentProviderRecord cpr = jt.next();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012440 for (int i = cpr.connections.size()-1;
12441 i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
12442 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE);
12443 i--) {
12444 ContentProviderConnection conn = cpr.connections.get(i);
12445 ProcessRecord client = conn.client;
12446 if (client == app) {
12447 // Being our own client is not interesting.
12448 continue;
12449 }
12450 int myHiddenAdj = hiddenAdj;
12451 if (myHiddenAdj > client.hiddenAdj) {
12452 if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) {
12453 myHiddenAdj = client.hiddenAdj;
12454 } else {
12455 myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ;
The Android Open Source Project10592532009-03-18 17:39:46 -070012456 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012457 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012458 int myEmptyAdj = emptyAdj;
12459 if (myEmptyAdj > client.emptyAdj) {
12460 if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) {
12461 myEmptyAdj = client.emptyAdj;
12462 } else {
12463 myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ;
12464 }
12465 }
12466 int clientAdj = computeOomAdjLocked(client, myHiddenAdj,
12467 myEmptyAdj, TOP_APP, true, doingAll);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012468 if (adj > clientAdj) {
12469 if (app.hasShownUi && app != mHomeProcess
12470 && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12471 app.adjType = "bg-ui-provider";
12472 } else {
12473 adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ
12474 ? clientAdj : ProcessList.FOREGROUND_APP_ADJ;
12475 app.adjType = "provider";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012476 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012477 if (!client.hidden) {
12478 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012479 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012480 if (client.keeping) {
12481 app.keeping = true;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012482 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012483 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12484 .REASON_PROVIDER_IN_USE;
12485 app.adjSource = client;
12486 app.adjSourceOom = clientAdj;
12487 app.adjTarget = cpr.name;
12488 }
12489 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
12490 schedGroup = Process.THREAD_GROUP_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012491 }
12492 }
12493 // If the provider has external (non-framework) process
12494 // dependencies, ensure that its adjustment is at least
12495 // FOREGROUND_APP_ADJ.
Svetoslav Ganov25872aa2012-02-03 19:19:09 -080012496 if (cpr.hasExternalProcessHandles()) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012497 if (adj > ProcessList.FOREGROUND_APP_ADJ) {
12498 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012499 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012500 app.hidden = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012501 app.keeping = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012502 app.adjType = "provider";
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070012503 app.adjTarget = cpr.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012504 }
12505 }
12506 }
12507 }
12508
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012509 if (adj == ProcessList.SERVICE_ADJ) {
12510 if (doingAll) {
12511 app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3);
12512 mNewNumServiceProcs++;
12513 }
12514 if (app.serviceb) {
12515 adj = ProcessList.SERVICE_B_ADJ;
12516 }
12517 } else {
12518 app.serviceb = false;
12519 }
12520
12521 app.nonStoppingAdj = adj;
12522
12523 if (hasStoppingActivities) {
12524 // Only upgrade adjustment.
12525 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12526 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
12527 app.adjType = "stopping";
12528 }
12529 }
12530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 app.curRawAdj = adj;
12532
Joe Onorato8a9b2202010-02-26 18:56:32 -080012533 //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012534 // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj);
12535 if (adj > app.maxAdj) {
12536 adj = app.maxAdj;
Dianne Hackborn7d608422011-08-07 16:24:18 -070012537 if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012538 schedGroup = Process.THREAD_GROUP_DEFAULT;
12539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012540 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070012541 if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070012542 app.keeping = true;
12543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012545 if (app.hasAboveClient) {
12546 // If this process has bound to any services with BIND_ABOVE_CLIENT,
12547 // then we need to drop its adjustment to be lower than the service's
12548 // in order to honor the request. We want to drop it by one adjustment
12549 // level... but there is special meaning applied to various levels so
12550 // we will skip some of them.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012551 if (adj < ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012552 // System process will not get dropped, ever
Dianne Hackborn7d608422011-08-07 16:24:18 -070012553 } else if (adj < ProcessList.VISIBLE_APP_ADJ) {
12554 adj = ProcessList.VISIBLE_APP_ADJ;
12555 } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) {
12556 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
12557 } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) {
12558 adj = ProcessList.HIDDEN_APP_MIN_ADJ;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012559 } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012560 adj++;
12561 }
12562 }
12563
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012564 int importance = app.memImportance;
12565 if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) {
12566 app.curAdj = adj;
12567 app.curSchedGroup = schedGroup;
12568 if (!interesting) {
12569 // For this reporting, if there is not something explicitly
12570 // interesting in this process then we will push it to the
12571 // background importance.
12572 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
12573 } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
12574 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
12575 } else if (adj >= ProcessList.SERVICE_B_ADJ) {
12576 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
12577 } else if (adj >= ProcessList.HOME_APP_ADJ) {
12578 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
12579 } else if (adj >= ProcessList.SERVICE_ADJ) {
12580 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
12581 } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
12582 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
12583 } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
12584 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
12585 } else if (adj >= ProcessList.VISIBLE_APP_ADJ) {
12586 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
12587 } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) {
12588 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
12589 } else {
12590 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT;
12591 }
12592 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012593
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012594 int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0;
12595 if (foregroundActivities != app.foregroundActivities) {
12596 changes |= ProcessChangeItem.CHANGE_ACTIVITIES;
12597 }
12598 if (changes != 0) {
12599 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes);
12600 app.memImportance = importance;
12601 app.foregroundActivities = foregroundActivities;
12602 int i = mPendingProcessChanges.size()-1;
12603 ProcessChangeItem item = null;
12604 while (i >= 0) {
12605 item = mPendingProcessChanges.get(i);
12606 if (item.pid == app.pid) {
12607 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item);
12608 break;
12609 }
12610 i--;
12611 }
12612 if (i < 0) {
12613 // No existing item in pending changes; need a new one.
12614 final int NA = mAvailProcessChanges.size();
12615 if (NA > 0) {
12616 item = mAvailProcessChanges.remove(NA-1);
12617 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item);
12618 } else {
12619 item = new ProcessChangeItem();
12620 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item);
12621 }
12622 item.changes = 0;
12623 item.pid = app.pid;
12624 item.uid = app.info.uid;
12625 if (mPendingProcessChanges.size() == 0) {
12626 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG,
12627 "*** Enqueueing dispatch processes changed!");
12628 mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget();
12629 }
12630 mPendingProcessChanges.add(item);
12631 }
12632 item.changes |= changes;
12633 item.importance = importance;
12634 item.foregroundActivities = foregroundActivities;
12635 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item "
12636 + Integer.toHexString(System.identityHashCode(item))
12637 + " " + app.toShortString() + ": changes=" + item.changes
12638 + " importance=" + item.importance
12639 + " foreground=" + item.foregroundActivities
12640 + " type=" + app.adjType + " source=" + app.adjSource
12641 + " target=" + app.adjTarget);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012642 }
12643
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012644 return app.curRawAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012645 }
12646
12647 /**
12648 * Ask a given process to GC right now.
12649 */
12650 final void performAppGcLocked(ProcessRecord app) {
12651 try {
12652 app.lastRequestedGc = SystemClock.uptimeMillis();
12653 if (app.thread != null) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012654 if (app.reportLowMemory) {
12655 app.reportLowMemory = false;
12656 app.thread.scheduleLowMemory();
12657 } else {
12658 app.thread.processInBackground();
12659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012660 }
12661 } catch (Exception e) {
12662 // whatever.
12663 }
12664 }
12665
12666 /**
12667 * Returns true if things are idle enough to perform GCs.
12668 */
Josh Bartel7f208742010-02-25 11:01:44 -060012669 private final boolean canGcNowLocked() {
Christopher Tatef46723b2012-01-26 14:19:24 -080012670 boolean processingBroadcasts = false;
12671 for (BroadcastQueue q : mBroadcastQueues) {
12672 if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) {
12673 processingBroadcasts = true;
12674 }
12675 }
12676 return !processingBroadcasts
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012677 && (mSleeping || (mMainStack.mResumedActivity != null &&
12678 mMainStack.mResumedActivity.idle));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012679 }
12680
12681 /**
12682 * Perform GCs on all processes that are waiting for it, but only
12683 * if things are idle.
12684 */
12685 final void performAppGcsLocked() {
12686 final int N = mProcessesToGc.size();
12687 if (N <= 0) {
12688 return;
12689 }
Josh Bartel7f208742010-02-25 11:01:44 -060012690 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 while (mProcessesToGc.size() > 0) {
12692 ProcessRecord proc = mProcessesToGc.remove(0);
Dianne Hackborn7d608422011-08-07 16:24:18 -070012693 if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012694 if ((proc.lastRequestedGc+GC_MIN_INTERVAL)
12695 <= SystemClock.uptimeMillis()) {
12696 // To avoid spamming the system, we will GC processes one
12697 // at a time, waiting a few seconds between each.
12698 performAppGcLocked(proc);
12699 scheduleAppGcsLocked();
12700 return;
12701 } else {
12702 // It hasn't been long enough since we last GCed this
12703 // process... put it in the list to wait for its time.
12704 addProcessToGcListLocked(proc);
12705 break;
12706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012707 }
12708 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012709
12710 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012711 }
12712 }
12713
12714 /**
12715 * If all looks good, perform GCs on all processes waiting for them.
12716 */
12717 final void performAppGcsIfAppropriateLocked() {
Josh Bartel7f208742010-02-25 11:01:44 -060012718 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012719 performAppGcsLocked();
12720 return;
12721 }
12722 // Still not idle, wait some more.
12723 scheduleAppGcsLocked();
12724 }
12725
12726 /**
12727 * Schedule the execution of all pending app GCs.
12728 */
12729 final void scheduleAppGcsLocked() {
12730 mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG);
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012731
12732 if (mProcessesToGc.size() > 0) {
12733 // Schedule a GC for the time to the next process.
12734 ProcessRecord proc = mProcessesToGc.get(0);
12735 Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG);
12736
Dianne Hackbornce86ba82011-07-13 19:33:41 -070012737 long when = proc.lastRequestedGc + GC_MIN_INTERVAL;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012738 long now = SystemClock.uptimeMillis();
12739 if (when < (now+GC_TIMEOUT)) {
12740 when = now + GC_TIMEOUT;
12741 }
12742 mHandler.sendMessageAtTime(msg, when);
12743 }
12744 }
12745
12746 /**
12747 * Add a process to the array of processes waiting to be GCed. Keeps the
12748 * list in sorted order by the last GC time. The process can't already be
12749 * on the list.
12750 */
12751 final void addProcessToGcListLocked(ProcessRecord proc) {
12752 boolean added = false;
12753 for (int i=mProcessesToGc.size()-1; i>=0; i--) {
12754 if (mProcessesToGc.get(i).lastRequestedGc <
12755 proc.lastRequestedGc) {
12756 added = true;
12757 mProcessesToGc.add(i+1, proc);
12758 break;
12759 }
12760 }
12761 if (!added) {
12762 mProcessesToGc.add(0, proc);
12763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012764 }
12765
12766 /**
12767 * Set up to ask a process to GC itself. This will either do it
12768 * immediately, or put it on the list of processes to gc the next
12769 * time things are idle.
12770 */
12771 final void scheduleAppGcLocked(ProcessRecord app) {
12772 long now = SystemClock.uptimeMillis();
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012773 if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012774 return;
12775 }
12776 if (!mProcessesToGc.contains(app)) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012777 addProcessToGcListLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012778 scheduleAppGcsLocked();
12779 }
12780 }
12781
Dianne Hackborn287952c2010-09-22 22:34:31 -070012782 final void checkExcessivePowerUsageLocked(boolean doKills) {
12783 updateCpuStatsNow();
12784
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012785 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
Dianne Hackborn287952c2010-09-22 22:34:31 -070012786 boolean doWakeKills = doKills;
12787 boolean doCpuKills = doKills;
12788 if (mLastPowerCheckRealtime == 0) {
12789 doWakeKills = false;
12790 }
12791 if (mLastPowerCheckUptime == 0) {
12792 doCpuKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012793 }
12794 if (stats.isScreenOn()) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070012795 doWakeKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012796 }
12797 final long curRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn287952c2010-09-22 22:34:31 -070012798 final long realtimeSince = curRealtime - mLastPowerCheckRealtime;
12799 final long curUptime = SystemClock.uptimeMillis();
12800 final long uptimeSince = curUptime - mLastPowerCheckUptime;
12801 mLastPowerCheckRealtime = curRealtime;
12802 mLastPowerCheckUptime = curUptime;
12803 if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) {
12804 doWakeKills = false;
12805 }
12806 if (uptimeSince < CPU_MIN_CHECK_DURATION) {
12807 doCpuKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012808 }
12809 int i = mLruProcesses.size();
12810 while (i > 0) {
12811 i--;
12812 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn287952c2010-09-22 22:34:31 -070012813 if (!app.keeping) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012814 long wtime;
12815 synchronized (stats) {
12816 wtime = stats.getProcessWakeTime(app.info.uid,
12817 app.pid, curRealtime);
12818 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012819 long wtimeUsed = wtime - app.lastWakeTime;
12820 long cputimeUsed = app.curCpuTime - app.lastCpuTime;
12821 if (DEBUG_POWER) {
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012822 StringBuilder sb = new StringBuilder(128);
12823 sb.append("Wake for ");
12824 app.toShortString(sb);
12825 sb.append(": over ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012826 TimeUtils.formatDuration(realtimeSince, sb);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012827 sb.append(" used ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012828 TimeUtils.formatDuration(wtimeUsed, sb);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012829 sb.append(" (");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012830 sb.append((wtimeUsed*100)/realtimeSince);
12831 sb.append("%)");
12832 Slog.i(TAG, sb.toString());
12833 sb.setLength(0);
12834 sb.append("CPU for ");
12835 app.toShortString(sb);
12836 sb.append(": over ");
12837 TimeUtils.formatDuration(uptimeSince, sb);
12838 sb.append(" used ");
12839 TimeUtils.formatDuration(cputimeUsed, sb);
12840 sb.append(" (");
12841 sb.append((cputimeUsed*100)/uptimeSince);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012842 sb.append("%)");
12843 Slog.i(TAG, sb.toString());
12844 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012845 // If a process has held a wake lock for more
12846 // than 50% of the time during this period,
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012847 // that sounds bad. Kill!
Dianne Hackborn287952c2010-09-22 22:34:31 -070012848 if (doWakeKills && realtimeSince > 0
12849 && ((wtimeUsed*100)/realtimeSince) >= 50) {
12850 synchronized (stats) {
12851 stats.reportExcessiveWakeLocked(app.info.uid, app.processName,
12852 realtimeSince, wtimeUsed);
12853 }
12854 Slog.w(TAG, "Excessive wake lock in " + app.processName
12855 + " (pid " + app.pid + "): held " + wtimeUsed
12856 + " during " + realtimeSince);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012857 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
12858 app.processName, app.setAdj, "excessive wake lock");
12859 Process.killProcessQuiet(app.pid);
Dianne Hackborn287952c2010-09-22 22:34:31 -070012860 } else if (doCpuKills && uptimeSince > 0
12861 && ((cputimeUsed*100)/uptimeSince) >= 50) {
12862 synchronized (stats) {
12863 stats.reportExcessiveCpuLocked(app.info.uid, app.processName,
12864 uptimeSince, cputimeUsed);
12865 }
12866 Slog.w(TAG, "Excessive CPU in " + app.processName
12867 + " (pid " + app.pid + "): used " + cputimeUsed
12868 + " during " + uptimeSince);
12869 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
12870 app.processName, app.setAdj, "excessive cpu");
12871 Process.killProcessQuiet(app.pid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012872 } else {
12873 app.lastWakeTime = wtime;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012874 app.lastCpuTime = app.curCpuTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012875 }
12876 }
12877 }
12878 }
12879
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012880 private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj,
12881 int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012882 app.hiddenAdj = hiddenAdj;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012883 app.emptyAdj = emptyAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012884
12885 if (app.thread == null) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -070012886 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012887 }
12888
Dianne Hackborn287952c2010-09-22 22:34:31 -070012889 final boolean wasKeeping = app.keeping;
12890
Dianne Hackborn295e3c22011-08-25 13:19:08 -070012891 boolean success = true;
12892
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012893 computeOomAdjLocked(app, hiddenAdj, emptyAdj, TOP_APP, false, doingAll);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012894
Jeff Brown10e89712011-07-08 18:52:57 -070012895 if (app.curRawAdj != app.setRawAdj) {
Jeff Brown10e89712011-07-08 18:52:57 -070012896 if (wasKeeping && !app.keeping) {
12897 // This app is no longer something we want to keep. Note
12898 // its current wake lock time to later know to kill it if
12899 // it is not behaving well.
12900 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
12901 synchronized (stats) {
12902 app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
12903 app.pid, SystemClock.elapsedRealtime());
12904 }
12905 app.lastCpuTime = app.curCpuTime;
12906 }
12907
12908 app.setRawAdj = app.curRawAdj;
12909 }
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012910
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012911 if (app.curAdj != app.setAdj) {
12912 if (Process.setOomAdj(app.pid, app.curAdj)) {
Dianne Hackbornbbb09ac2011-11-30 11:31:29 -080012913 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012914 TAG, "Set " + app.pid + " " + app.processName +
12915 " adj " + app.curAdj + ": " + app.adjType);
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012916 app.setAdj = app.curAdj;
Jeff Brown10e89712011-07-08 18:52:57 -070012917 } else {
Dianne Hackborn295e3c22011-08-25 13:19:08 -070012918 success = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012919 Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj);
Jeff Brown10e89712011-07-08 18:52:57 -070012920 }
12921 }
12922 if (app.setSchedGroup != app.curSchedGroup) {
12923 app.setSchedGroup = app.curSchedGroup;
12924 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
12925 "Setting process group of " + app.processName
12926 + " to " + app.curSchedGroup);
12927 if (app.waitingToKill != null &&
12928 app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
12929 Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill);
12930 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
12931 app.processName, app.setAdj, app.waitingToKill);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -070012932 app.killedBackground = true;
Jeff Brown10e89712011-07-08 18:52:57 -070012933 Process.killProcessQuiet(app.pid);
Dianne Hackborn295e3c22011-08-25 13:19:08 -070012934 success = false;
Jeff Brown10e89712011-07-08 18:52:57 -070012935 } else {
12936 if (true) {
12937 long oldId = Binder.clearCallingIdentity();
12938 try {
12939 Process.setProcessGroup(app.pid, app.curSchedGroup);
12940 } catch (Exception e) {
12941 Slog.w(TAG, "Failed setting process group of " + app.pid
12942 + " to " + app.curSchedGroup);
12943 e.printStackTrace();
12944 } finally {
12945 Binder.restoreCallingIdentity(oldId);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012946 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070012947 } else {
Jeff Brown10e89712011-07-08 18:52:57 -070012948 if (app.thread != null) {
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070012949 try {
Jeff Brown10e89712011-07-08 18:52:57 -070012950 app.thread.setSchedulingGroup(app.curSchedGroup);
12951 } catch (RemoteException e) {
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070012952 }
12953 }
12954 }
12955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012956 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -070012957 return success;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012958 }
12959
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012960 private final ActivityRecord resumedAppLocked() {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012961 ActivityRecord resumedActivity = mMainStack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012962 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -080012963 resumedActivity = mMainStack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012964 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012965 resumedActivity = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012966 }
12967 }
12968 return resumedActivity;
12969 }
12970
Dianne Hackborn599db5c2012-08-03 19:28:48 -070012971 final boolean updateOomAdjLocked(ProcessRecord app) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012972 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012973 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
12974 int curAdj = app.curAdj;
Dianne Hackborn7d608422011-08-07 16:24:18 -070012975 final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ
12976 && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012977
12978 mAdjSeq++;
12979
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012980 boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.emptyAdj,
12981 TOP_APP, false);
Dianne Hackborn7d608422011-08-07 16:24:18 -070012982 final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ
12983 && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070012984 if (nowHidden != wasHidden) {
12985 // Changed to/from hidden state, so apps after it in the LRU
12986 // list may also be changed.
12987 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012988 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -070012989 return success;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012990 }
12991
Dianne Hackbornce86ba82011-07-13 19:33:41 -070012992 final void updateOomAdjLocked() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012993 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012994 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
12995
12996 if (false) {
12997 RuntimeException e = new RuntimeException();
12998 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -080012999 Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013000 }
13001
13002 mAdjSeq++;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013003 mNewNumServiceProcs = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013004
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080013005 // Let's determine how many processes we have running vs.
13006 // how many slots we have for background processes; we may want
13007 // to put multiple processes in a slot of there are enough of
13008 // them.
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013009 int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ
13010 - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2;
13011 int emptyFactor = (mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs)/numSlots;
13012 if (emptyFactor < 1) emptyFactor = 1;
13013 int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots;
13014 if (hiddenFactor < 1) hiddenFactor = 1;
13015 int stepHidden = 0;
13016 int stepEmpty = 0;
13017 final int emptyProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit;
13018 final int hiddenProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit;
Dianne Hackborn8633e682010-04-22 16:03:41 -070013019 int numHidden = 0;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013020 int numEmpty = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013021 int numTrimming = 0;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013022
13023 mNumNonHiddenProcs = 0;
13024 mNumHiddenProcs = 0;
13025
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013026 // First update the OOM adjustment for each of the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013027 // application processes based on their current state.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013028 int i = mLruProcesses.size();
Dianne Hackborn7d608422011-08-07 16:24:18 -070013029 int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013030 int nextHiddenAdj = curHiddenAdj+1;
13031 int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
13032 int nextEmptyAdj = curEmptyAdj+2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013033 while (i > 0) {
13034 i--;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013035 ProcessRecord app = mLruProcesses.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080013036 //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj);
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013037 updateOomAdjLocked(app, curHiddenAdj, curEmptyAdj, TOP_APP, true);
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013038 if (!app.killedBackground) {
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013039 if (app.curRawAdj == curHiddenAdj && app.hasActivities) {
13040 // This process was assigned as a hidden process... step the
13041 // hidden level.
13042 mNumHiddenProcs++;
13043 if (curHiddenAdj != nextHiddenAdj) {
13044 stepHidden++;
13045 if (stepHidden >= hiddenFactor) {
13046 stepHidden = 0;
13047 curHiddenAdj = nextHiddenAdj;
13048 nextHiddenAdj += 2;
13049 if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) {
13050 nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ;
13051 }
13052 }
13053 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013054 numHidden++;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013055 if (numHidden > hiddenProcessLimit) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013056 Slog.i(TAG, "No longer want " + app.processName
13057 + " (pid " + app.pid + "): hidden #" + numHidden);
13058 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13059 app.processName, app.setAdj, "too many background");
13060 app.killedBackground = true;
13061 Process.killProcessQuiet(app.pid);
Dianne Hackborn8633e682010-04-22 16:03:41 -070013062 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013063 } else {
13064 if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) {
13065 // This process was assigned as an empty process... step the
13066 // empty level.
13067 if (curEmptyAdj != nextEmptyAdj) {
13068 stepEmpty++;
13069 if (stepEmpty >= emptyFactor) {
13070 stepEmpty = 0;
13071 curEmptyAdj = nextEmptyAdj;
13072 nextEmptyAdj += 2;
13073 if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) {
13074 nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ;
13075 }
13076 }
13077 }
13078 } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) {
13079 mNumNonHiddenProcs++;
13080 }
13081 if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
13082 numEmpty++;
13083 if (numEmpty > emptyProcessLimit) {
13084 Slog.i(TAG, "No longer want " + app.processName
13085 + " (pid " + app.pid + "): empty #" + numEmpty);
13086 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13087 app.processName, app.setAdj, "too many background");
13088 app.killedBackground = true;
13089 Process.killProcessQuiet(app.pid);
13090 }
13091 }
Dianne Hackborn8633e682010-04-22 16:03:41 -070013092 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013093 if (app.isolated && app.services.size() <= 0) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -080013094 // If this is an isolated process, and there are no
13095 // services running in it, then the process is no longer
13096 // needed. We agressively kill these because we can by
13097 // definition not re-use the same process again, and it is
13098 // good to avoid having whatever code was running in them
13099 // left sitting around after no longer needed.
13100 Slog.i(TAG, "Isolated process " + app.processName
13101 + " (pid " + app.pid + ") no longer needed");
13102 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13103 app.processName, app.setAdj, "isolated not needed");
13104 app.killedBackground = true;
13105 Process.killProcessQuiet(app.pid);
13106 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013107 if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ
13108 && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ
13109 && !app.killedBackground) {
13110 numTrimming++;
13111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013112 }
13113 }
13114
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013115 mNumServiceProcs = mNewNumServiceProcs;
13116
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013117 // Now determine the memory trimming level of background processes.
13118 // Unfortunately we need to start at the back of the list to do this
13119 // properly. We only do this if the number of background apps we
13120 // are managing to keep around is less than half the maximum we desire;
13121 // if we are keeping a good number around, we'll let them use whatever
13122 // memory they want.
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013123 if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/4)
13124 && numEmpty <= (ProcessList.MAX_HIDDEN_APPS/4)) {
13125 final int numHiddenAndEmpty = numHidden + numEmpty;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013126 final int N = mLruProcesses.size();
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013127 int factor = numTrimming/3;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080013128 int minFactor = 2;
13129 if (mHomeProcess != null) minFactor++;
13130 if (mPreviousProcess != null) minFactor++;
13131 if (factor < minFactor) factor = minFactor;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013132 int step = 0;
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013133 int fgTrimLevel;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013134 if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/5)) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013135 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013136 } else if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/3)) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013137 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
13138 } else {
13139 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE;
13140 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013141 int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013142 for (i=0; i<N; i++) {
13143 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013144 if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ
13145 && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080013146 && !app.killedBackground) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013147 if (app.trimMemoryLevel < curLevel && app.thread != null) {
13148 try {
13149 app.thread.scheduleTrimMemory(curLevel);
13150 } catch (RemoteException e) {
13151 }
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013152 if (false) {
13153 // For now we won't do this; our memory trimming seems
13154 // to be good enough at this point that destroying
13155 // activities causes more harm than good.
13156 if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE
13157 && app != mHomeProcess && app != mPreviousProcess) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013158 // Need to do this on its own message because the stack may not
13159 // be in a consistent state at this point.
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013160 // For these apps we will also finish their activities
13161 // to help them free memory.
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013162 mMainStack.scheduleDestroyActivities(app, false, "trim");
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013163 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013164 }
13165 }
13166 app.trimMemoryLevel = curLevel;
13167 step++;
13168 if (step >= factor) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013169 step = 0;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013170 switch (curLevel) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013171 case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
13172 curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013173 break;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013174 case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
13175 curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013176 break;
13177 }
13178 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013179 } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013180 if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013181 && app.thread != null) {
13182 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013183 app.thread.scheduleTrimMemory(
13184 ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013185 } catch (RemoteException e) {
13186 }
13187 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013188 app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013189 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013190 if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi)
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013191 && app.pendingUiClean) {
13192 // If this application is now in the background and it
13193 // had done UI, then give it the special trim level to
13194 // have it free UI resources.
13195 final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
13196 if (app.trimMemoryLevel < level && app.thread != null) {
13197 try {
13198 app.thread.scheduleTrimMemory(level);
13199 } catch (RemoteException e) {
13200 }
13201 }
13202 app.pendingUiClean = false;
13203 }
13204 if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013205 try {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013206 app.thread.scheduleTrimMemory(fgTrimLevel);
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013207 } catch (RemoteException e) {
13208 }
13209 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013210 app.trimMemoryLevel = fgTrimLevel;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013211 }
13212 }
13213 } else {
13214 final int N = mLruProcesses.size();
13215 for (i=0; i<N; i++) {
13216 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013217 if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi)
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013218 && app.pendingUiClean) {
13219 if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
13220 && app.thread != null) {
13221 try {
13222 app.thread.scheduleTrimMemory(
13223 ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
13224 } catch (RemoteException e) {
13225 }
13226 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013227 app.pendingUiClean = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013228 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013229 app.trimMemoryLevel = 0;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013230 }
13231 }
13232
13233 if (mAlwaysFinishActivities) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013234 // Need to do this on its own message because the stack may not
13235 // be in a consistent state at this point.
13236 mMainStack.scheduleDestroyActivities(null, false, "always-finish");
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013238 }
13239
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070013240 final void trimApplications() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013241 synchronized (this) {
13242 int i;
13243
13244 // First remove any unused application processes whose package
13245 // has been removed.
13246 for (i=mRemovedProcesses.size()-1; i>=0; i--) {
13247 final ProcessRecord app = mRemovedProcesses.get(i);
13248 if (app.activities.size() == 0
13249 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013250 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013251 TAG, "Exiting empty application process "
13252 + app.processName + " ("
13253 + (app.thread != null ? app.thread.asBinder() : null)
13254 + ")\n");
13255 if (app.pid > 0 && app.pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070013256 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13257 app.processName, app.setAdj, "empty");
13258 Process.killProcessQuiet(app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013259 } else {
13260 try {
13261 app.thread.scheduleExit();
13262 } catch (Exception e) {
13263 // Ignore exceptions.
13264 }
13265 }
Dianne Hackborn130b0d22011-07-26 22:07:48 -070013266 cleanUpApplicationRecordLocked(app, false, true, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013267 mRemovedProcesses.remove(i);
13268
13269 if (app.persistent) {
13270 if (app.persistent) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -080013271 addAppLocked(app.info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013272 }
13273 }
13274 }
13275 }
13276
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013277 // Now update the oom adj for all processes.
13278 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013279 }
13280 }
13281
13282 /** This method sends the specified signal to each of the persistent apps */
13283 public void signalPersistentProcesses(int sig) throws RemoteException {
13284 if (sig != Process.SIGNAL_USR1) {
13285 throw new SecurityException("Only SIGNAL_USR1 is allowed");
13286 }
13287
13288 synchronized (this) {
13289 if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES)
13290 != PackageManager.PERMISSION_GRANTED) {
13291 throw new SecurityException("Requires permission "
13292 + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES);
13293 }
13294
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013295 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
13296 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013297 if (r.thread != null && r.persistent) {
13298 Process.sendSignal(r.pid, sig);
13299 }
13300 }
13301 }
13302 }
13303
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013304 private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) {
13305 if (proc == null || proc == mProfileProc) {
13306 proc = mProfileProc;
13307 path = mProfileFile;
13308 profileType = mProfileType;
13309 clearProfilerLocked();
13310 }
13311 if (proc == null) {
13312 return;
13313 }
13314 try {
13315 proc.thread.profilerControl(false, path, null, profileType);
13316 } catch (RemoteException e) {
13317 throw new IllegalStateException("Process disappeared");
13318 }
13319 }
13320
13321 private void clearProfilerLocked() {
13322 if (mProfileFd != null) {
13323 try {
13324 mProfileFd.close();
13325 } catch (IOException e) {
13326 }
13327 }
13328 mProfileApp = null;
13329 mProfileProc = null;
13330 mProfileFile = null;
13331 mProfileType = 0;
13332 mAutoStopProfiler = false;
13333 }
13334
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013335 public boolean profileControl(String process, boolean start,
Romain Guy7eabe552011-07-21 14:56:34 -070013336 String path, ParcelFileDescriptor fd, int profileType) throws RemoteException {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013337
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013338 try {
13339 synchronized (this) {
13340 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
13341 // its own permission.
13342 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
13343 != PackageManager.PERMISSION_GRANTED) {
13344 throw new SecurityException("Requires permission "
13345 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013346 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013347
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013348 if (start && fd == null) {
13349 throw new IllegalArgumentException("null fd");
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013350 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013351
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013352 ProcessRecord proc = null;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013353 if (process != null) {
13354 try {
13355 int pid = Integer.parseInt(process);
13356 synchronized (mPidsSelfLocked) {
13357 proc = mPidsSelfLocked.get(pid);
13358 }
13359 } catch (NumberFormatException e) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013360 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013361
13362 if (proc == null) {
13363 HashMap<String, SparseArray<ProcessRecord>> all
13364 = mProcessNames.getMap();
13365 SparseArray<ProcessRecord> procs = all.get(process);
13366 if (procs != null && procs.size() > 0) {
13367 proc = procs.valueAt(0);
13368 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013369 }
13370 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013371
13372 if (start && (proc == null || proc.thread == null)) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013373 throw new IllegalArgumentException("Unknown process: " + process);
13374 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013375
13376 if (start) {
13377 stopProfilerLocked(null, null, 0);
13378 setProfileApp(proc.info, proc.processName, path, fd, false);
13379 mProfileProc = proc;
13380 mProfileType = profileType;
13381 try {
13382 fd = fd.dup();
13383 } catch (IOException e) {
13384 fd = null;
13385 }
13386 proc.thread.profilerControl(start, path, fd, profileType);
13387 fd = null;
13388 mProfileFd = null;
13389 } else {
13390 stopProfilerLocked(proc, path, profileType);
13391 if (fd != null) {
13392 try {
13393 fd.close();
13394 } catch (IOException e) {
13395 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013396 }
13397 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013398
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013399 return true;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013400 }
13401 } catch (RemoteException e) {
13402 throw new IllegalStateException("Process disappeared");
13403 } finally {
13404 if (fd != null) {
13405 try {
13406 fd.close();
13407 } catch (IOException e) {
13408 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013409 }
13410 }
13411 }
Andy McFadden824c5102010-07-09 16:26:57 -070013412
13413 public boolean dumpHeap(String process, boolean managed,
13414 String path, ParcelFileDescriptor fd) throws RemoteException {
13415
13416 try {
13417 synchronized (this) {
13418 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
13419 // its own permission (same as profileControl).
13420 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
13421 != PackageManager.PERMISSION_GRANTED) {
13422 throw new SecurityException("Requires permission "
13423 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
13424 }
13425
13426 if (fd == null) {
13427 throw new IllegalArgumentException("null fd");
13428 }
13429
13430 ProcessRecord proc = null;
13431 try {
13432 int pid = Integer.parseInt(process);
13433 synchronized (mPidsSelfLocked) {
13434 proc = mPidsSelfLocked.get(pid);
13435 }
13436 } catch (NumberFormatException e) {
13437 }
13438
13439 if (proc == null) {
13440 HashMap<String, SparseArray<ProcessRecord>> all
13441 = mProcessNames.getMap();
13442 SparseArray<ProcessRecord> procs = all.get(process);
13443 if (procs != null && procs.size() > 0) {
13444 proc = procs.valueAt(0);
13445 }
13446 }
13447
13448 if (proc == null || proc.thread == null) {
13449 throw new IllegalArgumentException("Unknown process: " + process);
13450 }
13451
Dianne Hackbornf02e57b2011-03-01 22:21:04 -080013452 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
13453 if (!isDebuggable) {
Andy McFadden824c5102010-07-09 16:26:57 -070013454 if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
13455 throw new SecurityException("Process not debuggable: " + proc);
13456 }
13457 }
13458
13459 proc.thread.dumpHeap(managed, path, fd);
13460 fd = null;
13461 return true;
13462 }
13463 } catch (RemoteException e) {
13464 throw new IllegalStateException("Process disappeared");
13465 } finally {
13466 if (fd != null) {
13467 try {
13468 fd.close();
13469 } catch (IOException e) {
13470 }
13471 }
13472 }
13473 }
13474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013475 /** In this method we try to acquire our lock to make sure that we have not deadlocked */
13476 public void monitor() {
13477 synchronized (this) { }
13478 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080013479
Dianne Hackborna573f6a2012-02-09 16:12:18 -080013480 void onCoreSettingsChange(Bundle settings) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080013481 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
13482 ProcessRecord processRecord = mLruProcesses.get(i);
13483 try {
13484 if (processRecord.thread != null) {
13485 processRecord.thread.setCoreSettings(settings);
13486 }
13487 } catch (RemoteException re) {
13488 /* ignore */
13489 }
13490 }
13491 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070013492
13493 // Multi-user methods
13494
Amith Yamasani742a6712011-05-04 14:49:28 -070013495 public boolean switchUser(int userId) {
13496 final int callingUid = Binder.getCallingUid();
13497 if (callingUid != 0 && callingUid != Process.myUid()) {
13498 Slog.e(TAG, "Trying to switch user from unauthorized app");
13499 return false;
13500 }
13501 if (mCurrentUserId == userId)
13502 return true;
13503
13504 synchronized (this) {
13505 // Check if user is already logged in, otherwise check if user exists first before
13506 // adding to the list of logged in users.
13507 if (mLoggedInUsers.indexOfKey(userId) < 0) {
13508 if (!userExists(userId)) {
13509 return false;
13510 }
13511 mLoggedInUsers.append(userId, userId);
13512 }
13513
13514 mCurrentUserId = userId;
13515 boolean haveActivities = mMainStack.switchUser(userId);
13516 if (!haveActivities) {
13517 startHomeActivityLocked(userId);
13518 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -080013519
Amith Yamasani37ce3a82012-02-06 12:04:42 -080013520 }
Amith Yamasani13593602012-03-22 16:16:17 -070013521
13522 // Inform of user switch
13523 Intent addedIntent = new Intent(Intent.ACTION_USER_SWITCHED);
Amith Yamasani2a003292012-08-14 18:25:45 -070013524 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
Amith Yamasani1ab36c12012-08-17 13:55:02 -070013525 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
Amith Yamasani13593602012-03-22 16:16:17 -070013526
Amith Yamasani4b2e9342011-03-31 12:38:53 -070013527 return true;
13528 }
Amith Yamasani742a6712011-05-04 14:49:28 -070013529
Amith Yamasani52f1d752012-03-28 18:19:29 -070013530 @Override
13531 public UserInfo getCurrentUser() throws RemoteException {
13532 final int callingUid = Binder.getCallingUid();
13533 if (callingUid != 0 && callingUid != Process.myUid()) {
13534 Slog.e(TAG, "Trying to get user from unauthorized app");
13535 return null;
13536 }
Amith Yamasani258848d2012-08-10 17:06:33 -070013537 return getUserManager().getUserInfo(mCurrentUserId);
Amith Yamasani52f1d752012-03-28 18:19:29 -070013538 }
13539
Amith Yamasani13593602012-03-22 16:16:17 -070013540 private void onUserRemoved(Intent intent) {
Amith Yamasani2a003292012-08-14 18:25:45 -070013541 int extraUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
Amith Yamasani13593602012-03-22 16:16:17 -070013542 if (extraUserId < 1) return;
13543
13544 // Kill all the processes for the user
13545 ArrayList<Pair<String, Integer>> pkgAndUids = new ArrayList<Pair<String,Integer>>();
13546 synchronized (this) {
13547 HashMap<String,SparseArray<ProcessRecord>> map = mProcessNames.getMap();
13548 for (Entry<String, SparseArray<ProcessRecord>> uidMap : map.entrySet()) {
13549 SparseArray<ProcessRecord> uids = uidMap.getValue();
13550 for (int i = 0; i < uids.size(); i++) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070013551 if (UserHandle.getUserId(uids.keyAt(i)) == extraUserId) {
Amith Yamasani13593602012-03-22 16:16:17 -070013552 pkgAndUids.add(new Pair<String,Integer>(uidMap.getKey(), uids.keyAt(i)));
13553 }
13554 }
13555 }
13556
13557 for (Pair<String,Integer> pkgAndUid : pkgAndUids) {
13558 forceStopPackageLocked(pkgAndUid.first, pkgAndUid.second,
13559 false, false, true, true, extraUserId);
13560 }
13561 }
13562 }
13563
Amith Yamasani742a6712011-05-04 14:49:28 -070013564 private boolean userExists(int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -070013565 UserInfo user = getUserManager().getUserInfo(userId);
13566 return user != null;
13567 }
Amith Yamasani742a6712011-05-04 14:49:28 -070013568
Amith Yamasani258848d2012-08-10 17:06:33 -070013569 UserManager getUserManager() {
13570 if (mUserManager == null) {
13571 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
13572 }
13573 return mUserManager;
Amith Yamasani742a6712011-05-04 14:49:28 -070013574 }
13575
Amith Yamasani37ce3a82012-02-06 12:04:42 -080013576 private void checkValidCaller(int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070013577 if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080013578
13579 throw new SecurityException("Caller uid=" + uid
13580 + " is not privileged to communicate with user=" + userId);
13581 }
Amith Yamasani742a6712011-05-04 14:49:28 -070013582
13583 private int applyUserId(int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070013584 return UserHandle.getUid(userId, uid);
Amith Yamasani742a6712011-05-04 14:49:28 -070013585 }
13586
Dianne Hackborn599db5c2012-08-03 19:28:48 -070013587 ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) {
Amith Yamasani2f6c9eb2012-02-06 15:31:35 -080013588 if (info == null) return null;
Amith Yamasani742a6712011-05-04 14:49:28 -070013589 ApplicationInfo newInfo = new ApplicationInfo(info);
13590 newInfo.uid = applyUserId(info.uid, userId);
Amith Yamasania4a54e22012-04-16 15:44:19 -070013591 newInfo.dataDir = USER_DATA_DIR + userId + "/"
13592 + info.packageName;
Amith Yamasani742a6712011-05-04 14:49:28 -070013593 return newInfo;
13594 }
13595
13596 ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) {
Amith Yamasania4a54e22012-04-16 15:44:19 -070013597 if (aInfo == null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070013598 || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) {
Amith Yamasani742a6712011-05-04 14:49:28 -070013599 return aInfo;
13600 }
13601
13602 ActivityInfo info = new ActivityInfo(aInfo);
13603 info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId);
13604 return info;
13605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606}